Changeset e01eb4a for src/InitTweak/InitTweak.cc
- Timestamp:
- Sep 22, 2022, 2:23:18 PM (21 months ago)
- Branches:
- ADT, ast-experimental, master
- Children:
- 5d8dae7
- Parents:
- 0bd46fd
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/InitTweak/InitTweak.cc
r0bd46fd re01eb4a 10 10 // Created On : Fri May 13 11:26:36 2016 11 11 // Last Modified By : Andrew Beach 12 // Last Modified On : Mon Dec 6 13:21:00 202113 // Update Count : 2 012 // Last Modified On : Wed Sep 22 9:50:00 2022 13 // Update Count : 21 14 14 // 15 15 … … 23 23 #include "AST/Expr.hpp" 24 24 #include "AST/Init.hpp" 25 #include "AST/Inspect.hpp" 25 26 #include "AST/Node.hpp" 26 27 #include "AST/Pass.hpp" … … 654 655 namespace { 655 656 DeclarationWithType * getCalledFunction( Expression * expr ); 656 const ast::DeclWithType * getCalledFunction( const ast::Expr * expr );657 657 658 658 template<typename CallExpr> … … 664 664 return getCalledFunction( expr->get_args().front() ); 665 665 } 666 667 template<typename CallExpr>668 const ast::DeclWithType * handleDerefCalledFunction( const CallExpr * expr ) {669 // (*f)(x) => should get "f"670 std::string name = getFunctionName( expr );671 assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() );672 assertf( ! expr->args.empty(), "Cannot get called function from dereference with no arguments" );673 return getCalledFunction( expr->args.front() );674 }675 676 666 677 667 DeclarationWithType * getCalledFunction( Expression * expr ) { … … 695 685 } 696 686 697 const ast::DeclWithType * getCalledFunction( const ast::Expr * expr ) {698 assert( expr );699 if ( const ast::VariableExpr * varExpr = dynamic_cast< const ast::VariableExpr * >( expr ) ) {700 return varExpr->var;701 } else if ( const ast::MemberExpr * memberExpr = dynamic_cast< const ast::MemberExpr * >( expr ) ) {702 return memberExpr->member;703 } else if ( const ast::CastExpr * castExpr = dynamic_cast< const ast::CastExpr * >( expr ) ) {704 return getCalledFunction( castExpr->arg );705 } else if ( const ast::UntypedExpr * untypedExpr = dynamic_cast< const ast::UntypedExpr * >( expr ) ) {706 return handleDerefCalledFunction( untypedExpr );707 } else if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * > ( expr ) ) {708 return handleDerefCalledFunction( appExpr );709 } else if ( const ast::AddressExpr * addrExpr = dynamic_cast< const ast::AddressExpr * >( expr ) ) {710 return getCalledFunction( addrExpr->arg );711 } else if ( const ast::CommaExpr * commaExpr = dynamic_cast< const ast::CommaExpr * >( expr ) ) {712 return getCalledFunction( commaExpr->arg2 );713 }714 return nullptr;715 }716 717 687 DeclarationWithType * getFunctionCore( const Expression * expr ) { 718 688 if ( const auto * appExpr = dynamic_cast< const ApplicationExpr * >( expr ) ) { … … 731 701 const DeclarationWithType * getFunction( const Expression * expr ) { 732 702 return getFunctionCore( expr ); 733 }734 735 const ast::DeclWithType * getFunction( const ast::Expr * expr ) {736 if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * >( expr ) ) {737 return getCalledFunction( appExpr->func );738 } else if ( const ast::UntypedExpr * untyped = dynamic_cast< const ast::UntypedExpr * > ( expr ) ) {739 return getCalledFunction( untyped->func );740 }741 assertf( false, "getFunction received unknown expression: %s", toString( expr ).c_str() );742 703 } 743 704 … … 752 713 } 753 714 754 const ast::ApplicationExpr * isIntrinsicCallExpr( const ast::Expr * expr ) {755 auto appExpr = dynamic_cast< const ast::ApplicationExpr * >( expr );756 if ( ! appExpr ) return nullptr;757 758 const ast::DeclWithType * func = getCalledFunction( appExpr->func );759 assertf( func,760 "getCalledFunction returned nullptr: %s", toString( appExpr->func ).c_str() );761 762 // check for Intrinsic only -- don't want to remove all overridable ctor/dtor because763 // autogenerated ctor/dtor will call all member dtors, and some members may have a764 // user-defined dtor765 return func->linkage == ast::Linkage::Intrinsic ? appExpr : nullptr;766 }767 768 715 namespace { 769 716 template <typename Predicate> … … 817 764 if ( pos == 0 ) return arg; 818 765 pos--; 819 }820 assert( false );821 }822 823 template<typename CallExpr>824 const ast::Expr * callArg( const CallExpr * call, unsigned int pos ) {825 if( pos >= call->args.size() ) {826 assertf( false, "getCallArg for argument that doesn't exist: (%u); %s.",827 pos, toString( call ).c_str() );828 }829 for ( const ast::Expr * arg : call->args ) {830 if ( pos == 0 ) return arg;831 --pos;832 766 } 833 767 assert( false ); … … 854 788 } 855 789 856 const ast::Expr * getCallArg( const ast::Expr * call, unsigned pos ) {857 if ( auto app = dynamic_cast< const ast::ApplicationExpr * >( call ) ) {858 return callArg( app, pos );859 } else if ( auto untyped = dynamic_cast< const ast::UntypedExpr * >( call ) ) {860 return callArg( untyped, pos );861 } else if ( auto tupleAssn = dynamic_cast< const ast::TupleAssignExpr * >( call ) ) {862 const std::list<ast::ptr<ast::Stmt>>& stmts = tupleAssn->stmtExpr->stmts->kids;863 assertf( ! stmts.empty(), "TupleAssignExpr missing statements." );864 auto stmt = strict_dynamic_cast< const ast::ExprStmt * >( stmts.back().get() );865 auto tuple = strict_dynamic_cast< const ast::TupleExpr * >( stmt->expr.get() );866 assertf( ! tuple->exprs.empty(), "TupleAssignExpr has empty tuple expr.");867 return getCallArg( tuple->exprs.front(), pos );868 } else if ( auto ctor = dynamic_cast< const ast::ImplicitCopyCtorExpr * >( call ) ) {869 return getCallArg( ctor->callExpr, pos );870 } else {871 assertf( false, "Unexpected expression type passed to getCallArg: %s",872 toString( call ).c_str() );873 }874 }875 876 790 namespace { 877 791 std::string funcName( Expression * func ); 878 std::string funcName( const ast::Expr * func );879 792 880 793 template<typename CallExpr> … … 885 798 assertf( ! expr->get_args().empty(), "Cannot get function name from dereference with no arguments" ); 886 799 return funcName( expr->get_args().front() ); 887 }888 889 template<typename CallExpr>890 std::string handleDerefName( const CallExpr * expr ) {891 // (*f)(x) => should get name "f"892 std::string name = getFunctionName( expr );893 assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() );894 assertf( ! expr->args.empty(), "Cannot get function name from dereference with no arguments" );895 return funcName( expr->args.front() );896 800 } 897 801 … … 917 821 } 918 822 } 919 920 std::string funcName( const ast::Expr * func ) {921 if ( const ast::NameExpr * nameExpr = dynamic_cast< const ast::NameExpr * >( func ) ) {922 return nameExpr->name;923 } else if ( const ast::VariableExpr * varExpr = dynamic_cast< const ast::VariableExpr * >( func ) ) {924 return varExpr->var->name;925 } else if ( const ast::CastExpr * castExpr = dynamic_cast< const ast::CastExpr * >( func ) ) {926 return funcName( castExpr->arg );927 } else if ( const ast::MemberExpr * memberExpr = dynamic_cast< const ast::MemberExpr * >( func ) ) {928 return memberExpr->member->name;929 } else if ( const ast::UntypedMemberExpr * memberExpr = dynamic_cast< const ast::UntypedMemberExpr * > ( func ) ) {930 return funcName( memberExpr->member );931 } else if ( const ast::UntypedExpr * untypedExpr = dynamic_cast< const ast::UntypedExpr * >( func ) ) {932 return handleDerefName( untypedExpr );933 } else if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * >( func ) ) {934 return handleDerefName( appExpr );935 } else if ( const ast::ConstructorExpr * ctorExpr = dynamic_cast< const ast::ConstructorExpr * >( func ) ) {936 return funcName( getCallArg( ctorExpr->callExpr, 0 ) );937 } else {938 assertf( false, "Unexpected expression type being called as a function in call expression: %s", toString( func ).c_str() );939 }940 }941 823 } 942 824 … … 955 837 } 956 838 957 std::string getFunctionName( const ast::Expr * expr ) {958 // there's some unforunate overlap here with getCalledFunction. Ideally this would be able to use getCalledFunction and959 // return the name of the DeclarationWithType, but this needs to work for NameExpr and UntypedMemberExpr, where getCalledFunction960 // can't possibly do anything reasonable.961 if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * >( expr ) ) {962 return funcName( appExpr->func );963 } else if ( const ast::UntypedExpr * untypedExpr = dynamic_cast< const ast::UntypedExpr * > ( expr ) ) {964 return funcName( untypedExpr->func );965 } else {966 std::cerr << expr << std::endl;967 assertf( false, "Unexpected expression type passed to getFunctionName" );968 }969 }970 971 839 Type * getPointerBase( Type * type ) { 972 840 if ( PointerType * ptrType = dynamic_cast< PointerType * >( type ) ) { … … 979 847 return nullptr; 980 848 } 981 }982 const ast::Type* getPointerBase( const ast::Type* t ) {983 if ( const auto * p = dynamic_cast< const ast::PointerType * >( t ) ) {984 return p->base;985 } else if ( const auto * a = dynamic_cast< const ast::ArrayType * >( t ) ) {986 return a->base;987 } else if ( const auto * r = dynamic_cast< const ast::ReferenceType * >( t ) ) {988 return r->base;989 } else return nullptr;990 849 } 991 850 … … 1203 1062 if ( ftype->params.size() != 2 ) return false; 1204 1063 1205 const ast::Type * t1 = getPointerBase( ftype->params.front() );1064 const ast::Type * t1 = ast::getPointerBase( ftype->params.front() ); 1206 1065 if ( ! t1 ) return false; 1207 1066 const ast::Type * t2 = ftype->params.back(); 1208 1067 1209 return ResolvExpr::typesCompatibleIgnoreQualifiers( t1, t2, ast::SymbolTable {});1068 return ResolvExpr::typesCompatibleIgnoreQualifiers( t1, t2, ast::SymbolTable() ); 1210 1069 } 1070 1211 1071 1212 1072 const FunctionDecl * isAssignment( const Declaration * decl ) {
Note: See TracChangeset
for help on using the changeset viewer.