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