Changes in src/AST/Inspect.cpp [c02cef1:e01eb4a]
- File:
-
- 1 edited
-
src/AST/Inspect.cpp (modified) (12 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/AST/Inspect.cpp
rc02cef1 re01eb4a 10 10 // Created On : Fri Jun 24 13:16:31 2022 11 11 // Last Modified By : Andrew Beach 12 // Last Modified On : Mon Oct 3 11:04:00 202213 // Update Count : 312 // Last Modified On : Wed Sep 22 13:50:00 2022 13 // Update Count : 2 14 14 // 15 15 … … 17 17 18 18 #include <iostream> 19 #include <iterator>20 19 21 20 #include "AST/Decl.hpp" … … 28 27 namespace ast { 29 28 30 const Type * getPointerBase( const Type * t ype) {31 if ( const auto * p = dynamic_cast< const PointerType * >( t ype) ) {29 const Type * getPointerBase( const Type * t ) { 30 if ( const auto * p = dynamic_cast< const PointerType * >( t ) ) { 32 31 return p->base; 33 } else if ( auto a = dynamic_cast< const ArrayType * >( type) ) {32 } else if ( const auto * a = dynamic_cast< const ArrayType * >( t ) ) { 34 33 return a->base; 35 } else if ( auto r = dynamic_cast< const ReferenceType * >( type) ) {34 } else if ( const auto * r = dynamic_cast< const ReferenceType * >( t ) ) { 36 35 return r->base; 37 36 } else { 38 37 return nullptr; 39 38 } 39 } 40 41 template<typename CallExpr> 42 static const Expr * callArg( const CallExpr * call, unsigned int pos ) { 43 assertf( pos < call->args.size(), 44 "getCallArg for argument that doesn't exist: (%u); %s.", 45 pos, toString( call ).c_str() ); 46 for ( const Expr * arg : call->args ) { 47 if ( 0 == pos ) return arg; 48 --pos; 49 } 50 assert( false ); 40 51 } 41 52 … … 45 56 std::string name = getFunctionName( expr ); 46 57 assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() ); 47 assertf( !expr->args.empty(), "Cannot pass through dereference with no arguments." );58 assertf( !expr->args.empty(), "Cannot get function name from dereference with no arguments" ); 48 59 return func( expr->args.front() ); 49 60 } … … 51 62 static const DeclWithType * getCalledFunction( const Expr * expr ) { 52 63 assert( expr ); 53 if ( const a uto * varExpr = dynamic_cast< constVariableExpr * >( expr ) ) {64 if ( const ast::VariableExpr * varExpr = dynamic_cast< const ast::VariableExpr * >( expr ) ) { 54 65 return varExpr->var; 55 } else if ( auto memberExpr = dynamic_cast< constMemberExpr * >( expr ) ) {66 } else if ( const ast::MemberExpr * memberExpr = dynamic_cast< const ast::MemberExpr * >( expr ) ) { 56 67 return memberExpr->member; 57 } else if ( auto castExpr = dynamic_cast< constCastExpr * >( expr ) ) {68 } else if ( const ast::CastExpr * castExpr = dynamic_cast< const ast::CastExpr * >( expr ) ) { 58 69 return getCalledFunction( castExpr->arg ); 59 } else if ( auto untypedExpr = dynamic_cast< constUntypedExpr * >( expr ) ) {70 } else if ( const ast::UntypedExpr * untypedExpr = dynamic_cast< const ast::UntypedExpr * >( expr ) ) { 60 71 return throughDeref( untypedExpr, getCalledFunction ); 61 } else if ( auto appExpr = dynamic_cast< constApplicationExpr * > ( expr ) ) {72 } else if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * > ( expr ) ) { 62 73 return throughDeref( appExpr, getCalledFunction ); 63 } else if ( auto addrExpr = dynamic_cast< constAddressExpr * >( expr ) ) {74 } else if ( const ast::AddressExpr * addrExpr = dynamic_cast< const ast::AddressExpr * >( expr ) ) { 64 75 return getCalledFunction( addrExpr->arg ); 65 } else if ( auto commaExpr = dynamic_cast< constCommaExpr * >( expr ) ) {76 } else if ( const ast::CommaExpr * commaExpr = dynamic_cast< const ast::CommaExpr * >( expr ) ) { 66 77 return getCalledFunction( commaExpr->arg2 ); 67 } else {68 return nullptr;69 78 } 79 return nullptr; 70 80 } 71 81 … … 75 85 } else if ( auto untyped = dynamic_cast< const UntypedExpr * >( expr ) ) { 76 86 return getCalledFunction( untyped->func ); 77 } else {78 assertf( false, "getFunction received unknown expression: %s", toString( expr ).c_str() );79 87 } 88 assertf( false, "getFunction received unknown expression: %s", toString( expr ).c_str() ); 80 89 } 81 90 … … 85 94 static std::string funcName( const Expr * func ) { 86 95 assert( func ); 87 if ( const a uto * nameExpr = dynamic_cast< constNameExpr * >( func ) ) {96 if ( const ast::NameExpr * nameExpr = dynamic_cast< const ast::NameExpr * >( func ) ) { 88 97 return nameExpr->name; 89 } else if ( auto varExpr = dynamic_cast< constVariableExpr * >( func ) ) {98 } else if ( const ast::VariableExpr * varExpr = dynamic_cast< const ast::VariableExpr * >( func ) ) { 90 99 return varExpr->var->name; 91 } else if ( auto castExpr = dynamic_cast< constCastExpr * >( func ) ) {100 } else if ( const ast::CastExpr * castExpr = dynamic_cast< const ast::CastExpr * >( func ) ) { 92 101 return funcName( castExpr->arg ); 93 } else if ( auto memberExpr = dynamic_cast< constMemberExpr * >( func ) ) {102 } else if ( const ast::MemberExpr * memberExpr = dynamic_cast< const ast::MemberExpr * >( func ) ) { 94 103 return memberExpr->member->name; 95 } else if ( auto memberExpr = dynamic_cast< const UntypedMemberExpr * >( func ) ) {104 } else if ( const ast::UntypedMemberExpr * memberExpr = dynamic_cast< const ast::UntypedMemberExpr * > ( func ) ) { 96 105 return funcName( memberExpr->member ); 97 } else if ( auto untypedExpr = dynamic_cast< constUntypedExpr * >( func ) ) {106 } else if ( const ast::UntypedExpr * untypedExpr = dynamic_cast< const ast::UntypedExpr * >( func ) ) { 98 107 return throughDeref( untypedExpr, funcName ); 99 } else if ( auto appExpr = dynamic_cast< constApplicationExpr * >( func ) ) {108 } else if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * >( func ) ) { 100 109 return throughDeref( appExpr, funcName ); 101 } else if ( auto ctorExpr = dynamic_cast< constConstructorExpr * >( func ) ) {110 } else if ( const ast::ConstructorExpr * ctorExpr = dynamic_cast< const ast::ConstructorExpr * >( func ) ) { 102 111 return funcName( getCallArg( ctorExpr->callExpr, 0 ) ); 103 112 } else { … … 107 116 108 117 std::string getFunctionName( const Expr * expr ) { 109 // There's some unforunate overlap here with getFunction. See above. 118 // There's some unforunate overlap here with getCalledFunction. Ideally 119 // this would be able to use getCalledFunction and return the name of the 120 // DeclWithType, but this needs to work for NameExpr and UntypedMemberExpr, 121 // where getCalledFunction can't possibly do anything reasonable. 110 122 if ( auto app = dynamic_cast< const ApplicationExpr * >( expr ) ) { 111 123 return funcName( app->func ); … … 113 125 return funcName( untyped->func ); 114 126 } else { 115 assertf( false, " getFunctionName received unknown expression: %s", toString( expr ).c_str() );127 assertf( false, "Unexpected expression type passed to getFunctionName: %s", toString( expr ).c_str() ); 116 128 } 117 }118 119 template<typename CallExpr>120 static const Expr * callArg( const CallExpr * call, unsigned int pos ) {121 assertf( pos < call->args.size(),122 "callArg for argument that doesn't exist: (%u); %s.",123 pos, toString( call ).c_str() );124 auto it = call->args.begin();125 std::advance( it, pos );126 return *it;127 129 } 128 130 … … 135 137 const std::list<ptr<Stmt>>& stmts = tupleAssn->stmtExpr->stmts->kids; 136 138 assertf( !stmts.empty(), "TupleAssignExpr missing statements." ); 137 auto stmt = st mts.back().strict_as< ExprStmt >();138 auto tuple = st mt->expr.strict_as< TupleExpr >();139 auto stmt = strict_dynamic_cast< const ExprStmt * >( stmts.back().get() ); 140 auto tuple = strict_dynamic_cast< const TupleExpr * >( stmt->expr.get() ); 139 141 assertf( !tuple->exprs.empty(), "TupleAssignExpr has empty tuple expr." ); 140 142 return getCallArg( tuple->exprs.front(), pos ); … … 142 144 return getCallArg( ctor->callExpr, pos ); 143 145 } else { 144 assertf( false, "Unexpected expression type passed to getCallArg: %s", toString( call ).c_str() ); 146 assertf( false, "Unexpected expression type passed to getCallArg: %s", 147 toString( call ).c_str() ); 145 148 } 146 149 } … … 162 165 163 166 const DeclWithType * func = getCalledFunction( appExpr->func ); 164 assertf( func, "getCalledFunction returned nullptr: %s",165 toString( appExpr->func ).c_str() );167 assertf( func, 168 "getCalledFunction returned nullptr: %s", toString( appExpr->func ).c_str() ); 166 169 167 return func->linkage == Linkage::Intrinsic ? appExpr : nullptr;170 return func->linkage == ast::Linkage::Intrinsic ? appExpr : nullptr; 168 171 } 169 172
Note:
See TracChangeset
for help on using the changeset viewer.