Changeset c02cef1
- Timestamp:
- Oct 3, 2022, 11:13:43 AM (2 years ago)
- Branches:
- ADT, ast-experimental, master
- Children:
- 4b8b2a4
- Parents:
- f92e7b9
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/AST/Inspect.cpp
rf92e7b9 rc02cef1 10 10 // Created On : Fri Jun 24 13:16:31 2022 11 11 // Last Modified By : Andrew Beach 12 // Last Modified On : Wed Sep 22 13:50:00 202213 // Update Count : 212 // Last Modified On : Mon Oct 3 11:04:00 2022 13 // Update Count : 3 14 14 // 15 15 … … 17 17 18 18 #include <iostream> 19 #include <iterator> 19 20 20 21 #include "AST/Decl.hpp" … … 27 28 namespace ast { 28 29 29 const Type * getPointerBase( const Type * t ) {30 if ( const auto * p = dynamic_cast< const PointerType * >( t ) ) {30 const Type * getPointerBase( const Type * type ) { 31 if ( const auto * p = dynamic_cast< const PointerType * >( type ) ) { 31 32 return p->base; 32 } else if ( const auto * a = dynamic_cast< const ArrayType * >( t) ) {33 } else if ( auto a = dynamic_cast< const ArrayType * >( type ) ) { 33 34 return a->base; 34 } else if ( const auto * r = dynamic_cast< const ReferenceType * >( t) ) {35 } else if ( auto r = dynamic_cast< const ReferenceType * >( type ) ) { 35 36 return r->base; 36 37 } else { 37 38 return nullptr; 38 39 } 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 );51 40 } 52 41 … … 56 45 std::string name = getFunctionName( expr ); 57 46 assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() ); 58 assertf( !expr->args.empty(), "Cannot get function name from dereference with no arguments" );47 assertf( !expr->args.empty(), "Cannot pass through dereference with no arguments." ); 59 48 return func( expr->args.front() ); 60 49 } … … 62 51 static const DeclWithType * getCalledFunction( const Expr * expr ) { 63 52 assert( expr ); 64 if ( const a st::VariableExpr * varExpr = dynamic_cast< const ast::VariableExpr * >( expr ) ) {53 if ( const auto * varExpr = dynamic_cast< const VariableExpr * >( expr ) ) { 65 54 return varExpr->var; 66 } else if ( const ast::MemberExpr * memberExpr = dynamic_cast< const ast::MemberExpr * >( expr ) ) {55 } else if ( auto memberExpr = dynamic_cast< const MemberExpr * >( expr ) ) { 67 56 return memberExpr->member; 68 } else if ( const ast::CastExpr * castExpr = dynamic_cast< const ast::CastExpr * >( expr ) ) {57 } else if ( auto castExpr = dynamic_cast< const CastExpr * >( expr ) ) { 69 58 return getCalledFunction( castExpr->arg ); 70 } else if ( const ast::UntypedExpr * untypedExpr = dynamic_cast< const ast::UntypedExpr * >( expr ) ) {59 } else if ( auto untypedExpr = dynamic_cast< const UntypedExpr * >( expr ) ) { 71 60 return throughDeref( untypedExpr, getCalledFunction ); 72 } else if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * > ( expr ) ) {61 } else if ( auto appExpr = dynamic_cast< const ApplicationExpr * > ( expr ) ) { 73 62 return throughDeref( appExpr, getCalledFunction ); 74 } else if ( const ast::AddressExpr * addrExpr = dynamic_cast< const ast::AddressExpr * >( expr ) ) {63 } else if ( auto addrExpr = dynamic_cast< const AddressExpr * >( expr ) ) { 75 64 return getCalledFunction( addrExpr->arg ); 76 } else if ( const ast::CommaExpr * commaExpr = dynamic_cast< const ast::CommaExpr * >( expr ) ) {65 } else if ( auto commaExpr = dynamic_cast< const CommaExpr * >( expr ) ) { 77 66 return getCalledFunction( commaExpr->arg2 ); 67 } else { 68 return nullptr; 78 69 } 79 return nullptr;80 70 } 81 71 … … 85 75 } else if ( auto untyped = dynamic_cast< const UntypedExpr * >( expr ) ) { 86 76 return getCalledFunction( untyped->func ); 77 } else { 78 assertf( false, "getFunction received unknown expression: %s", toString( expr ).c_str() ); 87 79 } 88 assertf( false, "getFunction received unknown expression: %s", toString( expr ).c_str() );89 80 } 90 81 … … 94 85 static std::string funcName( const Expr * func ) { 95 86 assert( func ); 96 if ( const a st::NameExpr * nameExpr = dynamic_cast< const ast::NameExpr * >( func ) ) {87 if ( const auto * nameExpr = dynamic_cast< const NameExpr * >( func ) ) { 97 88 return nameExpr->name; 98 } else if ( const ast::VariableExpr * varExpr = dynamic_cast< const ast::VariableExpr * >( func ) ) {89 } else if ( auto varExpr = dynamic_cast< const VariableExpr * >( func ) ) { 99 90 return varExpr->var->name; 100 } else if ( const ast::CastExpr * castExpr = dynamic_cast< const ast::CastExpr * >( func ) ) {91 } else if ( auto castExpr = dynamic_cast< const CastExpr * >( func ) ) { 101 92 return funcName( castExpr->arg ); 102 } else if ( const ast::MemberExpr * memberExpr = dynamic_cast< const ast::MemberExpr * >( func ) ) {93 } else if ( auto memberExpr = dynamic_cast< const MemberExpr * >( func ) ) { 103 94 return memberExpr->member->name; 104 } else if ( const ast::UntypedMemberExpr * memberExpr = dynamic_cast< const ast::UntypedMemberExpr * >( func ) ) {95 } else if ( auto memberExpr = dynamic_cast< const UntypedMemberExpr * >( func ) ) { 105 96 return funcName( memberExpr->member ); 106 } else if ( const ast::UntypedExpr * untypedExpr = dynamic_cast< const ast::UntypedExpr * >( func ) ) {97 } else if ( auto untypedExpr = dynamic_cast< const UntypedExpr * >( func ) ) { 107 98 return throughDeref( untypedExpr, funcName ); 108 } else if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * >( func ) ) {99 } else if ( auto appExpr = dynamic_cast< const ApplicationExpr * >( func ) ) { 109 100 return throughDeref( appExpr, funcName ); 110 } else if ( const ast::ConstructorExpr * ctorExpr = dynamic_cast< const ast::ConstructorExpr * >( func ) ) {101 } else if ( auto ctorExpr = dynamic_cast< const ConstructorExpr * >( func ) ) { 111 102 return funcName( getCallArg( ctorExpr->callExpr, 0 ) ); 112 103 } else { … … 116 107 117 108 std::string getFunctionName( const Expr * expr ) { 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. 109 // There's some unforunate overlap here with getFunction. See above. 122 110 if ( auto app = dynamic_cast< const ApplicationExpr * >( expr ) ) { 123 111 return funcName( app->func ); … … 125 113 return funcName( untyped->func ); 126 114 } else { 127 assertf( false, " Unexpected expression type passed to getFunctionName: %s", toString( expr ).c_str() );115 assertf( false, "getFunctionName received unknown expression: %s", toString( expr ).c_str() ); 128 116 } 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; 129 127 } 130 128 … … 137 135 const std::list<ptr<Stmt>>& stmts = tupleAssn->stmtExpr->stmts->kids; 138 136 assertf( !stmts.empty(), "TupleAssignExpr missing statements." ); 139 auto stmt = st rict_dynamic_cast< const ExprStmt * >( stmts.back().get());140 auto tuple = st rict_dynamic_cast< const TupleExpr * >( stmt->expr.get());137 auto stmt = stmts.back().strict_as< ExprStmt >(); 138 auto tuple = stmt->expr.strict_as< TupleExpr >(); 141 139 assertf( !tuple->exprs.empty(), "TupleAssignExpr has empty tuple expr." ); 142 140 return getCallArg( tuple->exprs.front(), pos ); … … 144 142 return getCallArg( ctor->callExpr, pos ); 145 143 } else { 146 assertf( false, "Unexpected expression type passed to getCallArg: %s", 147 toString( call ).c_str() ); 144 assertf( false, "Unexpected expression type passed to getCallArg: %s", toString( call ).c_str() ); 148 145 } 149 146 } … … 165 162 166 163 const DeclWithType * func = getCalledFunction( appExpr->func ); 167 assertf( func, 168 "getCalledFunction returned nullptr: %s",toString( appExpr->func ).c_str() );164 assertf( func, "getCalledFunction returned nullptr: %s", 165 toString( appExpr->func ).c_str() ); 169 166 170 return func->linkage == ast::Linkage::Intrinsic ? appExpr : nullptr;167 return func->linkage == Linkage::Intrinsic ? appExpr : nullptr; 171 168 } 172 169
Note: See TracChangeset
for help on using the changeset viewer.