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