| 1 | #include <algorithm>               // for find, all_of | 
|---|
| 2 | #include <cassert>                 // for assertf, assert, strict_dynamic_cast | 
|---|
| 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/PassVisitor.h" | 
|---|
| 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 | 
|---|
| 12 | #include "InitTweak.h" | 
|---|
| 13 | #include "Parser/LinkageSpec.h"    // for Spec, isBuiltin, Intrinsic | 
|---|
| 14 | #include "ResolvExpr/typeops.h"    // for typesCompatibleIgnoreQualifiers | 
|---|
| 15 | #include "SymTab/Autogen.h" | 
|---|
| 16 | #include "SymTab/Indexer.h"        // for Indexer | 
|---|
| 17 | #include "SynTree/Attribute.h"     // for Attribute | 
|---|
| 18 | #include "SynTree/Constant.h"      // for Constant | 
|---|
| 19 | #include "SynTree/Declaration.h"   // for ObjectDecl, DeclarationWithType | 
|---|
| 20 | #include "SynTree/Expression.h"    // for Expression, UntypedExpr, Applicati... | 
|---|
| 21 | #include "SynTree/Initializer.h"   // for Initializer, ListInit, Designation | 
|---|
| 22 | #include "SynTree/Label.h"         // for Label | 
|---|
| 23 | #include "SynTree/Statement.h"     // for CompoundStmt, ExprStmt, BranchStmt | 
|---|
| 24 | #include "SynTree/Type.h"          // for FunctionType, ArrayType, PointerType | 
|---|
| 25 | #include "SynTree/Visitor.h"       // for Visitor, maybeAccept | 
|---|
| 26 | #include "Tuples/Tuples.h"         // for Tuples::isTtype | 
|---|
| 27 |  | 
|---|
| 28 | class UntypedValofExpr; | 
|---|
| 29 |  | 
|---|
| 30 | namespace InitTweak { | 
|---|
| 31 | namespace { | 
|---|
| 32 | struct HasDesignations : public WithShortCircuiting { | 
|---|
| 33 | bool hasDesignations = false; | 
|---|
| 34 |  | 
|---|
| 35 | void previsit( BaseSyntaxNode * ) { | 
|---|
| 36 | // short circuit if we already know there are designations | 
|---|
| 37 | if ( hasDesignations ) visit_children = false; | 
|---|
| 38 | } | 
|---|
| 39 |  | 
|---|
| 40 | void previsit( Designation * des ) { | 
|---|
| 41 | // short circuit if we already know there are designations | 
|---|
| 42 | if ( hasDesignations ) visit_children = false; | 
|---|
| 43 | else if ( ! des->get_designators().empty() ) { | 
|---|
| 44 | hasDesignations = true; | 
|---|
| 45 | visit_children = false; | 
|---|
| 46 | } | 
|---|
| 47 | } | 
|---|
| 48 | }; | 
|---|
| 49 |  | 
|---|
| 50 | struct InitDepthChecker : public WithGuards { | 
|---|
| 51 | bool depthOkay = true; | 
|---|
| 52 | Type * type; | 
|---|
| 53 | int curDepth = 0, maxDepth = 0; | 
|---|
| 54 | InitDepthChecker( Type * type ) : type( type ) { | 
|---|
| 55 | Type * t = type; | 
|---|
| 56 | while ( ArrayType * at = dynamic_cast< ArrayType * >( t ) ) { | 
|---|
| 57 | maxDepth++; | 
|---|
| 58 | t = at->get_base(); | 
|---|
| 59 | } | 
|---|
| 60 | maxDepth++; | 
|---|
| 61 | } | 
|---|
| 62 | void previsit( ListInit * ) { | 
|---|
| 63 | curDepth++; | 
|---|
| 64 | GuardAction( [this]() { curDepth--; } ); | 
|---|
| 65 | if ( curDepth > maxDepth ) depthOkay = false; | 
|---|
| 66 | } | 
|---|
| 67 | }; | 
|---|
| 68 |  | 
|---|
| 69 | struct InitFlattener : public WithShortCircuiting { | 
|---|
| 70 | void previsit( SingleInit * singleInit ) { | 
|---|
| 71 | visit_children = false; | 
|---|
| 72 | argList.push_back( singleInit->value->clone() ); | 
|---|
| 73 | } | 
|---|
| 74 | std::list< Expression * > argList; | 
|---|
| 75 | }; | 
|---|
| 76 |  | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | std::list< Expression * > makeInitList( Initializer * init ) { | 
|---|
| 80 | PassVisitor<InitFlattener> flattener; | 
|---|
| 81 | maybeAccept( init, flattener ); | 
|---|
| 82 | return flattener.pass.argList; | 
|---|
| 83 | } | 
|---|
| 84 |  | 
|---|
| 85 | bool isDesignated( Initializer * init ) { | 
|---|
| 86 | PassVisitor<HasDesignations> finder; | 
|---|
| 87 | maybeAccept( init, finder ); | 
|---|
| 88 | return finder.pass.hasDesignations; | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | bool checkInitDepth( ObjectDecl * objDecl ) { | 
|---|
| 92 | PassVisitor<InitDepthChecker> checker( objDecl->type ); | 
|---|
| 93 | maybeAccept( objDecl->init, checker ); | 
|---|
| 94 | return checker.pass.depthOkay; | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | class InitExpander::ExpanderImpl { | 
|---|
| 98 | public: | 
|---|
| 99 | virtual ~ExpanderImpl() = default; | 
|---|
| 100 | virtual std::list< Expression * > next( std::list< Expression * > & indices ) = 0; | 
|---|
| 101 | virtual Statement * buildListInit( UntypedExpr * callExpr, std::list< Expression * > & indices ) = 0; | 
|---|
| 102 | }; | 
|---|
| 103 |  | 
|---|
| 104 | class InitImpl : public InitExpander::ExpanderImpl { | 
|---|
| 105 | public: | 
|---|
| 106 | InitImpl( Initializer * init ) : init( init ) {} | 
|---|
| 107 | virtual ~InitImpl() = default; | 
|---|
| 108 |  | 
|---|
| 109 | virtual std::list< Expression * > next( __attribute((unused)) std::list< Expression * > & indices ) { | 
|---|
| 110 | // this is wrong, but just a placeholder for now | 
|---|
| 111 | // if ( ! flattened ) flatten( indices ); | 
|---|
| 112 | // return ! inits.empty() ? makeInitList( inits.front() ) : std::list< Expression * >(); | 
|---|
| 113 | return makeInitList( init ); | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | virtual Statement * buildListInit( UntypedExpr * callExpr, std::list< Expression * > & indices ); | 
|---|
| 117 | private: | 
|---|
| 118 | Initializer * init; | 
|---|
| 119 | }; | 
|---|
| 120 |  | 
|---|
| 121 | class ExprImpl : public InitExpander::ExpanderImpl { | 
|---|
| 122 | public: | 
|---|
| 123 | ExprImpl( Expression * expr ) : arg( expr ) {} | 
|---|
| 124 | virtual ~ExprImpl() = default; | 
|---|
| 125 |  | 
|---|
| 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 | } | 
|---|
| 142 |  | 
|---|
| 143 | virtual Statement * buildListInit( UntypedExpr * callExpr, std::list< Expression * > & indices ); | 
|---|
| 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 |  | 
|---|
| 167 | void InitExpander::clearArrayIndices() { | 
|---|
| 168 | indices.clear(); | 
|---|
| 169 | } | 
|---|
| 170 |  | 
|---|
| 171 | bool InitExpander::addReference() { | 
|---|
| 172 | bool added = false; | 
|---|
| 173 | for ( Expression *& expr : cur ) { | 
|---|
| 174 | expr = new AddressExpr( expr ); | 
|---|
| 175 | added = true; | 
|---|
| 176 | } | 
|---|
| 177 | return added; | 
|---|
| 178 | } | 
|---|
| 179 |  | 
|---|
| 180 | namespace { | 
|---|
| 181 | /// given index i, dimension d, initializer init, and callExpr f, generates | 
|---|
| 182 | ///   if (i < d) f(..., init) | 
|---|
| 183 | ///   ++i; | 
|---|
| 184 | /// so that only elements within the range of the array are constructed | 
|---|
| 185 | template< typename OutIterator > | 
|---|
| 186 | void buildCallExpr( UntypedExpr * callExpr, Expression * index, Expression * dimension, Initializer * init, OutIterator out ) { | 
|---|
| 187 | UntypedExpr * cond = new UntypedExpr( new NameExpr( "?<?") ); | 
|---|
| 188 | cond->get_args().push_back( index->clone() ); | 
|---|
| 189 | cond->get_args().push_back( dimension->clone() ); | 
|---|
| 190 |  | 
|---|
| 191 | std::list< Expression * > args = makeInitList( init ); | 
|---|
| 192 | callExpr->get_args().splice( callExpr->get_args().end(), args ); | 
|---|
| 193 |  | 
|---|
| 194 | *out++ = new IfStmt( cond, new ExprStmt( callExpr ), nullptr ); | 
|---|
| 195 |  | 
|---|
| 196 | UntypedExpr * increment = new UntypedExpr( new NameExpr( "++?" ) ); | 
|---|
| 197 | increment->get_args().push_back( index->clone() ); | 
|---|
| 198 | *out++ = new ExprStmt( increment ); | 
|---|
| 199 | } | 
|---|
| 200 |  | 
|---|
| 201 | template< typename OutIterator > | 
|---|
| 202 | void build( UntypedExpr * callExpr, InitExpander::IndexList::iterator idx, InitExpander::IndexList::iterator idxEnd, Initializer * init, OutIterator out ) { | 
|---|
| 203 | if ( idx == idxEnd ) return; | 
|---|
| 204 | Expression * index = *idx++; | 
|---|
| 205 | assert( idx != idxEnd ); | 
|---|
| 206 | Expression * dimension = *idx++; | 
|---|
| 207 |  | 
|---|
| 208 | // xxx - may want to eventually issue a warning here if we can detect | 
|---|
| 209 | // that the number of elements exceeds to dimension of the array | 
|---|
| 210 | if ( idx == idxEnd ) { | 
|---|
| 211 | if ( ListInit * listInit = dynamic_cast< ListInit * >( init ) ) { | 
|---|
| 212 | for ( Initializer * init : *listInit ) { | 
|---|
| 213 | buildCallExpr( callExpr->clone(), index, dimension, init, out ); | 
|---|
| 214 | } | 
|---|
| 215 | } else { | 
|---|
| 216 | buildCallExpr( callExpr->clone(), index, dimension, init, out ); | 
|---|
| 217 | } | 
|---|
| 218 | } else { | 
|---|
| 219 | std::list< Statement * > branches; | 
|---|
| 220 |  | 
|---|
| 221 | unsigned long cond = 0; | 
|---|
| 222 | ListInit * listInit = dynamic_cast< ListInit * >( init ); | 
|---|
| 223 | if ( ! listInit ) { | 
|---|
| 224 | // xxx - this shouldn't be an error, but need a way to | 
|---|
| 225 | // terminate without creating output, so should catch this error | 
|---|
| 226 | SemanticError( init->location, "unbalanced list initializers" ); | 
|---|
| 227 | } | 
|---|
| 228 |  | 
|---|
| 229 | static UniqueName targetLabel( "L__autogen__" ); | 
|---|
| 230 | Label switchLabel( targetLabel.newName(), 0, std::list< Attribute * >{ new Attribute("unused") } ); | 
|---|
| 231 | for ( Initializer * init : *listInit ) { | 
|---|
| 232 | Expression * condition; | 
|---|
| 233 | // check for designations | 
|---|
| 234 | // if ( init-> ) { | 
|---|
| 235 | condition = new ConstantExpr( Constant::from_ulong( cond ) ); | 
|---|
| 236 | ++cond; | 
|---|
| 237 | // } else { | 
|---|
| 238 | //      condition = // ... take designation | 
|---|
| 239 | //      cond = // ... take designation+1 | 
|---|
| 240 | // } | 
|---|
| 241 | std::list< Statement * > stmts; | 
|---|
| 242 | build( callExpr, idx, idxEnd, init, back_inserter( stmts ) ); | 
|---|
| 243 | stmts.push_back( new BranchStmt( switchLabel, BranchStmt::Break ) ); | 
|---|
| 244 | CaseStmt * caseStmt = new CaseStmt( condition, stmts ); | 
|---|
| 245 | branches.push_back( caseStmt ); | 
|---|
| 246 | } | 
|---|
| 247 | *out++ = new SwitchStmt( index->clone(), branches ); | 
|---|
| 248 | *out++ = new NullStmt( { switchLabel } ); | 
|---|
| 249 | } | 
|---|
| 250 | } | 
|---|
| 251 | } | 
|---|
| 252 |  | 
|---|
| 253 | // if array came with an initializer list: initialize each element | 
|---|
| 254 | // may have more initializers than elements in the array - need to check at each index that | 
|---|
| 255 | // we haven't exceeded size. | 
|---|
| 256 | // may have fewer initializers than elements in the array - need to default construct | 
|---|
| 257 | // remaining elements. | 
|---|
| 258 | // To accomplish this, generate switch statement, consuming all of expander's elements | 
|---|
| 259 | Statement * InitImpl::buildListInit( UntypedExpr * dst, std::list< Expression * > & indices ) { | 
|---|
| 260 | if ( ! init ) return nullptr; | 
|---|
| 261 | CompoundStmt * block = new CompoundStmt(); | 
|---|
| 262 | build( dst, indices.begin(), indices.end(), init, back_inserter( block->get_kids() ) ); | 
|---|
| 263 | if ( block->get_kids().empty() ) { | 
|---|
| 264 | return nullptr; | 
|---|
| 265 | } else { | 
|---|
| 266 | init = nullptr; // init was consumed in creating the list init | 
|---|
| 267 | return block; | 
|---|
| 268 | } | 
|---|
| 269 | } | 
|---|
| 270 |  | 
|---|
| 271 | Statement * ExprImpl::buildListInit( UntypedExpr *, std::list< Expression * > & ) { | 
|---|
| 272 | return nullptr; | 
|---|
| 273 | } | 
|---|
| 274 |  | 
|---|
| 275 | Statement * InitExpander::buildListInit( UntypedExpr * dst ) { | 
|---|
| 276 | return expander->buildListInit( dst, indices ); | 
|---|
| 277 | } | 
|---|
| 278 |  | 
|---|
| 279 | Type * getTypeofThis( FunctionType * ftype ) { | 
|---|
| 280 | assertf( ftype, "getTypeofThis: nullptr ftype" ); | 
|---|
| 281 | ObjectDecl * thisParam = getParamThis( ftype ); | 
|---|
| 282 | ReferenceType * refType = strict_dynamic_cast< ReferenceType * >( thisParam->type ); | 
|---|
| 283 | return refType->base; | 
|---|
| 284 | } | 
|---|
| 285 |  | 
|---|
| 286 | ObjectDecl * getParamThis( FunctionType * ftype ) { | 
|---|
| 287 | assertf( ftype, "getParamThis: nullptr ftype" ); | 
|---|
| 288 | auto & params = ftype->parameters; | 
|---|
| 289 | assertf( ! params.empty(), "getParamThis: ftype with 0 parameters: %s", toString( ftype ).c_str() ); | 
|---|
| 290 | return strict_dynamic_cast< ObjectDecl * >( params.front() ); | 
|---|
| 291 | } | 
|---|
| 292 |  | 
|---|
| 293 | bool tryConstruct( DeclarationWithType * dwt ) { | 
|---|
| 294 | ObjectDecl * objDecl = dynamic_cast< ObjectDecl * >( dwt ); | 
|---|
| 295 | if ( ! objDecl ) return false; | 
|---|
| 296 | return (objDecl->get_init() == nullptr || | 
|---|
| 297 | ( objDecl->get_init() != nullptr && objDecl->get_init()->get_maybeConstructed() )) | 
|---|
| 298 | && ! objDecl->get_storageClasses().is_extern | 
|---|
| 299 | && isConstructable( objDecl->type ); | 
|---|
| 300 | } | 
|---|
| 301 |  | 
|---|
| 302 | bool isConstructable( Type * type ) { | 
|---|
| 303 | return ! dynamic_cast< VarArgsType * >( type ) && ! dynamic_cast< ReferenceType * >( type ) && ! dynamic_cast< FunctionType * >( type ) && ! Tuples::isTtype( type ); | 
|---|
| 304 | } | 
|---|
| 305 |  | 
|---|
| 306 | struct CallFinder { | 
|---|
| 307 | CallFinder( const std::list< std::string > & names ) : names( names ) {} | 
|---|
| 308 |  | 
|---|
| 309 | void postvisit( ApplicationExpr * appExpr ) { | 
|---|
| 310 | handleCallExpr( appExpr ); | 
|---|
| 311 | } | 
|---|
| 312 |  | 
|---|
| 313 | void postvisit( UntypedExpr * untypedExpr ) { | 
|---|
| 314 | handleCallExpr( untypedExpr ); | 
|---|
| 315 | } | 
|---|
| 316 |  | 
|---|
| 317 | std::list< Expression * > * matches; | 
|---|
| 318 | private: | 
|---|
| 319 | const std::list< std::string > names; | 
|---|
| 320 |  | 
|---|
| 321 | template< typename CallExpr > | 
|---|
| 322 | void handleCallExpr( CallExpr * expr ) { | 
|---|
| 323 | std::string fname = getFunctionName( expr ); | 
|---|
| 324 | if ( std::find( names.begin(), names.end(), fname ) != names.end() ) { | 
|---|
| 325 | matches->push_back( expr ); | 
|---|
| 326 | } | 
|---|
| 327 | } | 
|---|
| 328 | }; | 
|---|
| 329 |  | 
|---|
| 330 | void collectCtorDtorCalls( Statement * stmt, std::list< Expression * > & matches ) { | 
|---|
| 331 | static PassVisitor<CallFinder> finder( std::list< std::string >{ "?{}", "^?{}" } ); | 
|---|
| 332 | finder.pass.matches = &matches; | 
|---|
| 333 | maybeAccept( stmt, finder ); | 
|---|
| 334 | } | 
|---|
| 335 |  | 
|---|
| 336 | Expression * getCtorDtorCall( Statement * stmt ) { | 
|---|
| 337 | std::list< Expression * > matches; | 
|---|
| 338 | collectCtorDtorCalls( stmt, matches ); | 
|---|
| 339 | assert( matches.size() <= 1 ); | 
|---|
| 340 | return matches.size() == 1 ? matches.front() : nullptr; | 
|---|
| 341 | } | 
|---|
| 342 |  | 
|---|
| 343 | namespace { | 
|---|
| 344 | DeclarationWithType * getCalledFunction( Expression * expr ); | 
|---|
| 345 |  | 
|---|
| 346 | template<typename CallExpr> | 
|---|
| 347 | DeclarationWithType * handleDerefCalledFunction( CallExpr * expr ) { | 
|---|
| 348 | // (*f)(x) => should get "f" | 
|---|
| 349 | std::string name = getFunctionName( expr ); | 
|---|
| 350 | assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() ); | 
|---|
| 351 | assertf( ! expr->get_args().empty(), "Cannot get called function from dereference with no arguments" ); | 
|---|
| 352 | return getCalledFunction( expr->get_args().front() ); | 
|---|
| 353 | } | 
|---|
| 354 |  | 
|---|
| 355 | DeclarationWithType * getCalledFunction( Expression * expr ) { | 
|---|
| 356 | assert( expr ); | 
|---|
| 357 | if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( expr ) ) { | 
|---|
| 358 | return varExpr->var; | 
|---|
| 359 | } else if ( MemberExpr * memberExpr = dynamic_cast< MemberExpr * >( expr ) ) { | 
|---|
| 360 | return memberExpr->member; | 
|---|
| 361 | } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) { | 
|---|
| 362 | return getCalledFunction( castExpr->arg ); | 
|---|
| 363 | } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * >( expr ) ) { | 
|---|
| 364 | return handleDerefCalledFunction( untypedExpr ); | 
|---|
| 365 | } else if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * > ( expr ) ) { | 
|---|
| 366 | return handleDerefCalledFunction( appExpr ); | 
|---|
| 367 | } else if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( expr ) ) { | 
|---|
| 368 | return getCalledFunction( addrExpr->arg ); | 
|---|
| 369 | } else if ( CommaExpr * commaExpr = dynamic_cast< CommaExpr * >( expr ) ) { | 
|---|
| 370 | return getCalledFunction( commaExpr->arg2 ); | 
|---|
| 371 | } | 
|---|
| 372 | return nullptr; | 
|---|
| 373 | } | 
|---|
| 374 | } | 
|---|
| 375 |  | 
|---|
| 376 | DeclarationWithType * getFunction( Expression * expr ) { | 
|---|
| 377 | if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr ) ) { | 
|---|
| 378 | return getCalledFunction( appExpr->get_function() ); | 
|---|
| 379 | } else if ( UntypedExpr * untyped = dynamic_cast< UntypedExpr * > ( expr ) ) { | 
|---|
| 380 | return getCalledFunction( untyped->get_function() ); | 
|---|
| 381 | } | 
|---|
| 382 | assertf( false, "getFunction received unknown expression: %s", toString( expr ).c_str() ); | 
|---|
| 383 | } | 
|---|
| 384 |  | 
|---|
| 385 | ApplicationExpr * isIntrinsicCallExpr( Expression * expr ) { | 
|---|
| 386 | ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr ); | 
|---|
| 387 | if ( ! appExpr ) return nullptr; | 
|---|
| 388 | DeclarationWithType * function = getCalledFunction( appExpr->get_function() ); | 
|---|
| 389 | assertf( function, "getCalledFunction returned nullptr: %s", toString( appExpr->get_function() ).c_str() ); | 
|---|
| 390 | // check for Intrinsic only - don't want to remove all overridable ctor/dtors because autogenerated ctor/dtor | 
|---|
| 391 | // will call all member dtors, and some members may have a user defined dtor. | 
|---|
| 392 | return function->get_linkage() == LinkageSpec::Intrinsic ? appExpr : nullptr; | 
|---|
| 393 | } | 
|---|
| 394 |  | 
|---|
| 395 | namespace { | 
|---|
| 396 | template <typename Predicate> | 
|---|
| 397 | bool allofCtorDtor( Statement * stmt, const Predicate & pred ) { | 
|---|
| 398 | std::list< Expression * > callExprs; | 
|---|
| 399 | collectCtorDtorCalls( stmt, callExprs ); | 
|---|
| 400 | // if ( callExprs.empty() ) return false; // xxx - do I still need this check? | 
|---|
| 401 | return std::all_of( callExprs.begin(), callExprs.end(), pred); | 
|---|
| 402 | } | 
|---|
| 403 | } | 
|---|
| 404 |  | 
|---|
| 405 | bool isIntrinsicSingleArgCallStmt( Statement * stmt ) { | 
|---|
| 406 | return allofCtorDtor( stmt, []( Expression * callExpr ){ | 
|---|
| 407 | if ( ApplicationExpr * appExpr = isIntrinsicCallExpr( callExpr ) ) { | 
|---|
| 408 | FunctionType *funcType = GenPoly::getFunctionType( appExpr->get_function()->get_result() ); | 
|---|
| 409 | assert( funcType ); | 
|---|
| 410 | return funcType->get_parameters().size() == 1; | 
|---|
| 411 | } | 
|---|
| 412 | return false; | 
|---|
| 413 | }); | 
|---|
| 414 | } | 
|---|
| 415 |  | 
|---|
| 416 | bool isIntrinsicCallStmt( Statement * stmt ) { | 
|---|
| 417 | return allofCtorDtor( stmt, []( Expression * callExpr ) { | 
|---|
| 418 | return isIntrinsicCallExpr( callExpr ); | 
|---|
| 419 | }); | 
|---|
| 420 | } | 
|---|
| 421 |  | 
|---|
| 422 | namespace { | 
|---|
| 423 | template<typename CallExpr> | 
|---|
| 424 | Expression *& callArg( CallExpr * callExpr, unsigned int pos ) { | 
|---|
| 425 | if ( pos >= callExpr->get_args().size() ) assertf( false, "getCallArg for argument that doesn't exist: (%u); %s.", pos, toString( callExpr ).c_str() ); | 
|---|
| 426 | for ( Expression *& arg : callExpr->get_args() ) { | 
|---|
| 427 | if ( pos == 0 ) return arg; | 
|---|
| 428 | pos--; | 
|---|
| 429 | } | 
|---|
| 430 | assert( false ); | 
|---|
| 431 | } | 
|---|
| 432 | } | 
|---|
| 433 |  | 
|---|
| 434 | Expression *& getCallArg( Expression * callExpr, unsigned int pos ) { | 
|---|
| 435 | if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( callExpr ) ) { | 
|---|
| 436 | return callArg( appExpr, pos ); | 
|---|
| 437 | } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * >( callExpr ) ) { | 
|---|
| 438 | return callArg( untypedExpr, pos ); | 
|---|
| 439 | } else if ( TupleAssignExpr * tupleExpr = dynamic_cast< TupleAssignExpr * > ( callExpr ) ) { | 
|---|
| 440 | std::list< Statement * > & stmts = tupleExpr->get_stmtExpr()->get_statements()->get_kids(); | 
|---|
| 441 | assertf( ! stmts.empty(), "TupleAssignExpr somehow has no statements." ); | 
|---|
| 442 | ExprStmt * stmt = strict_dynamic_cast< ExprStmt * >( stmts.back() ); | 
|---|
| 443 | TupleExpr * tuple = strict_dynamic_cast< TupleExpr * >( stmt->get_expr() ); | 
|---|
| 444 | assertf( ! tuple->get_exprs().empty(), "TupleAssignExpr somehow has empty tuple expr." ); | 
|---|
| 445 | return getCallArg( tuple->get_exprs().front(), pos ); | 
|---|
| 446 | } else if ( ImplicitCopyCtorExpr * copyCtor = dynamic_cast< ImplicitCopyCtorExpr * >( callExpr ) ) { | 
|---|
| 447 | return getCallArg( copyCtor->callExpr, pos ); | 
|---|
| 448 | } else { | 
|---|
| 449 | assertf( false, "Unexpected expression type passed to getCallArg: %s", toString( callExpr ).c_str() ); | 
|---|
| 450 | } | 
|---|
| 451 | } | 
|---|
| 452 |  | 
|---|
| 453 | namespace { | 
|---|
| 454 | std::string funcName( Expression * func ); | 
|---|
| 455 |  | 
|---|
| 456 | template<typename CallExpr> | 
|---|
| 457 | std::string handleDerefName( CallExpr * expr ) { | 
|---|
| 458 | // (*f)(x) => should get name "f" | 
|---|
| 459 | std::string name = getFunctionName( expr ); | 
|---|
| 460 | assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() ); | 
|---|
| 461 | assertf( ! expr->get_args().empty(), "Cannot get function name from dereference with no arguments" ); | 
|---|
| 462 | return funcName( expr->get_args().front() ); | 
|---|
| 463 | } | 
|---|
| 464 |  | 
|---|
| 465 | std::string funcName( Expression * func ) { | 
|---|
| 466 | if ( NameExpr * nameExpr = dynamic_cast< NameExpr * >( func ) ) { | 
|---|
| 467 | return nameExpr->get_name(); | 
|---|
| 468 | } else if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( func ) ) { | 
|---|
| 469 | return varExpr->get_var()->get_name(); | 
|---|
| 470 | }       else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( func ) ) { | 
|---|
| 471 | return funcName( castExpr->get_arg() ); | 
|---|
| 472 | } else if ( MemberExpr * memberExpr = dynamic_cast< MemberExpr * >( func ) ) { | 
|---|
| 473 | return memberExpr->get_member()->get_name(); | 
|---|
| 474 | } else if ( UntypedMemberExpr * memberExpr = dynamic_cast< UntypedMemberExpr * > ( func ) ) { | 
|---|
| 475 | return funcName( memberExpr->get_member() ); | 
|---|
| 476 | } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * >( func ) ) { | 
|---|
| 477 | return handleDerefName( untypedExpr ); | 
|---|
| 478 | } else if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( func ) ) { | 
|---|
| 479 | return handleDerefName( appExpr ); | 
|---|
| 480 | } else if ( ConstructorExpr * ctorExpr = dynamic_cast< ConstructorExpr * >( func ) ) { | 
|---|
| 481 | return funcName( getCallArg( ctorExpr->get_callExpr(), 0 ) ); | 
|---|
| 482 | } else { | 
|---|
| 483 | assertf( false, "Unexpected expression type being called as a function in call expression: %s", toString( func ).c_str() ); | 
|---|
| 484 | } | 
|---|
| 485 | } | 
|---|
| 486 | } | 
|---|
| 487 |  | 
|---|
| 488 | std::string getFunctionName( Expression * expr ) { | 
|---|
| 489 | // there's some unforunate overlap here with getCalledFunction. Ideally this would be able to use getCalledFunction and | 
|---|
| 490 | // return the name of the DeclarationWithType, but this needs to work for NameExpr and UntypedMemberExpr, where getCalledFunction | 
|---|
| 491 | // can't possibly do anything reasonable. | 
|---|
| 492 | if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr ) ) { | 
|---|
| 493 | return funcName( appExpr->get_function() ); | 
|---|
| 494 | } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * > ( expr ) ) { | 
|---|
| 495 | return funcName( untypedExpr->get_function() ); | 
|---|
| 496 | } else { | 
|---|
| 497 | std::cerr << expr << std::endl; | 
|---|
| 498 | assertf( false, "Unexpected expression type passed to getFunctionName" ); | 
|---|
| 499 | } | 
|---|
| 500 | } | 
|---|
| 501 |  | 
|---|
| 502 | Type * getPointerBase( Type * type ) { | 
|---|
| 503 | if ( PointerType * ptrType = dynamic_cast< PointerType * >( type ) ) { | 
|---|
| 504 | return ptrType->get_base(); | 
|---|
| 505 | } else if ( ArrayType * arrayType = dynamic_cast< ArrayType * >( type ) ) { | 
|---|
| 506 | return arrayType->get_base(); | 
|---|
| 507 | } else if ( ReferenceType * refType = dynamic_cast< ReferenceType * >( type ) ) { | 
|---|
| 508 | return refType->get_base(); | 
|---|
| 509 | } else { | 
|---|
| 510 | return nullptr; | 
|---|
| 511 | } | 
|---|
| 512 | } | 
|---|
| 513 |  | 
|---|
| 514 | Type * isPointerType( Type * type ) { | 
|---|
| 515 | if ( getPointerBase( type ) ) return type; | 
|---|
| 516 | else return nullptr; | 
|---|
| 517 | } | 
|---|
| 518 |  | 
|---|
| 519 | ApplicationExpr * createBitwiseAssignment( Expression * dst, Expression * src ) { | 
|---|
| 520 | static FunctionDecl * assign = nullptr; | 
|---|
| 521 | if ( ! assign ) { | 
|---|
| 522 | // temporary? Generate a fake assignment operator to represent bitwise assignments. | 
|---|
| 523 | // This operator could easily exist as a real function, but it's tricky because nothing should resolve to this function. | 
|---|
| 524 | TypeDecl * td = new TypeDecl( "T", noStorageClasses, nullptr, TypeDecl::Dtype, true ); | 
|---|
| 525 | assign = new FunctionDecl( "?=?", noStorageClasses, LinkageSpec::Intrinsic, SymTab::genAssignType( new TypeInstType( noQualifiers, td->name, td ) ), nullptr ); | 
|---|
| 526 | } | 
|---|
| 527 | if ( dynamic_cast< ReferenceType * >( dst->result ) ) { | 
|---|
| 528 | dst = new AddressExpr( dst ); | 
|---|
| 529 | } else { | 
|---|
| 530 | dst = new CastExpr( dst, new ReferenceType( noQualifiers, dst->result->clone() ) ); | 
|---|
| 531 | } | 
|---|
| 532 | if ( dynamic_cast< ReferenceType * >( src->result ) ) { | 
|---|
| 533 | src = new CastExpr( src, new ReferenceType( noQualifiers, src->result->stripReferences()->clone() ) ); | 
|---|
| 534 | } | 
|---|
| 535 | return new ApplicationExpr( VariableExpr::functionPointer( assign ), { dst, src } ); | 
|---|
| 536 | } | 
|---|
| 537 |  | 
|---|
| 538 | struct ConstExprChecker : public WithShortCircuiting { | 
|---|
| 539 | // most expressions are not const expr | 
|---|
| 540 | void previsit( Expression * ) { isConstExpr = false; visit_children = false; } | 
|---|
| 541 |  | 
|---|
| 542 | void previsit( AddressExpr *addressExpr ) { | 
|---|
| 543 | visit_children = false; | 
|---|
| 544 |  | 
|---|
| 545 | // address of a variable or member expression is constexpr | 
|---|
| 546 | Expression * arg = addressExpr->get_arg(); | 
|---|
| 547 | if ( ! dynamic_cast< NameExpr * >( arg) && ! dynamic_cast< VariableExpr * >( arg ) && ! dynamic_cast< MemberExpr * >( arg ) && ! dynamic_cast< UntypedMemberExpr * >( arg ) ) isConstExpr = false; | 
|---|
| 548 | } | 
|---|
| 549 |  | 
|---|
| 550 | // these expressions may be const expr, depending on their children | 
|---|
| 551 | void previsit( SizeofExpr * ) {} | 
|---|
| 552 | void previsit( AlignofExpr * ) {} | 
|---|
| 553 | void previsit( UntypedOffsetofExpr * ) {} | 
|---|
| 554 | void previsit( OffsetofExpr * ) {} | 
|---|
| 555 | void previsit( OffsetPackExpr * ) {} | 
|---|
| 556 | void previsit( AttrExpr * ) {} | 
|---|
| 557 | void previsit( CommaExpr * ) {} | 
|---|
| 558 | void previsit( LogicalExpr * ) {} | 
|---|
| 559 | void previsit( ConditionalExpr * ) {} | 
|---|
| 560 | void previsit( CastExpr * ) {} | 
|---|
| 561 | void previsit( ConstantExpr * ) {} | 
|---|
| 562 |  | 
|---|
| 563 | void previsit( VariableExpr * varExpr ) { | 
|---|
| 564 | visit_children = false; | 
|---|
| 565 |  | 
|---|
| 566 | if ( EnumInstType * inst = dynamic_cast< EnumInstType * >( varExpr->result ) ) { | 
|---|
| 567 | long long int value; | 
|---|
| 568 | if ( inst->baseEnum->valueOf( varExpr->var, value ) ) { | 
|---|
| 569 | // enumerators are const expr | 
|---|
| 570 | return; | 
|---|
| 571 | } | 
|---|
| 572 | } | 
|---|
| 573 | isConstExpr = false; | 
|---|
| 574 | } | 
|---|
| 575 |  | 
|---|
| 576 | bool isConstExpr = true; | 
|---|
| 577 | }; | 
|---|
| 578 |  | 
|---|
| 579 | bool isConstExpr( Expression * expr ) { | 
|---|
| 580 | if ( expr ) { | 
|---|
| 581 | PassVisitor<ConstExprChecker> checker; | 
|---|
| 582 | expr->accept( checker ); | 
|---|
| 583 | return checker.pass.isConstExpr; | 
|---|
| 584 | } | 
|---|
| 585 | return true; | 
|---|
| 586 | } | 
|---|
| 587 |  | 
|---|
| 588 | bool isConstExpr( Initializer * init ) { | 
|---|
| 589 | if ( init ) { | 
|---|
| 590 | PassVisitor<ConstExprChecker> checker; | 
|---|
| 591 | init->accept( checker ); | 
|---|
| 592 | return checker.pass.isConstExpr; | 
|---|
| 593 | } // if | 
|---|
| 594 | // for all intents and purposes, no initializer means const expr | 
|---|
| 595 | return true; | 
|---|
| 596 | } | 
|---|
| 597 |  | 
|---|
| 598 | bool isConstructor( const std::string & str ) { return str == "?{}"; } | 
|---|
| 599 | bool isDestructor( const std::string & str ) { return str == "^?{}"; } | 
|---|
| 600 | bool isAssignment( const std::string & str ) { return str == "?=?"; } | 
|---|
| 601 | bool isCtorDtor( const std::string & str ) { return isConstructor( str ) || isDestructor( str ); } | 
|---|
| 602 | bool isCtorDtorAssign( const std::string & str ) { return isCtorDtor( str ) || isAssignment( str ); } | 
|---|
| 603 |  | 
|---|
| 604 | FunctionDecl * isCopyFunction( Declaration * decl, const std::string & fname ) { | 
|---|
| 605 | FunctionDecl * function = dynamic_cast< FunctionDecl * >( decl ); | 
|---|
| 606 | if ( ! function ) return nullptr; | 
|---|
| 607 | if ( function->name != fname ) return nullptr; | 
|---|
| 608 | FunctionType * ftype = function->type; | 
|---|
| 609 | if ( ftype->parameters.size() != 2 ) return nullptr; | 
|---|
| 610 |  | 
|---|
| 611 | Type * t1 = getPointerBase( ftype->get_parameters().front()->get_type() ); | 
|---|
| 612 | Type * t2 = ftype->parameters.back()->get_type(); | 
|---|
| 613 | assert( t1 ); | 
|---|
| 614 |  | 
|---|
| 615 | if ( ResolvExpr::typesCompatibleIgnoreQualifiers( t1, t2, SymTab::Indexer() ) ) { | 
|---|
| 616 | return function; | 
|---|
| 617 | } else { | 
|---|
| 618 | return nullptr; | 
|---|
| 619 | } | 
|---|
| 620 | } | 
|---|
| 621 |  | 
|---|
| 622 | FunctionDecl * isAssignment( Declaration * decl ) { | 
|---|
| 623 | return isCopyFunction( decl, "?=?" ); | 
|---|
| 624 | } | 
|---|
| 625 | FunctionDecl * isDestructor( Declaration * decl ) { | 
|---|
| 626 | if ( isDestructor( decl->get_name() ) ) { | 
|---|
| 627 | return dynamic_cast< FunctionDecl * >( decl ); | 
|---|
| 628 | } | 
|---|
| 629 | return nullptr; | 
|---|
| 630 | } | 
|---|
| 631 | FunctionDecl * isDefaultConstructor( Declaration * decl ) { | 
|---|
| 632 | if ( isConstructor( decl->name ) ) { | 
|---|
| 633 | if ( FunctionDecl * func = dynamic_cast< FunctionDecl * >( decl ) ) { | 
|---|
| 634 | if ( func->type->parameters.size() == 1 ) { | 
|---|
| 635 | return func; | 
|---|
| 636 | } | 
|---|
| 637 | } | 
|---|
| 638 | } | 
|---|
| 639 | return nullptr; | 
|---|
| 640 | } | 
|---|
| 641 | FunctionDecl * isCopyConstructor( Declaration * decl ) { | 
|---|
| 642 | return isCopyFunction( decl, "?{}" ); | 
|---|
| 643 | } | 
|---|
| 644 | } | 
|---|