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