| [2d11663] | 1 | //
|
|---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
|
|---|
| 3 | //
|
|---|
| 4 | // The contents of this file are covered under the licence agreement in the
|
|---|
| 5 | // file "LICENCE" distributed with Cforall.
|
|---|
| 6 | //
|
|---|
| 7 | // InitTweak.cc --
|
|---|
| 8 | //
|
|---|
| 9 | // Author : Rob Schluntz
|
|---|
| 10 | // Created On : Fri May 13 11:26:36 2016
|
|---|
| 11 | // Last Modified By : Aaron B. Moss
|
|---|
| 12 | // Last Modified On : Mon Jun 10 13:30:00 2019
|
|---|
| 13 | // Update Count : 5
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| [d180746] | 16 | #include <algorithm> // for find, all_of
|
|---|
| [e3e16bc] | 17 | #include <cassert> // for assertf, assert, strict_dynamic_cast
|
|---|
| [d180746] | 18 | #include <iostream> // for ostream, cerr, endl
|
|---|
| 19 | #include <iterator> // for back_insert_iterator, back_inserter
|
|---|
| 20 | #include <memory> // for __shared_ptr
|
|---|
| [2d11663] | 21 | #include <vector>
|
|---|
| [d180746] | 22 |
|
|---|
| [9b4f329] | 23 | #include "AST/Expr.hpp"
|
|---|
| [b8524ca] | 24 | #include "AST/Node.hpp"
|
|---|
| [9b4f329] | 25 | #include "AST/Stmt.hpp"
|
|---|
| [9e1d485] | 26 | #include "AST/Type.hpp"
|
|---|
| [fd236ed] | 27 | #include "Common/PassVisitor.h"
|
|---|
| [d180746] | 28 | #include "Common/SemanticError.h" // for SemanticError
|
|---|
| 29 | #include "Common/UniqueName.h" // for UniqueName
|
|---|
| 30 | #include "Common/utility.h" // for toString, deleteAll, maybeClone
|
|---|
| 31 | #include "GenPoly/GenPoly.h" // for getFunctionType
|
|---|
| [2b46a13] | 32 | #include "InitTweak.h"
|
|---|
| [d180746] | 33 | #include "Parser/LinkageSpec.h" // for Spec, isBuiltin, Intrinsic
|
|---|
| 34 | #include "ResolvExpr/typeops.h" // for typesCompatibleIgnoreQualifiers
|
|---|
| [f5c3b6c] | 35 | #include "SymTab/Autogen.h"
|
|---|
| [d180746] | 36 | #include "SymTab/Indexer.h" // for Indexer
|
|---|
| 37 | #include "SynTree/Attribute.h" // for Attribute
|
|---|
| 38 | #include "SynTree/Constant.h" // for Constant
|
|---|
| 39 | #include "SynTree/Declaration.h" // for ObjectDecl, DeclarationWithType
|
|---|
| 40 | #include "SynTree/Expression.h" // for Expression, UntypedExpr, Applicati...
|
|---|
| 41 | #include "SynTree/Initializer.h" // for Initializer, ListInit, Designation
|
|---|
| [ba3706f] | 42 | #include "SynTree/Label.h" // for Label
|
|---|
| [d180746] | 43 | #include "SynTree/Statement.h" // for CompoundStmt, ExprStmt, BranchStmt
|
|---|
| 44 | #include "SynTree/Type.h" // for FunctionType, ArrayType, PointerType
|
|---|
| 45 | #include "SynTree/Visitor.h" // for Visitor, maybeAccept
|
|---|
| [29bc63e] | 46 | #include "Tuples/Tuples.h" // for Tuples::isTtype
|
|---|
| [d180746] | 47 |
|
|---|
| [2b46a13] | 48 | namespace InitTweak {
|
|---|
| [64071c2] | 49 | namespace {
|
|---|
| [1fbeebd] | 50 | struct HasDesignations : public WithShortCircuiting {
|
|---|
| [64071c2] | 51 | bool hasDesignations = false;
|
|---|
| [fd236ed] | 52 |
|
|---|
| 53 | void previsit( BaseSyntaxNode * ) {
|
|---|
| 54 | // short circuit if we already know there are designations
|
|---|
| 55 | if ( hasDesignations ) visit_children = false;
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | void previsit( Designation * des ) {
|
|---|
| 59 | // short circuit if we already know there are designations
|
|---|
| 60 | if ( hasDesignations ) visit_children = false;
|
|---|
| 61 | else if ( ! des->get_designators().empty() ) {
|
|---|
| 62 | hasDesignations = true;
|
|---|
| 63 | visit_children = false;
|
|---|
| 64 | }
|
|---|
| [64071c2] | 65 | }
|
|---|
| 66 | };
|
|---|
| [2b46a13] | 67 |
|
|---|
| [ef3d798] | 68 | struct InitDepthChecker : public WithGuards {
|
|---|
| [dcd73d1] | 69 | bool depthOkay = true;
|
|---|
| 70 | Type * type;
|
|---|
| 71 | int curDepth = 0, maxDepth = 0;
|
|---|
| 72 | InitDepthChecker( Type * type ) : type( type ) {
|
|---|
| 73 | Type * t = type;
|
|---|
| 74 | while ( ArrayType * at = dynamic_cast< ArrayType * >( t ) ) {
|
|---|
| 75 | maxDepth++;
|
|---|
| 76 | t = at->get_base();
|
|---|
| 77 | }
|
|---|
| 78 | maxDepth++;
|
|---|
| 79 | }
|
|---|
| [ef3d798] | 80 | void previsit( ListInit * ) {
|
|---|
| [dcd73d1] | 81 | curDepth++;
|
|---|
| [ef3d798] | 82 | GuardAction( [this]() { curDepth--; } );
|
|---|
| [dcd73d1] | 83 | if ( curDepth > maxDepth ) depthOkay = false;
|
|---|
| 84 | }
|
|---|
| 85 | };
|
|---|
| 86 |
|
|---|
| [c3f551b] | 87 | struct InitFlattener : public WithShortCircuiting {
|
|---|
| 88 | void previsit( SingleInit * singleInit ) {
|
|---|
| 89 | visit_children = false;
|
|---|
| 90 | argList.push_back( singleInit->value->clone() );
|
|---|
| 91 | }
|
|---|
| [64071c2] | 92 | std::list< Expression * > argList;
|
|---|
| 93 | };
|
|---|
| [2b46a13] | 94 |
|
|---|
| [64071c2] | 95 | }
|
|---|
| [2b46a13] | 96 |
|
|---|
| [64071c2] | 97 | std::list< Expression * > makeInitList( Initializer * init ) {
|
|---|
| [c3f551b] | 98 | PassVisitor<InitFlattener> flattener;
|
|---|
| [4d2434a] | 99 | maybeAccept( init, flattener );
|
|---|
| [c3f551b] | 100 | return flattener.pass.argList;
|
|---|
| [64071c2] | 101 | }
|
|---|
| [2b46a13] | 102 |
|
|---|
| [64071c2] | 103 | bool isDesignated( Initializer * init ) {
|
|---|
| [fd236ed] | 104 | PassVisitor<HasDesignations> finder;
|
|---|
| [64071c2] | 105 | maybeAccept( init, finder );
|
|---|
| [fd236ed] | 106 | return finder.pass.hasDesignations;
|
|---|
| [dcd73d1] | 107 | }
|
|---|
| 108 |
|
|---|
| 109 | bool checkInitDepth( ObjectDecl * objDecl ) {
|
|---|
| [ef3d798] | 110 | PassVisitor<InitDepthChecker> checker( objDecl->type );
|
|---|
| 111 | maybeAccept( objDecl->init, checker );
|
|---|
| 112 | return checker.pass.depthOkay;
|
|---|
| [64071c2] | 113 | }
|
|---|
| [2b46a13] | 114 |
|
|---|
| [b8524ca] | 115 | std::vector< ast::ptr< ast::Expr > > makeInitList( const ast::Init * init ) {
|
|---|
| 116 | #warning unimplmented
|
|---|
| 117 | (void)init;
|
|---|
| 118 | assert(false);
|
|---|
| 119 | return {};
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | class InitExpander_old::ExpanderImpl {
|
|---|
| [39f84a4] | 123 | public:
|
|---|
| [3351cc0] | 124 | virtual ~ExpanderImpl() = default;
|
|---|
| [39f84a4] | 125 | virtual std::list< Expression * > next( std::list< Expression * > & indices ) = 0;
|
|---|
| [4d2434a] | 126 | virtual Statement * buildListInit( UntypedExpr * callExpr, std::list< Expression * > & indices ) = 0;
|
|---|
| [39f84a4] | 127 | };
|
|---|
| 128 |
|
|---|
| [b8524ca] | 129 | class InitImpl_old : public InitExpander_old::ExpanderImpl {
|
|---|
| [39f84a4] | 130 | public:
|
|---|
| [b8524ca] | 131 | InitImpl_old( Initializer * init ) : init( init ) {}
|
|---|
| 132 | virtual ~InitImpl_old() = default;
|
|---|
| [39f84a4] | 133 |
|
|---|
| [7e003011] | 134 | virtual std::list< Expression * > next( __attribute((unused)) std::list< Expression * > & indices ) {
|
|---|
| [39f84a4] | 135 | // this is wrong, but just a placeholder for now
|
|---|
| [4d2434a] | 136 | // if ( ! flattened ) flatten( indices );
|
|---|
| 137 | // return ! inits.empty() ? makeInitList( inits.front() ) : std::list< Expression * >();
|
|---|
| 138 | return makeInitList( init );
|
|---|
| [39f84a4] | 139 | }
|
|---|
| [4d2434a] | 140 |
|
|---|
| 141 | virtual Statement * buildListInit( UntypedExpr * callExpr, std::list< Expression * > & indices );
|
|---|
| [39f84a4] | 142 | private:
|
|---|
| [4d2434a] | 143 | Initializer * init;
|
|---|
| [39f84a4] | 144 | };
|
|---|
| 145 |
|
|---|
| [b8524ca] | 146 | class ExprImpl_old : public InitExpander_old::ExpanderImpl {
|
|---|
| [39f84a4] | 147 | public:
|
|---|
| [b8524ca] | 148 | ExprImpl_old( Expression * expr ) : arg( expr ) {}
|
|---|
| 149 | virtual ~ExprImpl_old() { delete arg; }
|
|---|
| [9b4c936] | 150 |
|
|---|
| [39f84a4] | 151 | virtual std::list< Expression * > next( std::list< Expression * > & indices ) {
|
|---|
| 152 | std::list< Expression * > ret;
|
|---|
| 153 | Expression * expr = maybeClone( arg );
|
|---|
| 154 | if ( expr ) {
|
|---|
| 155 | for ( std::list< Expression * >::reverse_iterator it = indices.rbegin(); it != indices.rend(); ++it ) {
|
|---|
| 156 | // go through indices and layer on subscript exprs ?[?]
|
|---|
| 157 | ++it;
|
|---|
| 158 | UntypedExpr * subscriptExpr = new UntypedExpr( new NameExpr( "?[?]") );
|
|---|
| 159 | subscriptExpr->get_args().push_back( expr );
|
|---|
| 160 | subscriptExpr->get_args().push_back( (*it)->clone() );
|
|---|
| 161 | expr = subscriptExpr;
|
|---|
| 162 | }
|
|---|
| 163 | ret.push_back( expr );
|
|---|
| 164 | }
|
|---|
| 165 | return ret;
|
|---|
| 166 | }
|
|---|
| [4d2434a] | 167 |
|
|---|
| 168 | virtual Statement * buildListInit( UntypedExpr * callExpr, std::list< Expression * > & indices );
|
|---|
| [39f84a4] | 169 | private:
|
|---|
| 170 | Expression * arg;
|
|---|
| 171 | };
|
|---|
| 172 |
|
|---|
| [b8524ca] | 173 | InitExpander_old::InitExpander_old( Initializer * init ) : expander( new InitImpl_old( init ) ) {}
|
|---|
| [39f84a4] | 174 |
|
|---|
| [b8524ca] | 175 | InitExpander_old::InitExpander_old( Expression * expr ) : expander( new ExprImpl_old( expr ) ) {}
|
|---|
| [39f84a4] | 176 |
|
|---|
| [b8524ca] | 177 | std::list< Expression * > InitExpander_old::operator*() {
|
|---|
| [39f84a4] | 178 | return cur;
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| [b8524ca] | 181 | InitExpander_old & InitExpander_old::operator++() {
|
|---|
| [39f84a4] | 182 | cur = expander->next( indices );
|
|---|
| 183 | return *this;
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | // use array indices list to build switch statement
|
|---|
| [b8524ca] | 187 | void InitExpander_old::addArrayIndex( Expression * index, Expression * dimension ) {
|
|---|
| [39f84a4] | 188 | indices.push_back( index );
|
|---|
| 189 | indices.push_back( dimension );
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| [b8524ca] | 192 | void InitExpander_old::clearArrayIndices() {
|
|---|
| [9b4c936] | 193 | deleteAll( indices );
|
|---|
| [4d2434a] | 194 | indices.clear();
|
|---|
| [1a5ad8c] | 195 | }
|
|---|
| 196 |
|
|---|
| [b8524ca] | 197 | bool InitExpander_old::addReference() {
|
|---|
| [1a5ad8c] | 198 | bool added = false;
|
|---|
| 199 | for ( Expression *& expr : cur ) {
|
|---|
| 200 | expr = new AddressExpr( expr );
|
|---|
| 201 | added = true;
|
|---|
| 202 | }
|
|---|
| 203 | return added;
|
|---|
| [4d2434a] | 204 | }
|
|---|
| 205 |
|
|---|
| 206 | namespace {
|
|---|
| [f9cebb5] | 207 | /// given index i, dimension d, initializer init, and callExpr f, generates
|
|---|
| 208 | /// if (i < d) f(..., init)
|
|---|
| 209 | /// ++i;
|
|---|
| 210 | /// so that only elements within the range of the array are constructed
|
|---|
| [4d2434a] | 211 | template< typename OutIterator >
|
|---|
| [f9cebb5] | 212 | void buildCallExpr( UntypedExpr * callExpr, Expression * index, Expression * dimension, Initializer * init, OutIterator out ) {
|
|---|
| [4d2434a] | 213 | UntypedExpr * cond = new UntypedExpr( new NameExpr( "?<?") );
|
|---|
| 214 | cond->get_args().push_back( index->clone() );
|
|---|
| 215 | cond->get_args().push_back( dimension->clone() );
|
|---|
| 216 |
|
|---|
| 217 | std::list< Expression * > args = makeInitList( init );
|
|---|
| 218 | callExpr->get_args().splice( callExpr->get_args().end(), args );
|
|---|
| 219 |
|
|---|
| [ba3706f] | 220 | *out++ = new IfStmt( cond, new ExprStmt( callExpr ), nullptr );
|
|---|
| [4d2434a] | 221 |
|
|---|
| 222 | UntypedExpr * increment = new UntypedExpr( new NameExpr( "++?" ) );
|
|---|
| [175ad32b] | 223 | increment->get_args().push_back( index->clone() );
|
|---|
| [ba3706f] | 224 | *out++ = new ExprStmt( increment );
|
|---|
| [4d2434a] | 225 | }
|
|---|
| 226 |
|
|---|
| 227 | template< typename OutIterator >
|
|---|
| [b8524ca] | 228 | void build( UntypedExpr * callExpr, InitExpander_old::IndexList::iterator idx, InitExpander_old::IndexList::iterator idxEnd, Initializer * init, OutIterator out ) {
|
|---|
| [4d2434a] | 229 | if ( idx == idxEnd ) return;
|
|---|
| 230 | Expression * index = *idx++;
|
|---|
| 231 | assert( idx != idxEnd );
|
|---|
| 232 | Expression * dimension = *idx++;
|
|---|
| 233 |
|
|---|
| [f9cebb5] | 234 | // xxx - may want to eventually issue a warning here if we can detect
|
|---|
| 235 | // that the number of elements exceeds to dimension of the array
|
|---|
| [4d2434a] | 236 | if ( idx == idxEnd ) {
|
|---|
| 237 | if ( ListInit * listInit = dynamic_cast< ListInit * >( init ) ) {
|
|---|
| 238 | for ( Initializer * init : *listInit ) {
|
|---|
| [f9cebb5] | 239 | buildCallExpr( callExpr->clone(), index, dimension, init, out );
|
|---|
| [4d2434a] | 240 | }
|
|---|
| 241 | } else {
|
|---|
| [f9cebb5] | 242 | buildCallExpr( callExpr->clone(), index, dimension, init, out );
|
|---|
| [4d2434a] | 243 | }
|
|---|
| 244 | } else {
|
|---|
| 245 | std::list< Statement * > branches;
|
|---|
| 246 |
|
|---|
| 247 | unsigned long cond = 0;
|
|---|
| 248 | ListInit * listInit = dynamic_cast< ListInit * >( init );
|
|---|
| 249 | if ( ! listInit ) {
|
|---|
| 250 | // xxx - this shouldn't be an error, but need a way to
|
|---|
| 251 | // terminate without creating output, so should catch this error
|
|---|
| [a16764a6] | 252 | SemanticError( init->location, "unbalanced list initializers" );
|
|---|
| [4d2434a] | 253 | }
|
|---|
| [f9cebb5] | 254 |
|
|---|
| 255 | static UniqueName targetLabel( "L__autogen__" );
|
|---|
| 256 | Label switchLabel( targetLabel.newName(), 0, std::list< Attribute * >{ new Attribute("unused") } );
|
|---|
| [4d2434a] | 257 | for ( Initializer * init : *listInit ) {
|
|---|
| 258 | Expression * condition;
|
|---|
| 259 | // check for designations
|
|---|
| 260 | // if ( init-> ) {
|
|---|
| 261 | condition = new ConstantExpr( Constant::from_ulong( cond ) );
|
|---|
| 262 | ++cond;
|
|---|
| 263 | // } else {
|
|---|
| 264 | // condition = // ... take designation
|
|---|
| 265 | // cond = // ... take designation+1
|
|---|
| 266 | // }
|
|---|
| 267 | std::list< Statement * > stmts;
|
|---|
| 268 | build( callExpr, idx, idxEnd, init, back_inserter( stmts ) );
|
|---|
| [ba3706f] | 269 | stmts.push_back( new BranchStmt( switchLabel, BranchStmt::Break ) );
|
|---|
| 270 | CaseStmt * caseStmt = new CaseStmt( condition, stmts );
|
|---|
| [4d2434a] | 271 | branches.push_back( caseStmt );
|
|---|
| 272 | }
|
|---|
| [ba3706f] | 273 | *out++ = new SwitchStmt( index->clone(), branches );
|
|---|
| 274 | *out++ = new NullStmt( { switchLabel } );
|
|---|
| [4d2434a] | 275 | }
|
|---|
| 276 | }
|
|---|
| 277 | }
|
|---|
| 278 |
|
|---|
| 279 | // if array came with an initializer list: initialize each element
|
|---|
| 280 | // may have more initializers than elements in the array - need to check at each index that
|
|---|
| 281 | // we haven't exceeded size.
|
|---|
| 282 | // may have fewer initializers than elements in the array - need to default construct
|
|---|
| 283 | // remaining elements.
|
|---|
| 284 | // To accomplish this, generate switch statement, consuming all of expander's elements
|
|---|
| [b8524ca] | 285 | Statement * InitImpl_old::buildListInit( UntypedExpr * dst, std::list< Expression * > & indices ) {
|
|---|
| [22bc276] | 286 | if ( ! init ) return nullptr;
|
|---|
| [ba3706f] | 287 | CompoundStmt * block = new CompoundStmt();
|
|---|
| [f9cebb5] | 288 | build( dst, indices.begin(), indices.end(), init, back_inserter( block->get_kids() ) );
|
|---|
| 289 | if ( block->get_kids().empty() ) {
|
|---|
| 290 | delete block;
|
|---|
| [22bc276] | 291 | return nullptr;
|
|---|
| [4d2434a] | 292 | } else {
|
|---|
| [22bc276] | 293 | init = nullptr; // init was consumed in creating the list init
|
|---|
| [f9cebb5] | 294 | return block;
|
|---|
| [4d2434a] | 295 | }
|
|---|
| [39f84a4] | 296 | }
|
|---|
| 297 |
|
|---|
| [b8524ca] | 298 | Statement * ExprImpl_old::buildListInit( UntypedExpr *, std::list< Expression * > & ) {
|
|---|
| [22bc276] | 299 | return nullptr;
|
|---|
| [4d2434a] | 300 | }
|
|---|
| 301 |
|
|---|
| [b8524ca] | 302 | Statement * InitExpander_old::buildListInit( UntypedExpr * dst ) {
|
|---|
| [4d2434a] | 303 | return expander->buildListInit( dst, indices );
|
|---|
| 304 | }
|
|---|
| 305 |
|
|---|
| [b8524ca] | 306 | class InitExpander_new::ExpanderImpl {
|
|---|
| 307 | public:
|
|---|
| 308 | virtual ~ExpanderImpl() = default;
|
|---|
| 309 | virtual std::vector< ast::ptr< ast::Expr > > next( IndexList & indices ) = 0;
|
|---|
| 310 | virtual ast::ptr< ast::Stmt > buildListInit(
|
|---|
| 311 | const ast::UntypedExpr * callExpr, IndexList & indices ) = 0;
|
|---|
| 312 | };
|
|---|
| 313 |
|
|---|
| 314 | namespace {
|
|---|
| 315 | class InitImpl_new final : public InitExpander_new::ExpanderImpl {
|
|---|
| 316 | ast::ptr< ast::Init > init;
|
|---|
| 317 | public:
|
|---|
| 318 | InitImpl_new( const ast::Init * i ) : init( i ) {}
|
|---|
| 319 |
|
|---|
| 320 | std::vector< ast::ptr< ast::Expr > > next( InitExpander_new::IndexList & ) override {
|
|---|
| 321 | return makeInitList( init );
|
|---|
| 322 | }
|
|---|
| 323 |
|
|---|
| 324 | ast::ptr< ast::Stmt > buildListInit(
|
|---|
| 325 | const ast::UntypedExpr * callExpr, InitExpander_new::IndexList & indices
|
|---|
| 326 | ) override {
|
|---|
| 327 | #warning unimplemented
|
|---|
| 328 | (void)callExpr; (void)indices;
|
|---|
| 329 | assert(false);
|
|---|
| 330 | return {};
|
|---|
| 331 | }
|
|---|
| 332 | };
|
|---|
| 333 |
|
|---|
| 334 | class ExprImpl_new final : public InitExpander_new::ExpanderImpl {
|
|---|
| 335 | ast::ptr< ast::Expr > arg;
|
|---|
| 336 | public:
|
|---|
| 337 | ExprImpl_new( const ast::Expr * a ) : arg( a ) {}
|
|---|
| 338 |
|
|---|
| 339 | std::vector< ast::ptr< ast::Expr > > next(
|
|---|
| 340 | InitExpander_new::IndexList & indices
|
|---|
| 341 | ) override {
|
|---|
| 342 | #warning unimplemented
|
|---|
| 343 | (void)indices;
|
|---|
| 344 | assert(false);
|
|---|
| 345 | return {};
|
|---|
| 346 | }
|
|---|
| 347 |
|
|---|
| 348 | ast::ptr< ast::Stmt > buildListInit(
|
|---|
| 349 | const ast::UntypedExpr *, InitExpander_new::IndexList &
|
|---|
| 350 | ) override {
|
|---|
| 351 | return {};
|
|---|
| 352 | }
|
|---|
| 353 | };
|
|---|
| 354 | } // anonymous namespace
|
|---|
| 355 |
|
|---|
| 356 | InitExpander_new::InitExpander_new( const ast::Init * init )
|
|---|
| 357 | : expander( new InitImpl_new{ init } ), crnt(), indices() {}
|
|---|
| 358 |
|
|---|
| 359 | InitExpander_new::InitExpander_new( const ast::Expr * expr )
|
|---|
| 360 | : expander( new ExprImpl_new{ expr } ), crnt(), indices() {}
|
|---|
| 361 |
|
|---|
| 362 | std::vector< ast::ptr< ast::Expr > > InitExpander_new::operator* () { return crnt; }
|
|---|
| 363 |
|
|---|
| 364 | InitExpander_new & InitExpander_new::operator++ () {
|
|---|
| 365 | crnt = expander->next( indices );
|
|---|
| 366 | return *this;
|
|---|
| 367 | }
|
|---|
| 368 |
|
|---|
| 369 | /// builds statement which has the same semantics as a C-style list initializer (for array
|
|---|
| 370 | /// initializers) using callExpr as the base expression to perform initialization
|
|---|
| 371 | ast::ptr< ast::Stmt > InitExpander_new::buildListInit( const ast::UntypedExpr * callExpr ) {
|
|---|
| 372 | return expander->buildListInit( callExpr, indices );
|
|---|
| 373 | }
|
|---|
| 374 |
|
|---|
| 375 | void InitExpander_new::addArrayIndex( const ast::Expr * index, const ast::Expr * dimension ) {
|
|---|
| 376 | indices.emplace_back( index );
|
|---|
| 377 | indices.emplace_back( dimension );
|
|---|
| 378 | }
|
|---|
| 379 |
|
|---|
| 380 | void InitExpander_new::clearArrayIndices() { indices.clear(); }
|
|---|
| 381 |
|
|---|
| 382 | bool InitExpander_new::addReference() {
|
|---|
| 383 | for ( ast::ptr< ast::Expr > & expr : crnt ) {
|
|---|
| 384 | expr = new ast::AddressExpr{ expr };
|
|---|
| 385 | }
|
|---|
| 386 | return ! crnt.empty();
|
|---|
| 387 | }
|
|---|
| 388 |
|
|---|
| [549c006] | 389 | Type * getTypeofThis( FunctionType * ftype ) {
|
|---|
| 390 | assertf( ftype, "getTypeofThis: nullptr ftype" );
|
|---|
| 391 | ObjectDecl * thisParam = getParamThis( ftype );
|
|---|
| [7fc7cdb] | 392 | ReferenceType * refType = strict_dynamic_cast< ReferenceType * >( thisParam->type );
|
|---|
| 393 | return refType->base;
|
|---|
| 394 | }
|
|---|
| 395 |
|
|---|
| [549c006] | 396 | ObjectDecl * getParamThis( FunctionType * ftype ) {
|
|---|
| 397 | assertf( ftype, "getParamThis: nullptr ftype" );
|
|---|
| [7fc7cdb] | 398 | auto & params = ftype->parameters;
|
|---|
| [549c006] | 399 | assertf( ! params.empty(), "getParamThis: ftype with 0 parameters: %s", toString( ftype ).c_str() );
|
|---|
| [7fc7cdb] | 400 | return strict_dynamic_cast< ObjectDecl * >( params.front() );
|
|---|
| 401 | }
|
|---|
| 402 |
|
|---|
| [22bc276] | 403 | bool tryConstruct( DeclarationWithType * dwt ) {
|
|---|
| 404 | ObjectDecl * objDecl = dynamic_cast< ObjectDecl * >( dwt );
|
|---|
| 405 | if ( ! objDecl ) return false;
|
|---|
| [df7a162] | 406 | return (objDecl->get_init() == nullptr ||
|
|---|
| [22bc276] | 407 | ( objDecl->get_init() != nullptr && objDecl->get_init()->get_maybeConstructed() ))
|
|---|
| 408 | && ! objDecl->get_storageClasses().is_extern
|
|---|
| [29bc63e] | 409 | && isConstructable( objDecl->type );
|
|---|
| 410 | }
|
|---|
| 411 |
|
|---|
| 412 | bool isConstructable( Type * type ) {
|
|---|
| 413 | return ! dynamic_cast< VarArgsType * >( type ) && ! dynamic_cast< ReferenceType * >( type ) && ! dynamic_cast< FunctionType * >( type ) && ! Tuples::isTtype( type );
|
|---|
| [64071c2] | 414 | }
|
|---|
| [2b46a13] | 415 |
|
|---|
| [2d11663] | 416 | struct CallFinder_old {
|
|---|
| 417 | CallFinder_old( const std::list< std::string > & names ) : names( names ) {}
|
|---|
| [4d2434a] | 418 |
|
|---|
| [0a6aad4] | 419 | void postvisit( ApplicationExpr * appExpr ) {
|
|---|
| [4d2434a] | 420 | handleCallExpr( appExpr );
|
|---|
| 421 | }
|
|---|
| 422 |
|
|---|
| [0a6aad4] | 423 | void postvisit( UntypedExpr * untypedExpr ) {
|
|---|
| [4d2434a] | 424 | handleCallExpr( untypedExpr );
|
|---|
| 425 | }
|
|---|
| 426 |
|
|---|
| 427 | std::list< Expression * > * matches;
|
|---|
| 428 | private:
|
|---|
| 429 | const std::list< std::string > names;
|
|---|
| 430 |
|
|---|
| 431 | template< typename CallExpr >
|
|---|
| 432 | void handleCallExpr( CallExpr * expr ) {
|
|---|
| 433 | std::string fname = getFunctionName( expr );
|
|---|
| 434 | if ( std::find( names.begin(), names.end(), fname ) != names.end() ) {
|
|---|
| 435 | matches->push_back( expr );
|
|---|
| [cad355a] | 436 | }
|
|---|
| [64071c2] | 437 | }
|
|---|
| [4d2434a] | 438 | };
|
|---|
| 439 |
|
|---|
| [2d11663] | 440 | struct CallFinder_new final {
|
|---|
| 441 | std::vector< ast::ptr< ast::Expr > > matches;
|
|---|
| 442 | const std::vector< std::string > names;
|
|---|
| 443 |
|
|---|
| 444 | CallFinder_new( std::vector< std::string > && ns ) : matches(), names( std::move(ns) ) {}
|
|---|
| 445 |
|
|---|
| 446 | void handleCallExpr( const ast::Expr * expr ) {
|
|---|
| 447 | std::string fname = getFunctionName( expr );
|
|---|
| 448 | if ( std::find( names.begin(), names.end(), fname ) != names.end() ) {
|
|---|
| 449 | matches.emplace_back( expr );
|
|---|
| 450 | }
|
|---|
| 451 | }
|
|---|
| 452 |
|
|---|
| 453 | void postvisit( const ast::ApplicationExpr * expr ) { handleCallExpr( expr ); }
|
|---|
| 454 | void postvisit( const ast::UntypedExpr * expr ) { handleCallExpr( expr ); }
|
|---|
| 455 | };
|
|---|
| 456 |
|
|---|
| [4d2434a] | 457 | void collectCtorDtorCalls( Statement * stmt, std::list< Expression * > & matches ) {
|
|---|
| [2d11663] | 458 | static PassVisitor<CallFinder_old> finder( std::list< std::string >{ "?{}", "^?{}" } );
|
|---|
| [0a6aad4] | 459 | finder.pass.matches = &matches;
|
|---|
| [4d2434a] | 460 | maybeAccept( stmt, finder );
|
|---|
| [64071c2] | 461 | }
|
|---|
| [4d2434a] | 462 |
|
|---|
| [2d11663] | 463 | std::vector< ast::ptr< ast::Expr > > collectCtorDtorCalls( const ast::Stmt * stmt ) {
|
|---|
| 464 | ast::Pass< CallFinder_new > finder{ std::vector< std::string >{ "?{}", "^?{}" } };
|
|---|
| 465 | maybe_accept( stmt, finder );
|
|---|
| 466 | return std::move( finder.pass.matches );
|
|---|
| 467 | }
|
|---|
| 468 |
|
|---|
| [4d2434a] | 469 | Expression * getCtorDtorCall( Statement * stmt ) {
|
|---|
| 470 | std::list< Expression * > matches;
|
|---|
| 471 | collectCtorDtorCalls( stmt, matches );
|
|---|
| [20eacb7] | 472 | assertf( matches.size() <= 1, "%zd constructor/destructors found in %s", matches.size(), toString( stmt ).c_str() );
|
|---|
| [22bc276] | 473 | return matches.size() == 1 ? matches.front() : nullptr;
|
|---|
| [4d2434a] | 474 | }
|
|---|
| 475 |
|
|---|
| [aedfd91] | 476 | namespace {
|
|---|
| [599b386] | 477 | DeclarationWithType * getCalledFunction( Expression * expr );
|
|---|
| [d7aa12c] | 478 | const ast::DeclWithType * getCalledFunction( const ast::Expr * expr );
|
|---|
| [599b386] | 479 |
|
|---|
| 480 | template<typename CallExpr>
|
|---|
| 481 | DeclarationWithType * handleDerefCalledFunction( CallExpr * expr ) {
|
|---|
| 482 | // (*f)(x) => should get "f"
|
|---|
| 483 | std::string name = getFunctionName( expr );
|
|---|
| 484 | assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() );
|
|---|
| [b128d3e] | 485 | assertf( ! expr->get_args().empty(), "Cannot get called function from dereference with no arguments" );
|
|---|
| [599b386] | 486 | return getCalledFunction( expr->get_args().front() );
|
|---|
| 487 | }
|
|---|
| 488 |
|
|---|
| [d7aa12c] | 489 | template<typename CallExpr>
|
|---|
| 490 | const ast::DeclWithType * handleDerefCalledFunction( const CallExpr * expr ) {
|
|---|
| 491 | // (*f)(x) => should get "f"
|
|---|
| 492 | std::string name = getFunctionName( expr );
|
|---|
| 493 | assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() );
|
|---|
| 494 | assertf( ! expr->args.empty(), "Cannot get called function from dereference with no arguments" );
|
|---|
| 495 | return getCalledFunction( expr->args.front() );
|
|---|
| 496 | }
|
|---|
| 497 |
|
|---|
| 498 |
|
|---|
| [ee1635c8] | 499 | DeclarationWithType * getCalledFunction( Expression * expr ) {
|
|---|
| 500 | assert( expr );
|
|---|
| 501 | if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( expr ) ) {
|
|---|
| [6fc5c14] | 502 | return varExpr->var;
|
|---|
| [ee1635c8] | 503 | } else if ( MemberExpr * memberExpr = dynamic_cast< MemberExpr * >( expr ) ) {
|
|---|
| [6fc5c14] | 504 | return memberExpr->member;
|
|---|
| [ee1635c8] | 505 | } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) {
|
|---|
| [6fc5c14] | 506 | return getCalledFunction( castExpr->arg );
|
|---|
| [599b386] | 507 | } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * >( expr ) ) {
|
|---|
| 508 | return handleDerefCalledFunction( untypedExpr );
|
|---|
| 509 | } else if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * > ( expr ) ) {
|
|---|
| 510 | return handleDerefCalledFunction( appExpr );
|
|---|
| [f3b0a07] | 511 | } else if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( expr ) ) {
|
|---|
| [6fc5c14] | 512 | return getCalledFunction( addrExpr->arg );
|
|---|
| 513 | } else if ( CommaExpr * commaExpr = dynamic_cast< CommaExpr * >( expr ) ) {
|
|---|
| 514 | return getCalledFunction( commaExpr->arg2 );
|
|---|
| [ee1635c8] | 515 | }
|
|---|
| 516 | return nullptr;
|
|---|
| [aedfd91] | 517 | }
|
|---|
| [d7aa12c] | 518 |
|
|---|
| 519 | const ast::DeclWithType * getCalledFunction( const ast::Expr * expr ) {
|
|---|
| 520 | assert( expr );
|
|---|
| 521 | if ( const ast::VariableExpr * varExpr = dynamic_cast< const ast::VariableExpr * >( expr ) ) {
|
|---|
| 522 | return varExpr->var;
|
|---|
| 523 | } else if ( const ast::MemberExpr * memberExpr = dynamic_cast< const ast::MemberExpr * >( expr ) ) {
|
|---|
| 524 | return memberExpr->member;
|
|---|
| 525 | } else if ( const ast::CastExpr * castExpr = dynamic_cast< const ast::CastExpr * >( expr ) ) {
|
|---|
| 526 | return getCalledFunction( castExpr->arg );
|
|---|
| 527 | } else if ( const ast::UntypedExpr * untypedExpr = dynamic_cast< const ast::UntypedExpr * >( expr ) ) {
|
|---|
| 528 | return handleDerefCalledFunction( untypedExpr );
|
|---|
| 529 | } else if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * > ( expr ) ) {
|
|---|
| 530 | return handleDerefCalledFunction( appExpr );
|
|---|
| 531 | } else if ( const ast::AddressExpr * addrExpr = dynamic_cast< const ast::AddressExpr * >( expr ) ) {
|
|---|
| 532 | return getCalledFunction( addrExpr->arg );
|
|---|
| 533 | } else if ( const ast::CommaExpr * commaExpr = dynamic_cast< const ast::CommaExpr * >( expr ) ) {
|
|---|
| 534 | return getCalledFunction( commaExpr->arg2 );
|
|---|
| 535 | }
|
|---|
| 536 | return nullptr;
|
|---|
| 537 | }
|
|---|
| [aedfd91] | 538 | }
|
|---|
| [70f89d00] | 539 |
|
|---|
| [b7b8674] | 540 | DeclarationWithType * getFunction( Expression * expr ) {
|
|---|
| 541 | if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr ) ) {
|
|---|
| 542 | return getCalledFunction( appExpr->get_function() );
|
|---|
| 543 | } else if ( UntypedExpr * untyped = dynamic_cast< UntypedExpr * > ( expr ) ) {
|
|---|
| 544 | return getCalledFunction( untyped->get_function() );
|
|---|
| 545 | }
|
|---|
| 546 | assertf( false, "getFunction received unknown expression: %s", toString( expr ).c_str() );
|
|---|
| 547 | }
|
|---|
| 548 |
|
|---|
| [d7aa12c] | 549 | const ast::DeclWithType * getFunction( const ast::Expr * expr ) {
|
|---|
| 550 | if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * >( expr ) ) {
|
|---|
| 551 | return getCalledFunction( appExpr->func );
|
|---|
| 552 | } else if ( const ast::UntypedExpr * untyped = dynamic_cast< const ast::UntypedExpr * > ( expr ) ) {
|
|---|
| 553 | return getCalledFunction( untyped->func );
|
|---|
| 554 | }
|
|---|
| 555 | assertf( false, "getFunction received unknown expression: %s", toString( expr ).c_str() );
|
|---|
| 556 | }
|
|---|
| 557 |
|
|---|
| [aedfd91] | 558 | ApplicationExpr * isIntrinsicCallExpr( Expression * expr ) {
|
|---|
| 559 | ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr );
|
|---|
| [22bc276] | 560 | if ( ! appExpr ) return nullptr;
|
|---|
| [ee1635c8] | 561 | DeclarationWithType * function = getCalledFunction( appExpr->get_function() );
|
|---|
| [f3b0a07] | 562 | assertf( function, "getCalledFunction returned nullptr: %s", toString( appExpr->get_function() ).c_str() );
|
|---|
| [64071c2] | 563 | // check for Intrinsic only - don't want to remove all overridable ctor/dtors because autogenerated ctor/dtor
|
|---|
| 564 | // will call all member dtors, and some members may have a user defined dtor.
|
|---|
| [22bc276] | 565 | return function->get_linkage() == LinkageSpec::Intrinsic ? appExpr : nullptr;
|
|---|
| [aedfd91] | 566 | }
|
|---|
| 567 |
|
|---|
| [2d11663] | 568 | const ast::ApplicationExpr * isIntrinsicCallExpr( const ast::Expr * expr ) {
|
|---|
| 569 | auto appExpr = dynamic_cast< const ast::ApplicationExpr * >( expr );
|
|---|
| 570 | if ( ! appExpr ) return nullptr;
|
|---|
| 571 |
|
|---|
| 572 | const ast::DeclWithType * func = getCalledFunction( appExpr->func );
|
|---|
| 573 | assertf( func,
|
|---|
| 574 | "getCalledFunction returned nullptr: %s", toString( appExpr->func ).c_str() );
|
|---|
| 575 |
|
|---|
| 576 | // check for Intrinsic only -- don't want to remove all overridable ctor/dtor because
|
|---|
| 577 | // autogenerated ctor/dtor will call all member dtors, and some members may have a
|
|---|
| 578 | // user-defined dtor
|
|---|
| 579 | return func->linkage == ast::Linkage::Intrinsic ? appExpr : nullptr;
|
|---|
| 580 | }
|
|---|
| 581 |
|
|---|
| [a465caff] | 582 | namespace {
|
|---|
| 583 | template <typename Predicate>
|
|---|
| 584 | bool allofCtorDtor( Statement * stmt, const Predicate & pred ) {
|
|---|
| 585 | std::list< Expression * > callExprs;
|
|---|
| 586 | collectCtorDtorCalls( stmt, callExprs );
|
|---|
| 587 | // if ( callExprs.empty() ) return false; // xxx - do I still need this check?
|
|---|
| 588 | return std::all_of( callExprs.begin(), callExprs.end(), pred);
|
|---|
| 589 | }
|
|---|
| [2d11663] | 590 |
|
|---|
| 591 | template <typename Predicate>
|
|---|
| 592 | bool allofCtorDtor( const ast::Stmt * stmt, const Predicate & pred ) {
|
|---|
| 593 | std::vector< ast::ptr< ast::Expr > > callExprs = collectCtorDtorCalls( stmt );
|
|---|
| 594 | return std::all_of( callExprs.begin(), callExprs.end(), pred );
|
|---|
| 595 | }
|
|---|
| [a465caff] | 596 | }
|
|---|
| 597 |
|
|---|
| [f9cebb5] | 598 | bool isIntrinsicSingleArgCallStmt( Statement * stmt ) {
|
|---|
| [a465caff] | 599 | return allofCtorDtor( stmt, []( Expression * callExpr ){
|
|---|
| [4d2434a] | 600 | if ( ApplicationExpr * appExpr = isIntrinsicCallExpr( callExpr ) ) {
|
|---|
| [f072892] | 601 | FunctionType *funcType = GenPoly::getFunctionType( appExpr->function->result );
|
|---|
| [4d2434a] | 602 | assert( funcType );
|
|---|
| 603 | return funcType->get_parameters().size() == 1;
|
|---|
| 604 | }
|
|---|
| 605 | return false;
|
|---|
| [2d11663] | 606 | });
|
|---|
| 607 | }
|
|---|
| 608 |
|
|---|
| 609 | bool isIntrinsicSingleArgCallStmt( const ast::Stmt * stmt ) {
|
|---|
| 610 | return allofCtorDtor( stmt, []( const ast::Expr * callExpr ){
|
|---|
| 611 | if ( const ast::ApplicationExpr * appExpr = isIntrinsicCallExpr( callExpr ) ) {
|
|---|
| 612 | const ast::FunctionType * funcType =
|
|---|
| 613 | GenPoly::getFunctionType( appExpr->func->result );
|
|---|
| 614 | assert( funcType );
|
|---|
| 615 | return funcType->params.size() == 1;
|
|---|
| 616 | }
|
|---|
| 617 | return false;
|
|---|
| [4d2434a] | 618 | });
|
|---|
| [64071c2] | 619 | }
|
|---|
| [f1b1e4c] | 620 |
|
|---|
| [a465caff] | 621 | bool isIntrinsicCallStmt( Statement * stmt ) {
|
|---|
| 622 | return allofCtorDtor( stmt, []( Expression * callExpr ) {
|
|---|
| 623 | return isIntrinsicCallExpr( callExpr );
|
|---|
| 624 | });
|
|---|
| 625 | }
|
|---|
| 626 |
|
|---|
| [64071c2] | 627 | namespace {
|
|---|
| 628 | template<typename CallExpr>
|
|---|
| 629 | Expression *& callArg( CallExpr * callExpr, unsigned int pos ) {
|
|---|
| [a61ad31] | 630 | if ( pos >= callExpr->get_args().size() ) assertf( false, "getCallArg for argument that doesn't exist: (%u); %s.", pos, toString( callExpr ).c_str() );
|
|---|
| [64071c2] | 631 | for ( Expression *& arg : callExpr->get_args() ) {
|
|---|
| 632 | if ( pos == 0 ) return arg;
|
|---|
| 633 | pos--;
|
|---|
| 634 | }
|
|---|
| 635 | assert( false );
|
|---|
| 636 | }
|
|---|
| [9b4f329] | 637 |
|
|---|
| [b5fed34] | 638 | template<typename CallExpr>
|
|---|
| 639 | const ast::Expr * callArg( const CallExpr * call, unsigned int pos ) {
|
|---|
| 640 | if( pos >= call->args.size() ) {
|
|---|
| 641 | assertf( false, "getCallArg for argument that doesn't exist: (%u); %s.",
|
|---|
| 642 | pos, toString( call ).c_str() );
|
|---|
| 643 | }
|
|---|
| 644 | for ( const ast::Expr * arg : call->args ) {
|
|---|
| 645 | if ( pos == 0 ) return arg;
|
|---|
| 646 | --pos;
|
|---|
| 647 | }
|
|---|
| 648 | assert( false );
|
|---|
| 649 | }
|
|---|
| [64071c2] | 650 | }
|
|---|
| [f1b1e4c] | 651 |
|
|---|
| [64071c2] | 652 | Expression *& getCallArg( Expression * callExpr, unsigned int pos ) {
|
|---|
| 653 | if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( callExpr ) ) {
|
|---|
| 654 | return callArg( appExpr, pos );
|
|---|
| 655 | } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * >( callExpr ) ) {
|
|---|
| 656 | return callArg( untypedExpr, pos );
|
|---|
| [f3b0a07] | 657 | } else if ( TupleAssignExpr * tupleExpr = dynamic_cast< TupleAssignExpr * > ( callExpr ) ) {
|
|---|
| 658 | std::list< Statement * > & stmts = tupleExpr->get_stmtExpr()->get_statements()->get_kids();
|
|---|
| 659 | assertf( ! stmts.empty(), "TupleAssignExpr somehow has no statements." );
|
|---|
| [e3e16bc] | 660 | ExprStmt * stmt = strict_dynamic_cast< ExprStmt * >( stmts.back() );
|
|---|
| 661 | TupleExpr * tuple = strict_dynamic_cast< TupleExpr * >( stmt->get_expr() );
|
|---|
| [f3b0a07] | 662 | assertf( ! tuple->get_exprs().empty(), "TupleAssignExpr somehow has empty tuple expr." );
|
|---|
| 663 | return getCallArg( tuple->get_exprs().front(), pos );
|
|---|
| [62a05d1] | 664 | } else if ( ImplicitCopyCtorExpr * copyCtor = dynamic_cast< ImplicitCopyCtorExpr * >( callExpr ) ) {
|
|---|
| 665 | return getCallArg( copyCtor->callExpr, pos );
|
|---|
| [64071c2] | 666 | } else {
|
|---|
| [f3b0a07] | 667 | assertf( false, "Unexpected expression type passed to getCallArg: %s", toString( callExpr ).c_str() );
|
|---|
| [64071c2] | 668 | }
|
|---|
| 669 | }
|
|---|
| [b5fed34] | 670 |
|
|---|
| [9b4f329] | 671 | const ast::Expr * getCallArg( const ast::Expr * call, unsigned pos ) {
|
|---|
| [b5fed34] | 672 | if ( auto app = dynamic_cast< const ast::ApplicationExpr * >( call ) ) {
|
|---|
| 673 | return callArg( app, pos );
|
|---|
| 674 | } else if ( auto untyped = dynamic_cast< const ast::UntypedExpr * >( call ) ) {
|
|---|
| 675 | return callArg( untyped, pos );
|
|---|
| 676 | } else if ( auto tupleAssn = dynamic_cast< const ast::TupleAssignExpr * >( call ) ) {
|
|---|
| 677 | const std::list<ast::ptr<ast::Stmt>>& stmts = tupleAssn->stmtExpr->stmts->kids;
|
|---|
| 678 | assertf( ! stmts.empty(), "TupleAssignExpr missing statements." );
|
|---|
| 679 | auto stmt = strict_dynamic_cast< const ast::ExprStmt * >( stmts.back().get() );
|
|---|
| 680 | auto tuple = strict_dynamic_cast< const ast::TupleExpr * >( stmt->expr.get() );
|
|---|
| 681 | assertf( ! tuple->exprs.empty(), "TupleAssignExpr has empty tuple expr.");
|
|---|
| 682 | return getCallArg( tuple->exprs.front(), pos );
|
|---|
| 683 | } else if ( auto ctor = dynamic_cast< const ast::ImplicitCopyCtorExpr * >( call ) ) {
|
|---|
| 684 | return getCallArg( ctor->callExpr, pos );
|
|---|
| 685 | } else {
|
|---|
| 686 | assertf( false, "Unexpected expression type passed to getCallArg: %s",
|
|---|
| 687 | toString( call ).c_str() );
|
|---|
| 688 | }
|
|---|
| [9b4f329] | 689 | }
|
|---|
| [f1b1e4c] | 690 |
|
|---|
| [64071c2] | 691 | namespace {
|
|---|
| [599b386] | 692 | std::string funcName( Expression * func );
|
|---|
| [d7aa12c] | 693 | std::string funcName( const ast::Expr * func );
|
|---|
| [599b386] | 694 |
|
|---|
| 695 | template<typename CallExpr>
|
|---|
| 696 | std::string handleDerefName( CallExpr * expr ) {
|
|---|
| 697 | // (*f)(x) => should get name "f"
|
|---|
| 698 | std::string name = getFunctionName( expr );
|
|---|
| 699 | assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() );
|
|---|
| [b128d3e] | 700 | assertf( ! expr->get_args().empty(), "Cannot get function name from dereference with no arguments" );
|
|---|
| [599b386] | 701 | return funcName( expr->get_args().front() );
|
|---|
| 702 | }
|
|---|
| 703 |
|
|---|
| [d7aa12c] | 704 | template<typename CallExpr>
|
|---|
| 705 | std::string handleDerefName( const CallExpr * expr ) {
|
|---|
| 706 | // (*f)(x) => should get name "f"
|
|---|
| 707 | std::string name = getFunctionName( expr );
|
|---|
| 708 | assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() );
|
|---|
| 709 | assertf( ! expr->args.empty(), "Cannot get function name from dereference with no arguments" );
|
|---|
| 710 | return funcName( expr->args.front() );
|
|---|
| 711 | }
|
|---|
| 712 |
|
|---|
| [c738ca4] | 713 | std::string funcName( Expression * func ) {
|
|---|
| [64071c2] | 714 | if ( NameExpr * nameExpr = dynamic_cast< NameExpr * >( func ) ) {
|
|---|
| 715 | return nameExpr->get_name();
|
|---|
| 716 | } else if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( func ) ) {
|
|---|
| 717 | return varExpr->get_var()->get_name();
|
|---|
| [c738ca4] | 718 | } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( func ) ) {
|
|---|
| 719 | return funcName( castExpr->get_arg() );
|
|---|
| [ee1635c8] | 720 | } else if ( MemberExpr * memberExpr = dynamic_cast< MemberExpr * >( func ) ) {
|
|---|
| 721 | return memberExpr->get_member()->get_name();
|
|---|
| [96a10cdd] | 722 | } else if ( UntypedMemberExpr * memberExpr = dynamic_cast< UntypedMemberExpr * > ( func ) ) {
|
|---|
| [fd782b2] | 723 | return funcName( memberExpr->get_member() );
|
|---|
| [599b386] | 724 | } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * >( func ) ) {
|
|---|
| 725 | return handleDerefName( untypedExpr );
|
|---|
| 726 | } else if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( func ) ) {
|
|---|
| 727 | return handleDerefName( appExpr );
|
|---|
| [19a9822] | 728 | } else if ( ConstructorExpr * ctorExpr = dynamic_cast< ConstructorExpr * >( func ) ) {
|
|---|
| 729 | return funcName( getCallArg( ctorExpr->get_callExpr(), 0 ) );
|
|---|
| [64071c2] | 730 | } else {
|
|---|
| [19a9822] | 731 | assertf( false, "Unexpected expression type being called as a function in call expression: %s", toString( func ).c_str() );
|
|---|
| [64071c2] | 732 | }
|
|---|
| 733 | }
|
|---|
| [d7aa12c] | 734 |
|
|---|
| 735 | std::string funcName( const ast::Expr * func ) {
|
|---|
| 736 | if ( const ast::NameExpr * nameExpr = dynamic_cast< const ast::NameExpr * >( func ) ) {
|
|---|
| 737 | return nameExpr->name;
|
|---|
| 738 | } else if ( const ast::VariableExpr * varExpr = dynamic_cast< const ast::VariableExpr * >( func ) ) {
|
|---|
| 739 | return varExpr->var->name;
|
|---|
| 740 | } else if ( const ast::CastExpr * castExpr = dynamic_cast< const ast::CastExpr * >( func ) ) {
|
|---|
| 741 | return funcName( castExpr->arg );
|
|---|
| 742 | } else if ( const ast::MemberExpr * memberExpr = dynamic_cast< const ast::MemberExpr * >( func ) ) {
|
|---|
| 743 | return memberExpr->member->name;
|
|---|
| 744 | } else if ( const ast::UntypedMemberExpr * memberExpr = dynamic_cast< const ast::UntypedMemberExpr * > ( func ) ) {
|
|---|
| 745 | return funcName( memberExpr->member );
|
|---|
| 746 | } else if ( const ast::UntypedExpr * untypedExpr = dynamic_cast< const ast::UntypedExpr * >( func ) ) {
|
|---|
| 747 | return handleDerefName( untypedExpr );
|
|---|
| 748 | } else if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * >( func ) ) {
|
|---|
| 749 | return handleDerefName( appExpr );
|
|---|
| 750 | } else if ( const ast::ConstructorExpr * ctorExpr = dynamic_cast< const ast::ConstructorExpr * >( func ) ) {
|
|---|
| 751 | return funcName( getCallArg( ctorExpr->callExpr, 0 ) );
|
|---|
| 752 | } else {
|
|---|
| 753 | assertf( false, "Unexpected expression type being called as a function in call expression: %s", toString( func ).c_str() );
|
|---|
| 754 | }
|
|---|
| 755 | }
|
|---|
| [64071c2] | 756 | }
|
|---|
| [70f89d00] | 757 |
|
|---|
| [64071c2] | 758 | std::string getFunctionName( Expression * expr ) {
|
|---|
| [599b386] | 759 | // there's some unforunate overlap here with getCalledFunction. Ideally this would be able to use getCalledFunction and
|
|---|
| 760 | // return the name of the DeclarationWithType, but this needs to work for NameExpr and UntypedMemberExpr, where getCalledFunction
|
|---|
| 761 | // can't possibly do anything reasonable.
|
|---|
| [64071c2] | 762 | if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr ) ) {
|
|---|
| [c738ca4] | 763 | return funcName( appExpr->get_function() );
|
|---|
| [64071c2] | 764 | } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * > ( expr ) ) {
|
|---|
| [c738ca4] | 765 | return funcName( untypedExpr->get_function() );
|
|---|
| [64071c2] | 766 | } else {
|
|---|
| [c738ca4] | 767 | std::cerr << expr << std::endl;
|
|---|
| [d1969a6] | 768 | assertf( false, "Unexpected expression type passed to getFunctionName" );
|
|---|
| [64071c2] | 769 | }
|
|---|
| 770 | }
|
|---|
| [10a7775] | 771 |
|
|---|
| [d7aa12c] | 772 | std::string getFunctionName( const ast::Expr * expr ) {
|
|---|
| 773 | // there's some unforunate overlap here with getCalledFunction. Ideally this would be able to use getCalledFunction and
|
|---|
| 774 | // return the name of the DeclarationWithType, but this needs to work for NameExpr and UntypedMemberExpr, where getCalledFunction
|
|---|
| 775 | // can't possibly do anything reasonable.
|
|---|
| 776 | if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * >( expr ) ) {
|
|---|
| 777 | return funcName( appExpr->func );
|
|---|
| 778 | } else if ( const ast::UntypedExpr * untypedExpr = dynamic_cast< const ast::UntypedExpr * > ( expr ) ) {
|
|---|
| 779 | return funcName( untypedExpr->func );
|
|---|
| 780 | } else {
|
|---|
| 781 | std::cerr << expr << std::endl;
|
|---|
| 782 | assertf( false, "Unexpected expression type passed to getFunctionName" );
|
|---|
| 783 | }
|
|---|
| 784 | }
|
|---|
| 785 |
|
|---|
| [64071c2] | 786 | Type * getPointerBase( Type * type ) {
|
|---|
| 787 | if ( PointerType * ptrType = dynamic_cast< PointerType * >( type ) ) {
|
|---|
| 788 | return ptrType->get_base();
|
|---|
| 789 | } else if ( ArrayType * arrayType = dynamic_cast< ArrayType * >( type ) ) {
|
|---|
| 790 | return arrayType->get_base();
|
|---|
| [ce8c12f] | 791 | } else if ( ReferenceType * refType = dynamic_cast< ReferenceType * >( type ) ) {
|
|---|
| 792 | return refType->get_base();
|
|---|
| [64071c2] | 793 | } else {
|
|---|
| [22bc276] | 794 | return nullptr;
|
|---|
| [64071c2] | 795 | }
|
|---|
| 796 | }
|
|---|
| [9e1d485] | 797 | const ast::Type* getPointerBase( const ast::Type* t ) {
|
|---|
| [b5fed34] | 798 | if ( const auto * p = dynamic_cast< const ast::PointerType * >( t ) ) {
|
|---|
| 799 | return p->base;
|
|---|
| 800 | } else if ( const auto * a = dynamic_cast< const ast::ArrayType * >( t ) ) {
|
|---|
| 801 | return a->base;
|
|---|
| 802 | } else if ( const auto * r = dynamic_cast< const ast::ReferenceType * >( t ) ) {
|
|---|
| 803 | return r->base;
|
|---|
| 804 | } else return nullptr;
|
|---|
| [9e1d485] | 805 | }
|
|---|
| [10a7775] | 806 |
|
|---|
| [64071c2] | 807 | Type * isPointerType( Type * type ) {
|
|---|
| 808 | if ( getPointerBase( type ) ) return type;
|
|---|
| [22bc276] | 809 | else return nullptr;
|
|---|
| [64071c2] | 810 | }
|
|---|
| [40e636a] | 811 |
|
|---|
| [f5c3b6c] | 812 | ApplicationExpr * createBitwiseAssignment( Expression * dst, Expression * src ) {
|
|---|
| 813 | static FunctionDecl * assign = nullptr;
|
|---|
| 814 | if ( ! assign ) {
|
|---|
| 815 | // temporary? Generate a fake assignment operator to represent bitwise assignments.
|
|---|
| 816 | // This operator could easily exist as a real function, but it's tricky because nothing should resolve to this function.
|
|---|
| 817 | TypeDecl * td = new TypeDecl( "T", noStorageClasses, nullptr, TypeDecl::Dtype, true );
|
|---|
| 818 | assign = new FunctionDecl( "?=?", noStorageClasses, LinkageSpec::Intrinsic, SymTab::genAssignType( new TypeInstType( noQualifiers, td->name, td ) ), nullptr );
|
|---|
| 819 | }
|
|---|
| 820 | if ( dynamic_cast< ReferenceType * >( dst->result ) ) {
|
|---|
| [5002738] | 821 | for (int depth = dst->result->referenceDepth(); depth > 0; depth--) {
|
|---|
| 822 | dst = new AddressExpr( dst );
|
|---|
| 823 | }
|
|---|
| [f5c3b6c] | 824 | } else {
|
|---|
| 825 | dst = new CastExpr( dst, new ReferenceType( noQualifiers, dst->result->clone() ) );
|
|---|
| 826 | }
|
|---|
| 827 | if ( dynamic_cast< ReferenceType * >( src->result ) ) {
|
|---|
| [5002738] | 828 | for (int depth = src->result->referenceDepth(); depth > 0; depth--) {
|
|---|
| 829 | src = new AddressExpr( src );
|
|---|
| 830 | }
|
|---|
| [ba89e9b7] | 831 | // src = new CastExpr( src, new ReferenceType( noQualifiers, src->result->stripReferences()->clone() ) );
|
|---|
| [f5c3b6c] | 832 | }
|
|---|
| 833 | return new ApplicationExpr( VariableExpr::functionPointer( assign ), { dst, src } );
|
|---|
| 834 | }
|
|---|
| 835 |
|
|---|
| [c5f3c68] | 836 | struct ConstExprChecker : public WithShortCircuiting {
|
|---|
| 837 | // most expressions are not const expr
|
|---|
| 838 | void previsit( Expression * ) { isConstExpr = false; visit_children = false; }
|
|---|
| [40e636a] | 839 |
|
|---|
| [c5f3c68] | 840 | void previsit( AddressExpr *addressExpr ) {
|
|---|
| 841 | visit_children = false;
|
|---|
| [65dc863] | 842 |
|
|---|
| [1ba88a0] | 843 | // address of a variable or member expression is constexpr
|
|---|
| 844 | Expression * arg = addressExpr->get_arg();
|
|---|
| 845 | if ( ! dynamic_cast< NameExpr * >( arg) && ! dynamic_cast< VariableExpr * >( arg ) && ! dynamic_cast< MemberExpr * >( arg ) && ! dynamic_cast< UntypedMemberExpr * >( arg ) ) isConstExpr = false;
|
|---|
| 846 | }
|
|---|
| [c5f3c68] | 847 |
|
|---|
| 848 | // these expressions may be const expr, depending on their children
|
|---|
| 849 | void previsit( SizeofExpr * ) {}
|
|---|
| 850 | void previsit( AlignofExpr * ) {}
|
|---|
| 851 | void previsit( UntypedOffsetofExpr * ) {}
|
|---|
| 852 | void previsit( OffsetofExpr * ) {}
|
|---|
| 853 | void previsit( OffsetPackExpr * ) {}
|
|---|
| 854 | void previsit( AttrExpr * ) {}
|
|---|
| 855 | void previsit( CommaExpr * ) {}
|
|---|
| 856 | void previsit( LogicalExpr * ) {}
|
|---|
| 857 | void previsit( ConditionalExpr * ) {}
|
|---|
| 858 | void previsit( CastExpr * ) {}
|
|---|
| 859 | void previsit( ConstantExpr * ) {}
|
|---|
| 860 |
|
|---|
| [caab997] | 861 | void previsit( VariableExpr * varExpr ) {
|
|---|
| 862 | visit_children = false;
|
|---|
| 863 |
|
|---|
| 864 | if ( EnumInstType * inst = dynamic_cast< EnumInstType * >( varExpr->result ) ) {
|
|---|
| 865 | long long int value;
|
|---|
| 866 | if ( inst->baseEnum->valueOf( varExpr->var, value ) ) {
|
|---|
| 867 | // enumerators are const expr
|
|---|
| 868 | return;
|
|---|
| 869 | }
|
|---|
| 870 | }
|
|---|
| 871 | isConstExpr = false;
|
|---|
| 872 | }
|
|---|
| 873 |
|
|---|
| [c5f3c68] | 874 | bool isConstExpr = true;
|
|---|
| [40e636a] | 875 | };
|
|---|
| 876 |
|
|---|
| 877 | bool isConstExpr( Expression * expr ) {
|
|---|
| 878 | if ( expr ) {
|
|---|
| [c5f3c68] | 879 | PassVisitor<ConstExprChecker> checker;
|
|---|
| [40e636a] | 880 | expr->accept( checker );
|
|---|
| [c5f3c68] | 881 | return checker.pass.isConstExpr;
|
|---|
| [40e636a] | 882 | }
|
|---|
| 883 | return true;
|
|---|
| 884 | }
|
|---|
| 885 |
|
|---|
| 886 | bool isConstExpr( Initializer * init ) {
|
|---|
| 887 | if ( init ) {
|
|---|
| [c5f3c68] | 888 | PassVisitor<ConstExprChecker> checker;
|
|---|
| [40e636a] | 889 | init->accept( checker );
|
|---|
| [c5f3c68] | 890 | return checker.pass.isConstExpr;
|
|---|
| [40e636a] | 891 | } // if
|
|---|
| 892 | // for all intents and purposes, no initializer means const expr
|
|---|
| 893 | return true;
|
|---|
| 894 | }
|
|---|
| 895 |
|
|---|
| [79970ed] | 896 | bool isConstructor( const std::string & str ) { return str == "?{}"; }
|
|---|
| 897 | bool isDestructor( const std::string & str ) { return str == "^?{}"; }
|
|---|
| [ee1635c8] | 898 | bool isAssignment( const std::string & str ) { return str == "?=?"; }
|
|---|
| [79970ed] | 899 | bool isCtorDtor( const std::string & str ) { return isConstructor( str ) || isDestructor( str ); }
|
|---|
| [ee1635c8] | 900 | bool isCtorDtorAssign( const std::string & str ) { return isCtorDtor( str ) || isAssignment( str ); }
|
|---|
| [4d4882a] | 901 |
|
|---|
| [ee1635c8] | 902 | FunctionDecl * isCopyFunction( Declaration * decl, const std::string & fname ) {
|
|---|
| [4d4882a] | 903 | FunctionDecl * function = dynamic_cast< FunctionDecl * >( decl );
|
|---|
| [0a267c1] | 904 | if ( ! function ) return nullptr;
|
|---|
| 905 | if ( function->name != fname ) return nullptr;
|
|---|
| 906 | FunctionType * ftype = function->type;
|
|---|
| 907 | if ( ftype->parameters.size() != 2 ) return nullptr;
|
|---|
| [4d4882a] | 908 |
|
|---|
| [ce8c12f] | 909 | Type * t1 = getPointerBase( ftype->get_parameters().front()->get_type() );
|
|---|
| [0a267c1] | 910 | Type * t2 = ftype->parameters.back()->get_type();
|
|---|
| [ce8c12f] | 911 | assert( t1 );
|
|---|
| [4d4882a] | 912 |
|
|---|
| [ce8c12f] | 913 | if ( ResolvExpr::typesCompatibleIgnoreQualifiers( t1, t2, SymTab::Indexer() ) ) {
|
|---|
| [4d4882a] | 914 | return function;
|
|---|
| 915 | } else {
|
|---|
| [ee1635c8] | 916 | return nullptr;
|
|---|
| [4d4882a] | 917 | }
|
|---|
| 918 | }
|
|---|
| [ee1635c8] | 919 |
|
|---|
| [d76c588] | 920 | bool isCopyFunction( const ast::FunctionDecl * decl ) {
|
|---|
| 921 | const ast::FunctionType * ftype = decl->type;
|
|---|
| 922 | if ( ftype->params.size() != 2 ) return false;
|
|---|
| 923 |
|
|---|
| 924 | const ast::Type * t1 = getPointerBase( ftype->params.front()->get_type() );
|
|---|
| 925 | if ( ! t1 ) return false;
|
|---|
| 926 | const ast::Type * t2 = ftype->params.back()->get_type();
|
|---|
| 927 |
|
|---|
| 928 | return ResolvExpr::typesCompatibleIgnoreQualifiers( t1, t2, ast::SymbolTable{} );
|
|---|
| 929 | }
|
|---|
| 930 |
|
|---|
| [207c7e1d] | 931 | FunctionDecl * isAssignment( Declaration * decl ) {
|
|---|
| 932 | return isCopyFunction( decl, "?=?" );
|
|---|
| 933 | }
|
|---|
| 934 | FunctionDecl * isDestructor( Declaration * decl ) {
|
|---|
| 935 | if ( isDestructor( decl->get_name() ) ) {
|
|---|
| 936 | return dynamic_cast< FunctionDecl * >( decl );
|
|---|
| 937 | }
|
|---|
| 938 | return nullptr;
|
|---|
| 939 | }
|
|---|
| 940 | FunctionDecl * isDefaultConstructor( Declaration * decl ) {
|
|---|
| [0a267c1] | 941 | if ( isConstructor( decl->name ) ) {
|
|---|
| [207c7e1d] | 942 | if ( FunctionDecl * func = dynamic_cast< FunctionDecl * >( decl ) ) {
|
|---|
| [0a267c1] | 943 | if ( func->type->parameters.size() == 1 ) {
|
|---|
| [207c7e1d] | 944 | return func;
|
|---|
| 945 | }
|
|---|
| 946 | }
|
|---|
| 947 | }
|
|---|
| 948 | return nullptr;
|
|---|
| 949 | }
|
|---|
| [ee1635c8] | 950 | FunctionDecl * isCopyConstructor( Declaration * decl ) {
|
|---|
| 951 | return isCopyFunction( decl, "?{}" );
|
|---|
| 952 | }
|
|---|
| [2b46a13] | 953 | }
|
|---|