[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 | |
---|
| 7 | #include "Common/SemanticError.h" // for SemanticError |
---|
| 8 | #include "Common/UniqueName.h" // for UniqueName |
---|
| 9 | #include "Common/utility.h" // for toString, deleteAll, maybeClone |
---|
| 10 | #include "GenPoly/GenPoly.h" // for getFunctionType |
---|
[2b46a13] | 11 | #include "InitTweak.h" |
---|
[d180746] | 12 | #include "Parser/LinkageSpec.h" // for Spec, isBuiltin, Intrinsic |
---|
| 13 | #include "ResolvExpr/typeops.h" // for typesCompatibleIgnoreQualifiers |
---|
| 14 | #include "SymTab/Indexer.h" // for Indexer |
---|
| 15 | #include "SynTree/Attribute.h" // for Attribute |
---|
| 16 | #include "SynTree/Constant.h" // for Constant |
---|
| 17 | #include "SynTree/Declaration.h" // for ObjectDecl, DeclarationWithType |
---|
| 18 | #include "SynTree/Expression.h" // for Expression, UntypedExpr, Applicati... |
---|
| 19 | #include "SynTree/Initializer.h" // for Initializer, ListInit, Designation |
---|
| 20 | #include "SynTree/Label.h" // for Label, noLabels |
---|
| 21 | #include "SynTree/Statement.h" // for CompoundStmt, ExprStmt, BranchStmt |
---|
| 22 | #include "SynTree/Type.h" // for FunctionType, ArrayType, PointerType |
---|
| 23 | #include "SynTree/Visitor.h" // for Visitor, maybeAccept |
---|
[29bc63e] | 24 | #include "Tuples/Tuples.h" // for Tuples::isTtype |
---|
[d180746] | 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: |
---|
[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 | |
---|
[22bc276] | 196 | *out++ = new IfStmt( noLabels, cond, new ExprStmt( noLabels, callExpr ), nullptr ); |
---|
[4d2434a] | 197 | |
---|
| 198 | UntypedExpr * increment = new UntypedExpr( new NameExpr( "++?" ) ); |
---|
[175ad32b] | 199 | increment->get_args().push_back( index->clone() ); |
---|
[4d2434a] | 200 | *out++ = new ExprStmt( noLabels, increment ); |
---|
| 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 |
---|
| 228 | throw SemanticError( "unbalanced list initializers" ); |
---|
| 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 ) ); |
---|
[f9cebb5] | 245 | stmts.push_back( new BranchStmt( noLabels, switchLabel, BranchStmt::Break ) ); |
---|
[4d2434a] | 246 | CaseStmt * caseStmt = new CaseStmt( noLabels, condition, stmts ); |
---|
| 247 | branches.push_back( caseStmt ); |
---|
| 248 | } |
---|
| 249 | *out++ = new SwitchStmt( noLabels, index->clone(), branches ); |
---|
[f9cebb5] | 250 | *out++ = new NullStmt( std::list<Label>{ 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; |
---|
[f9cebb5] | 263 | CompoundStmt * block = new CompoundStmt( noLabels ); |
---|
| 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; |
---|
[64071c2] | 299 | return ! LinkageSpec::isBuiltin( objDecl->get_linkage() ) && |
---|
[22bc276] | 300 | (objDecl->get_init() == nullptr || |
---|
| 301 | ( objDecl->get_init() != nullptr && objDecl->get_init()->get_maybeConstructed() )) |
---|
| 302 | && ! objDecl->get_storageClasses().is_extern |
---|
[29bc63e] | 303 | && isConstructable( objDecl->type ); |
---|
| 304 | } |
---|
| 305 | |
---|
| 306 | bool isConstructable( Type * type ) { |
---|
| 307 | return ! dynamic_cast< VarArgsType * >( type ) && ! dynamic_cast< ReferenceType * >( type ) && ! dynamic_cast< FunctionType * >( type ) && ! Tuples::isTtype( type ); |
---|
[64071c2] | 308 | } |
---|
[2b46a13] | 309 | |
---|
[4d2434a] | 310 | class CallFinder : public Visitor { |
---|
| 311 | public: |
---|
| 312 | typedef Visitor Parent; |
---|
| 313 | CallFinder( const std::list< std::string > & names ) : names( names ) {} |
---|
| 314 | |
---|
| 315 | virtual void visit( ApplicationExpr * appExpr ) { |
---|
| 316 | handleCallExpr( appExpr ); |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | virtual void visit( UntypedExpr * untypedExpr ) { |
---|
| 320 | handleCallExpr( untypedExpr ); |
---|
| 321 | } |
---|
| 322 | |
---|
| 323 | std::list< Expression * > * matches; |
---|
| 324 | private: |
---|
| 325 | const std::list< std::string > names; |
---|
| 326 | |
---|
| 327 | template< typename CallExpr > |
---|
| 328 | void handleCallExpr( CallExpr * expr ) { |
---|
| 329 | Parent::visit( expr ); |
---|
| 330 | std::string fname = getFunctionName( expr ); |
---|
| 331 | if ( std::find( names.begin(), names.end(), fname ) != names.end() ) { |
---|
| 332 | matches->push_back( expr ); |
---|
[cad355a] | 333 | } |
---|
[64071c2] | 334 | } |
---|
[4d2434a] | 335 | }; |
---|
| 336 | |
---|
| 337 | void collectCtorDtorCalls( Statement * stmt, std::list< Expression * > & matches ) { |
---|
| 338 | static CallFinder finder( std::list< std::string >{ "?{}", "^?{}" } ); |
---|
| 339 | finder.matches = &matches; |
---|
| 340 | maybeAccept( stmt, finder ); |
---|
[64071c2] | 341 | } |
---|
[4d2434a] | 342 | |
---|
| 343 | Expression * getCtorDtorCall( Statement * stmt ) { |
---|
| 344 | std::list< Expression * > matches; |
---|
| 345 | collectCtorDtorCalls( stmt, matches ); |
---|
| 346 | assert( matches.size() <= 1 ); |
---|
[22bc276] | 347 | return matches.size() == 1 ? matches.front() : nullptr; |
---|
[4d2434a] | 348 | } |
---|
| 349 | |
---|
[aedfd91] | 350 | namespace { |
---|
[599b386] | 351 | DeclarationWithType * getCalledFunction( Expression * expr ); |
---|
| 352 | |
---|
| 353 | template<typename CallExpr> |
---|
| 354 | DeclarationWithType * handleDerefCalledFunction( CallExpr * expr ) { |
---|
| 355 | // (*f)(x) => should get "f" |
---|
| 356 | std::string name = getFunctionName( expr ); |
---|
| 357 | assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() ); |
---|
[b128d3e] | 358 | assertf( ! expr->get_args().empty(), "Cannot get called function from dereference with no arguments" ); |
---|
[599b386] | 359 | return getCalledFunction( expr->get_args().front() ); |
---|
| 360 | } |
---|
| 361 | |
---|
[ee1635c8] | 362 | DeclarationWithType * getCalledFunction( Expression * expr ) { |
---|
| 363 | assert( expr ); |
---|
| 364 | if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( expr ) ) { |
---|
[6fc5c14] | 365 | return varExpr->var; |
---|
[ee1635c8] | 366 | } else if ( MemberExpr * memberExpr = dynamic_cast< MemberExpr * >( expr ) ) { |
---|
[6fc5c14] | 367 | return memberExpr->member; |
---|
[ee1635c8] | 368 | } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) { |
---|
[6fc5c14] | 369 | return getCalledFunction( castExpr->arg ); |
---|
[599b386] | 370 | } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * >( expr ) ) { |
---|
| 371 | return handleDerefCalledFunction( untypedExpr ); |
---|
| 372 | } else if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * > ( expr ) ) { |
---|
| 373 | return handleDerefCalledFunction( appExpr ); |
---|
[f3b0a07] | 374 | } else if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( expr ) ) { |
---|
[6fc5c14] | 375 | return getCalledFunction( addrExpr->arg ); |
---|
| 376 | } else if ( CommaExpr * commaExpr = dynamic_cast< CommaExpr * >( expr ) ) { |
---|
| 377 | return getCalledFunction( commaExpr->arg2 ); |
---|
[ee1635c8] | 378 | } |
---|
| 379 | return nullptr; |
---|
[aedfd91] | 380 | } |
---|
| 381 | } |
---|
[70f89d00] | 382 | |
---|
[b7b8674] | 383 | DeclarationWithType * getFunction( Expression * expr ) { |
---|
| 384 | if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr ) ) { |
---|
| 385 | return getCalledFunction( appExpr->get_function() ); |
---|
| 386 | } else if ( UntypedExpr * untyped = dynamic_cast< UntypedExpr * > ( expr ) ) { |
---|
| 387 | return getCalledFunction( untyped->get_function() ); |
---|
| 388 | } |
---|
| 389 | assertf( false, "getFunction received unknown expression: %s", toString( expr ).c_str() ); |
---|
| 390 | } |
---|
| 391 | |
---|
[aedfd91] | 392 | ApplicationExpr * isIntrinsicCallExpr( Expression * expr ) { |
---|
| 393 | ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr ); |
---|
[22bc276] | 394 | if ( ! appExpr ) return nullptr; |
---|
[ee1635c8] | 395 | DeclarationWithType * function = getCalledFunction( appExpr->get_function() ); |
---|
[f3b0a07] | 396 | assertf( function, "getCalledFunction returned nullptr: %s", toString( appExpr->get_function() ).c_str() ); |
---|
[64071c2] | 397 | // check for Intrinsic only - don't want to remove all overridable ctor/dtors because autogenerated ctor/dtor |
---|
| 398 | // will call all member dtors, and some members may have a user defined dtor. |
---|
[22bc276] | 399 | return function->get_linkage() == LinkageSpec::Intrinsic ? appExpr : nullptr; |
---|
[aedfd91] | 400 | } |
---|
| 401 | |
---|
[a465caf] | 402 | namespace { |
---|
| 403 | template <typename Predicate> |
---|
| 404 | bool allofCtorDtor( Statement * stmt, const Predicate & pred ) { |
---|
| 405 | std::list< Expression * > callExprs; |
---|
| 406 | collectCtorDtorCalls( stmt, callExprs ); |
---|
| 407 | // if ( callExprs.empty() ) return false; // xxx - do I still need this check? |
---|
| 408 | return std::all_of( callExprs.begin(), callExprs.end(), pred); |
---|
| 409 | } |
---|
| 410 | } |
---|
| 411 | |
---|
[f9cebb5] | 412 | bool isIntrinsicSingleArgCallStmt( Statement * stmt ) { |
---|
[a465caf] | 413 | return allofCtorDtor( stmt, []( Expression * callExpr ){ |
---|
[4d2434a] | 414 | if ( ApplicationExpr * appExpr = isIntrinsicCallExpr( callExpr ) ) { |
---|
[906e24d] | 415 | FunctionType *funcType = GenPoly::getFunctionType( appExpr->get_function()->get_result() ); |
---|
[4d2434a] | 416 | assert( funcType ); |
---|
| 417 | return funcType->get_parameters().size() == 1; |
---|
| 418 | } |
---|
| 419 | return false; |
---|
| 420 | }); |
---|
[64071c2] | 421 | } |
---|
[f1b1e4c] | 422 | |
---|
[a465caf] | 423 | bool isIntrinsicCallStmt( Statement * stmt ) { |
---|
| 424 | return allofCtorDtor( stmt, []( Expression * callExpr ) { |
---|
| 425 | return isIntrinsicCallExpr( callExpr ); |
---|
| 426 | }); |
---|
| 427 | } |
---|
| 428 | |
---|
[64071c2] | 429 | namespace { |
---|
| 430 | template<typename CallExpr> |
---|
| 431 | Expression *& callArg( CallExpr * callExpr, unsigned int pos ) { |
---|
[a61ad31] | 432 | if ( pos >= callExpr->get_args().size() ) assertf( false, "getCallArg for argument that doesn't exist: (%u); %s.", pos, toString( callExpr ).c_str() ); |
---|
[64071c2] | 433 | for ( Expression *& arg : callExpr->get_args() ) { |
---|
| 434 | if ( pos == 0 ) return arg; |
---|
| 435 | pos--; |
---|
| 436 | } |
---|
| 437 | assert( false ); |
---|
| 438 | } |
---|
| 439 | } |
---|
[f1b1e4c] | 440 | |
---|
[64071c2] | 441 | Expression *& getCallArg( Expression * callExpr, unsigned int pos ) { |
---|
| 442 | if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( callExpr ) ) { |
---|
| 443 | return callArg( appExpr, pos ); |
---|
| 444 | } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * >( callExpr ) ) { |
---|
| 445 | return callArg( untypedExpr, pos ); |
---|
[f3b0a07] | 446 | } else if ( TupleAssignExpr * tupleExpr = dynamic_cast< TupleAssignExpr * > ( callExpr ) ) { |
---|
| 447 | std::list< Statement * > & stmts = tupleExpr->get_stmtExpr()->get_statements()->get_kids(); |
---|
| 448 | assertf( ! stmts.empty(), "TupleAssignExpr somehow has no statements." ); |
---|
[e3e16bc] | 449 | ExprStmt * stmt = strict_dynamic_cast< ExprStmt * >( stmts.back() ); |
---|
| 450 | TupleExpr * tuple = strict_dynamic_cast< TupleExpr * >( stmt->get_expr() ); |
---|
[f3b0a07] | 451 | assertf( ! tuple->get_exprs().empty(), "TupleAssignExpr somehow has empty tuple expr." ); |
---|
| 452 | return getCallArg( tuple->get_exprs().front(), pos ); |
---|
[62a05d1] | 453 | } else if ( ImplicitCopyCtorExpr * copyCtor = dynamic_cast< ImplicitCopyCtorExpr * >( callExpr ) ) { |
---|
| 454 | return getCallArg( copyCtor->callExpr, pos ); |
---|
[64071c2] | 455 | } else { |
---|
[f3b0a07] | 456 | assertf( false, "Unexpected expression type passed to getCallArg: %s", toString( callExpr ).c_str() ); |
---|
[64071c2] | 457 | } |
---|
| 458 | } |
---|
[f1b1e4c] | 459 | |
---|
[64071c2] | 460 | namespace { |
---|
[599b386] | 461 | std::string funcName( Expression * func ); |
---|
| 462 | |
---|
| 463 | template<typename CallExpr> |
---|
| 464 | std::string handleDerefName( CallExpr * expr ) { |
---|
| 465 | // (*f)(x) => should get name "f" |
---|
| 466 | std::string name = getFunctionName( expr ); |
---|
| 467 | assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() ); |
---|
[b128d3e] | 468 | assertf( ! expr->get_args().empty(), "Cannot get function name from dereference with no arguments" ); |
---|
[599b386] | 469 | return funcName( expr->get_args().front() ); |
---|
| 470 | } |
---|
| 471 | |
---|
[c738ca4] | 472 | std::string funcName( Expression * func ) { |
---|
[64071c2] | 473 | if ( NameExpr * nameExpr = dynamic_cast< NameExpr * >( func ) ) { |
---|
| 474 | return nameExpr->get_name(); |
---|
| 475 | } else if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( func ) ) { |
---|
| 476 | return varExpr->get_var()->get_name(); |
---|
[c738ca4] | 477 | } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( func ) ) { |
---|
| 478 | return funcName( castExpr->get_arg() ); |
---|
[ee1635c8] | 479 | } else if ( MemberExpr * memberExpr = dynamic_cast< MemberExpr * >( func ) ) { |
---|
| 480 | return memberExpr->get_member()->get_name(); |
---|
[96a10cd] | 481 | } else if ( UntypedMemberExpr * memberExpr = dynamic_cast< UntypedMemberExpr * > ( func ) ) { |
---|
[fd782b2] | 482 | return funcName( memberExpr->get_member() ); |
---|
[599b386] | 483 | } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * >( func ) ) { |
---|
| 484 | return handleDerefName( untypedExpr ); |
---|
| 485 | } else if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( func ) ) { |
---|
| 486 | return handleDerefName( appExpr ); |
---|
[19a9822] | 487 | } else if ( ConstructorExpr * ctorExpr = dynamic_cast< ConstructorExpr * >( func ) ) { |
---|
| 488 | return funcName( getCallArg( ctorExpr->get_callExpr(), 0 ) ); |
---|
[64071c2] | 489 | } else { |
---|
[19a9822] | 490 | assertf( false, "Unexpected expression type being called as a function in call expression: %s", toString( func ).c_str() ); |
---|
[64071c2] | 491 | } |
---|
| 492 | } |
---|
| 493 | } |
---|
[70f89d00] | 494 | |
---|
[64071c2] | 495 | std::string getFunctionName( Expression * expr ) { |
---|
[599b386] | 496 | // there's some unforunate overlap here with getCalledFunction. Ideally this would be able to use getCalledFunction and |
---|
| 497 | // return the name of the DeclarationWithType, but this needs to work for NameExpr and UntypedMemberExpr, where getCalledFunction |
---|
| 498 | // can't possibly do anything reasonable. |
---|
[64071c2] | 499 | if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr ) ) { |
---|
[c738ca4] | 500 | return funcName( appExpr->get_function() ); |
---|
[64071c2] | 501 | } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * > ( expr ) ) { |
---|
[c738ca4] | 502 | return funcName( untypedExpr->get_function() ); |
---|
[64071c2] | 503 | } else { |
---|
[c738ca4] | 504 | std::cerr << expr << std::endl; |
---|
[d1969a6] | 505 | assertf( false, "Unexpected expression type passed to getFunctionName" ); |
---|
[64071c2] | 506 | } |
---|
| 507 | } |
---|
[10a7775] | 508 | |
---|
[64071c2] | 509 | Type * getPointerBase( Type * type ) { |
---|
| 510 | if ( PointerType * ptrType = dynamic_cast< PointerType * >( type ) ) { |
---|
| 511 | return ptrType->get_base(); |
---|
| 512 | } else if ( ArrayType * arrayType = dynamic_cast< ArrayType * >( type ) ) { |
---|
| 513 | return arrayType->get_base(); |
---|
[ce8c12f] | 514 | } else if ( ReferenceType * refType = dynamic_cast< ReferenceType * >( type ) ) { |
---|
| 515 | return refType->get_base(); |
---|
[64071c2] | 516 | } else { |
---|
[22bc276] | 517 | return nullptr; |
---|
[64071c2] | 518 | } |
---|
| 519 | } |
---|
[10a7775] | 520 | |
---|
[64071c2] | 521 | Type * isPointerType( Type * type ) { |
---|
| 522 | if ( getPointerBase( type ) ) return type; |
---|
[22bc276] | 523 | else return nullptr; |
---|
[64071c2] | 524 | } |
---|
[40e636a] | 525 | |
---|
| 526 | class ConstExprChecker : public Visitor { |
---|
| 527 | public: |
---|
| 528 | ConstExprChecker() : isConstExpr( true ) {} |
---|
| 529 | |
---|
[65dc863] | 530 | using Visitor::visit; |
---|
| 531 | |
---|
[5809461] | 532 | virtual void visit( ApplicationExpr * ) { isConstExpr = false; } |
---|
| 533 | virtual void visit( UntypedExpr * ) { isConstExpr = false; } |
---|
| 534 | virtual void visit( NameExpr * ) { isConstExpr = false; } |
---|
[1ba88a0] | 535 | // virtual void visit( CastExpr *castExpr ) { isConstExpr = false; } |
---|
| 536 | virtual void visit( AddressExpr *addressExpr ) { |
---|
| 537 | // address of a variable or member expression is constexpr |
---|
| 538 | Expression * arg = addressExpr->get_arg(); |
---|
| 539 | if ( ! dynamic_cast< NameExpr * >( arg) && ! dynamic_cast< VariableExpr * >( arg ) && ! dynamic_cast< MemberExpr * >( arg ) && ! dynamic_cast< UntypedMemberExpr * >( arg ) ) isConstExpr = false; |
---|
| 540 | } |
---|
[5809461] | 541 | virtual void visit( UntypedMemberExpr * ) { isConstExpr = false; } |
---|
| 542 | virtual void visit( MemberExpr * ) { isConstExpr = false; } |
---|
| 543 | virtual void visit( VariableExpr * ) { isConstExpr = false; } |
---|
[40e636a] | 544 | // these might be okay? |
---|
| 545 | // virtual void visit( SizeofExpr *sizeofExpr ); |
---|
| 546 | // virtual void visit( AlignofExpr *alignofExpr ); |
---|
| 547 | // virtual void visit( UntypedOffsetofExpr *offsetofExpr ); |
---|
| 548 | // virtual void visit( OffsetofExpr *offsetofExpr ); |
---|
| 549 | // virtual void visit( OffsetPackExpr *offsetPackExpr ); |
---|
| 550 | // virtual void visit( AttrExpr *attrExpr ); |
---|
| 551 | // virtual void visit( CommaExpr *commaExpr ); |
---|
| 552 | // virtual void visit( LogicalExpr *logicalExpr ); |
---|
| 553 | // virtual void visit( ConditionalExpr *conditionalExpr ); |
---|
[5809461] | 554 | virtual void visit( TypeExpr * ) { isConstExpr = false; } |
---|
| 555 | virtual void visit( AsmExpr * ) { isConstExpr = false; } |
---|
| 556 | virtual void visit( UntypedValofExpr * ) { isConstExpr = false; } |
---|
| 557 | virtual void visit( CompoundLiteralExpr * ) { isConstExpr = false; } |
---|
| 558 | virtual void visit( UntypedTupleExpr * ) { isConstExpr = false; } |
---|
| 559 | virtual void visit( TupleExpr * ) { isConstExpr = false; } |
---|
| 560 | virtual void visit( TupleAssignExpr * ) { isConstExpr = false; } |
---|
[40e636a] | 561 | |
---|
| 562 | bool isConstExpr; |
---|
| 563 | }; |
---|
| 564 | |
---|
| 565 | bool isConstExpr( Expression * expr ) { |
---|
| 566 | if ( expr ) { |
---|
| 567 | ConstExprChecker checker; |
---|
| 568 | expr->accept( checker ); |
---|
| 569 | return checker.isConstExpr; |
---|
| 570 | } |
---|
| 571 | return true; |
---|
| 572 | } |
---|
| 573 | |
---|
| 574 | bool isConstExpr( Initializer * init ) { |
---|
| 575 | if ( init ) { |
---|
| 576 | ConstExprChecker checker; |
---|
| 577 | init->accept( checker ); |
---|
| 578 | return checker.isConstExpr; |
---|
| 579 | } // if |
---|
| 580 | // for all intents and purposes, no initializer means const expr |
---|
| 581 | return true; |
---|
| 582 | } |
---|
| 583 | |
---|
[79970ed] | 584 | bool isConstructor( const std::string & str ) { return str == "?{}"; } |
---|
| 585 | bool isDestructor( const std::string & str ) { return str == "^?{}"; } |
---|
[ee1635c8] | 586 | bool isAssignment( const std::string & str ) { return str == "?=?"; } |
---|
[79970ed] | 587 | bool isCtorDtor( const std::string & str ) { return isConstructor( str ) || isDestructor( str ); } |
---|
[ee1635c8] | 588 | bool isCtorDtorAssign( const std::string & str ) { return isCtorDtor( str ) || isAssignment( str ); } |
---|
[4d4882a] | 589 | |
---|
[ee1635c8] | 590 | FunctionDecl * isCopyFunction( Declaration * decl, const std::string & fname ) { |
---|
[4d4882a] | 591 | FunctionDecl * function = dynamic_cast< FunctionDecl * >( decl ); |
---|
[0a267c1] | 592 | if ( ! function ) return nullptr; |
---|
| 593 | if ( function->name != fname ) return nullptr; |
---|
| 594 | FunctionType * ftype = function->type; |
---|
| 595 | if ( ftype->parameters.size() != 2 ) return nullptr; |
---|
[4d4882a] | 596 | |
---|
[ce8c12f] | 597 | Type * t1 = getPointerBase( ftype->get_parameters().front()->get_type() ); |
---|
[0a267c1] | 598 | Type * t2 = ftype->parameters.back()->get_type(); |
---|
[ce8c12f] | 599 | assert( t1 ); |
---|
[4d4882a] | 600 | |
---|
[ce8c12f] | 601 | if ( ResolvExpr::typesCompatibleIgnoreQualifiers( t1, t2, SymTab::Indexer() ) ) { |
---|
[4d4882a] | 602 | return function; |
---|
| 603 | } else { |
---|
[ee1635c8] | 604 | return nullptr; |
---|
[4d4882a] | 605 | } |
---|
| 606 | } |
---|
[ee1635c8] | 607 | |
---|
[207c7e1d] | 608 | FunctionDecl * isAssignment( Declaration * decl ) { |
---|
| 609 | return isCopyFunction( decl, "?=?" ); |
---|
| 610 | } |
---|
| 611 | FunctionDecl * isDestructor( Declaration * decl ) { |
---|
| 612 | if ( isDestructor( decl->get_name() ) ) { |
---|
| 613 | return dynamic_cast< FunctionDecl * >( decl ); |
---|
| 614 | } |
---|
| 615 | return nullptr; |
---|
| 616 | } |
---|
| 617 | FunctionDecl * isDefaultConstructor( Declaration * decl ) { |
---|
[0a267c1] | 618 | if ( isConstructor( decl->name ) ) { |
---|
[207c7e1d] | 619 | if ( FunctionDecl * func = dynamic_cast< FunctionDecl * >( decl ) ) { |
---|
[0a267c1] | 620 | if ( func->type->parameters.size() == 1 ) { |
---|
[207c7e1d] | 621 | return func; |
---|
| 622 | } |
---|
| 623 | } |
---|
| 624 | } |
---|
| 625 | return nullptr; |
---|
| 626 | } |
---|
[ee1635c8] | 627 | FunctionDecl * isCopyConstructor( Declaration * decl ) { |
---|
| 628 | return isCopyFunction( decl, "?{}" ); |
---|
| 629 | } |
---|
[2b46a13] | 630 | } |
---|