[d180746] | 1 | #include <algorithm> // for find, all_of |
---|
[e3e16bc] | 2 | #include <cassert> // for assertf, assert, strict_dynamic_cast |
---|
[d180746] | 3 | #include <iostream> // for ostream, cerr, endl |
---|
| 4 | #include <iterator> // for back_insert_iterator, back_inserter |
---|
| 5 | #include <memory> // for __shared_ptr |
---|
| 6 | |
---|
[9b4f329] | 7 | #include "AST/Expr.hpp" |
---|
| 8 | #include "AST/Stmt.hpp" |
---|
[9e1d485] | 9 | #include "AST/Type.hpp" |
---|
[fd236ed] | 10 | #include "Common/PassVisitor.h" |
---|
[d180746] | 11 | #include "Common/SemanticError.h" // for SemanticError |
---|
| 12 | #include "Common/UniqueName.h" // for UniqueName |
---|
| 13 | #include "Common/utility.h" // for toString, deleteAll, maybeClone |
---|
| 14 | #include "GenPoly/GenPoly.h" // for getFunctionType |
---|
[2b46a13] | 15 | #include "InitTweak.h" |
---|
[d180746] | 16 | #include "Parser/LinkageSpec.h" // for Spec, isBuiltin, Intrinsic |
---|
| 17 | #include "ResolvExpr/typeops.h" // for typesCompatibleIgnoreQualifiers |
---|
[f5c3b6c] | 18 | #include "SymTab/Autogen.h" |
---|
[d180746] | 19 | #include "SymTab/Indexer.h" // for Indexer |
---|
| 20 | #include "SynTree/Attribute.h" // for Attribute |
---|
| 21 | #include "SynTree/Constant.h" // for Constant |
---|
| 22 | #include "SynTree/Declaration.h" // for ObjectDecl, DeclarationWithType |
---|
| 23 | #include "SynTree/Expression.h" // for Expression, UntypedExpr, Applicati... |
---|
| 24 | #include "SynTree/Initializer.h" // for Initializer, ListInit, Designation |
---|
[ba3706f] | 25 | #include "SynTree/Label.h" // for Label |
---|
[d180746] | 26 | #include "SynTree/Statement.h" // for CompoundStmt, ExprStmt, BranchStmt |
---|
| 27 | #include "SynTree/Type.h" // for FunctionType, ArrayType, PointerType |
---|
| 28 | #include "SynTree/Visitor.h" // for Visitor, maybeAccept |
---|
[29bc63e] | 29 | #include "Tuples/Tuples.h" // for Tuples::isTtype |
---|
[d180746] | 30 | |
---|
[2b46a13] | 31 | namespace InitTweak { |
---|
[64071c2] | 32 | namespace { |
---|
[1fbeebd] | 33 | struct HasDesignations : public WithShortCircuiting { |
---|
[64071c2] | 34 | bool hasDesignations = false; |
---|
[fd236ed] | 35 | |
---|
| 36 | void previsit( BaseSyntaxNode * ) { |
---|
| 37 | // short circuit if we already know there are designations |
---|
| 38 | if ( hasDesignations ) visit_children = false; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | void previsit( Designation * des ) { |
---|
| 42 | // short circuit if we already know there are designations |
---|
| 43 | if ( hasDesignations ) visit_children = false; |
---|
| 44 | else if ( ! des->get_designators().empty() ) { |
---|
| 45 | hasDesignations = true; |
---|
| 46 | visit_children = false; |
---|
| 47 | } |
---|
[64071c2] | 48 | } |
---|
| 49 | }; |
---|
[2b46a13] | 50 | |
---|
[ef3d798] | 51 | struct InitDepthChecker : public WithGuards { |
---|
[dcd73d1] | 52 | bool depthOkay = true; |
---|
| 53 | Type * type; |
---|
| 54 | int curDepth = 0, maxDepth = 0; |
---|
| 55 | InitDepthChecker( Type * type ) : type( type ) { |
---|
| 56 | Type * t = type; |
---|
| 57 | while ( ArrayType * at = dynamic_cast< ArrayType * >( t ) ) { |
---|
| 58 | maxDepth++; |
---|
| 59 | t = at->get_base(); |
---|
| 60 | } |
---|
| 61 | maxDepth++; |
---|
| 62 | } |
---|
[ef3d798] | 63 | void previsit( ListInit * ) { |
---|
[dcd73d1] | 64 | curDepth++; |
---|
[ef3d798] | 65 | GuardAction( [this]() { curDepth--; } ); |
---|
[dcd73d1] | 66 | if ( curDepth > maxDepth ) depthOkay = false; |
---|
| 67 | } |
---|
| 68 | }; |
---|
| 69 | |
---|
[c3f551b] | 70 | struct InitFlattener : public WithShortCircuiting { |
---|
| 71 | void previsit( SingleInit * singleInit ) { |
---|
| 72 | visit_children = false; |
---|
| 73 | argList.push_back( singleInit->value->clone() ); |
---|
| 74 | } |
---|
[64071c2] | 75 | std::list< Expression * > argList; |
---|
| 76 | }; |
---|
[2b46a13] | 77 | |
---|
[64071c2] | 78 | } |
---|
[2b46a13] | 79 | |
---|
[64071c2] | 80 | std::list< Expression * > makeInitList( Initializer * init ) { |
---|
[c3f551b] | 81 | PassVisitor<InitFlattener> flattener; |
---|
[4d2434a] | 82 | maybeAccept( init, flattener ); |
---|
[c3f551b] | 83 | return flattener.pass.argList; |
---|
[64071c2] | 84 | } |
---|
[2b46a13] | 85 | |
---|
[64071c2] | 86 | bool isDesignated( Initializer * init ) { |
---|
[fd236ed] | 87 | PassVisitor<HasDesignations> finder; |
---|
[64071c2] | 88 | maybeAccept( init, finder ); |
---|
[fd236ed] | 89 | return finder.pass.hasDesignations; |
---|
[dcd73d1] | 90 | } |
---|
| 91 | |
---|
| 92 | bool checkInitDepth( ObjectDecl * objDecl ) { |
---|
[ef3d798] | 93 | PassVisitor<InitDepthChecker> checker( objDecl->type ); |
---|
| 94 | maybeAccept( objDecl->init, checker ); |
---|
| 95 | return checker.pass.depthOkay; |
---|
[64071c2] | 96 | } |
---|
[2b46a13] | 97 | |
---|
[39f84a4] | 98 | class InitExpander::ExpanderImpl { |
---|
| 99 | public: |
---|
[3351cc0] | 100 | virtual ~ExpanderImpl() = default; |
---|
[39f84a4] | 101 | virtual std::list< Expression * > next( std::list< Expression * > & indices ) = 0; |
---|
[4d2434a] | 102 | virtual Statement * buildListInit( UntypedExpr * callExpr, std::list< Expression * > & indices ) = 0; |
---|
[39f84a4] | 103 | }; |
---|
| 104 | |
---|
| 105 | class InitImpl : public InitExpander::ExpanderImpl { |
---|
| 106 | public: |
---|
[4d2434a] | 107 | InitImpl( Initializer * init ) : init( init ) {} |
---|
[bd41764] | 108 | virtual ~InitImpl() = default; |
---|
[39f84a4] | 109 | |
---|
[7e003011] | 110 | virtual std::list< Expression * > next( __attribute((unused)) std::list< Expression * > & indices ) { |
---|
[39f84a4] | 111 | // this is wrong, but just a placeholder for now |
---|
[4d2434a] | 112 | // if ( ! flattened ) flatten( indices ); |
---|
| 113 | // return ! inits.empty() ? makeInitList( inits.front() ) : std::list< Expression * >(); |
---|
| 114 | return makeInitList( init ); |
---|
[39f84a4] | 115 | } |
---|
[4d2434a] | 116 | |
---|
| 117 | virtual Statement * buildListInit( UntypedExpr * callExpr, std::list< Expression * > & indices ); |
---|
[39f84a4] | 118 | private: |
---|
[4d2434a] | 119 | Initializer * init; |
---|
[39f84a4] | 120 | }; |
---|
| 121 | |
---|
| 122 | class ExprImpl : public InitExpander::ExpanderImpl { |
---|
| 123 | public: |
---|
| 124 | ExprImpl( Expression * expr ) : arg( expr ) {} |
---|
[bd41764] | 125 | virtual ~ExprImpl() { delete arg; } |
---|
[9b4c936] | 126 | |
---|
[39f84a4] | 127 | virtual std::list< Expression * > next( std::list< Expression * > & indices ) { |
---|
| 128 | std::list< Expression * > ret; |
---|
| 129 | Expression * expr = maybeClone( arg ); |
---|
| 130 | if ( expr ) { |
---|
| 131 | for ( std::list< Expression * >::reverse_iterator it = indices.rbegin(); it != indices.rend(); ++it ) { |
---|
| 132 | // go through indices and layer on subscript exprs ?[?] |
---|
| 133 | ++it; |
---|
| 134 | UntypedExpr * subscriptExpr = new UntypedExpr( new NameExpr( "?[?]") ); |
---|
| 135 | subscriptExpr->get_args().push_back( expr ); |
---|
| 136 | subscriptExpr->get_args().push_back( (*it)->clone() ); |
---|
| 137 | expr = subscriptExpr; |
---|
| 138 | } |
---|
| 139 | ret.push_back( expr ); |
---|
| 140 | } |
---|
| 141 | return ret; |
---|
| 142 | } |
---|
[4d2434a] | 143 | |
---|
| 144 | virtual Statement * buildListInit( UntypedExpr * callExpr, std::list< Expression * > & indices ); |
---|
[39f84a4] | 145 | private: |
---|
| 146 | Expression * arg; |
---|
| 147 | }; |
---|
| 148 | |
---|
| 149 | InitExpander::InitExpander( Initializer * init ) : expander( new InitImpl( init ) ) {} |
---|
| 150 | |
---|
| 151 | InitExpander::InitExpander( Expression * expr ) : expander( new ExprImpl( expr ) ) {} |
---|
| 152 | |
---|
| 153 | std::list< Expression * > InitExpander::operator*() { |
---|
| 154 | return cur; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | InitExpander & InitExpander::operator++() { |
---|
| 158 | cur = expander->next( indices ); |
---|
| 159 | return *this; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | // use array indices list to build switch statement |
---|
| 163 | void InitExpander::addArrayIndex( Expression * index, Expression * dimension ) { |
---|
| 164 | indices.push_back( index ); |
---|
| 165 | indices.push_back( dimension ); |
---|
| 166 | } |
---|
| 167 | |
---|
[4d2434a] | 168 | void InitExpander::clearArrayIndices() { |
---|
[9b4c936] | 169 | deleteAll( indices ); |
---|
[4d2434a] | 170 | indices.clear(); |
---|
[1a5ad8c] | 171 | } |
---|
| 172 | |
---|
| 173 | bool InitExpander::addReference() { |
---|
| 174 | bool added = false; |
---|
| 175 | for ( Expression *& expr : cur ) { |
---|
| 176 | expr = new AddressExpr( expr ); |
---|
| 177 | added = true; |
---|
| 178 | } |
---|
| 179 | return added; |
---|
[4d2434a] | 180 | } |
---|
| 181 | |
---|
| 182 | namespace { |
---|
[f9cebb5] | 183 | /// given index i, dimension d, initializer init, and callExpr f, generates |
---|
| 184 | /// if (i < d) f(..., init) |
---|
| 185 | /// ++i; |
---|
| 186 | /// so that only elements within the range of the array are constructed |
---|
[4d2434a] | 187 | template< typename OutIterator > |
---|
[f9cebb5] | 188 | void buildCallExpr( UntypedExpr * callExpr, Expression * index, Expression * dimension, Initializer * init, OutIterator out ) { |
---|
[4d2434a] | 189 | UntypedExpr * cond = new UntypedExpr( new NameExpr( "?<?") ); |
---|
| 190 | cond->get_args().push_back( index->clone() ); |
---|
| 191 | cond->get_args().push_back( dimension->clone() ); |
---|
| 192 | |
---|
| 193 | std::list< Expression * > args = makeInitList( init ); |
---|
| 194 | callExpr->get_args().splice( callExpr->get_args().end(), args ); |
---|
| 195 | |
---|
[ba3706f] | 196 | *out++ = new IfStmt( cond, new ExprStmt( callExpr ), nullptr ); |
---|
[4d2434a] | 197 | |
---|
| 198 | UntypedExpr * increment = new UntypedExpr( new NameExpr( "++?" ) ); |
---|
[175ad32b] | 199 | increment->get_args().push_back( index->clone() ); |
---|
[ba3706f] | 200 | *out++ = new ExprStmt( increment ); |
---|
[4d2434a] | 201 | } |
---|
| 202 | |
---|
| 203 | template< typename OutIterator > |
---|
| 204 | void build( UntypedExpr * callExpr, InitExpander::IndexList::iterator idx, InitExpander::IndexList::iterator idxEnd, Initializer * init, OutIterator out ) { |
---|
| 205 | if ( idx == idxEnd ) return; |
---|
| 206 | Expression * index = *idx++; |
---|
| 207 | assert( idx != idxEnd ); |
---|
| 208 | Expression * dimension = *idx++; |
---|
| 209 | |
---|
[f9cebb5] | 210 | // xxx - may want to eventually issue a warning here if we can detect |
---|
| 211 | // that the number of elements exceeds to dimension of the array |
---|
[4d2434a] | 212 | if ( idx == idxEnd ) { |
---|
| 213 | if ( ListInit * listInit = dynamic_cast< ListInit * >( init ) ) { |
---|
| 214 | for ( Initializer * init : *listInit ) { |
---|
[f9cebb5] | 215 | buildCallExpr( callExpr->clone(), index, dimension, init, out ); |
---|
[4d2434a] | 216 | } |
---|
| 217 | } else { |
---|
[f9cebb5] | 218 | buildCallExpr( callExpr->clone(), index, dimension, init, out ); |
---|
[4d2434a] | 219 | } |
---|
| 220 | } else { |
---|
| 221 | std::list< Statement * > branches; |
---|
| 222 | |
---|
| 223 | unsigned long cond = 0; |
---|
| 224 | ListInit * listInit = dynamic_cast< ListInit * >( init ); |
---|
| 225 | if ( ! listInit ) { |
---|
| 226 | // xxx - this shouldn't be an error, but need a way to |
---|
| 227 | // terminate without creating output, so should catch this error |
---|
[a16764a6] | 228 | SemanticError( init->location, "unbalanced list initializers" ); |
---|
[4d2434a] | 229 | } |
---|
[f9cebb5] | 230 | |
---|
| 231 | static UniqueName targetLabel( "L__autogen__" ); |
---|
| 232 | Label switchLabel( targetLabel.newName(), 0, std::list< Attribute * >{ new Attribute("unused") } ); |
---|
[4d2434a] | 233 | for ( Initializer * init : *listInit ) { |
---|
| 234 | Expression * condition; |
---|
| 235 | // check for designations |
---|
| 236 | // if ( init-> ) { |
---|
| 237 | condition = new ConstantExpr( Constant::from_ulong( cond ) ); |
---|
| 238 | ++cond; |
---|
| 239 | // } else { |
---|
| 240 | // condition = // ... take designation |
---|
| 241 | // cond = // ... take designation+1 |
---|
| 242 | // } |
---|
| 243 | std::list< Statement * > stmts; |
---|
| 244 | build( callExpr, idx, idxEnd, init, back_inserter( stmts ) ); |
---|
[ba3706f] | 245 | stmts.push_back( new BranchStmt( switchLabel, BranchStmt::Break ) ); |
---|
| 246 | CaseStmt * caseStmt = new CaseStmt( condition, stmts ); |
---|
[4d2434a] | 247 | branches.push_back( caseStmt ); |
---|
| 248 | } |
---|
[ba3706f] | 249 | *out++ = new SwitchStmt( index->clone(), branches ); |
---|
| 250 | *out++ = new NullStmt( { switchLabel } ); |
---|
[4d2434a] | 251 | } |
---|
| 252 | } |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | // if array came with an initializer list: initialize each element |
---|
| 256 | // may have more initializers than elements in the array - need to check at each index that |
---|
| 257 | // we haven't exceeded size. |
---|
| 258 | // may have fewer initializers than elements in the array - need to default construct |
---|
| 259 | // remaining elements. |
---|
| 260 | // To accomplish this, generate switch statement, consuming all of expander's elements |
---|
| 261 | Statement * InitImpl::buildListInit( UntypedExpr * dst, std::list< Expression * > & indices ) { |
---|
[22bc276] | 262 | if ( ! init ) return nullptr; |
---|
[ba3706f] | 263 | CompoundStmt * block = new CompoundStmt(); |
---|
[f9cebb5] | 264 | build( dst, indices.begin(), indices.end(), init, back_inserter( block->get_kids() ) ); |
---|
| 265 | if ( block->get_kids().empty() ) { |
---|
| 266 | delete block; |
---|
[22bc276] | 267 | return nullptr; |
---|
[4d2434a] | 268 | } else { |
---|
[22bc276] | 269 | init = nullptr; // init was consumed in creating the list init |
---|
[f9cebb5] | 270 | return block; |
---|
[4d2434a] | 271 | } |
---|
[39f84a4] | 272 | } |
---|
| 273 | |
---|
[22bc276] | 274 | Statement * ExprImpl::buildListInit( UntypedExpr *, std::list< Expression * > & ) { |
---|
| 275 | return nullptr; |
---|
[4d2434a] | 276 | } |
---|
| 277 | |
---|
| 278 | Statement * InitExpander::buildListInit( UntypedExpr * dst ) { |
---|
| 279 | return expander->buildListInit( dst, indices ); |
---|
| 280 | } |
---|
| 281 | |
---|
[549c006] | 282 | Type * getTypeofThis( FunctionType * ftype ) { |
---|
| 283 | assertf( ftype, "getTypeofThis: nullptr ftype" ); |
---|
| 284 | ObjectDecl * thisParam = getParamThis( ftype ); |
---|
[7fc7cdb] | 285 | ReferenceType * refType = strict_dynamic_cast< ReferenceType * >( thisParam->type ); |
---|
| 286 | return refType->base; |
---|
| 287 | } |
---|
| 288 | |
---|
[549c006] | 289 | ObjectDecl * getParamThis( FunctionType * ftype ) { |
---|
| 290 | assertf( ftype, "getParamThis: nullptr ftype" ); |
---|
[7fc7cdb] | 291 | auto & params = ftype->parameters; |
---|
[549c006] | 292 | assertf( ! params.empty(), "getParamThis: ftype with 0 parameters: %s", toString( ftype ).c_str() ); |
---|
[7fc7cdb] | 293 | return strict_dynamic_cast< ObjectDecl * >( params.front() ); |
---|
| 294 | } |
---|
| 295 | |
---|
[22bc276] | 296 | bool tryConstruct( DeclarationWithType * dwt ) { |
---|
| 297 | ObjectDecl * objDecl = dynamic_cast< ObjectDecl * >( dwt ); |
---|
| 298 | if ( ! objDecl ) return false; |
---|
[df7a162] | 299 | return (objDecl->get_init() == nullptr || |
---|
[22bc276] | 300 | ( objDecl->get_init() != nullptr && objDecl->get_init()->get_maybeConstructed() )) |
---|
| 301 | && ! objDecl->get_storageClasses().is_extern |
---|
[29bc63e] | 302 | && isConstructable( objDecl->type ); |
---|
| 303 | } |
---|
| 304 | |
---|
| 305 | bool isConstructable( Type * type ) { |
---|
| 306 | return ! dynamic_cast< VarArgsType * >( type ) && ! dynamic_cast< ReferenceType * >( type ) && ! dynamic_cast< FunctionType * >( type ) && ! Tuples::isTtype( type ); |
---|
[64071c2] | 307 | } |
---|
[2b46a13] | 308 | |
---|
[0a6aad4] | 309 | struct CallFinder { |
---|
[4d2434a] | 310 | CallFinder( const std::list< std::string > & names ) : names( names ) {} |
---|
| 311 | |
---|
[0a6aad4] | 312 | void postvisit( ApplicationExpr * appExpr ) { |
---|
[4d2434a] | 313 | handleCallExpr( appExpr ); |
---|
| 314 | } |
---|
| 315 | |
---|
[0a6aad4] | 316 | void postvisit( UntypedExpr * untypedExpr ) { |
---|
[4d2434a] | 317 | handleCallExpr( untypedExpr ); |
---|
| 318 | } |
---|
| 319 | |
---|
| 320 | std::list< Expression * > * matches; |
---|
| 321 | private: |
---|
| 322 | const std::list< std::string > names; |
---|
| 323 | |
---|
| 324 | template< typename CallExpr > |
---|
| 325 | void handleCallExpr( CallExpr * expr ) { |
---|
| 326 | std::string fname = getFunctionName( expr ); |
---|
| 327 | if ( std::find( names.begin(), names.end(), fname ) != names.end() ) { |
---|
| 328 | matches->push_back( expr ); |
---|
[cad355a] | 329 | } |
---|
[64071c2] | 330 | } |
---|
[4d2434a] | 331 | }; |
---|
| 332 | |
---|
| 333 | void collectCtorDtorCalls( Statement * stmt, std::list< Expression * > & matches ) { |
---|
[0a6aad4] | 334 | static PassVisitor<CallFinder> finder( std::list< std::string >{ "?{}", "^?{}" } ); |
---|
| 335 | finder.pass.matches = &matches; |
---|
[4d2434a] | 336 | maybeAccept( stmt, finder ); |
---|
[64071c2] | 337 | } |
---|
[4d2434a] | 338 | |
---|
| 339 | Expression * getCtorDtorCall( Statement * stmt ) { |
---|
| 340 | std::list< Expression * > matches; |
---|
| 341 | collectCtorDtorCalls( stmt, matches ); |
---|
[20eacb7] | 342 | assertf( matches.size() <= 1, "%zd constructor/destructors found in %s", matches.size(), toString( stmt ).c_str() ); |
---|
[22bc276] | 343 | return matches.size() == 1 ? matches.front() : nullptr; |
---|
[4d2434a] | 344 | } |
---|
| 345 | |
---|
[aedfd91] | 346 | namespace { |
---|
[599b386] | 347 | DeclarationWithType * getCalledFunction( Expression * expr ); |
---|
[d7aa12c] | 348 | const ast::DeclWithType * getCalledFunction( const ast::Expr * expr ); |
---|
[599b386] | 349 | |
---|
| 350 | template<typename CallExpr> |
---|
| 351 | DeclarationWithType * handleDerefCalledFunction( CallExpr * expr ) { |
---|
| 352 | // (*f)(x) => should get "f" |
---|
| 353 | std::string name = getFunctionName( expr ); |
---|
| 354 | assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() ); |
---|
[b128d3e] | 355 | assertf( ! expr->get_args().empty(), "Cannot get called function from dereference with no arguments" ); |
---|
[599b386] | 356 | return getCalledFunction( expr->get_args().front() ); |
---|
| 357 | } |
---|
| 358 | |
---|
[d7aa12c] | 359 | template<typename CallExpr> |
---|
| 360 | const ast::DeclWithType * handleDerefCalledFunction( const CallExpr * expr ) { |
---|
| 361 | // (*f)(x) => should get "f" |
---|
| 362 | std::string name = getFunctionName( expr ); |
---|
| 363 | assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() ); |
---|
| 364 | assertf( ! expr->args.empty(), "Cannot get called function from dereference with no arguments" ); |
---|
| 365 | return getCalledFunction( expr->args.front() ); |
---|
| 366 | } |
---|
| 367 | |
---|
| 368 | |
---|
[ee1635c8] | 369 | DeclarationWithType * getCalledFunction( Expression * expr ) { |
---|
| 370 | assert( expr ); |
---|
| 371 | if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( expr ) ) { |
---|
[6fc5c14] | 372 | return varExpr->var; |
---|
[ee1635c8] | 373 | } else if ( MemberExpr * memberExpr = dynamic_cast< MemberExpr * >( expr ) ) { |
---|
[6fc5c14] | 374 | return memberExpr->member; |
---|
[ee1635c8] | 375 | } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) { |
---|
[6fc5c14] | 376 | return getCalledFunction( castExpr->arg ); |
---|
[599b386] | 377 | } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * >( expr ) ) { |
---|
| 378 | return handleDerefCalledFunction( untypedExpr ); |
---|
| 379 | } else if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * > ( expr ) ) { |
---|
| 380 | return handleDerefCalledFunction( appExpr ); |
---|
[f3b0a07] | 381 | } else if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( expr ) ) { |
---|
[6fc5c14] | 382 | return getCalledFunction( addrExpr->arg ); |
---|
| 383 | } else if ( CommaExpr * commaExpr = dynamic_cast< CommaExpr * >( expr ) ) { |
---|
| 384 | return getCalledFunction( commaExpr->arg2 ); |
---|
[ee1635c8] | 385 | } |
---|
| 386 | return nullptr; |
---|
[aedfd91] | 387 | } |
---|
[d7aa12c] | 388 | |
---|
| 389 | const ast::DeclWithType * getCalledFunction( const ast::Expr * expr ) { |
---|
| 390 | assert( expr ); |
---|
| 391 | if ( const ast::VariableExpr * varExpr = dynamic_cast< const ast::VariableExpr * >( expr ) ) { |
---|
| 392 | return varExpr->var; |
---|
| 393 | } else if ( const ast::MemberExpr * memberExpr = dynamic_cast< const ast::MemberExpr * >( expr ) ) { |
---|
| 394 | return memberExpr->member; |
---|
| 395 | } else if ( const ast::CastExpr * castExpr = dynamic_cast< const ast::CastExpr * >( expr ) ) { |
---|
| 396 | return getCalledFunction( castExpr->arg ); |
---|
| 397 | } else if ( const ast::UntypedExpr * untypedExpr = dynamic_cast< const ast::UntypedExpr * >( expr ) ) { |
---|
| 398 | return handleDerefCalledFunction( untypedExpr ); |
---|
| 399 | } else if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * > ( expr ) ) { |
---|
| 400 | return handleDerefCalledFunction( appExpr ); |
---|
| 401 | } else if ( const ast::AddressExpr * addrExpr = dynamic_cast< const ast::AddressExpr * >( expr ) ) { |
---|
| 402 | return getCalledFunction( addrExpr->arg ); |
---|
| 403 | } else if ( const ast::CommaExpr * commaExpr = dynamic_cast< const ast::CommaExpr * >( expr ) ) { |
---|
| 404 | return getCalledFunction( commaExpr->arg2 ); |
---|
| 405 | } |
---|
| 406 | return nullptr; |
---|
| 407 | } |
---|
[aedfd91] | 408 | } |
---|
[70f89d00] | 409 | |
---|
[b7b8674] | 410 | DeclarationWithType * getFunction( Expression * expr ) { |
---|
| 411 | if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr ) ) { |
---|
| 412 | return getCalledFunction( appExpr->get_function() ); |
---|
| 413 | } else if ( UntypedExpr * untyped = dynamic_cast< UntypedExpr * > ( expr ) ) { |
---|
| 414 | return getCalledFunction( untyped->get_function() ); |
---|
| 415 | } |
---|
| 416 | assertf( false, "getFunction received unknown expression: %s", toString( expr ).c_str() ); |
---|
| 417 | } |
---|
| 418 | |
---|
[d7aa12c] | 419 | const ast::DeclWithType * getFunction( const ast::Expr * expr ) { |
---|
| 420 | if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * >( expr ) ) { |
---|
| 421 | return getCalledFunction( appExpr->func ); |
---|
| 422 | } else if ( const ast::UntypedExpr * untyped = dynamic_cast< const ast::UntypedExpr * > ( expr ) ) { |
---|
| 423 | return getCalledFunction( untyped->func ); |
---|
| 424 | } |
---|
| 425 | assertf( false, "getFunction received unknown expression: %s", toString( expr ).c_str() ); |
---|
| 426 | } |
---|
| 427 | |
---|
[aedfd91] | 428 | ApplicationExpr * isIntrinsicCallExpr( Expression * expr ) { |
---|
| 429 | ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr ); |
---|
[22bc276] | 430 | if ( ! appExpr ) return nullptr; |
---|
[ee1635c8] | 431 | DeclarationWithType * function = getCalledFunction( appExpr->get_function() ); |
---|
[f3b0a07] | 432 | assertf( function, "getCalledFunction returned nullptr: %s", toString( appExpr->get_function() ).c_str() ); |
---|
[64071c2] | 433 | // check for Intrinsic only - don't want to remove all overridable ctor/dtors because autogenerated ctor/dtor |
---|
| 434 | // will call all member dtors, and some members may have a user defined dtor. |
---|
[22bc276] | 435 | return function->get_linkage() == LinkageSpec::Intrinsic ? appExpr : nullptr; |
---|
[aedfd91] | 436 | } |
---|
| 437 | |
---|
[a465caf] | 438 | namespace { |
---|
| 439 | template <typename Predicate> |
---|
| 440 | bool allofCtorDtor( Statement * stmt, const Predicate & pred ) { |
---|
| 441 | std::list< Expression * > callExprs; |
---|
| 442 | collectCtorDtorCalls( stmt, callExprs ); |
---|
| 443 | // if ( callExprs.empty() ) return false; // xxx - do I still need this check? |
---|
| 444 | return std::all_of( callExprs.begin(), callExprs.end(), pred); |
---|
| 445 | } |
---|
| 446 | } |
---|
| 447 | |
---|
[f9cebb5] | 448 | bool isIntrinsicSingleArgCallStmt( Statement * stmt ) { |
---|
[a465caf] | 449 | return allofCtorDtor( stmt, []( Expression * callExpr ){ |
---|
[4d2434a] | 450 | if ( ApplicationExpr * appExpr = isIntrinsicCallExpr( callExpr ) ) { |
---|
[f072892] | 451 | FunctionType *funcType = GenPoly::getFunctionType( appExpr->function->result ); |
---|
[4d2434a] | 452 | assert( funcType ); |
---|
| 453 | return funcType->get_parameters().size() == 1; |
---|
| 454 | } |
---|
| 455 | return false; |
---|
| 456 | }); |
---|
[64071c2] | 457 | } |
---|
[f1b1e4c] | 458 | |
---|
[a465caf] | 459 | bool isIntrinsicCallStmt( Statement * stmt ) { |
---|
| 460 | return allofCtorDtor( stmt, []( Expression * callExpr ) { |
---|
| 461 | return isIntrinsicCallExpr( callExpr ); |
---|
| 462 | }); |
---|
| 463 | } |
---|
| 464 | |
---|
[64071c2] | 465 | namespace { |
---|
| 466 | template<typename CallExpr> |
---|
| 467 | Expression *& callArg( CallExpr * callExpr, unsigned int pos ) { |
---|
[a61ad31] | 468 | if ( pos >= callExpr->get_args().size() ) assertf( false, "getCallArg for argument that doesn't exist: (%u); %s.", pos, toString( callExpr ).c_str() ); |
---|
[64071c2] | 469 | for ( Expression *& arg : callExpr->get_args() ) { |
---|
| 470 | if ( pos == 0 ) return arg; |
---|
| 471 | pos--; |
---|
| 472 | } |
---|
| 473 | assert( false ); |
---|
| 474 | } |
---|
[9b4f329] | 475 | |
---|
[b5fed34] | 476 | template<typename CallExpr> |
---|
| 477 | const ast::Expr * callArg( const CallExpr * call, unsigned int pos ) { |
---|
| 478 | if( pos >= call->args.size() ) { |
---|
| 479 | assertf( false, "getCallArg for argument that doesn't exist: (%u); %s.", |
---|
| 480 | pos, toString( call ).c_str() ); |
---|
| 481 | } |
---|
| 482 | for ( const ast::Expr * arg : call->args ) { |
---|
| 483 | if ( pos == 0 ) return arg; |
---|
| 484 | --pos; |
---|
| 485 | } |
---|
| 486 | assert( false ); |
---|
| 487 | } |
---|
[64071c2] | 488 | } |
---|
[f1b1e4c] | 489 | |
---|
[64071c2] | 490 | Expression *& getCallArg( Expression * callExpr, unsigned int pos ) { |
---|
| 491 | if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( callExpr ) ) { |
---|
| 492 | return callArg( appExpr, pos ); |
---|
| 493 | } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * >( callExpr ) ) { |
---|
| 494 | return callArg( untypedExpr, pos ); |
---|
[f3b0a07] | 495 | } else if ( TupleAssignExpr * tupleExpr = dynamic_cast< TupleAssignExpr * > ( callExpr ) ) { |
---|
| 496 | std::list< Statement * > & stmts = tupleExpr->get_stmtExpr()->get_statements()->get_kids(); |
---|
| 497 | assertf( ! stmts.empty(), "TupleAssignExpr somehow has no statements." ); |
---|
[e3e16bc] | 498 | ExprStmt * stmt = strict_dynamic_cast< ExprStmt * >( stmts.back() ); |
---|
| 499 | TupleExpr * tuple = strict_dynamic_cast< TupleExpr * >( stmt->get_expr() ); |
---|
[f3b0a07] | 500 | assertf( ! tuple->get_exprs().empty(), "TupleAssignExpr somehow has empty tuple expr." ); |
---|
| 501 | return getCallArg( tuple->get_exprs().front(), pos ); |
---|
[62a05d1] | 502 | } else if ( ImplicitCopyCtorExpr * copyCtor = dynamic_cast< ImplicitCopyCtorExpr * >( callExpr ) ) { |
---|
| 503 | return getCallArg( copyCtor->callExpr, pos ); |
---|
[64071c2] | 504 | } else { |
---|
[f3b0a07] | 505 | assertf( false, "Unexpected expression type passed to getCallArg: %s", toString( callExpr ).c_str() ); |
---|
[64071c2] | 506 | } |
---|
| 507 | } |
---|
[b5fed34] | 508 | |
---|
[9b4f329] | 509 | const ast::Expr * getCallArg( const ast::Expr * call, unsigned pos ) { |
---|
[b5fed34] | 510 | if ( auto app = dynamic_cast< const ast::ApplicationExpr * >( call ) ) { |
---|
| 511 | return callArg( app, pos ); |
---|
| 512 | } else if ( auto untyped = dynamic_cast< const ast::UntypedExpr * >( call ) ) { |
---|
| 513 | return callArg( untyped, pos ); |
---|
| 514 | } else if ( auto tupleAssn = dynamic_cast< const ast::TupleAssignExpr * >( call ) ) { |
---|
| 515 | const std::list<ast::ptr<ast::Stmt>>& stmts = tupleAssn->stmtExpr->stmts->kids; |
---|
| 516 | assertf( ! stmts.empty(), "TupleAssignExpr missing statements." ); |
---|
| 517 | auto stmt = strict_dynamic_cast< const ast::ExprStmt * >( stmts.back().get() ); |
---|
| 518 | auto tuple = strict_dynamic_cast< const ast::TupleExpr * >( stmt->expr.get() ); |
---|
| 519 | assertf( ! tuple->exprs.empty(), "TupleAssignExpr has empty tuple expr."); |
---|
| 520 | return getCallArg( tuple->exprs.front(), pos ); |
---|
| 521 | } else if ( auto ctor = dynamic_cast< const ast::ImplicitCopyCtorExpr * >( call ) ) { |
---|
| 522 | return getCallArg( ctor->callExpr, pos ); |
---|
| 523 | } else { |
---|
| 524 | assertf( false, "Unexpected expression type passed to getCallArg: %s", |
---|
| 525 | toString( call ).c_str() ); |
---|
| 526 | } |
---|
[9b4f329] | 527 | } |
---|
[f1b1e4c] | 528 | |
---|
[64071c2] | 529 | namespace { |
---|
[599b386] | 530 | std::string funcName( Expression * func ); |
---|
[d7aa12c] | 531 | std::string funcName( const ast::Expr * func ); |
---|
[599b386] | 532 | |
---|
| 533 | template<typename CallExpr> |
---|
| 534 | std::string handleDerefName( CallExpr * expr ) { |
---|
| 535 | // (*f)(x) => should get name "f" |
---|
| 536 | std::string name = getFunctionName( expr ); |
---|
| 537 | assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() ); |
---|
[b128d3e] | 538 | assertf( ! expr->get_args().empty(), "Cannot get function name from dereference with no arguments" ); |
---|
[599b386] | 539 | return funcName( expr->get_args().front() ); |
---|
| 540 | } |
---|
| 541 | |
---|
[d7aa12c] | 542 | template<typename CallExpr> |
---|
| 543 | std::string handleDerefName( const CallExpr * expr ) { |
---|
| 544 | // (*f)(x) => should get name "f" |
---|
| 545 | std::string name = getFunctionName( expr ); |
---|
| 546 | assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() ); |
---|
| 547 | assertf( ! expr->args.empty(), "Cannot get function name from dereference with no arguments" ); |
---|
| 548 | return funcName( expr->args.front() ); |
---|
| 549 | } |
---|
| 550 | |
---|
[c738ca4] | 551 | std::string funcName( Expression * func ) { |
---|
[64071c2] | 552 | if ( NameExpr * nameExpr = dynamic_cast< NameExpr * >( func ) ) { |
---|
| 553 | return nameExpr->get_name(); |
---|
| 554 | } else if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( func ) ) { |
---|
| 555 | return varExpr->get_var()->get_name(); |
---|
[c738ca4] | 556 | } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( func ) ) { |
---|
| 557 | return funcName( castExpr->get_arg() ); |
---|
[ee1635c8] | 558 | } else if ( MemberExpr * memberExpr = dynamic_cast< MemberExpr * >( func ) ) { |
---|
| 559 | return memberExpr->get_member()->get_name(); |
---|
[96a10cd] | 560 | } else if ( UntypedMemberExpr * memberExpr = dynamic_cast< UntypedMemberExpr * > ( func ) ) { |
---|
[fd782b2] | 561 | return funcName( memberExpr->get_member() ); |
---|
[599b386] | 562 | } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * >( func ) ) { |
---|
| 563 | return handleDerefName( untypedExpr ); |
---|
| 564 | } else if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( func ) ) { |
---|
| 565 | return handleDerefName( appExpr ); |
---|
[19a9822] | 566 | } else if ( ConstructorExpr * ctorExpr = dynamic_cast< ConstructorExpr * >( func ) ) { |
---|
| 567 | return funcName( getCallArg( ctorExpr->get_callExpr(), 0 ) ); |
---|
[64071c2] | 568 | } else { |
---|
[19a9822] | 569 | assertf( false, "Unexpected expression type being called as a function in call expression: %s", toString( func ).c_str() ); |
---|
[64071c2] | 570 | } |
---|
| 571 | } |
---|
[d7aa12c] | 572 | |
---|
| 573 | std::string funcName( const ast::Expr * func ) { |
---|
| 574 | if ( const ast::NameExpr * nameExpr = dynamic_cast< const ast::NameExpr * >( func ) ) { |
---|
| 575 | return nameExpr->name; |
---|
| 576 | } else if ( const ast::VariableExpr * varExpr = dynamic_cast< const ast::VariableExpr * >( func ) ) { |
---|
| 577 | return varExpr->var->name; |
---|
| 578 | } else if ( const ast::CastExpr * castExpr = dynamic_cast< const ast::CastExpr * >( func ) ) { |
---|
| 579 | return funcName( castExpr->arg ); |
---|
| 580 | } else if ( const ast::MemberExpr * memberExpr = dynamic_cast< const ast::MemberExpr * >( func ) ) { |
---|
| 581 | return memberExpr->member->name; |
---|
| 582 | } else if ( const ast::UntypedMemberExpr * memberExpr = dynamic_cast< const ast::UntypedMemberExpr * > ( func ) ) { |
---|
| 583 | return funcName( memberExpr->member ); |
---|
| 584 | } else if ( const ast::UntypedExpr * untypedExpr = dynamic_cast< const ast::UntypedExpr * >( func ) ) { |
---|
| 585 | return handleDerefName( untypedExpr ); |
---|
| 586 | } else if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * >( func ) ) { |
---|
| 587 | return handleDerefName( appExpr ); |
---|
| 588 | } else if ( const ast::ConstructorExpr * ctorExpr = dynamic_cast< const ast::ConstructorExpr * >( func ) ) { |
---|
| 589 | return funcName( getCallArg( ctorExpr->callExpr, 0 ) ); |
---|
| 590 | } else { |
---|
| 591 | assertf( false, "Unexpected expression type being called as a function in call expression: %s", toString( func ).c_str() ); |
---|
| 592 | } |
---|
| 593 | } |
---|
[64071c2] | 594 | } |
---|
[70f89d00] | 595 | |
---|
[64071c2] | 596 | std::string getFunctionName( Expression * expr ) { |
---|
[599b386] | 597 | // there's some unforunate overlap here with getCalledFunction. Ideally this would be able to use getCalledFunction and |
---|
| 598 | // return the name of the DeclarationWithType, but this needs to work for NameExpr and UntypedMemberExpr, where getCalledFunction |
---|
| 599 | // can't possibly do anything reasonable. |
---|
[64071c2] | 600 | if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr ) ) { |
---|
[c738ca4] | 601 | return funcName( appExpr->get_function() ); |
---|
[64071c2] | 602 | } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * > ( expr ) ) { |
---|
[c738ca4] | 603 | return funcName( untypedExpr->get_function() ); |
---|
[64071c2] | 604 | } else { |
---|
[c738ca4] | 605 | std::cerr << expr << std::endl; |
---|
[d1969a6] | 606 | assertf( false, "Unexpected expression type passed to getFunctionName" ); |
---|
[64071c2] | 607 | } |
---|
| 608 | } |
---|
[10a7775] | 609 | |
---|
[d7aa12c] | 610 | std::string getFunctionName( const ast::Expr * expr ) { |
---|
| 611 | // there's some unforunate overlap here with getCalledFunction. Ideally this would be able to use getCalledFunction and |
---|
| 612 | // return the name of the DeclarationWithType, but this needs to work for NameExpr and UntypedMemberExpr, where getCalledFunction |
---|
| 613 | // can't possibly do anything reasonable. |
---|
| 614 | if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * >( expr ) ) { |
---|
| 615 | return funcName( appExpr->func ); |
---|
| 616 | } else if ( const ast::UntypedExpr * untypedExpr = dynamic_cast< const ast::UntypedExpr * > ( expr ) ) { |
---|
| 617 | return funcName( untypedExpr->func ); |
---|
| 618 | } else { |
---|
| 619 | std::cerr << expr << std::endl; |
---|
| 620 | assertf( false, "Unexpected expression type passed to getFunctionName" ); |
---|
| 621 | } |
---|
| 622 | } |
---|
| 623 | |
---|
[64071c2] | 624 | Type * getPointerBase( Type * type ) { |
---|
| 625 | if ( PointerType * ptrType = dynamic_cast< PointerType * >( type ) ) { |
---|
| 626 | return ptrType->get_base(); |
---|
| 627 | } else if ( ArrayType * arrayType = dynamic_cast< ArrayType * >( type ) ) { |
---|
| 628 | return arrayType->get_base(); |
---|
[ce8c12f] | 629 | } else if ( ReferenceType * refType = dynamic_cast< ReferenceType * >( type ) ) { |
---|
| 630 | return refType->get_base(); |
---|
[64071c2] | 631 | } else { |
---|
[22bc276] | 632 | return nullptr; |
---|
[64071c2] | 633 | } |
---|
| 634 | } |
---|
[9e1d485] | 635 | const ast::Type* getPointerBase( const ast::Type* t ) { |
---|
[b5fed34] | 636 | if ( const auto * p = dynamic_cast< const ast::PointerType * >( t ) ) { |
---|
| 637 | return p->base; |
---|
| 638 | } else if ( const auto * a = dynamic_cast< const ast::ArrayType * >( t ) ) { |
---|
| 639 | return a->base; |
---|
| 640 | } else if ( const auto * r = dynamic_cast< const ast::ReferenceType * >( t ) ) { |
---|
| 641 | return r->base; |
---|
| 642 | } else return nullptr; |
---|
[9e1d485] | 643 | } |
---|
[10a7775] | 644 | |
---|
[64071c2] | 645 | Type * isPointerType( Type * type ) { |
---|
| 646 | if ( getPointerBase( type ) ) return type; |
---|
[22bc276] | 647 | else return nullptr; |
---|
[64071c2] | 648 | } |
---|
[40e636a] | 649 | |
---|
[f5c3b6c] | 650 | ApplicationExpr * createBitwiseAssignment( Expression * dst, Expression * src ) { |
---|
| 651 | static FunctionDecl * assign = nullptr; |
---|
| 652 | if ( ! assign ) { |
---|
| 653 | // temporary? Generate a fake assignment operator to represent bitwise assignments. |
---|
| 654 | // This operator could easily exist as a real function, but it's tricky because nothing should resolve to this function. |
---|
| 655 | TypeDecl * td = new TypeDecl( "T", noStorageClasses, nullptr, TypeDecl::Dtype, true ); |
---|
| 656 | assign = new FunctionDecl( "?=?", noStorageClasses, LinkageSpec::Intrinsic, SymTab::genAssignType( new TypeInstType( noQualifiers, td->name, td ) ), nullptr ); |
---|
| 657 | } |
---|
| 658 | if ( dynamic_cast< ReferenceType * >( dst->result ) ) { |
---|
[5002738] | 659 | for (int depth = dst->result->referenceDepth(); depth > 0; depth--) { |
---|
| 660 | dst = new AddressExpr( dst ); |
---|
| 661 | } |
---|
[f5c3b6c] | 662 | } else { |
---|
| 663 | dst = new CastExpr( dst, new ReferenceType( noQualifiers, dst->result->clone() ) ); |
---|
| 664 | } |
---|
| 665 | if ( dynamic_cast< ReferenceType * >( src->result ) ) { |
---|
[5002738] | 666 | for (int depth = src->result->referenceDepth(); depth > 0; depth--) { |
---|
| 667 | src = new AddressExpr( src ); |
---|
| 668 | } |
---|
[ba89e9b7] | 669 | // src = new CastExpr( src, new ReferenceType( noQualifiers, src->result->stripReferences()->clone() ) ); |
---|
[f5c3b6c] | 670 | } |
---|
| 671 | return new ApplicationExpr( VariableExpr::functionPointer( assign ), { dst, src } ); |
---|
| 672 | } |
---|
| 673 | |
---|
[c5f3c68] | 674 | struct ConstExprChecker : public WithShortCircuiting { |
---|
| 675 | // most expressions are not const expr |
---|
| 676 | void previsit( Expression * ) { isConstExpr = false; visit_children = false; } |
---|
[40e636a] | 677 | |
---|
[c5f3c68] | 678 | void previsit( AddressExpr *addressExpr ) { |
---|
| 679 | visit_children = false; |
---|
[65dc863] | 680 | |
---|
[1ba88a0] | 681 | // address of a variable or member expression is constexpr |
---|
| 682 | Expression * arg = addressExpr->get_arg(); |
---|
| 683 | if ( ! dynamic_cast< NameExpr * >( arg) && ! dynamic_cast< VariableExpr * >( arg ) && ! dynamic_cast< MemberExpr * >( arg ) && ! dynamic_cast< UntypedMemberExpr * >( arg ) ) isConstExpr = false; |
---|
| 684 | } |
---|
[c5f3c68] | 685 | |
---|
| 686 | // these expressions may be const expr, depending on their children |
---|
| 687 | void previsit( SizeofExpr * ) {} |
---|
| 688 | void previsit( AlignofExpr * ) {} |
---|
| 689 | void previsit( UntypedOffsetofExpr * ) {} |
---|
| 690 | void previsit( OffsetofExpr * ) {} |
---|
| 691 | void previsit( OffsetPackExpr * ) {} |
---|
| 692 | void previsit( AttrExpr * ) {} |
---|
| 693 | void previsit( CommaExpr * ) {} |
---|
| 694 | void previsit( LogicalExpr * ) {} |
---|
| 695 | void previsit( ConditionalExpr * ) {} |
---|
| 696 | void previsit( CastExpr * ) {} |
---|
| 697 | void previsit( ConstantExpr * ) {} |
---|
| 698 | |
---|
[caab997] | 699 | void previsit( VariableExpr * varExpr ) { |
---|
| 700 | visit_children = false; |
---|
| 701 | |
---|
| 702 | if ( EnumInstType * inst = dynamic_cast< EnumInstType * >( varExpr->result ) ) { |
---|
| 703 | long long int value; |
---|
| 704 | if ( inst->baseEnum->valueOf( varExpr->var, value ) ) { |
---|
| 705 | // enumerators are const expr |
---|
| 706 | return; |
---|
| 707 | } |
---|
| 708 | } |
---|
| 709 | isConstExpr = false; |
---|
| 710 | } |
---|
| 711 | |
---|
[c5f3c68] | 712 | bool isConstExpr = true; |
---|
[40e636a] | 713 | }; |
---|
| 714 | |
---|
| 715 | bool isConstExpr( Expression * expr ) { |
---|
| 716 | if ( expr ) { |
---|
[c5f3c68] | 717 | PassVisitor<ConstExprChecker> checker; |
---|
[40e636a] | 718 | expr->accept( checker ); |
---|
[c5f3c68] | 719 | return checker.pass.isConstExpr; |
---|
[40e636a] | 720 | } |
---|
| 721 | return true; |
---|
| 722 | } |
---|
| 723 | |
---|
| 724 | bool isConstExpr( Initializer * init ) { |
---|
| 725 | if ( init ) { |
---|
[c5f3c68] | 726 | PassVisitor<ConstExprChecker> checker; |
---|
[40e636a] | 727 | init->accept( checker ); |
---|
[c5f3c68] | 728 | return checker.pass.isConstExpr; |
---|
[40e636a] | 729 | } // if |
---|
| 730 | // for all intents and purposes, no initializer means const expr |
---|
| 731 | return true; |
---|
| 732 | } |
---|
| 733 | |
---|
[79970ed] | 734 | bool isConstructor( const std::string & str ) { return str == "?{}"; } |
---|
| 735 | bool isDestructor( const std::string & str ) { return str == "^?{}"; } |
---|
[ee1635c8] | 736 | bool isAssignment( const std::string & str ) { return str == "?=?"; } |
---|
[79970ed] | 737 | bool isCtorDtor( const std::string & str ) { return isConstructor( str ) || isDestructor( str ); } |
---|
[ee1635c8] | 738 | bool isCtorDtorAssign( const std::string & str ) { return isCtorDtor( str ) || isAssignment( str ); } |
---|
[4d4882a] | 739 | |
---|
[ee1635c8] | 740 | FunctionDecl * isCopyFunction( Declaration * decl, const std::string & fname ) { |
---|
[4d4882a] | 741 | FunctionDecl * function = dynamic_cast< FunctionDecl * >( decl ); |
---|
[0a267c1] | 742 | if ( ! function ) return nullptr; |
---|
| 743 | if ( function->name != fname ) return nullptr; |
---|
| 744 | FunctionType * ftype = function->type; |
---|
| 745 | if ( ftype->parameters.size() != 2 ) return nullptr; |
---|
[4d4882a] | 746 | |
---|
[ce8c12f] | 747 | Type * t1 = getPointerBase( ftype->get_parameters().front()->get_type() ); |
---|
[0a267c1] | 748 | Type * t2 = ftype->parameters.back()->get_type(); |
---|
[ce8c12f] | 749 | assert( t1 ); |
---|
[4d4882a] | 750 | |
---|
[ce8c12f] | 751 | if ( ResolvExpr::typesCompatibleIgnoreQualifiers( t1, t2, SymTab::Indexer() ) ) { |
---|
[4d4882a] | 752 | return function; |
---|
| 753 | } else { |
---|
[ee1635c8] | 754 | return nullptr; |
---|
[4d4882a] | 755 | } |
---|
| 756 | } |
---|
[ee1635c8] | 757 | |
---|
[207c7e1d] | 758 | FunctionDecl * isAssignment( Declaration * decl ) { |
---|
| 759 | return isCopyFunction( decl, "?=?" ); |
---|
| 760 | } |
---|
| 761 | FunctionDecl * isDestructor( Declaration * decl ) { |
---|
| 762 | if ( isDestructor( decl->get_name() ) ) { |
---|
| 763 | return dynamic_cast< FunctionDecl * >( decl ); |
---|
| 764 | } |
---|
| 765 | return nullptr; |
---|
| 766 | } |
---|
| 767 | FunctionDecl * isDefaultConstructor( Declaration * decl ) { |
---|
[0a267c1] | 768 | if ( isConstructor( decl->name ) ) { |
---|
[207c7e1d] | 769 | if ( FunctionDecl * func = dynamic_cast< FunctionDecl * >( decl ) ) { |
---|
[0a267c1] | 770 | if ( func->type->parameters.size() == 1 ) { |
---|
[207c7e1d] | 771 | return func; |
---|
| 772 | } |
---|
| 773 | } |
---|
| 774 | } |
---|
| 775 | return nullptr; |
---|
| 776 | } |
---|
[ee1635c8] | 777 | FunctionDecl * isCopyConstructor( Declaration * decl ) { |
---|
| 778 | return isCopyFunction( decl, "?{}" ); |
---|
| 779 | } |
---|
[2b46a13] | 780 | } |
---|