Changes in / [69867ad9:caaf424]
- Files:
-
- 3 deleted
- 12 edited
-
libcfa/src/enum.cfa (deleted)
-
src/ResolvExpr/CandidateFinder.cpp (modified) (3 diffs)
-
src/ResolvExpr/CastCost.cc (modified) (2 diffs)
-
src/ResolvExpr/ConversionCost.cc (modified) (3 diffs)
-
src/Validate/HoistStruct.cpp (modified) (5 diffs)
-
src/Validate/ImplementEnumFunc.cpp (modified) (8 diffs)
-
src/main.cc (modified) (2 diffs)
-
tests/enum_tests/.expect/funcEnum.txt (deleted)
-
tests/enum_tests/.expect/position.txt (deleted)
-
tests/enum_tests/.expect/stringEnum.txt (modified) (1 diff)
-
tests/enum_tests/anonymous.cfa (modified) (1 diff)
-
tests/enum_tests/enumInlineValue.cfa (modified) (1 diff)
-
tests/enum_tests/position.cfa (modified) (2 diffs)
-
tests/enum_tests/stringEnum.cfa (modified) (2 diffs)
-
tests/enum_tests/typedIntEnum.cfa (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/ResolvExpr/CandidateFinder.cpp
r69867ad9 rcaaf424 906 906 } 907 907 CandidateRef & choice = winners.front(); 908 choice->cost.inc Safe();908 choice->cost.incVar(); 909 909 candidates.emplace_back( std::move(choice) ); 910 910 } … … 1376 1376 ast::Expr * newExpr = data.combine( nameExpr->location, cost ); 1377 1377 1378 // bool bentConversion = false; 1379 // if ( auto inst = newExpr->result.as<ast::EnumInstType>() ) { 1380 // if ( inst->base && inst->base->base ) { 1381 // bentConversion = true; 1382 // } 1383 // } 1384 1385 // CandidateRef newCand = std::make_shared<Candidate>( 1386 // newExpr, copy( tenv ), ast::OpenVarSet{}, ast::AssertionSet{}, bentConversion? Cost::safe: Cost::zero, 1387 // cost ); 1378 bool bentConversion = false; 1379 if ( auto inst = newExpr->result.as<ast::EnumInstType>() ) { 1380 if ( inst->base && inst->base->base ) { 1381 bentConversion = true; 1382 } 1383 } 1384 1388 1385 CandidateRef newCand = std::make_shared<Candidate>( 1389 newExpr, copy( tenv ), ast::OpenVarSet{}, ast::AssertionSet{}, Cost::zero,1386 newExpr, copy( tenv ), ast::OpenVarSet{}, ast::AssertionSet{}, bentConversion? Cost::safe: Cost::zero, 1390 1387 cost ); 1388 1391 1389 if (newCand->expr->env) { 1392 1390 newCand->env.add(*newCand->expr->env); … … 1831 1829 Cost cost = Cost::zero; 1832 1830 ast::Expr * newExpr = data.combine( expr->location, cost ); 1833 // CandidateRef newCand =1834 // std::make_shared<Candidate>(1835 // newExpr, copy( tenv ), ast::OpenVarSet{},1836 // ast::AssertionSet{}, Cost::safe, cost1837 // );1838 1831 CandidateRef newCand = 1839 1832 std::make_shared<Candidate>( 1840 1833 newExpr, copy( tenv ), ast::OpenVarSet{}, 1841 ast::AssertionSet{}, Cost:: zero, cost1834 ast::AssertionSet{}, Cost::safe, cost 1842 1835 ); 1836 1843 1837 if (newCand->expr->env) { 1844 1838 newCand->env.add(*newCand->expr->env); -
src/ResolvExpr/CastCost.cc
r69867ad9 rcaaf424 53 53 } else { 54 54 cost = conversionCost( basicType, dst, srcIsLvalue, symtab, env ); 55 if ( Cost::unsafe < cost ) {56 if (auto enumInst = dynamic_cast<const ast::EnumInstType *>(dst)) {57 assert(enumInst->base->base);58 cost = Cost::unsafe;59 }60 }61 55 } 62 }63 64 void postvisit( const ast::ZeroType * zero ) {65 // auto ptr = dynamic_cast< const ast::PointerType * >( dst );66 // if ( ptr && basicType->isInteger() ) {67 // // needed for, e.g. unsigned long => void *68 // cost = Cost::unsafe;69 // } else {70 cost = conversionCost( zero, dst, srcIsLvalue, symtab, env );71 if ( Cost::unsafe < cost ) {72 if (auto enumInst = dynamic_cast<const ast::EnumInstType *>(dst)) {73 assert(enumInst->base->base);74 cost = Cost::unsafe;75 }76 }77 // }78 }79 80 void postvisit( const ast::OneType * one ) {81 // auto ptr = dynamic_cast< const ast::PointerType * >( dst );82 // if ( ptr && basicType->isInteger() ) {83 // // needed for, e.g. unsigned long => void *84 // cost = Cost::unsafe;85 // } else {86 cost = conversionCost( one, dst, srcIsLvalue, symtab, env );87 if ( Cost::unsafe < cost ) {88 if (auto enumInst = dynamic_cast<const ast::EnumInstType *>(dst)) {89 assert(enumInst->base->base);90 cost = Cost::unsafe;91 }92 }93 // }94 56 } 95 57 … … 118 80 cost = Cost::unsafe; 119 81 } 120 }121 }122 123 void postvist( const ast::EnumInstType * ) {124 if ( auto basic = dynamic_cast< const ast::BasicType * >(dst) ) {125 if ( basic->isInteger() ) cost = Cost::unsafe;126 82 } 127 83 } -
src/ResolvExpr/ConversionCost.cc
r69867ad9 rcaaf424 284 284 } else if ( auto dstAsEnumInst = dynamic_cast< const ast::EnumInstType * >( dst ) ) { 285 285 if ( dstAsEnumInst->base && !dstAsEnumInst->base->base ) { 286 cost = Cost::unsafe; 286 cost = Cost::zero; 287 cost.incUnsafe(); 287 288 } 288 289 } … … 481 482 } else if ( auto dstAsEnumInst = dynamic_cast< const ast::EnumInstType * >( dst ) ) { 482 483 if ( dstAsEnumInst->base && !dstAsEnumInst->base->base ) { 483 cost = Cost::unsafe; 484 cost = Cost::zero; 485 cost.incUnsafe(); 484 486 } 485 487 } … … 502 504 } else if ( auto dstAsEnumInst = dynamic_cast< const ast::EnumInstType * >( dst ) ) { 503 505 if ( dstAsEnumInst->base && !dstAsEnumInst->base->base ) { 504 cost = Cost::unsafe; 506 cost = Cost::zero; 507 cost.incUnsafe(); 505 508 } 506 509 } -
src/Validate/HoistStruct.cpp
r69867ad9 rcaaf424 27 27 namespace { 28 28 29 /// Is this a declaration can appear in a struct/union and should be hoisted?30 29 bool shouldHoist( ast::Decl const * decl ) { 31 30 return dynamic_cast< ast::StructDecl const * >( decl ) … … 35 34 } 36 35 37 /// Helper that updates an InstType if the base name could be updated. 38 template<typename InstType> 39 InstType const * preInstType( InstType const * type ) { 40 assert( type->base ); 41 if ( nullptr == type->base->parent ) return type; 42 auto mut = ast::mutate( type ); 43 mut->name = mut->base->name; 44 return mut; 45 } 46 47 /// Update StructInstType and UnionInstType names. 48 struct NameUpdater { 49 ast::StructInstType const * previsit( ast::StructInstType const * type ) { 50 return preInstType( type ); 51 } 52 53 ast::UnionInstType const * previsit( ast::UnionInstType const * type ) { 54 return preInstType( type ); 55 } 56 }; 57 58 ast::Decl const * updateNames( ast::Decl const * decl ) { 59 ast::Pass<NameUpdater> visitor; 60 return decl->accept( visitor ); 61 } 62 63 /* This pass hoists from structs/unions. Hoisted declarations should always 36 /* This pass also does some renaming and internal field alteration, but the 37 * complex part is the actual hoisting. Hoisted declarations should always 64 38 * appear before the declaration they are hoisted out of and if two types are 65 39 * nested in the same declaration their order should not change. 66 * It also sets up parent relationships, does name mangling of hoisted types67 * and updates instance types of the hoisted types.68 40 */ 69 41 struct HoistStructCore final : … … 124 96 auto mut = ast::mutate( decl ); 125 97 mut->parent = parent; 98 mut->name = qualifiedName( mut ); 126 99 extendParams( mut->params, parent->params ); 127 100 decl = mut; … … 143 116 } 144 117 } 145 // Is this a nested type? Then update the name, after the parent's name146 // has been updated (hence the post visit).147 if ( mut->parent ) {148 mut->name = qualifiedName( mut );149 // Top level type that has hoisted? Then do a second pass subpass to make150 // sure we update instance type names after the declaration is renamed.151 } else if ( !declsToAddBefore.empty() ) {152 for ( ast::ptr<ast::Decl> & member : mut->members ) {153 member = updateNames( member.get() );154 }155 for ( ast::ptr<ast::Decl> & declToAdd : declsToAddBefore ) {156 declToAdd = updateNames( declToAdd );157 }158 }159 118 return mut; 160 119 } … … 208 167 } 209 168 169 template<typename InstType> 170 InstType const * preInstType( InstType const * type ) { 171 assert( type->base ); 172 auto mut = ast::mutate( type ); 173 mut->name = mut->base->name; 174 return mut; 175 } 176 210 177 ast::StructInstType const * HoistStructCore::previsit( ast::StructInstType const * type ) { 211 178 return preInstType( preCollectionInstType( type ) ); -
src/Validate/ImplementEnumFunc.cpp
r69867ad9 rcaaf424 30 30 void genAttrFunctions(); 31 31 void genSuccPredPosn(); 32 //void genSuccPredDecl();32 void genSuccPredDecl(); 33 33 34 34 void appendReturnThis(ast::FunctionDecl* decl) { … … 73 73 const ast::Decl* getDecl() const { return decl; } 74 74 75 // Implement Bounded trait for enum76 void genBoundedFunctions();77 // Implement Serial trait for enum78 void genSerialTraitFuncs();79 80 // Bounded trait81 ast::FunctionDecl* genLowerBoundProto() const;82 ast::FunctionDecl* genUpperBoundProto() const;83 void genLowerBoundBody(ast::FunctionDecl* func) const;84 void genUpperBoundBody(ast::FunctionDecl* func) const;85 86 75 ast::FunctionDecl* genPosnProto() const; 87 76 ast::FunctionDecl* genLabelProto() const; 88 77 ast::FunctionDecl* genValueProto() const; 89 90 // Serial trait91 ast::FunctionDecl* genFromIntProto() const;92 ast::FunctionDecl* genFromInstanceProto() const;93 78 ast::FunctionDecl* genSuccProto() const; 94 79 ast::FunctionDecl* genPredProto() const; 95 96 void genFromIntBody(ast::FunctionDecl *) const;97 void genFromInstanceBody(ast::FunctionDecl *) const;98 ////////////////99 80 100 81 ast::FunctionDecl* genSuccPosProto() const; … … 308 289 309 290 ast::FunctionDecl* EnumAttrFuncGenerator::genPosnProto() const { 310 return genProto(311 "posE",312 {new ast::ObjectDecl(getLocation(), "_i", new ast::EnumInstType(decl))},313 {new ast::ObjectDecl(getLocation(), "_ret",314 new ast::BasicType(ast::BasicKind::UnsignedInt))});291 return genProto( 292 "posE", 293 {new ast::ObjectDecl(getLocation(), "_i", new ast::EnumInstType(decl))}, 294 {new ast::ObjectDecl(getLocation(), "_ret", 295 new ast::BasicType(ast::BasicKind::UnsignedInt))}); 315 296 } 316 297 … … 332 313 } 333 314 334 ast::FunctionDecl* EnumAttrFuncGenerator::genFromIntProto() const {335 return genProto(336 "fromInt",337 {new ast::ObjectDecl(getLocation(), "_i", new ast::BasicType(ast::BasicKind::UnsignedInt))},338 {new ast::ObjectDecl(getLocation(), "_ret", new ast::EnumInstType(decl))}339 );340 }341 342 ast::FunctionDecl* EnumAttrFuncGenerator::genFromInstanceProto() const {343 return genProto(344 "fromInstance",345 {new ast::ObjectDecl(getLocation(), "_i", new ast::EnumInstType(decl))},346 {new ast::ObjectDecl(getLocation(), "_ret", new ast::BasicType(ast::BasicKind::UnsignedInt))}347 );348 }349 350 void EnumAttrFuncGenerator::genFromIntBody(ast::FunctionDecl* func) const {351 auto params = func->params;352 assert( params.size() == 1 );353 auto param = params.front();354 auto castExpr = new ast::CastExpr(355 func->location,356 new ast::VariableExpr(func->location, param),357 new ast::EnumInstType(decl),358 ast::GeneratedFlag::ExplicitCast359 );360 func->stmts = new ast::CompoundStmt(361 func->location, {new ast::ReturnStmt(func->location, castExpr)}362 );363 }364 365 void EnumAttrFuncGenerator::genFromInstanceBody(ast::FunctionDecl* func) const {366 auto params = func->params;367 assert( params.size() == 1 );368 auto param = params.front();369 ast::UntypedExpr* untyped = ast::UntypedExpr::createCall(370 func->location, "posE", { new ast::VariableExpr(func->location, param) });371 func->stmts = new ast::CompoundStmt(372 func->location, {new ast::ReturnStmt(func->location, untyped)}373 );374 }375 376 void EnumAttrFuncGenerator::genSerialTraitFuncs() {377 auto fromIntProto = genFromIntProto();378 produceForwardDecl(fromIntProto);379 genFromIntBody(fromIntProto);380 produceDecl(fromIntProto);381 382 auto fromInstanceProto = genFromInstanceProto();383 produceForwardDecl(fromInstanceProto);384 genFromInstanceBody(fromInstanceProto);385 produceDecl(fromInstanceProto);386 387 auto succProto = genSuccProto();388 auto predProto = genPredProto();389 produceForwardDecl(succProto);390 produceForwardDecl(predProto);391 }392 393 315 ast::FunctionDecl* EnumAttrFuncGenerator::genSuccProto() const { 394 316 return genProto( … … 405 327 {new ast::ObjectDecl(getLocation(), "_ret", 406 328 new ast::EnumInstType(decl))}); 407 }408 409 ast::FunctionDecl* EnumAttrFuncGenerator::genLowerBoundProto() const {410 return genProto("lowerBound", {}, {411 new ast::ObjectDecl(getLocation(), "_i", new ast::EnumInstType(decl))412 });413 }414 415 ast::FunctionDecl* EnumAttrFuncGenerator::genUpperBoundProto() const {416 return genProto("upperBound", {}, {417 new ast::ObjectDecl(getLocation(), "_i", new ast::EnumInstType(decl))418 });419 }420 421 void EnumAttrFuncGenerator::genLowerBoundBody(ast::FunctionDecl* func) const {422 const CodeLocation & loc = func->location;423 auto mem = decl->members.front();424 // auto expr = new ast::QualifiedNameExpr( loc, decl, mem->name );425 // expr->result = new ast::EnumInstType( decl );426 auto expr = new ast::NameExpr( loc, mem->name );427 func->stmts = new ast::CompoundStmt( loc, {new ast::ReturnStmt(loc, expr)});428 }429 430 void EnumAttrFuncGenerator::genUpperBoundBody(ast::FunctionDecl* func) const {431 const CodeLocation & loc = func->location;432 auto mem = decl->members.back();433 auto expr = new ast::NameExpr( loc, mem->name );434 // expr->result = new ast::EnumInstType( decl );435 func->stmts = new ast::CompoundStmt( loc, {new ast::ReturnStmt(loc, expr)});436 }437 438 void EnumAttrFuncGenerator::genBoundedFunctions() {439 ast::FunctionDecl * upperDecl = genUpperBoundProto();440 produceForwardDecl(upperDecl);441 genUpperBoundBody(upperDecl);442 produceDecl(upperDecl);443 444 ast::FunctionDecl * lowerDecl = genLowerBoundProto();445 produceForwardDecl(lowerDecl);446 genLowerBoundBody(lowerDecl);447 produceDecl(lowerDecl);448 329 } 449 330 … … 492 373 func->location, "?[?]", 493 374 {new ast::NameExpr(func->location, arrDecl->name), 494 new ast::CastExpr(495 func->location,496 new ast::VariableExpr( func->location, func->params.front() ),497 new ast::EnumAttrType( new ast::EnumInstType(decl),498 ast::EnumAttribute::Posn))});375 new ast::CastExpr( 376 func->location, 377 new ast::VariableExpr( func->location, func->params.front() ), 378 new ast::EnumAttrType( new ast::EnumInstType(decl), 379 ast::EnumAttribute::Posn))}); 499 380 func->stmts = new ast::CompoundStmt( 500 381 func->location, {new ast::ReturnStmt(func->location, untyped)}); … … 569 450 570 451 void EnumAttrFuncGenerator::genAttrFunctions() { 571 genAttributesDecls(ast::EnumAttribute::Value); 572 genAttributesDecls(ast::EnumAttribute::Label); 573 genAttributesDecls(ast::EnumAttribute::Posn); 574 } 575 576 // void EnumAttrFuncGenerator::genSuccPredDecl() { 577 // auto succProto = genSuccProto(); 578 // auto predProto = genPredProto(); 579 580 // produceForwardDecl(succProto); 581 // produceForwardDecl(predProto); 582 // } 452 if (decl->base) { 453 genAttributesDecls(ast::EnumAttribute::Value); 454 genAttributesDecls(ast::EnumAttribute::Label); 455 genAttributesDecls(ast::EnumAttribute::Posn); 456 } 457 } 458 459 void EnumAttrFuncGenerator::genSuccPredDecl() { 460 if (decl->base) { 461 auto succProto = genSuccProto(); 462 auto predProto = genPredProto(); 463 464 produceForwardDecl(succProto); 465 produceForwardDecl(predProto); 466 } 467 } 583 468 584 469 void EnumAttrFuncGenerator::genSuccPredPosn() { 585 ast::FunctionDecl* succ = genSuccPredFunc(true); 586 ast::FunctionDecl* pred = genSuccPredFunc(false); 587 588 produceDecl(succ); 589 produceDecl(pred); 470 if (decl->base) { 471 ast::FunctionDecl* succ = genSuccPredFunc(true); 472 ast::FunctionDecl* pred = genSuccPredFunc(false); 473 474 produceDecl(succ); 475 produceDecl(pred); 476 } 590 477 } 591 478 … … 595 482 genAttrStandardFuncs(); 596 483 genAttrFunctions(); 597 genSerialTraitFuncs(); 598 genSuccPredPosn(); 599 // problematic 600 genBoundedFunctions(); 484 genSuccPredDecl(); 485 genSuccPredPosn(); // Posn 601 486 // Now export the lists contents. 602 487 decls.splice(decls.end(), forwards); -
src/main.cc
r69867ad9 rcaaf424 325 325 PASS( "Validate Generic Parameters", Validate::fillGenericParameters, transUnit ); 326 326 PASS( "Translate Dimensions", Validate::translateDimensionParameters, transUnit ); 327 // Need to happen before fixing returns because implementEnumFunc has ReturnStmt328 329 PASS( "Generate Enum Attributes Functions", Validate::implementEnumFunc, transUnit );330 327 PASS( "Check Function Returns", Validate::checkReturnStatements, transUnit ); 331 328 PASS( "Fix Return Statements", InitTweak::fixReturnStatements, transUnit ); … … 336 333 337 334 PASS( "Generate Autogen Routines", Validate::autogenerateRoutines, transUnit ); 338 335 PASS( "Generate Enum Attributes Functions", Validate::implementEnumFunc, transUnit ); 336 339 337 PASS( "Implement Actors", Concurrency::implementActors, transUnit ); 340 338 PASS( "Implement Virtual Destructors", Virtual::implementVirtDtors, transUnit ); -
tests/enum_tests/.expect/stringEnum.txt
r69867ad9 rcaaf424 1 1 zero: zero, one: one 2 zero: 0, one: 1 -
tests/enum_tests/anonymous.cfa
r69867ad9 rcaaf424 1 #include < fstream.hfa>1 #include <stdio.h> 2 2 enum(unsigned long int ) { nthreads = 17 }; 3 3 4 4 int main() { 5 sout | nthreads;5 printf("%lu", nthreads); 6 6 } 7 7 -
tests/enum_tests/enumInlineValue.cfa
r69867ad9 rcaaf424 12 12 enum enumB val = A; 13 13 sout | "enumB.A is" | val; 14 enum enumB val2 = enumB.B;14 enum enumB val2= enumB.B; 15 15 sout | "enumB.B is" | val2; 16 16 sout | "enumB.D is" | enumB.D; -
tests/enum_tests/position.cfa
r69867ad9 rcaaf424 12 12 13 13 int main () { 14 Colour fishy; 15 fishy = Colour.Green; 16 fishy; 14 Colour fishy; // posE() 15 fishy = Colour.Green; // value(fishy) = Colour.Green; 16 fishy; // valueE( fishy ); 17 18 printf( "Compile Time: blue value: %s, position: %d, label: %s, default return value: %s \n", valueE(Colour.Blue), posE(Colour.Blue), labelE(Colour.Blue), Colour.Blue ); 19 printf( "Runtime: fishy value: %s, position: %d, label: %s, default return value: %s\n", valueE(fishy), posE(fishy), labelE(fishy), fishy ); 17 20 Colour c2 = fishy; 18 19 sout | "Compile Time: blue value: " | valueE(Colour.Blue) | ", position: " | posE(Colour.Blue) | ", label: " | labelE(Colour.Blue) | ", default return value: " | Colour.Blue; 20 sout | "Runtime: fishy value: " | valueE(fishy) | ", position: " | posE(fishy) | ", label: " | labelE(fishy) | ", default return value: " | fishy; 21 sout | "Runtime: C2 value: " | valueE(c2) | ", position: " | posE(c2) | ", label: " | labelE(c2) | ", default return value: " | c2; 21 printf( "Runtime: C2 value: %s, position: %d, label: %s, default return value: %s\n", valueE(c2), posE(c2), labelE(c2), c2 ); 22 22 Colour.Red; 23 23 char * ao = Colour.Red; … … 29 29 Plain a = B; 30 30 printf( "%d \n", a ); 31 32 31 } -
tests/enum_tests/stringEnum.cfa
r69867ad9 rcaaf424 1 #include <fstream.hfa>2 1 #include <stdio.h> 3 2 … … 8 7 9 8 int main() { 10 sout | "zero: "| zero | ", one: " | one; 11 printf("zero: %d, one: %d\n", zero, one); 9 printf("zero: %s, one: %s\n", zero, one); 12 10 return 0; 13 11 } -
tests/enum_tests/typedIntEnum.cfa
r69867ad9 rcaaf424 1 #include < fstream.hfa>1 #include <stdio.h> 2 2 3 3 enum(int) IntEnum { … … 12 12 13 13 int main() { 14 sout | "0=" | zero;15 sout | "1=" | one;16 sout | "1000=" | thousand;17 sout | "1001=" | thousand_one;18 sout | "2000=" | two_thousand;19 sout | "2001=" | two_thousand_one;20 sout | "2002=" | two_thousand_two;14 printf("0=%d\n", zero); 15 printf("1=%d\n", one); 16 printf("1000=%d\n", thousand); 17 printf("1001=%d\n", thousand_one); 18 printf("2000=%d\n", two_thousand); 19 printf("2001=%d\n", two_thousand_one); 20 printf("2002=%d\n", two_thousand_two); 21 21 return 0; 22 22 }
Note:
See TracChangeset
for help on using the changeset viewer.