[4d2434a] | 1 | #include <algorithm> |
---|
[2b46a13] | 2 | #include "InitTweak.h" |
---|
| 3 | #include "SynTree/Visitor.h" |
---|
| 4 | #include "SynTree/Statement.h" |
---|
| 5 | #include "SynTree/Initializer.h" |
---|
| 6 | #include "SynTree/Expression.h" |
---|
[f9cebb5] | 7 | #include "SynTree/Attribute.h" |
---|
[2b46a13] | 8 | #include "GenPoly/GenPoly.h" |
---|
| 9 | |
---|
| 10 | namespace InitTweak { |
---|
[64071c2] | 11 | namespace { |
---|
| 12 | class HasDesignations : public Visitor { |
---|
| 13 | public: |
---|
| 14 | bool hasDesignations = false; |
---|
| 15 | template<typename Init> |
---|
| 16 | void handleInit( Init * init ) { |
---|
| 17 | if ( ! init->get_designators().empty() ) hasDesignations = true; |
---|
| 18 | else Visitor::visit( init ); |
---|
| 19 | } |
---|
| 20 | virtual void visit( SingleInit * singleInit ) { handleInit( singleInit); } |
---|
| 21 | virtual void visit( ListInit * listInit ) { handleInit( listInit); } |
---|
| 22 | }; |
---|
[2b46a13] | 23 | |
---|
[4d2434a] | 24 | class InitFlattener : public Visitor { |
---|
[64071c2] | 25 | public: |
---|
| 26 | virtual void visit( SingleInit * singleInit ); |
---|
| 27 | virtual void visit( ListInit * listInit ); |
---|
| 28 | std::list< Expression * > argList; |
---|
| 29 | }; |
---|
[2b46a13] | 30 | |
---|
[4d2434a] | 31 | void InitFlattener::visit( SingleInit * singleInit ) { |
---|
[64071c2] | 32 | argList.push_back( singleInit->get_value()->clone() ); |
---|
| 33 | } |
---|
[2b46a13] | 34 | |
---|
[4d2434a] | 35 | void InitFlattener::visit( ListInit * listInit ) { |
---|
| 36 | // flatten nested list inits |
---|
| 37 | std::list<Initializer*>::iterator it = listInit->begin(); |
---|
| 38 | for ( ; it != listInit->end(); ++it ) { |
---|
[64071c2] | 39 | (*it)->accept( *this ); |
---|
| 40 | } |
---|
| 41 | } |
---|
| 42 | } |
---|
[2b46a13] | 43 | |
---|
[64071c2] | 44 | std::list< Expression * > makeInitList( Initializer * init ) { |
---|
[4d2434a] | 45 | InitFlattener flattener; |
---|
| 46 | maybeAccept( init, flattener ); |
---|
| 47 | return flattener.argList; |
---|
[64071c2] | 48 | } |
---|
[2b46a13] | 49 | |
---|
[64071c2] | 50 | bool isDesignated( Initializer * init ) { |
---|
| 51 | HasDesignations finder; |
---|
| 52 | maybeAccept( init, finder ); |
---|
| 53 | return finder.hasDesignations; |
---|
| 54 | } |
---|
[2b46a13] | 55 | |
---|
[39f84a4] | 56 | class InitExpander::ExpanderImpl { |
---|
| 57 | public: |
---|
| 58 | virtual std::list< Expression * > next( std::list< Expression * > & indices ) = 0; |
---|
[4d2434a] | 59 | virtual Statement * buildListInit( UntypedExpr * callExpr, std::list< Expression * > & indices ) = 0; |
---|
[39f84a4] | 60 | }; |
---|
| 61 | |
---|
| 62 | class InitImpl : public InitExpander::ExpanderImpl { |
---|
| 63 | public: |
---|
[4d2434a] | 64 | InitImpl( Initializer * init ) : init( init ) {} |
---|
[39f84a4] | 65 | |
---|
| 66 | virtual std::list< Expression * > next( std::list< Expression * > & indices ) { |
---|
| 67 | // this is wrong, but just a placeholder for now |
---|
[4d2434a] | 68 | // if ( ! flattened ) flatten( indices ); |
---|
| 69 | // return ! inits.empty() ? makeInitList( inits.front() ) : std::list< Expression * >(); |
---|
| 70 | return makeInitList( init ); |
---|
[39f84a4] | 71 | } |
---|
[4d2434a] | 72 | |
---|
| 73 | virtual Statement * buildListInit( UntypedExpr * callExpr, std::list< Expression * > & indices ); |
---|
[39f84a4] | 74 | private: |
---|
[4d2434a] | 75 | Initializer * init; |
---|
[39f84a4] | 76 | }; |
---|
| 77 | |
---|
| 78 | class ExprImpl : public InitExpander::ExpanderImpl { |
---|
| 79 | public: |
---|
| 80 | ExprImpl( Expression * expr ) : arg( expr ) {} |
---|
| 81 | |
---|
| 82 | virtual std::list< Expression * > next( std::list< Expression * > & indices ) { |
---|
| 83 | std::list< Expression * > ret; |
---|
| 84 | Expression * expr = maybeClone( arg ); |
---|
| 85 | if ( expr ) { |
---|
| 86 | for ( std::list< Expression * >::reverse_iterator it = indices.rbegin(); it != indices.rend(); ++it ) { |
---|
| 87 | // go through indices and layer on subscript exprs ?[?] |
---|
| 88 | ++it; |
---|
| 89 | UntypedExpr * subscriptExpr = new UntypedExpr( new NameExpr( "?[?]") ); |
---|
| 90 | subscriptExpr->get_args().push_back( expr ); |
---|
| 91 | subscriptExpr->get_args().push_back( (*it)->clone() ); |
---|
| 92 | expr = subscriptExpr; |
---|
| 93 | } |
---|
| 94 | ret.push_back( expr ); |
---|
| 95 | } |
---|
| 96 | return ret; |
---|
| 97 | } |
---|
[4d2434a] | 98 | |
---|
| 99 | virtual Statement * buildListInit( UntypedExpr * callExpr, std::list< Expression * > & indices ); |
---|
[39f84a4] | 100 | private: |
---|
| 101 | Expression * arg; |
---|
| 102 | }; |
---|
| 103 | |
---|
| 104 | InitExpander::InitExpander( Initializer * init ) : expander( new InitImpl( init ) ) {} |
---|
| 105 | |
---|
| 106 | InitExpander::InitExpander( Expression * expr ) : expander( new ExprImpl( expr ) ) {} |
---|
| 107 | |
---|
| 108 | std::list< Expression * > InitExpander::operator*() { |
---|
| 109 | return cur; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | InitExpander & InitExpander::operator++() { |
---|
| 113 | cur = expander->next( indices ); |
---|
| 114 | return *this; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | // use array indices list to build switch statement |
---|
| 118 | void InitExpander::addArrayIndex( Expression * index, Expression * dimension ) { |
---|
| 119 | indices.push_back( index ); |
---|
| 120 | indices.push_back( dimension ); |
---|
| 121 | } |
---|
| 122 | |
---|
[4d2434a] | 123 | void InitExpander::clearArrayIndices() { |
---|
| 124 | indices.clear(); |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | namespace { |
---|
[f9cebb5] | 128 | /// given index i, dimension d, initializer init, and callExpr f, generates |
---|
| 129 | /// if (i < d) f(..., init) |
---|
| 130 | /// ++i; |
---|
| 131 | /// so that only elements within the range of the array are constructed |
---|
[4d2434a] | 132 | template< typename OutIterator > |
---|
[f9cebb5] | 133 | void buildCallExpr( UntypedExpr * callExpr, Expression * index, Expression * dimension, Initializer * init, OutIterator out ) { |
---|
[4d2434a] | 134 | UntypedExpr * cond = new UntypedExpr( new NameExpr( "?<?") ); |
---|
| 135 | cond->get_args().push_back( index->clone() ); |
---|
| 136 | cond->get_args().push_back( dimension->clone() ); |
---|
| 137 | |
---|
| 138 | std::list< Expression * > args = makeInitList( init ); |
---|
| 139 | callExpr->get_args().splice( callExpr->get_args().end(), args ); |
---|
| 140 | |
---|
| 141 | *out++ = new IfStmt( noLabels, cond, new ExprStmt( noLabels, callExpr ), NULL ); |
---|
| 142 | |
---|
| 143 | UntypedExpr * increment = new UntypedExpr( new NameExpr( "++?" ) ); |
---|
| 144 | increment->get_args().push_back( new AddressExpr( index->clone() ) ); |
---|
| 145 | *out++ = new ExprStmt( noLabels, increment ); |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | template< typename OutIterator > |
---|
| 149 | void build( UntypedExpr * callExpr, InitExpander::IndexList::iterator idx, InitExpander::IndexList::iterator idxEnd, Initializer * init, OutIterator out ) { |
---|
| 150 | if ( idx == idxEnd ) return; |
---|
| 151 | Expression * index = *idx++; |
---|
| 152 | assert( idx != idxEnd ); |
---|
| 153 | Expression * dimension = *idx++; |
---|
| 154 | |
---|
[f9cebb5] | 155 | // xxx - may want to eventually issue a warning here if we can detect |
---|
| 156 | // that the number of elements exceeds to dimension of the array |
---|
[4d2434a] | 157 | if ( idx == idxEnd ) { |
---|
| 158 | if ( ListInit * listInit = dynamic_cast< ListInit * >( init ) ) { |
---|
| 159 | for ( Initializer * init : *listInit ) { |
---|
[f9cebb5] | 160 | buildCallExpr( callExpr->clone(), index, dimension, init, out ); |
---|
[4d2434a] | 161 | } |
---|
| 162 | } else { |
---|
[f9cebb5] | 163 | buildCallExpr( callExpr->clone(), index, dimension, init, out ); |
---|
[4d2434a] | 164 | } |
---|
| 165 | } else { |
---|
| 166 | std::list< Statement * > branches; |
---|
| 167 | |
---|
| 168 | unsigned long cond = 0; |
---|
| 169 | ListInit * listInit = dynamic_cast< ListInit * >( init ); |
---|
| 170 | if ( ! listInit ) { |
---|
| 171 | // xxx - this shouldn't be an error, but need a way to |
---|
| 172 | // terminate without creating output, so should catch this error |
---|
| 173 | throw SemanticError( "unbalanced list initializers" ); |
---|
| 174 | } |
---|
[f9cebb5] | 175 | |
---|
| 176 | static UniqueName targetLabel( "L__autogen__" ); |
---|
| 177 | Label switchLabel( targetLabel.newName(), 0, std::list< Attribute * >{ new Attribute("unused") } ); |
---|
[4d2434a] | 178 | for ( Initializer * init : *listInit ) { |
---|
| 179 | Expression * condition; |
---|
| 180 | // check for designations |
---|
| 181 | // if ( init-> ) { |
---|
| 182 | condition = new ConstantExpr( Constant::from_ulong( cond ) ); |
---|
| 183 | ++cond; |
---|
| 184 | // } else { |
---|
| 185 | // condition = // ... take designation |
---|
| 186 | // cond = // ... take designation+1 |
---|
| 187 | // } |
---|
| 188 | std::list< Statement * > stmts; |
---|
| 189 | build( callExpr, idx, idxEnd, init, back_inserter( stmts ) ); |
---|
[f9cebb5] | 190 | stmts.push_back( new BranchStmt( noLabels, switchLabel, BranchStmt::Break ) ); |
---|
[4d2434a] | 191 | CaseStmt * caseStmt = new CaseStmt( noLabels, condition, stmts ); |
---|
| 192 | branches.push_back( caseStmt ); |
---|
| 193 | } |
---|
| 194 | *out++ = new SwitchStmt( noLabels, index->clone(), branches ); |
---|
[f9cebb5] | 195 | *out++ = new NullStmt( std::list<Label>{ switchLabel } ); |
---|
[4d2434a] | 196 | } |
---|
| 197 | } |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | // if array came with an initializer list: initialize each element |
---|
| 201 | // may have more initializers than elements in the array - need to check at each index that |
---|
| 202 | // we haven't exceeded size. |
---|
| 203 | // may have fewer initializers than elements in the array - need to default construct |
---|
| 204 | // remaining elements. |
---|
| 205 | // To accomplish this, generate switch statement, consuming all of expander's elements |
---|
| 206 | Statement * InitImpl::buildListInit( UntypedExpr * dst, std::list< Expression * > & indices ) { |
---|
| 207 | if ( ! init ) return NULL; |
---|
[f9cebb5] | 208 | CompoundStmt * block = new CompoundStmt( noLabels ); |
---|
| 209 | build( dst, indices.begin(), indices.end(), init, back_inserter( block->get_kids() ) ); |
---|
| 210 | if ( block->get_kids().empty() ) { |
---|
| 211 | delete block; |
---|
[4d2434a] | 212 | return NULL; |
---|
| 213 | } else { |
---|
| 214 | init = NULL; // init was consumed in creating the list init |
---|
[f9cebb5] | 215 | return block; |
---|
[4d2434a] | 216 | } |
---|
[39f84a4] | 217 | } |
---|
| 218 | |
---|
[4d2434a] | 219 | Statement * ExprImpl::buildListInit( UntypedExpr * dst, std::list< Expression * > & indices ) { |
---|
| 220 | return NULL; |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | Statement * InitExpander::buildListInit( UntypedExpr * dst ) { |
---|
| 224 | return expander->buildListInit( dst, indices ); |
---|
| 225 | } |
---|
| 226 | |
---|
[64071c2] | 227 | bool tryConstruct( ObjectDecl * objDecl ) { |
---|
| 228 | return ! LinkageSpec::isBuiltin( objDecl->get_linkage() ) && |
---|
| 229 | (objDecl->get_init() == NULL || |
---|
| 230 | ( objDecl->get_init() != NULL && objDecl->get_init()->get_maybeConstructed() )) && |
---|
[6cf27a07] | 231 | ! isDesignated( objDecl->get_init() ) |
---|
| 232 | && objDecl->get_storageClass() != DeclarationNode::Extern; |
---|
[64071c2] | 233 | } |
---|
[2b46a13] | 234 | |
---|
[4d2434a] | 235 | class CallFinder : public Visitor { |
---|
| 236 | public: |
---|
| 237 | typedef Visitor Parent; |
---|
| 238 | CallFinder( const std::list< std::string > & names ) : names( names ) {} |
---|
| 239 | |
---|
| 240 | virtual void visit( ApplicationExpr * appExpr ) { |
---|
| 241 | handleCallExpr( appExpr ); |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | virtual void visit( UntypedExpr * untypedExpr ) { |
---|
| 245 | handleCallExpr( untypedExpr ); |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | std::list< Expression * > * matches; |
---|
| 249 | private: |
---|
| 250 | const std::list< std::string > names; |
---|
| 251 | |
---|
| 252 | template< typename CallExpr > |
---|
| 253 | void handleCallExpr( CallExpr * expr ) { |
---|
| 254 | Parent::visit( expr ); |
---|
| 255 | std::string fname = getFunctionName( expr ); |
---|
| 256 | if ( std::find( names.begin(), names.end(), fname ) != names.end() ) { |
---|
| 257 | matches->push_back( expr ); |
---|
[cad355a] | 258 | } |
---|
[64071c2] | 259 | } |
---|
[4d2434a] | 260 | }; |
---|
| 261 | |
---|
| 262 | void collectCtorDtorCalls( Statement * stmt, std::list< Expression * > & matches ) { |
---|
| 263 | static CallFinder finder( std::list< std::string >{ "?{}", "^?{}" } ); |
---|
| 264 | finder.matches = &matches; |
---|
| 265 | maybeAccept( stmt, finder ); |
---|
[64071c2] | 266 | } |
---|
[4d2434a] | 267 | |
---|
| 268 | Expression * getCtorDtorCall( Statement * stmt ) { |
---|
| 269 | std::list< Expression * > matches; |
---|
| 270 | collectCtorDtorCalls( stmt, matches ); |
---|
| 271 | assert( matches.size() <= 1 ); |
---|
| 272 | return matches.size() == 1 ? matches.front() : NULL; |
---|
| 273 | } |
---|
| 274 | |
---|
[aedfd91] | 275 | namespace { |
---|
| 276 | VariableExpr * getCalledFunction( ApplicationExpr * appExpr ) { |
---|
| 277 | assert( appExpr ); |
---|
[4d2434a] | 278 | // xxx - it's possible this can be other things, e.g. MemberExpr, so this is insufficient |
---|
[aedfd91] | 279 | return dynamic_cast< VariableExpr * >( appExpr->get_function() ); |
---|
| 280 | } |
---|
| 281 | } |
---|
[70f89d00] | 282 | |
---|
[aedfd91] | 283 | ApplicationExpr * isIntrinsicCallExpr( Expression * expr ) { |
---|
| 284 | ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr ); |
---|
| 285 | if ( ! appExpr ) return NULL; |
---|
| 286 | VariableExpr * function = getCalledFunction( appExpr ); |
---|
[64071c2] | 287 | assert( function ); |
---|
| 288 | // check for Intrinsic only - don't want to remove all overridable ctor/dtors because autogenerated ctor/dtor |
---|
| 289 | // will call all member dtors, and some members may have a user defined dtor. |
---|
[aedfd91] | 290 | return function->get_var()->get_linkage() == LinkageSpec::Intrinsic ? appExpr : NULL; |
---|
| 291 | } |
---|
| 292 | |
---|
[f9cebb5] | 293 | bool isIntrinsicSingleArgCallStmt( Statement * stmt ) { |
---|
[4d2434a] | 294 | std::list< Expression * > callExprs; |
---|
| 295 | collectCtorDtorCalls( stmt, callExprs ); |
---|
| 296 | // if ( callExprs.empty() ) return false; // xxx - do I still need this check? |
---|
| 297 | return std::all_of( callExprs.begin(), callExprs.end(), []( Expression * callExpr ){ |
---|
| 298 | if ( ApplicationExpr * appExpr = isIntrinsicCallExpr( callExpr ) ) { |
---|
| 299 | assert( ! appExpr->get_function()->get_results().empty() ); |
---|
| 300 | FunctionType *funcType = GenPoly::getFunctionType( appExpr->get_function()->get_results().front() ); |
---|
| 301 | assert( funcType ); |
---|
| 302 | return funcType->get_parameters().size() == 1; |
---|
| 303 | } |
---|
| 304 | return false; |
---|
| 305 | }); |
---|
[64071c2] | 306 | } |
---|
[f1b1e4c] | 307 | |
---|
[64071c2] | 308 | namespace { |
---|
| 309 | template<typename CallExpr> |
---|
| 310 | Expression *& callArg( CallExpr * callExpr, unsigned int pos ) { |
---|
| 311 | if ( pos >= callExpr->get_args().size() ) assert( false && "asking for argument that doesn't exist. Return NULL/throw exception?" ); |
---|
| 312 | for ( Expression *& arg : callExpr->get_args() ) { |
---|
| 313 | if ( pos == 0 ) return arg; |
---|
| 314 | pos--; |
---|
| 315 | } |
---|
| 316 | assert( false ); |
---|
| 317 | } |
---|
| 318 | } |
---|
[f1b1e4c] | 319 | |
---|
[64071c2] | 320 | Expression *& getCallArg( Expression * callExpr, unsigned int pos ) { |
---|
| 321 | if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( callExpr ) ) { |
---|
| 322 | return callArg( appExpr, pos ); |
---|
| 323 | } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * >( callExpr ) ) { |
---|
| 324 | return callArg( untypedExpr, pos ); |
---|
| 325 | } else { |
---|
| 326 | assert( false && "Unexpected expression type passed to getCallArg" ); |
---|
| 327 | } |
---|
| 328 | } |
---|
[f1b1e4c] | 329 | |
---|
[64071c2] | 330 | namespace { |
---|
[c738ca4] | 331 | std::string funcName( Expression * func ) { |
---|
[64071c2] | 332 | if ( NameExpr * nameExpr = dynamic_cast< NameExpr * >( func ) ) { |
---|
| 333 | return nameExpr->get_name(); |
---|
| 334 | } else if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( func ) ) { |
---|
| 335 | return varExpr->get_var()->get_name(); |
---|
[c738ca4] | 336 | } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( func ) ) { |
---|
| 337 | return funcName( castExpr->get_arg() ); |
---|
[64071c2] | 338 | } else { |
---|
| 339 | assert( false && "Unexpected expression type being called as a function in call expression" ); |
---|
| 340 | } |
---|
| 341 | } |
---|
| 342 | } |
---|
[70f89d00] | 343 | |
---|
[64071c2] | 344 | std::string getFunctionName( Expression * expr ) { |
---|
| 345 | if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr ) ) { |
---|
[c738ca4] | 346 | return funcName( appExpr->get_function() ); |
---|
[64071c2] | 347 | } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * > ( expr ) ) { |
---|
[c738ca4] | 348 | return funcName( untypedExpr->get_function() ); |
---|
[64071c2] | 349 | } else { |
---|
[c738ca4] | 350 | std::cerr << expr << std::endl; |
---|
[64071c2] | 351 | assert( false && "Unexpected expression type passed to getFunctionName" ); |
---|
| 352 | } |
---|
| 353 | } |
---|
[10a7775] | 354 | |
---|
[64071c2] | 355 | Type * getPointerBase( Type * type ) { |
---|
| 356 | if ( PointerType * ptrType = dynamic_cast< PointerType * >( type ) ) { |
---|
| 357 | return ptrType->get_base(); |
---|
| 358 | } else if ( ArrayType * arrayType = dynamic_cast< ArrayType * >( type ) ) { |
---|
| 359 | return arrayType->get_base(); |
---|
| 360 | } else { |
---|
| 361 | return NULL; |
---|
| 362 | } |
---|
| 363 | } |
---|
[10a7775] | 364 | |
---|
[64071c2] | 365 | Type * isPointerType( Type * type ) { |
---|
| 366 | if ( getPointerBase( type ) ) return type; |
---|
| 367 | else return NULL; |
---|
| 368 | } |
---|
[40e636a] | 369 | |
---|
| 370 | class ConstExprChecker : public Visitor { |
---|
| 371 | public: |
---|
| 372 | ConstExprChecker() : isConstExpr( true ) {} |
---|
| 373 | |
---|
| 374 | virtual void visit( ApplicationExpr *applicationExpr ) { isConstExpr = false; } |
---|
| 375 | virtual void visit( UntypedExpr *untypedExpr ) { isConstExpr = false; } |
---|
| 376 | virtual void visit( NameExpr *nameExpr ) { isConstExpr = false; } |
---|
| 377 | virtual void visit( CastExpr *castExpr ) { isConstExpr = false; } |
---|
| 378 | virtual void visit( LabelAddressExpr *labAddressExpr ) { isConstExpr = false; } |
---|
| 379 | virtual void visit( UntypedMemberExpr *memberExpr ) { isConstExpr = false; } |
---|
| 380 | virtual void visit( MemberExpr *memberExpr ) { isConstExpr = false; } |
---|
| 381 | virtual void visit( VariableExpr *variableExpr ) { isConstExpr = false; } |
---|
| 382 | virtual void visit( ConstantExpr *constantExpr ) { /* bottom out */ } |
---|
| 383 | // these might be okay? |
---|
| 384 | // virtual void visit( SizeofExpr *sizeofExpr ); |
---|
| 385 | // virtual void visit( AlignofExpr *alignofExpr ); |
---|
| 386 | // virtual void visit( UntypedOffsetofExpr *offsetofExpr ); |
---|
| 387 | // virtual void visit( OffsetofExpr *offsetofExpr ); |
---|
| 388 | // virtual void visit( OffsetPackExpr *offsetPackExpr ); |
---|
| 389 | // virtual void visit( AttrExpr *attrExpr ); |
---|
| 390 | // virtual void visit( CommaExpr *commaExpr ); |
---|
| 391 | // virtual void visit( LogicalExpr *logicalExpr ); |
---|
| 392 | // virtual void visit( ConditionalExpr *conditionalExpr ); |
---|
| 393 | virtual void visit( TupleExpr *tupleExpr ) { isConstExpr = false; } |
---|
| 394 | virtual void visit( SolvedTupleExpr *tupleExpr ) { isConstExpr = false; } |
---|
| 395 | virtual void visit( TypeExpr *typeExpr ) { isConstExpr = false; } |
---|
| 396 | virtual void visit( AsmExpr *asmExpr ) { isConstExpr = false; } |
---|
| 397 | virtual void visit( UntypedValofExpr *valofExpr ) { isConstExpr = false; } |
---|
| 398 | virtual void visit( CompoundLiteralExpr *compLitExpr ) { isConstExpr = false; } |
---|
| 399 | |
---|
| 400 | bool isConstExpr; |
---|
| 401 | }; |
---|
| 402 | |
---|
| 403 | bool isConstExpr( Expression * expr ) { |
---|
| 404 | if ( expr ) { |
---|
| 405 | ConstExprChecker checker; |
---|
| 406 | expr->accept( checker ); |
---|
| 407 | return checker.isConstExpr; |
---|
| 408 | } |
---|
| 409 | return true; |
---|
| 410 | } |
---|
| 411 | |
---|
| 412 | bool isConstExpr( Initializer * init ) { |
---|
| 413 | if ( init ) { |
---|
| 414 | ConstExprChecker checker; |
---|
| 415 | init->accept( checker ); |
---|
| 416 | return checker.isConstExpr; |
---|
| 417 | } // if |
---|
| 418 | // for all intents and purposes, no initializer means const expr |
---|
| 419 | return true; |
---|
| 420 | } |
---|
| 421 | |
---|
[2b46a13] | 422 | } |
---|