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