[d180746] | 1 | #include <stddef.h> // for NULL |
---|
| 2 | #include <algorithm> // for find, all_of |
---|
[e3e16bc] | 3 | #include <cassert> // for assertf, assert, strict_dynamic_cast |
---|
[d180746] | 4 | #include <iostream> // for ostream, cerr, endl |
---|
| 5 | #include <iterator> // for back_insert_iterator, back_inserter |
---|
| 6 | #include <memory> // for __shared_ptr |
---|
| 7 | |
---|
| 8 | #include "Common/SemanticError.h" // for SemanticError |
---|
| 9 | #include "Common/UniqueName.h" // for UniqueName |
---|
| 10 | #include "Common/utility.h" // for toString, deleteAll, maybeClone |
---|
| 11 | #include "GenPoly/GenPoly.h" // for getFunctionType |
---|
[2b46a13] | 12 | #include "InitTweak.h" |
---|
[d180746] | 13 | #include "Parser/LinkageSpec.h" // for Spec, isBuiltin, Intrinsic |
---|
| 14 | #include "ResolvExpr/typeops.h" // for typesCompatibleIgnoreQualifiers |
---|
| 15 | #include "SymTab/Indexer.h" // for Indexer |
---|
| 16 | #include "SynTree/Attribute.h" // for Attribute |
---|
| 17 | #include "SynTree/Constant.h" // for Constant |
---|
| 18 | #include "SynTree/Declaration.h" // for ObjectDecl, DeclarationWithType |
---|
| 19 | #include "SynTree/Expression.h" // for Expression, UntypedExpr, Applicati... |
---|
| 20 | #include "SynTree/Initializer.h" // for Initializer, ListInit, Designation |
---|
| 21 | #include "SynTree/Label.h" // for Label, noLabels |
---|
| 22 | #include "SynTree/Statement.h" // for CompoundStmt, ExprStmt, BranchStmt |
---|
| 23 | #include "SynTree/Type.h" // for FunctionType, ArrayType, PointerType |
---|
| 24 | #include "SynTree/Visitor.h" // for Visitor, maybeAccept |
---|
| 25 | |
---|
| 26 | class UntypedValofExpr; |
---|
[2b46a13] | 27 | |
---|
| 28 | namespace InitTweak { |
---|
[64071c2] | 29 | namespace { |
---|
| 30 | class HasDesignations : public Visitor { |
---|
| 31 | public: |
---|
| 32 | bool hasDesignations = false; |
---|
[e4d829b] | 33 | virtual void visit( Designation * des ) { |
---|
| 34 | if ( ! des->get_designators().empty() ) hasDesignations = true; |
---|
| 35 | else Visitor::visit( des ); |
---|
[64071c2] | 36 | } |
---|
| 37 | }; |
---|
[2b46a13] | 38 | |
---|
[dcd73d1] | 39 | class InitDepthChecker : public Visitor { |
---|
| 40 | public: |
---|
| 41 | bool depthOkay = true; |
---|
| 42 | Type * type; |
---|
| 43 | int curDepth = 0, maxDepth = 0; |
---|
| 44 | InitDepthChecker( Type * type ) : type( type ) { |
---|
| 45 | Type * t = type; |
---|
| 46 | while ( ArrayType * at = dynamic_cast< ArrayType * >( t ) ) { |
---|
| 47 | maxDepth++; |
---|
| 48 | t = at->get_base(); |
---|
| 49 | } |
---|
| 50 | maxDepth++; |
---|
| 51 | } |
---|
| 52 | virtual void visit( ListInit * listInit ) { |
---|
| 53 | curDepth++; |
---|
| 54 | if ( curDepth > maxDepth ) depthOkay = false; |
---|
| 55 | Visitor::visit( listInit ); |
---|
| 56 | curDepth--; |
---|
| 57 | } |
---|
| 58 | }; |
---|
| 59 | |
---|
[4d2434a] | 60 | class InitFlattener : public Visitor { |
---|
[64071c2] | 61 | public: |
---|
| 62 | virtual void visit( SingleInit * singleInit ); |
---|
| 63 | virtual void visit( ListInit * listInit ); |
---|
| 64 | std::list< Expression * > argList; |
---|
| 65 | }; |
---|
[2b46a13] | 66 | |
---|
[4d2434a] | 67 | void InitFlattener::visit( SingleInit * singleInit ) { |
---|
[64071c2] | 68 | argList.push_back( singleInit->get_value()->clone() ); |
---|
| 69 | } |
---|
[2b46a13] | 70 | |
---|
[4d2434a] | 71 | void InitFlattener::visit( ListInit * listInit ) { |
---|
| 72 | // flatten nested list inits |
---|
| 73 | std::list<Initializer*>::iterator it = listInit->begin(); |
---|
| 74 | for ( ; it != listInit->end(); ++it ) { |
---|
[64071c2] | 75 | (*it)->accept( *this ); |
---|
| 76 | } |
---|
| 77 | } |
---|
| 78 | } |
---|
[2b46a13] | 79 | |
---|
[64071c2] | 80 | std::list< Expression * > makeInitList( Initializer * init ) { |
---|
[4d2434a] | 81 | InitFlattener flattener; |
---|
| 82 | maybeAccept( init, flattener ); |
---|
| 83 | return flattener.argList; |
---|
[64071c2] | 84 | } |
---|
[2b46a13] | 85 | |
---|
[64071c2] | 86 | bool isDesignated( Initializer * init ) { |
---|
| 87 | HasDesignations finder; |
---|
| 88 | maybeAccept( init, finder ); |
---|
| 89 | return finder.hasDesignations; |
---|
[dcd73d1] | 90 | } |
---|
| 91 | |
---|
| 92 | bool checkInitDepth( ObjectDecl * objDecl ) { |
---|
| 93 | InitDepthChecker checker( objDecl->get_type() ); |
---|
| 94 | maybeAccept( objDecl->get_init(), checker ); |
---|
| 95 | return checker.depthOkay; |
---|
[64071c2] | 96 | } |
---|
[2b46a13] | 97 | |
---|
[39f84a4] | 98 | class InitExpander::ExpanderImpl { |
---|
| 99 | public: |
---|
| 100 | virtual std::list< Expression * > next( std::list< Expression * > & indices ) = 0; |
---|
[4d2434a] | 101 | virtual Statement * buildListInit( UntypedExpr * callExpr, std::list< Expression * > & indices ) = 0; |
---|
[39f84a4] | 102 | }; |
---|
| 103 | |
---|
| 104 | class InitImpl : public InitExpander::ExpanderImpl { |
---|
| 105 | public: |
---|
[4d2434a] | 106 | InitImpl( Initializer * init ) : init( init ) {} |
---|
[39f84a4] | 107 | |
---|
[7e003011] | 108 | virtual std::list< Expression * > next( __attribute((unused)) std::list< Expression * > & indices ) { |
---|
[39f84a4] | 109 | // this is wrong, but just a placeholder for now |
---|
[4d2434a] | 110 | // if ( ! flattened ) flatten( indices ); |
---|
| 111 | // return ! inits.empty() ? makeInitList( inits.front() ) : std::list< Expression * >(); |
---|
| 112 | return makeInitList( init ); |
---|
[39f84a4] | 113 | } |
---|
[4d2434a] | 114 | |
---|
| 115 | virtual Statement * buildListInit( UntypedExpr * callExpr, std::list< Expression * > & indices ); |
---|
[39f84a4] | 116 | private: |
---|
[4d2434a] | 117 | Initializer * init; |
---|
[39f84a4] | 118 | }; |
---|
| 119 | |
---|
| 120 | class ExprImpl : public InitExpander::ExpanderImpl { |
---|
| 121 | public: |
---|
| 122 | ExprImpl( Expression * expr ) : arg( expr ) {} |
---|
| 123 | |
---|
[9b4c936] | 124 | ~ExprImpl() { delete arg; } |
---|
| 125 | |
---|
[39f84a4] | 126 | virtual std::list< Expression * > next( std::list< Expression * > & indices ) { |
---|
| 127 | std::list< Expression * > ret; |
---|
| 128 | Expression * expr = maybeClone( arg ); |
---|
| 129 | if ( expr ) { |
---|
| 130 | for ( std::list< Expression * >::reverse_iterator it = indices.rbegin(); it != indices.rend(); ++it ) { |
---|
| 131 | // go through indices and layer on subscript exprs ?[?] |
---|
| 132 | ++it; |
---|
| 133 | UntypedExpr * subscriptExpr = new UntypedExpr( new NameExpr( "?[?]") ); |
---|
| 134 | subscriptExpr->get_args().push_back( expr ); |
---|
| 135 | subscriptExpr->get_args().push_back( (*it)->clone() ); |
---|
| 136 | expr = subscriptExpr; |
---|
| 137 | } |
---|
| 138 | ret.push_back( expr ); |
---|
| 139 | } |
---|
| 140 | return ret; |
---|
| 141 | } |
---|
[4d2434a] | 142 | |
---|
| 143 | virtual Statement * buildListInit( UntypedExpr * callExpr, std::list< Expression * > & indices ); |
---|
[39f84a4] | 144 | private: |
---|
| 145 | Expression * arg; |
---|
| 146 | }; |
---|
| 147 | |
---|
| 148 | InitExpander::InitExpander( Initializer * init ) : expander( new InitImpl( init ) ) {} |
---|
| 149 | |
---|
| 150 | InitExpander::InitExpander( Expression * expr ) : expander( new ExprImpl( expr ) ) {} |
---|
| 151 | |
---|
| 152 | std::list< Expression * > InitExpander::operator*() { |
---|
| 153 | return cur; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | InitExpander & InitExpander::operator++() { |
---|
| 157 | cur = expander->next( indices ); |
---|
| 158 | return *this; |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | // use array indices list to build switch statement |
---|
| 162 | void InitExpander::addArrayIndex( Expression * index, Expression * dimension ) { |
---|
| 163 | indices.push_back( index ); |
---|
| 164 | indices.push_back( dimension ); |
---|
| 165 | } |
---|
| 166 | |
---|
[4d2434a] | 167 | void InitExpander::clearArrayIndices() { |
---|
[9b4c936] | 168 | deleteAll( indices ); |
---|
[4d2434a] | 169 | indices.clear(); |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | namespace { |
---|
[f9cebb5] | 173 | /// given index i, dimension d, initializer init, and callExpr f, generates |
---|
| 174 | /// if (i < d) f(..., init) |
---|
| 175 | /// ++i; |
---|
| 176 | /// so that only elements within the range of the array are constructed |
---|
[4d2434a] | 177 | template< typename OutIterator > |
---|
[f9cebb5] | 178 | void buildCallExpr( UntypedExpr * callExpr, Expression * index, Expression * dimension, Initializer * init, OutIterator out ) { |
---|
[4d2434a] | 179 | UntypedExpr * cond = new UntypedExpr( new NameExpr( "?<?") ); |
---|
| 180 | cond->get_args().push_back( index->clone() ); |
---|
| 181 | cond->get_args().push_back( dimension->clone() ); |
---|
| 182 | |
---|
| 183 | std::list< Expression * > args = makeInitList( init ); |
---|
| 184 | callExpr->get_args().splice( callExpr->get_args().end(), args ); |
---|
| 185 | |
---|
| 186 | *out++ = new IfStmt( noLabels, cond, new ExprStmt( noLabels, callExpr ), NULL ); |
---|
| 187 | |
---|
| 188 | UntypedExpr * increment = new UntypedExpr( new NameExpr( "++?" ) ); |
---|
[175ad32] | 189 | increment->get_args().push_back( index->clone() ); |
---|
[4d2434a] | 190 | *out++ = new ExprStmt( noLabels, increment ); |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | template< typename OutIterator > |
---|
| 194 | void build( UntypedExpr * callExpr, InitExpander::IndexList::iterator idx, InitExpander::IndexList::iterator idxEnd, Initializer * init, OutIterator out ) { |
---|
| 195 | if ( idx == idxEnd ) return; |
---|
| 196 | Expression * index = *idx++; |
---|
| 197 | assert( idx != idxEnd ); |
---|
| 198 | Expression * dimension = *idx++; |
---|
| 199 | |
---|
[f9cebb5] | 200 | // xxx - may want to eventually issue a warning here if we can detect |
---|
| 201 | // that the number of elements exceeds to dimension of the array |
---|
[4d2434a] | 202 | if ( idx == idxEnd ) { |
---|
| 203 | if ( ListInit * listInit = dynamic_cast< ListInit * >( init ) ) { |
---|
| 204 | for ( Initializer * init : *listInit ) { |
---|
[f9cebb5] | 205 | buildCallExpr( callExpr->clone(), index, dimension, init, out ); |
---|
[4d2434a] | 206 | } |
---|
| 207 | } else { |
---|
[f9cebb5] | 208 | buildCallExpr( callExpr->clone(), index, dimension, init, out ); |
---|
[4d2434a] | 209 | } |
---|
| 210 | } else { |
---|
| 211 | std::list< Statement * > branches; |
---|
| 212 | |
---|
| 213 | unsigned long cond = 0; |
---|
| 214 | ListInit * listInit = dynamic_cast< ListInit * >( init ); |
---|
| 215 | if ( ! listInit ) { |
---|
| 216 | // xxx - this shouldn't be an error, but need a way to |
---|
| 217 | // terminate without creating output, so should catch this error |
---|
| 218 | throw SemanticError( "unbalanced list initializers" ); |
---|
| 219 | } |
---|
[f9cebb5] | 220 | |
---|
| 221 | static UniqueName targetLabel( "L__autogen__" ); |
---|
| 222 | Label switchLabel( targetLabel.newName(), 0, std::list< Attribute * >{ new Attribute("unused") } ); |
---|
[4d2434a] | 223 | for ( Initializer * init : *listInit ) { |
---|
| 224 | Expression * condition; |
---|
| 225 | // check for designations |
---|
| 226 | // if ( init-> ) { |
---|
| 227 | condition = new ConstantExpr( Constant::from_ulong( cond ) ); |
---|
| 228 | ++cond; |
---|
| 229 | // } else { |
---|
| 230 | // condition = // ... take designation |
---|
| 231 | // cond = // ... take designation+1 |
---|
| 232 | // } |
---|
| 233 | std::list< Statement * > stmts; |
---|
| 234 | build( callExpr, idx, idxEnd, init, back_inserter( stmts ) ); |
---|
[f9cebb5] | 235 | stmts.push_back( new BranchStmt( noLabels, switchLabel, BranchStmt::Break ) ); |
---|
[4d2434a] | 236 | CaseStmt * caseStmt = new CaseStmt( noLabels, condition, stmts ); |
---|
| 237 | branches.push_back( caseStmt ); |
---|
| 238 | } |
---|
| 239 | *out++ = new SwitchStmt( noLabels, index->clone(), branches ); |
---|
[f9cebb5] | 240 | *out++ = new NullStmt( std::list<Label>{ switchLabel } ); |
---|
[4d2434a] | 241 | } |
---|
| 242 | } |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | // if array came with an initializer list: initialize each element |
---|
| 246 | // may have more initializers than elements in the array - need to check at each index that |
---|
| 247 | // we haven't exceeded size. |
---|
| 248 | // may have fewer initializers than elements in the array - need to default construct |
---|
| 249 | // remaining elements. |
---|
| 250 | // To accomplish this, generate switch statement, consuming all of expander's elements |
---|
| 251 | Statement * InitImpl::buildListInit( UntypedExpr * dst, std::list< Expression * > & indices ) { |
---|
| 252 | if ( ! init ) return NULL; |
---|
[f9cebb5] | 253 | CompoundStmt * block = new CompoundStmt( noLabels ); |
---|
| 254 | build( dst, indices.begin(), indices.end(), init, back_inserter( block->get_kids() ) ); |
---|
| 255 | if ( block->get_kids().empty() ) { |
---|
| 256 | delete block; |
---|
[4d2434a] | 257 | return NULL; |
---|
| 258 | } else { |
---|
| 259 | init = NULL; // init was consumed in creating the list init |
---|
[f9cebb5] | 260 | return block; |
---|
[4d2434a] | 261 | } |
---|
[39f84a4] | 262 | } |
---|
| 263 | |
---|
[7e003011] | 264 | Statement * ExprImpl::buildListInit( __attribute((unused)) UntypedExpr * dst, __attribute((unused)) std::list< Expression * > & indices ) { |
---|
[4d2434a] | 265 | return NULL; |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | Statement * InitExpander::buildListInit( UntypedExpr * dst ) { |
---|
| 269 | return expander->buildListInit( dst, indices ); |
---|
| 270 | } |
---|
| 271 | |
---|
[64071c2] | 272 | bool tryConstruct( ObjectDecl * objDecl ) { |
---|
| 273 | return ! LinkageSpec::isBuiltin( objDecl->get_linkage() ) && |
---|
| 274 | (objDecl->get_init() == NULL || |
---|
[1ba88a0] | 275 | ( objDecl->get_init() != NULL && objDecl->get_init()->get_maybeConstructed() )) |
---|
[08d5507b] | 276 | && ! objDecl->get_storageClasses().is_extern; |
---|
[64071c2] | 277 | } |
---|
[2b46a13] | 278 | |
---|
[4d2434a] | 279 | class CallFinder : public Visitor { |
---|
| 280 | public: |
---|
| 281 | typedef Visitor Parent; |
---|
| 282 | CallFinder( const std::list< std::string > & names ) : names( names ) {} |
---|
| 283 | |
---|
| 284 | virtual void visit( ApplicationExpr * appExpr ) { |
---|
| 285 | handleCallExpr( appExpr ); |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | virtual void visit( UntypedExpr * untypedExpr ) { |
---|
| 289 | handleCallExpr( untypedExpr ); |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | std::list< Expression * > * matches; |
---|
| 293 | private: |
---|
| 294 | const std::list< std::string > names; |
---|
| 295 | |
---|
| 296 | template< typename CallExpr > |
---|
| 297 | void handleCallExpr( CallExpr * expr ) { |
---|
| 298 | Parent::visit( expr ); |
---|
| 299 | std::string fname = getFunctionName( expr ); |
---|
| 300 | if ( std::find( names.begin(), names.end(), fname ) != names.end() ) { |
---|
| 301 | matches->push_back( expr ); |
---|
[cad355a] | 302 | } |
---|
[64071c2] | 303 | } |
---|
[4d2434a] | 304 | }; |
---|
| 305 | |
---|
| 306 | void collectCtorDtorCalls( Statement * stmt, std::list< Expression * > & matches ) { |
---|
| 307 | static CallFinder finder( std::list< std::string >{ "?{}", "^?{}" } ); |
---|
| 308 | finder.matches = &matches; |
---|
| 309 | maybeAccept( stmt, finder ); |
---|
[64071c2] | 310 | } |
---|
[4d2434a] | 311 | |
---|
| 312 | Expression * getCtorDtorCall( Statement * stmt ) { |
---|
| 313 | std::list< Expression * > matches; |
---|
| 314 | collectCtorDtorCalls( stmt, matches ); |
---|
| 315 | assert( matches.size() <= 1 ); |
---|
| 316 | return matches.size() == 1 ? matches.front() : NULL; |
---|
| 317 | } |
---|
| 318 | |
---|
[aedfd91] | 319 | namespace { |
---|
[599b386] | 320 | DeclarationWithType * getCalledFunction( Expression * expr ); |
---|
| 321 | |
---|
| 322 | template<typename CallExpr> |
---|
| 323 | DeclarationWithType * handleDerefCalledFunction( CallExpr * expr ) { |
---|
| 324 | // (*f)(x) => should get "f" |
---|
| 325 | std::string name = getFunctionName( expr ); |
---|
| 326 | assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() ); |
---|
[b128d3e] | 327 | assertf( ! expr->get_args().empty(), "Cannot get called function from dereference with no arguments" ); |
---|
[599b386] | 328 | return getCalledFunction( expr->get_args().front() ); |
---|
| 329 | } |
---|
| 330 | |
---|
[ee1635c8] | 331 | DeclarationWithType * getCalledFunction( Expression * expr ) { |
---|
| 332 | assert( expr ); |
---|
| 333 | if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( expr ) ) { |
---|
| 334 | return varExpr->get_var(); |
---|
| 335 | } else if ( MemberExpr * memberExpr = dynamic_cast< MemberExpr * >( expr ) ) { |
---|
| 336 | return memberExpr->get_member(); |
---|
| 337 | } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) { |
---|
| 338 | return getCalledFunction( castExpr->get_arg() ); |
---|
[599b386] | 339 | } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * >( expr ) ) { |
---|
| 340 | return handleDerefCalledFunction( untypedExpr ); |
---|
| 341 | } else if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * > ( expr ) ) { |
---|
| 342 | return handleDerefCalledFunction( appExpr ); |
---|
[f3b0a07] | 343 | } else if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( expr ) ) { |
---|
| 344 | return getCalledFunction( addrExpr->get_arg() ); |
---|
[ee1635c8] | 345 | } |
---|
| 346 | return nullptr; |
---|
[aedfd91] | 347 | } |
---|
| 348 | } |
---|
[70f89d00] | 349 | |
---|
[b7b8674] | 350 | DeclarationWithType * getFunction( Expression * expr ) { |
---|
| 351 | if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr ) ) { |
---|
| 352 | return getCalledFunction( appExpr->get_function() ); |
---|
| 353 | } else if ( UntypedExpr * untyped = dynamic_cast< UntypedExpr * > ( expr ) ) { |
---|
| 354 | return getCalledFunction( untyped->get_function() ); |
---|
| 355 | } |
---|
| 356 | assertf( false, "getFunction received unknown expression: %s", toString( expr ).c_str() ); |
---|
| 357 | } |
---|
| 358 | |
---|
[aedfd91] | 359 | ApplicationExpr * isIntrinsicCallExpr( Expression * expr ) { |
---|
| 360 | ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr ); |
---|
| 361 | if ( ! appExpr ) return NULL; |
---|
[ee1635c8] | 362 | DeclarationWithType * function = getCalledFunction( appExpr->get_function() ); |
---|
[f3b0a07] | 363 | assertf( function, "getCalledFunction returned nullptr: %s", toString( appExpr->get_function() ).c_str() ); |
---|
[64071c2] | 364 | // check for Intrinsic only - don't want to remove all overridable ctor/dtors because autogenerated ctor/dtor |
---|
| 365 | // will call all member dtors, and some members may have a user defined dtor. |
---|
[ee1635c8] | 366 | return function->get_linkage() == LinkageSpec::Intrinsic ? appExpr : NULL; |
---|
[aedfd91] | 367 | } |
---|
| 368 | |
---|
[a465caf] | 369 | namespace { |
---|
| 370 | template <typename Predicate> |
---|
| 371 | bool allofCtorDtor( Statement * stmt, const Predicate & pred ) { |
---|
| 372 | std::list< Expression * > callExprs; |
---|
| 373 | collectCtorDtorCalls( stmt, callExprs ); |
---|
| 374 | // if ( callExprs.empty() ) return false; // xxx - do I still need this check? |
---|
| 375 | return std::all_of( callExprs.begin(), callExprs.end(), pred); |
---|
| 376 | } |
---|
| 377 | } |
---|
| 378 | |
---|
[f9cebb5] | 379 | bool isIntrinsicSingleArgCallStmt( Statement * stmt ) { |
---|
[a465caf] | 380 | return allofCtorDtor( stmt, []( Expression * callExpr ){ |
---|
[4d2434a] | 381 | if ( ApplicationExpr * appExpr = isIntrinsicCallExpr( callExpr ) ) { |
---|
[906e24d] | 382 | FunctionType *funcType = GenPoly::getFunctionType( appExpr->get_function()->get_result() ); |
---|
[4d2434a] | 383 | assert( funcType ); |
---|
| 384 | return funcType->get_parameters().size() == 1; |
---|
| 385 | } |
---|
| 386 | return false; |
---|
| 387 | }); |
---|
[64071c2] | 388 | } |
---|
[f1b1e4c] | 389 | |
---|
[a465caf] | 390 | bool isIntrinsicCallStmt( Statement * stmt ) { |
---|
| 391 | return allofCtorDtor( stmt, []( Expression * callExpr ) { |
---|
| 392 | return isIntrinsicCallExpr( callExpr ); |
---|
| 393 | }); |
---|
| 394 | } |
---|
| 395 | |
---|
[64071c2] | 396 | namespace { |
---|
| 397 | template<typename CallExpr> |
---|
| 398 | Expression *& callArg( CallExpr * callExpr, unsigned int pos ) { |
---|
[a61ad31] | 399 | if ( pos >= callExpr->get_args().size() ) assertf( false, "getCallArg for argument that doesn't exist: (%u); %s.", pos, toString( callExpr ).c_str() ); |
---|
[64071c2] | 400 | for ( Expression *& arg : callExpr->get_args() ) { |
---|
| 401 | if ( pos == 0 ) return arg; |
---|
| 402 | pos--; |
---|
| 403 | } |
---|
| 404 | assert( false ); |
---|
| 405 | } |
---|
| 406 | } |
---|
[f1b1e4c] | 407 | |
---|
[64071c2] | 408 | Expression *& getCallArg( Expression * callExpr, unsigned int pos ) { |
---|
| 409 | if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( callExpr ) ) { |
---|
| 410 | return callArg( appExpr, pos ); |
---|
| 411 | } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * >( callExpr ) ) { |
---|
| 412 | return callArg( untypedExpr, pos ); |
---|
[f3b0a07] | 413 | } else if ( TupleAssignExpr * tupleExpr = dynamic_cast< TupleAssignExpr * > ( callExpr ) ) { |
---|
| 414 | std::list< Statement * > & stmts = tupleExpr->get_stmtExpr()->get_statements()->get_kids(); |
---|
| 415 | assertf( ! stmts.empty(), "TupleAssignExpr somehow has no statements." ); |
---|
[e3e16bc] | 416 | ExprStmt * stmt = strict_dynamic_cast< ExprStmt * >( stmts.back() ); |
---|
| 417 | TupleExpr * tuple = strict_dynamic_cast< TupleExpr * >( stmt->get_expr() ); |
---|
[f3b0a07] | 418 | assertf( ! tuple->get_exprs().empty(), "TupleAssignExpr somehow has empty tuple expr." ); |
---|
| 419 | return getCallArg( tuple->get_exprs().front(), pos ); |
---|
[62a05d1] | 420 | } else if ( ImplicitCopyCtorExpr * copyCtor = dynamic_cast< ImplicitCopyCtorExpr * >( callExpr ) ) { |
---|
| 421 | return getCallArg( copyCtor->callExpr, pos ); |
---|
[64071c2] | 422 | } else { |
---|
[f3b0a07] | 423 | assertf( false, "Unexpected expression type passed to getCallArg: %s", toString( callExpr ).c_str() ); |
---|
[64071c2] | 424 | } |
---|
| 425 | } |
---|
[f1b1e4c] | 426 | |
---|
[64071c2] | 427 | namespace { |
---|
[599b386] | 428 | std::string funcName( Expression * func ); |
---|
| 429 | |
---|
| 430 | template<typename CallExpr> |
---|
| 431 | std::string handleDerefName( CallExpr * expr ) { |
---|
| 432 | // (*f)(x) => should get name "f" |
---|
| 433 | std::string name = getFunctionName( expr ); |
---|
| 434 | assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() ); |
---|
[b128d3e] | 435 | assertf( ! expr->get_args().empty(), "Cannot get function name from dereference with no arguments" ); |
---|
[599b386] | 436 | return funcName( expr->get_args().front() ); |
---|
| 437 | } |
---|
| 438 | |
---|
[c738ca4] | 439 | std::string funcName( Expression * func ) { |
---|
[64071c2] | 440 | if ( NameExpr * nameExpr = dynamic_cast< NameExpr * >( func ) ) { |
---|
| 441 | return nameExpr->get_name(); |
---|
| 442 | } else if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( func ) ) { |
---|
| 443 | return varExpr->get_var()->get_name(); |
---|
[c738ca4] | 444 | } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( func ) ) { |
---|
| 445 | return funcName( castExpr->get_arg() ); |
---|
[ee1635c8] | 446 | } else if ( MemberExpr * memberExpr = dynamic_cast< MemberExpr * >( func ) ) { |
---|
| 447 | return memberExpr->get_member()->get_name(); |
---|
[96a10cd] | 448 | } else if ( UntypedMemberExpr * memberExpr = dynamic_cast< UntypedMemberExpr * > ( func ) ) { |
---|
[fd782b2] | 449 | return funcName( memberExpr->get_member() ); |
---|
[599b386] | 450 | } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * >( func ) ) { |
---|
| 451 | return handleDerefName( untypedExpr ); |
---|
| 452 | } else if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( func ) ) { |
---|
| 453 | return handleDerefName( appExpr ); |
---|
[19a9822] | 454 | } else if ( ConstructorExpr * ctorExpr = dynamic_cast< ConstructorExpr * >( func ) ) { |
---|
| 455 | return funcName( getCallArg( ctorExpr->get_callExpr(), 0 ) ); |
---|
[64071c2] | 456 | } else { |
---|
[19a9822] | 457 | assertf( false, "Unexpected expression type being called as a function in call expression: %s", toString( func ).c_str() ); |
---|
[64071c2] | 458 | } |
---|
| 459 | } |
---|
| 460 | } |
---|
[70f89d00] | 461 | |
---|
[64071c2] | 462 | std::string getFunctionName( Expression * expr ) { |
---|
[599b386] | 463 | // there's some unforunate overlap here with getCalledFunction. Ideally this would be able to use getCalledFunction and |
---|
| 464 | // return the name of the DeclarationWithType, but this needs to work for NameExpr and UntypedMemberExpr, where getCalledFunction |
---|
| 465 | // can't possibly do anything reasonable. |
---|
[64071c2] | 466 | if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr ) ) { |
---|
[c738ca4] | 467 | return funcName( appExpr->get_function() ); |
---|
[64071c2] | 468 | } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * > ( expr ) ) { |
---|
[c738ca4] | 469 | return funcName( untypedExpr->get_function() ); |
---|
[64071c2] | 470 | } else { |
---|
[c738ca4] | 471 | std::cerr << expr << std::endl; |
---|
[d1969a6] | 472 | assertf( false, "Unexpected expression type passed to getFunctionName" ); |
---|
[64071c2] | 473 | } |
---|
| 474 | } |
---|
[10a7775] | 475 | |
---|
[64071c2] | 476 | Type * getPointerBase( Type * type ) { |
---|
| 477 | if ( PointerType * ptrType = dynamic_cast< PointerType * >( type ) ) { |
---|
| 478 | return ptrType->get_base(); |
---|
| 479 | } else if ( ArrayType * arrayType = dynamic_cast< ArrayType * >( type ) ) { |
---|
| 480 | return arrayType->get_base(); |
---|
[ce8c12f] | 481 | } else if ( ReferenceType * refType = dynamic_cast< ReferenceType * >( type ) ) { |
---|
| 482 | return refType->get_base(); |
---|
[64071c2] | 483 | } else { |
---|
| 484 | return NULL; |
---|
| 485 | } |
---|
| 486 | } |
---|
[10a7775] | 487 | |
---|
[64071c2] | 488 | Type * isPointerType( Type * type ) { |
---|
| 489 | if ( getPointerBase( type ) ) return type; |
---|
| 490 | else return NULL; |
---|
| 491 | } |
---|
[40e636a] | 492 | |
---|
| 493 | class ConstExprChecker : public Visitor { |
---|
| 494 | public: |
---|
| 495 | ConstExprChecker() : isConstExpr( true ) {} |
---|
| 496 | |
---|
[65dc863] | 497 | using Visitor::visit; |
---|
| 498 | |
---|
[5809461] | 499 | virtual void visit( ApplicationExpr * ) { isConstExpr = false; } |
---|
| 500 | virtual void visit( UntypedExpr * ) { isConstExpr = false; } |
---|
| 501 | virtual void visit( NameExpr * ) { isConstExpr = false; } |
---|
[1ba88a0] | 502 | // virtual void visit( CastExpr *castExpr ) { isConstExpr = false; } |
---|
| 503 | virtual void visit( AddressExpr *addressExpr ) { |
---|
| 504 | // address of a variable or member expression is constexpr |
---|
| 505 | Expression * arg = addressExpr->get_arg(); |
---|
| 506 | if ( ! dynamic_cast< NameExpr * >( arg) && ! dynamic_cast< VariableExpr * >( arg ) && ! dynamic_cast< MemberExpr * >( arg ) && ! dynamic_cast< UntypedMemberExpr * >( arg ) ) isConstExpr = false; |
---|
| 507 | } |
---|
[5809461] | 508 | virtual void visit( UntypedMemberExpr * ) { isConstExpr = false; } |
---|
| 509 | virtual void visit( MemberExpr * ) { isConstExpr = false; } |
---|
| 510 | virtual void visit( VariableExpr * ) { isConstExpr = false; } |
---|
[40e636a] | 511 | // these might be okay? |
---|
| 512 | // virtual void visit( SizeofExpr *sizeofExpr ); |
---|
| 513 | // virtual void visit( AlignofExpr *alignofExpr ); |
---|
| 514 | // virtual void visit( UntypedOffsetofExpr *offsetofExpr ); |
---|
| 515 | // virtual void visit( OffsetofExpr *offsetofExpr ); |
---|
| 516 | // virtual void visit( OffsetPackExpr *offsetPackExpr ); |
---|
| 517 | // virtual void visit( AttrExpr *attrExpr ); |
---|
| 518 | // virtual void visit( CommaExpr *commaExpr ); |
---|
| 519 | // virtual void visit( LogicalExpr *logicalExpr ); |
---|
| 520 | // virtual void visit( ConditionalExpr *conditionalExpr ); |
---|
[5809461] | 521 | virtual void visit( TypeExpr * ) { isConstExpr = false; } |
---|
| 522 | virtual void visit( AsmExpr * ) { isConstExpr = false; } |
---|
| 523 | virtual void visit( UntypedValofExpr * ) { isConstExpr = false; } |
---|
| 524 | virtual void visit( CompoundLiteralExpr * ) { isConstExpr = false; } |
---|
| 525 | virtual void visit( UntypedTupleExpr * ) { isConstExpr = false; } |
---|
| 526 | virtual void visit( TupleExpr * ) { isConstExpr = false; } |
---|
| 527 | virtual void visit( TupleAssignExpr * ) { isConstExpr = false; } |
---|
[40e636a] | 528 | |
---|
| 529 | bool isConstExpr; |
---|
| 530 | }; |
---|
| 531 | |
---|
| 532 | bool isConstExpr( Expression * expr ) { |
---|
| 533 | if ( expr ) { |
---|
| 534 | ConstExprChecker checker; |
---|
| 535 | expr->accept( checker ); |
---|
| 536 | return checker.isConstExpr; |
---|
| 537 | } |
---|
| 538 | return true; |
---|
| 539 | } |
---|
| 540 | |
---|
| 541 | bool isConstExpr( Initializer * init ) { |
---|
| 542 | if ( init ) { |
---|
| 543 | ConstExprChecker checker; |
---|
| 544 | init->accept( checker ); |
---|
| 545 | return checker.isConstExpr; |
---|
| 546 | } // if |
---|
| 547 | // for all intents and purposes, no initializer means const expr |
---|
| 548 | return true; |
---|
| 549 | } |
---|
| 550 | |
---|
[79970ed] | 551 | bool isConstructor( const std::string & str ) { return str == "?{}"; } |
---|
| 552 | bool isDestructor( const std::string & str ) { return str == "^?{}"; } |
---|
[ee1635c8] | 553 | bool isAssignment( const std::string & str ) { return str == "?=?"; } |
---|
[79970ed] | 554 | bool isCtorDtor( const std::string & str ) { return isConstructor( str ) || isDestructor( str ); } |
---|
[ee1635c8] | 555 | bool isCtorDtorAssign( const std::string & str ) { return isCtorDtor( str ) || isAssignment( str ); } |
---|
[4d4882a] | 556 | |
---|
[ee1635c8] | 557 | FunctionDecl * isCopyFunction( Declaration * decl, const std::string & fname ) { |
---|
[4d4882a] | 558 | FunctionDecl * function = dynamic_cast< FunctionDecl * >( decl ); |
---|
| 559 | if ( ! function ) return 0; |
---|
[ee1635c8] | 560 | if ( function->get_name() != fname ) return 0; |
---|
[4d4882a] | 561 | FunctionType * ftype = function->get_functionType(); |
---|
| 562 | if ( ftype->get_parameters().size() != 2 ) return 0; |
---|
| 563 | |
---|
[ce8c12f] | 564 | Type * t1 = getPointerBase( ftype->get_parameters().front()->get_type() ); |
---|
[4d4882a] | 565 | Type * t2 = ftype->get_parameters().back()->get_type(); |
---|
[ce8c12f] | 566 | assert( t1 ); |
---|
[4d4882a] | 567 | |
---|
[ce8c12f] | 568 | if ( ResolvExpr::typesCompatibleIgnoreQualifiers( t1, t2, SymTab::Indexer() ) ) { |
---|
[4d4882a] | 569 | return function; |
---|
| 570 | } else { |
---|
[ee1635c8] | 571 | return nullptr; |
---|
[4d4882a] | 572 | } |
---|
| 573 | } |
---|
[ee1635c8] | 574 | |
---|
[207c7e1d] | 575 | FunctionDecl * isAssignment( Declaration * decl ) { |
---|
| 576 | return isCopyFunction( decl, "?=?" ); |
---|
| 577 | } |
---|
| 578 | FunctionDecl * isDestructor( Declaration * decl ) { |
---|
| 579 | if ( isDestructor( decl->get_name() ) ) { |
---|
| 580 | return dynamic_cast< FunctionDecl * >( decl ); |
---|
| 581 | } |
---|
| 582 | return nullptr; |
---|
| 583 | } |
---|
| 584 | FunctionDecl * isDefaultConstructor( Declaration * decl ) { |
---|
| 585 | if ( isConstructor( decl->get_name() ) ) { |
---|
| 586 | if ( FunctionDecl * func = dynamic_cast< FunctionDecl * >( decl ) ) { |
---|
| 587 | if ( func->get_functionType()->get_parameters().size() == 1 ) { |
---|
| 588 | return func; |
---|
| 589 | } |
---|
| 590 | } |
---|
| 591 | } |
---|
| 592 | return nullptr; |
---|
| 593 | } |
---|
[ee1635c8] | 594 | FunctionDecl * isCopyConstructor( Declaration * decl ) { |
---|
| 595 | return isCopyFunction( decl, "?{}" ); |
---|
| 596 | } |
---|
[2b46a13] | 597 | } |
---|