Changeset 59c8dff
- Timestamp:
- Jan 19, 2024, 2:42:58 AM (20 months ago)
- Branches:
- master
- Children:
- f988834
- Parents:
- 8b4faf6
- Location:
- src
- Files:
-
- 20 edited
Legend:
- Unmodified
- Added
- Removed
-
src/AST/Expr.cpp
r8b4faf6 r59c8dff 307 307 } 308 308 309 // 310 311 // --- EnumPosExpr 312 EnumPosExpr::EnumPosExpr( const CodeLocation & loc, const EnumInstType * ty) 313 : Expr( loc, new BasicType{ BasicType::UnsignedInt }), type( ty ) { 314 assert( ty ); 315 } 316 317 EnumPosExpr::EnumPosExpr( const CodeLocation & loc, const Expr * expr ) 318 : Expr( loc, new BasicType{ BasicType::UnsignedInt }), expr(expr) { 319 assert( expr ); 320 } 321 322 309 323 // --- LogicalExpr 310 324 -
src/AST/Expr.hpp
r8b4faf6 r59c8dff 548 548 }; 549 549 550 class EnumPosExpr final : public Expr { 551 public: 552 ptr<EnumInstType> type; 553 ptr<Expr> expr; 554 555 EnumPosExpr( const CodeLocation & loc, const EnumInstType * ty ); 556 EnumPosExpr( const CodeLocation & loc, const Expr * expr ); 557 const Expr * accept( Visitor & v ) const override { return v.visit( this ); } 558 private: 559 EnumPosExpr * clone() const override { return new EnumPosExpr{ *this }; } 560 MUTATE_FRIEND 561 }; 562 550 563 /// Variants of short-circuiting logical expression 551 564 enum LogicalFlag { OrExpr, AndExpr }; -
src/AST/Fwd.hpp
r8b4faf6 r59c8dff 89 89 class OffsetofExpr; 90 90 class OffsetPackExpr; 91 class EnumPosExpr; 91 92 class LogicalExpr; 92 93 class ConditionalExpr; -
src/AST/Node.cpp
r8b4faf6 r59c8dff 232 232 template class ast::ptr_base< ast::OffsetPackExpr, ast::Node::ref_type::weak >; 233 233 template class ast::ptr_base< ast::OffsetPackExpr, ast::Node::ref_type::strong >; 234 template class ast::ptr_base< ast::EnumPosExpr, ast::Node::ref_type::weak >; 235 template class ast::ptr_base< ast::EnumPosExpr, ast::Node::ref_type::strong >; 234 236 template class ast::ptr_base< ast::LogicalExpr, ast::Node::ref_type::weak >; 235 237 template class ast::ptr_base< ast::LogicalExpr, ast::Node::ref_type::strong >; -
src/AST/Pass.hpp
r8b4faf6 r59c8dff 191 191 const ast::Expr * visit( const ast::OffsetofExpr * ) override final; 192 192 const ast::Expr * visit( const ast::OffsetPackExpr * ) override final; 193 const ast::Expr * visit( const ast::EnumPosExpr * ) override final; 193 194 const ast::Expr * visit( const ast::LogicalExpr * ) override final; 194 195 const ast::Expr * visit( const ast::ConditionalExpr * ) override final; -
src/AST/Pass.impl.hpp
r8b4faf6 r59c8dff 1452 1452 1453 1453 //-------------------------------------------------------------------------- 1454 // EnumPosExpr 1455 template< typename core_t> 1456 const ast::Expr * ast::Pass< core_t >::visit( const ast::EnumPosExpr * node ) { 1457 VISIT_START( node ); 1458 1459 if ( __visit_children() ) { 1460 guard_symtab guard { *this }; 1461 maybe_accept( node, &EnumPosExpr::type ); 1462 maybe_accept( node, &EnumPosExpr::expr ); 1463 } 1464 1465 VISIT_END( Expr, node ); 1466 } 1467 1468 //-------------------------------------------------------------------------- 1454 1469 // LogicalExpr 1455 1470 template< typename core_t > -
src/AST/Print.cpp
r8b4faf6 r59c8dff 1183 1183 } 1184 1184 1185 virtual const ast::Expr * visit( const ast::EnumPosExpr * node ) override final { 1186 os << "Enum Position Expression on: "; 1187 ++indent; 1188 safe_print( node->type ); 1189 --indent; 1190 postprint( node ); 1191 return node; 1192 } 1193 1185 1194 virtual const ast::Expr * visit( const ast::LogicalExpr * node ) override final { 1186 1195 os << "Short-circuited operation (" << (node->isAnd ? "and" : "or") << ") on: "; -
src/AST/Visitor.hpp
r8b4faf6 r59c8dff 79 79 virtual const ast::Expr * visit( const ast::OffsetofExpr * ) = 0; 80 80 virtual const ast::Expr * visit( const ast::OffsetPackExpr * ) = 0; 81 virtual const ast::Expr * visit( const ast::EnumPosExpr * ) = 0; 81 82 virtual const ast::Expr * visit( const ast::LogicalExpr * ) = 0; 82 83 virtual const ast::Expr * visit( const ast::ConditionalExpr * ) = 0; -
src/CodeGen/CodeGenerator.cpp
r8b4faf6 r59c8dff 778 778 } 779 779 780 void CodeGenerator::postvisit( ast::EnumPosExpr const * expr ) { 781 assertf( !options.genC, "EnumPosExpr should not reach code generation." ); 782 output << "__CFA_enumeration_pos(" << genType( expr->type, "", options ) << ")"; 783 } 784 780 785 void CodeGenerator::postvisit( ast::LogicalExpr const * expr ) { 781 786 extension( expr ); -
src/CodeGen/CodeGenerator.hpp
r8b4faf6 r59c8dff 76 76 void postvisit( ast::OffsetPackExpr const * ); 77 77 void postvisit( ast::LogicalExpr const * ); 78 void postvisit( ast::EnumPosExpr const * ); 78 79 void postvisit( ast::ConditionalExpr const * ); 79 80 void postvisit( ast::CommaExpr const * ); -
src/Common/CodeLocationTools.cpp
r8b4faf6 r59c8dff 158 158 macro(OffsetPackExpr, Expr) \ 159 159 macro(LogicalExpr, Expr) \ 160 macro(EnumPosExpr, Expr) \ 160 161 macro(ConditionalExpr, Expr) \ 161 162 macro(CommaExpr, Expr) \ -
src/GenPoly/Box.cpp
r8b4faf6 r59c8dff 1580 1580 ast::Expr const * postvisit( ast::OffsetPackExpr const * expr ); 1581 1581 1582 ast::Expr const * postvisit( ast::EnumPosExpr const * expr ); 1583 1582 1584 void beginScope(); 1583 1585 void endScope(); … … 1950 1952 1951 1953 return new ast::VariableExpr( expr->location, offsetArray ); 1954 } 1955 1956 // TODO 1957 ast::Expr const * PolyGenericCalculator::postvisit( ast::EnumPosExpr const * expr ) { 1958 return expr; 1952 1959 } 1953 1960 -
src/InitTweak/InitTweak.cc
r8b4faf6 r59c8dff 271 271 void previsit( const ast::OffsetofExpr * ) {} 272 272 void previsit( const ast::OffsetPackExpr * ) {} 273 void previsit( const ast::EnumPosExpr * ) {} 273 274 void previsit( const ast::CommaExpr * ) {} 274 275 void previsit( const ast::LogicalExpr * ) {} -
src/Parser/ExpressionNode.cc
r8b4faf6 r59c8dff 690 690 } // build_unary_val 691 691 692 ast::Expr * build_enum_pos_expr( const CodeLocation & location, ast::Expr * expr_node ) { 693 // return nullptr 694 return new ast::EnumPosExpr( location, std::move( expr_node ) ); 695 } 696 692 697 ast::Expr * build_binary_val( const CodeLocation & location, 693 698 OperKinds op, -
src/Parser/ExpressionNode.h
r8b4faf6 r59c8dff 83 83 ast::Expr * build_func( const CodeLocation &, ExpressionNode * function, ExpressionNode * expr_node ); 84 84 ast::Expr * build_compoundLiteral( const CodeLocation &, DeclarationNode * decl_node, InitializerNode * kids ); 85 86 ast::Expr * build_enum_pos_expr( const CodeLocation &, ast::Expr * expr_node ); -
src/ResolvExpr/CandidateFinder.cpp
r8b4faf6 r59c8dff 52 52 #include "Common/Stats/Counter.h" 53 53 54 #include "AST/Inspect.hpp" // for getFunctionName 55 54 56 #define PRINT( text ) if ( resolvep ) { text } 55 57 … … 656 658 void postvisit( const ast::OffsetofExpr * offsetofExpr ); 657 659 void postvisit( const ast::OffsetPackExpr * offsetPackExpr ); 660 void postvisit( const ast::EnumPosExpr * enumPosExpr ); 658 661 void postvisit( const ast::LogicalExpr * logicalExpr ); 659 662 void postvisit( const ast::ConditionalExpr * conditionalExpr ); … … 1471 1474 void Finder::postvisit( const ast::OffsetPackExpr * offsetPackExpr ) { 1472 1475 addCandidate( offsetPackExpr, tenv ); 1476 } 1477 1478 void Finder::postvisit( const ast::EnumPosExpr * enumPosExpr ) { 1479 CandidateFinder finder( context, tenv ); 1480 finder.find( enumPosExpr->expr ); 1481 CandidateList winners = findMinCost( finder.candidates ); 1482 if ( winners.size() != 1 ) SemanticError( enumPosExpr->expr.get(), "Ambiguous expression in position. "); 1483 CandidateRef & choice = winners.front(); 1484 auto refExpr = referenceToRvalueConversion( choice->expr, choice->cost ); 1485 auto refResult = (refExpr->result).as<ast::EnumInstType>(); 1486 if ( !refResult ) { 1487 SemanticError( refExpr, "Position for Non enum type is not supported" ); 1488 } 1489 // determineEnumPosConstant( enumPosExpr, refResult ); 1490 1491 const ast::NameExpr * const nameExpr = enumPosExpr->expr.strict_as<ast::NameExpr>(); 1492 const ast::EnumDecl * base = refResult->base; 1493 if ( !base ) { 1494 SemanticError( enumPosExpr, "Cannot be reference to a defined enumeration type" ); 1495 } 1496 auto it = std::find_if( std::begin( base->members ), std::end( base->members ), 1497 [nameExpr]( ast::ptr<ast::Decl> decl ) { return decl->name == nameExpr->name; } ); 1498 unsigned position = it - base->members.begin(); 1499 addCandidate( ast::ConstantExpr::from_int( enumPosExpr->location, position ), tenv ); 1473 1500 } 1474 1501 -
src/ResolvExpr/Resolver.cc
r8b4faf6 r59c8dff 420 420 const ast::ConstructorInit * previsit( const ast::ConstructorInit * ); 421 421 422 const ast::EnumPosExpr * previsit( const ast::EnumPosExpr * ); 423 422 424 void resolveWithExprs(std::vector<ast::ptr<ast::Expr>> & exprs, std::list<ast::ptr<ast::Stmt>> & stmtsToAdd); 423 425 … … 1239 1241 } 1240 1242 1243 const ast::EnumPosExpr * Resolver::previsit( const ast::EnumPosExpr * enumPos ) { 1244 visitor->maybe_accept( enumPos, &ast::EnumPosExpr::expr ); 1245 return enumPos; 1246 } 1247 1241 1248 // suppress error on autogen functions and mark invalid autogen as deleted. 1242 1249 bool Resolver::on_error(ast::ptr<ast::Decl> & decl) { -
src/Validate/Autogen.cpp
r8b4faf6 r59c8dff 186 186 // could possibly use a new linkage type. For now we just make the 187 187 // basic ones intrinsic to code-gen them as C assignments. 188 const auto & real_type = decl->base; 189 const auto & basic = real_type.as<ast::BasicType>(); 190 if(!real_type || (basic && basic->isInteger())) proto_linkage = ast::Linkage::Intrinsic; 188 // const auto & real_type = decl->base; 189 // const auto & basic = real_type.as<ast::BasicType>(); 190 191 // if(!real_type || (basic && basic->isInteger())) proto_linkage = ast::Linkage::Intrinsic; 192 193 // Turns other enumeration type into Intrinstic as well to temporarily fix the recursive 194 // construction bug 195 proto_linkage = ast::Linkage::Intrinsic; 191 196 } 192 197 … … 742 747 } 743 748 ); 749 // auto fname = ast::getFunctionName( callExpr ); 750 // if (fname == "posE" ) { 751 // std::cerr << "Found posE autogen" << std::endl; 752 // } 744 753 functionDecl->stmts = new ast::CompoundStmt( location, 745 754 { new ast::ExprStmt( location, callExpr ) } … … 792 801 } 793 802 803 struct PseudoFuncGenerateRoutine final : 804 public ast::WithDeclsToAdd<>, 805 public ast::WithShortCircuiting { 806 void previsit( const ast::EnumDecl * enumDecl ); 807 }; 808 809 void PseudoFuncGenerateRoutine::previsit( const ast::EnumDecl * enumDecl ) { 810 const CodeLocation& location = enumDecl->location; 811 if ( enumDecl->members.size() == 0 || !enumDecl->base || enumDecl->name == "" ) return; 812 813 std::vector<ast::ptr<ast::Init>> inits; 814 std::vector<ast::ptr<ast::Init>> labels; 815 for ( const ast::Decl * mem: enumDecl->members ) { 816 auto memAsObjectDecl = dynamic_cast< const ast::ObjectDecl * >( mem ); 817 inits.emplace_back( memAsObjectDecl->init ); 818 labels.emplace_back( new ast::SingleInit( 819 location, ast::ConstantExpr::from_string(location, mem->name) ) ); 820 } 821 auto init = new ast::ListInit( location, std::move( inits ) ); 822 auto label_strings = new ast::ListInit( location, std::move(labels) ); 823 auto values = new ast::ObjectDecl( 824 location, 825 "__enum_values_"+enumDecl->name, 826 new ast::ArrayType( 827 // new ast::PointerType( new ast::BasicType{ ast::BasicType::Char} ), 828 enumDecl->base, 829 ast::ConstantExpr::from_int( location, enumDecl->members.size() ), 830 ast::LengthFlag::FixedLen, ast::DimensionFlag::DynamicDim 831 ) 832 ,init 833 , 834 ast::Storage::Static, 835 ast::Linkage::AutoGen 836 ); 837 auto label_arr = new ast::ObjectDecl( 838 location, 839 "__enum_labels_"+enumDecl->name, 840 new ast::ArrayType( 841 new ast::PointerType( new ast::BasicType{ ast::BasicType::Char} ), 842 ast::ConstantExpr::from_int( location, enumDecl->members.size() ), 843 ast::LengthFlag::FixedLen, ast::DimensionFlag::DynamicDim 844 ), 845 label_strings, 846 ast::Storage::Static, 847 ast::Linkage::AutoGen 848 ); 849 850 declsToAddAfter.push_back( values ); 851 declsToAddAfter.push_back( label_arr ); 852 } 853 794 854 } // namespace 795 855 796 856 void autogenerateRoutines( ast::TranslationUnit & translationUnit ) { 797 857 ast::Pass<AutogenerateRoutines>::run( translationUnit ); 858 // ast::Pass<PseudoFuncGenerateRoutine>::run( translationUnit ); 798 859 } 799 860 -
src/Validate/module.mk
r8b4faf6 r59c8dff 52 52 Validate/ReturnCheck.hpp \ 53 53 Validate/VerifyCtorDtorAssign.cpp \ 54 Validate/VerifyCtorDtorAssign.hpp 54 Validate/VerifyCtorDtorAssign.hpp \ 55 Validate/ReplacePseudoFunc.cpp \ 56 Validate/ReplacePseudoFunc.hpp 55 57 56 58 SRCDEMANGLE += $(SRC_VALIDATE) -
src/main.cc
r8b4faf6 r59c8dff 82 82 #include "Validate/ReturnCheck.hpp" // for checkReturnStatements 83 83 #include "Validate/VerifyCtorDtorAssign.hpp" // for verifyCtorDtorAssign 84 #include "Validate/ReplacePseudoFunc.hpp" 84 85 #include "Virtual/ExpandCasts.h" // for expandCasts 85 86 #include "Virtual/VirtualDtor.hpp" // for implementVirtDtors … … 287 288 assertf( extras, "cannot open extras.cf\n" ); 288 289 parse( extras, ast::Linkage::BuiltinC ); 289 290 290 if ( ! libcfap ) { 291 291 // read the prelude in, if not generating the cfa library … … 322 322 PASS( "Forall Pointer Decay", Validate::decayForallPointers, transUnit ); 323 323 PASS( "Fix Qualified Types", Validate::fixQualifiedTypes, transUnit ); 324 324 325 PASS( "Eliminate Typedef", Validate::eliminateTypedef, transUnit ); 325 326 PASS( "Hoist Struct", Validate::hoistStruct, transUnit ); … … 381 382 PASS( "Resolve", ResolvExpr::resolve, transUnit ); 382 383 DUMP( exprp, std::move( transUnit ) ); 384 PASS( "Replace Pseudo Func", Validate::replacePseudoFunc, transUnit ); 383 385 384 386 PASS( "Fix Init", InitTweak::fix, transUnit, buildingLibrary() );
Note:
See TracChangeset
for help on using the changeset viewer.