[972e6f7] | 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 | // Autogen.h --
|
---|
| 8 | //
|
---|
| 9 | // Author : Rob Schluntz
|
---|
| 10 | // Created On : Sun May 17 21:53:34 2015
|
---|
[c0aa336] | 11 | // Last Modified By : Peter A. Buhr
|
---|
[6b0b624] | 12 | // Last Modified On : Sat Jul 22 09:50:25 2017
|
---|
| 13 | // Update Count : 15
|
---|
[972e6f7] | 14 | //
|
---|
| 15 |
|
---|
[6b0b624] | 16 | #pragma once
|
---|
[972e6f7] | 17 |
|
---|
[30f9072] | 18 | #include <cassert> // for assert
|
---|
[b8524ca] | 19 | #include <iterator> // for back_inserter
|
---|
[d180746] | 20 | #include <string> // for string
|
---|
[30f9072] | 21 |
|
---|
[b8524ca] | 22 | #include "AST/Decl.hpp"
|
---|
[417117e] | 23 | #include "AST/Eval.hpp"
|
---|
[b8524ca] | 24 | #include "AST/Expr.hpp"
|
---|
| 25 | #include "AST/Init.hpp"
|
---|
| 26 | #include "AST/Node.hpp"
|
---|
| 27 | #include "AST/Stmt.hpp"
|
---|
| 28 | #include "AST/Type.hpp"
|
---|
[1a5ad8c] | 29 | #include "CodeGen/OperatorTable.h"
|
---|
[30f9072] | 30 | #include "Common/UniqueName.h" // for UniqueName
|
---|
[b8524ca] | 31 | #include "Common/utility.h" // for splice
|
---|
[30f9072] | 32 | #include "InitTweak/InitTweak.h" // for InitExpander
|
---|
| 33 | #include "SynTree/Constant.h" // for Constant
|
---|
[d180746] | 34 | #include "SynTree/Declaration.h" // for DeclarationWithType, ObjectDecl
|
---|
| 35 | #include "SynTree/Expression.h" // for NameExpr, ConstantExpr, UntypedExpr...
|
---|
[30f9072] | 36 | #include "SynTree/Type.h" // for Type, ArrayType, Type::Qualifiers
|
---|
[972e6f7] | 37 |
|
---|
[d180746] | 38 | class CompoundStmt;
|
---|
| 39 | class Statement;
|
---|
| 40 |
|
---|
[972e6f7] | 41 | namespace SymTab {
|
---|
[2be1023] | 42 | /// Generates assignment operators, constructors, and destructor for aggregate types as required
|
---|
| 43 | void autogenerateRoutines( std::list< Declaration * > &translationUnit );
|
---|
| 44 |
|
---|
| 45 | /// returns true if obj's name is the empty string and it has a bitfield width
|
---|
| 46 | bool isUnnamedBitfield( ObjectDecl * obj );
|
---|
[b8524ca] | 47 | bool isUnnamedBitfield( const ast::ObjectDecl * obj );
|
---|
[2be1023] | 48 |
|
---|
[837ce06] | 49 | /// generate the type of an assignment function for paramType.
|
---|
| 50 | /// maybePolymorphic is true if the resulting FunctionType is allowed to be polymorphic
|
---|
| 51 | FunctionType * genAssignType( Type * paramType, bool maybePolymorphic = true );
|
---|
[8a6cf7e] | 52 |
|
---|
[837ce06] | 53 | /// generate the type of a default constructor or destructor for paramType.
|
---|
| 54 | /// maybePolymorphic is true if the resulting FunctionType is allowed to be polymorphic
|
---|
| 55 | FunctionType * genDefaultType( Type * paramType, bool maybePolymorphic = true );
|
---|
[8404321] | 56 |
|
---|
[837ce06] | 57 | /// generate the type of a copy constructor for paramType.
|
---|
| 58 | /// maybePolymorphic is true if the resulting FunctionType is allowed to be polymorphic
|
---|
| 59 | FunctionType * genCopyType( Type * paramType, bool maybePolymorphic = true );
|
---|
[8404321] | 60 |
|
---|
[b8524ca] | 61 | /// Enum for loop direction
|
---|
| 62 | enum LoopDirection { LoopBackward, LoopForward };
|
---|
| 63 |
|
---|
[2be1023] | 64 | /// inserts into out a generated call expression to function fname with arguments dstParam and srcParam. Intended to be used with generated ?=?, ?{}, and ^?{} calls.
|
---|
| 65 | template< typename OutputIterator >
|
---|
[b8524ca] | 66 | Statement * genCall( InitTweak::InitExpander_old & srcParam, Expression * dstParam, const std::string & fname, OutputIterator out, Type * type, Type * addCast = nullptr, bool forward = true );
|
---|
| 67 |
|
---|
| 68 | template< typename OutIter >
|
---|
| 69 | ast::ptr< ast::Stmt > genCall(
|
---|
| 70 | InitTweak::InitExpander_new & srcParam, const ast::Expr * dstParam,
|
---|
| 71 | const CodeLocation & loc, const std::string & fname, OutIter && out,
|
---|
| 72 | const ast::Type * type, const ast::Type * addCast, LoopDirection forward = LoopForward );
|
---|
[2be1023] | 73 |
|
---|
| 74 | /// inserts into out a generated call expression to function fname with arguments dstParam and srcParam. Should only be called with non-array types.
|
---|
[f9cebb5] | 75 | /// optionally returns a statement which must be inserted prior to the containing loop, if there is one
|
---|
[2be1023] | 76 | template< typename OutputIterator >
|
---|
[b8524ca] | 77 | Statement * genScalarCall( InitTweak::InitExpander_old & srcParam, Expression * dstParam, std::string fname, OutputIterator out, Type * type, Type * addCast = nullptr ) {
|
---|
[1a5ad8c] | 78 | bool isReferenceCtorDtor = false;
|
---|
| 79 | if ( dynamic_cast< ReferenceType * >( type ) && CodeGen::isCtorDtor( fname ) ) {
|
---|
| 80 | // reference constructors are essentially application of the rebind operator.
|
---|
| 81 | // apply & to both arguments, do not need a cast
|
---|
| 82 | fname = "?=?";
|
---|
| 83 | dstParam = new AddressExpr( dstParam );
|
---|
[b95fe40] | 84 | addCast = nullptr;
|
---|
[1a5ad8c] | 85 | isReferenceCtorDtor = true;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[49148d5] | 88 | // want to be able to generate assignment, ctor, and dtor generically,
|
---|
| 89 | // so fname is either ?=?, ?{}, or ^?{}
|
---|
[1a5ad8c] | 90 | UntypedExpr * fExpr = new UntypedExpr( new NameExpr( fname ) );
|
---|
[49148d5] | 91 |
|
---|
| 92 | if ( addCast ) {
|
---|
| 93 | // cast to T& with qualifiers removed, so that qualified objects can be constructed
|
---|
| 94 | // and destructed with the same functions as non-qualified objects.
|
---|
| 95 | // unfortunately, lvalue is considered a qualifier. For AddressExpr to resolve, its argument
|
---|
| 96 | // must have an lvalue qualified type, so remove all qualifiers except lvalue. If we ever
|
---|
| 97 | // remove lvalue as a qualifier, this can change to
|
---|
| 98 | // type->get_qualifiers() = Type::Qualifiers();
|
---|
[b95fe40] | 99 | Type * castType = addCast->clone();
|
---|
[49148d5] | 100 | castType->get_qualifiers() -= Type::Qualifiers( Type::Lvalue | Type::Const | Type::Volatile | Type::Restrict | Type::Atomic );
|
---|
| 101 | // castType->set_lvalue( true ); // xxx - might not need this
|
---|
| 102 | dstParam = new CastExpr( dstParam, new ReferenceType( Type::Qualifiers(), castType ) );
|
---|
| 103 | }
|
---|
[1a5ad8c] | 104 | fExpr->args.push_back( dstParam );
|
---|
[2be1023] | 105 |
|
---|
[49148d5] | 106 | Statement * listInit = srcParam.buildListInit( fExpr );
|
---|
[39f84a4] | 107 |
|
---|
[1a5ad8c] | 108 | // fetch next set of arguments
|
---|
| 109 | ++srcParam;
|
---|
| 110 |
|
---|
| 111 | // return if adding reference fails - will happen on default constructor and destructor
|
---|
| 112 | if ( isReferenceCtorDtor && ! srcParam.addReference() ) {
|
---|
| 113 | delete fExpr;
|
---|
| 114 | return listInit;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | std::list< Expression * > args = *srcParam;
|
---|
| 118 | fExpr->args.splice( fExpr->args.end(), args );
|
---|
[4d2434a] | 119 |
|
---|
[ba3706f] | 120 | *out++ = new ExprStmt( fExpr );
|
---|
[4d2434a] | 121 |
|
---|
[49148d5] | 122 | srcParam.clearArrayIndices();
|
---|
[f9cebb5] | 123 |
|
---|
[49148d5] | 124 | return listInit;
|
---|
[2be1023] | 125 | }
|
---|
| 126 |
|
---|
[b8524ca] | 127 | /// inserts into out a generated call expression to function fname with arguments dstParam and
|
---|
| 128 | /// srcParam. Should only be called with non-array types.
|
---|
| 129 | /// optionally returns a statement which must be inserted prior to the containing loop, if
|
---|
| 130 | /// there is one
|
---|
| 131 | template< typename OutIter >
|
---|
| 132 | ast::ptr< ast::Stmt > genScalarCall(
|
---|
| 133 | InitTweak::InitExpander_new & srcParam, const ast::Expr * dstParam,
|
---|
| 134 | const CodeLocation & loc, std::string fname, OutIter && out, const ast::Type * type,
|
---|
| 135 | const ast::Type * addCast = nullptr
|
---|
| 136 | ) {
|
---|
| 137 | bool isReferenceCtorDtor = false;
|
---|
| 138 | if ( dynamic_cast< const ast::ReferenceType * >( type ) && CodeGen::isCtorDtor( fname ) ) {
|
---|
| 139 | // reference constructors are essentially application of the rebind operator.
|
---|
| 140 | // apply & to both arguments, do not need a cast
|
---|
| 141 | fname = "?=?";
|
---|
| 142 | dstParam = new ast::AddressExpr{ dstParam };
|
---|
| 143 | addCast = nullptr;
|
---|
| 144 | isReferenceCtorDtor = true;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | // want to be able to generate assignment, ctor, and dtor generically, so fname is one of
|
---|
| 148 | // "?=?", "?{}", or "^?{}"
|
---|
| 149 | ast::UntypedExpr * fExpr = new ast::UntypedExpr{ loc, new ast::NameExpr{ loc, fname } };
|
---|
| 150 |
|
---|
| 151 | if ( addCast ) {
|
---|
| 152 | // cast to T& with qualifiers removed, so that qualified objects can be constructed and
|
---|
| 153 | // destructed with the same functions as non-qualified objects. Unfortunately, lvalue
|
---|
| 154 | // is considered a qualifier - for AddressExpr to resolve, its argument must have an
|
---|
| 155 | // lvalue-qualified type, so remove all qualifiers except lvalue.
|
---|
| 156 | // xxx -- old code actually removed lvalue too...
|
---|
| 157 | ast::ptr< ast::Type > guard = addCast; // prevent castType from mutating addCast
|
---|
| 158 | ast::ptr< ast::Type > castType = addCast;
|
---|
| 159 | ast::remove_qualifiers(
|
---|
| 160 | castType,
|
---|
| 161 | ast::CV::Const | ast::CV::Volatile | ast::CV::Restrict | ast::CV::Atomic );
|
---|
| 162 | dstParam = new ast::CastExpr{ dstParam, new ast::ReferenceType{ castType } };
|
---|
| 163 | }
|
---|
| 164 | fExpr->args.emplace_back( dstParam );
|
---|
| 165 |
|
---|
| 166 | const ast::Stmt * listInit = srcParam.buildListInit( fExpr );
|
---|
| 167 |
|
---|
| 168 | // fetch next set of arguments
|
---|
| 169 | ++srcParam;
|
---|
| 170 |
|
---|
| 171 | // return if adding reference fails -- will happen on default ctor and dtor
|
---|
| 172 | if ( isReferenceCtorDtor && ! srcParam.addReference() ) return listInit;
|
---|
| 173 |
|
---|
| 174 | std::vector< ast::ptr< ast::Expr > > args = *srcParam;
|
---|
| 175 | splice( fExpr->args, args );
|
---|
| 176 |
|
---|
| 177 | *out++ = new ast::ExprStmt{ loc, fExpr };
|
---|
| 178 |
|
---|
| 179 | srcParam.clearArrayIndices();
|
---|
| 180 |
|
---|
| 181 | return listInit;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
[2be1023] | 184 | /// Store in out a loop which calls fname on each element of the array with srcParam and dstParam as arguments.
|
---|
| 185 | /// If forward is true, loop goes from 0 to N-1, else N-1 to 0
|
---|
| 186 | template< typename OutputIterator >
|
---|
[b8524ca] | 187 | void genArrayCall( InitTweak::InitExpander_old & srcParam, Expression *dstParam, const std::string & fname, OutputIterator out, ArrayType *array, Type * addCast = nullptr, bool forward = true ) {
|
---|
[2be1023] | 188 | static UniqueName indexName( "_index" );
|
---|
| 189 |
|
---|
| 190 | // for a flexible array member nothing is done -- user must define own assignment
|
---|
[b95fe40] | 191 | if ( ! array->get_dimension() ) return;
|
---|
| 192 |
|
---|
| 193 | if ( addCast ) {
|
---|
| 194 | // peel off array layer from cast
|
---|
| 195 | ArrayType * at = strict_dynamic_cast< ArrayType * >( addCast );
|
---|
| 196 | addCast = at->base;
|
---|
| 197 | }
|
---|
[2be1023] | 198 |
|
---|
| 199 | Expression * begin, * end, * update, * cmp;
|
---|
| 200 | if ( forward ) {
|
---|
[579263a] | 201 | // generate: for ( int i = 0; i < N; ++i )
|
---|
| 202 | begin = new ConstantExpr( Constant::from_int( 0 ) );
|
---|
[1a5ad8c] | 203 | end = array->dimension->clone();
|
---|
[2be1023] | 204 | cmp = new NameExpr( "?<?" );
|
---|
| 205 | update = new NameExpr( "++?" );
|
---|
| 206 | } else {
|
---|
| 207 | // generate: for ( int i = N-1; i >= 0; --i )
|
---|
| 208 | begin = new UntypedExpr( new NameExpr( "?-?" ) );
|
---|
[1a5ad8c] | 209 | ((UntypedExpr*)begin)->args.push_back( array->dimension->clone() );
|
---|
| 210 | ((UntypedExpr*)begin)->args.push_back( new ConstantExpr( Constant::from_int( 1 ) ) );
|
---|
[579263a] | 211 | end = new ConstantExpr( Constant::from_int( 0 ) );
|
---|
[2be1023] | 212 | cmp = new NameExpr( "?>=?" );
|
---|
| 213 | update = new NameExpr( "--?" );
|
---|
| 214 | }
|
---|
| 215 |
|
---|
[e4d829b] | 216 | ObjectDecl *index = new ObjectDecl( indexName.newName(), Type::StorageClasses(), LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), new SingleInit( begin ) );
|
---|
[2be1023] | 217 |
|
---|
| 218 | UntypedExpr *cond = new UntypedExpr( cmp );
|
---|
[1a5ad8c] | 219 | cond->args.push_back( new VariableExpr( index ) );
|
---|
| 220 | cond->args.push_back( end );
|
---|
[2be1023] | 221 |
|
---|
| 222 | UntypedExpr *inc = new UntypedExpr( update );
|
---|
[1a5ad8c] | 223 | inc->args.push_back( new VariableExpr( index ) );
|
---|
[2be1023] | 224 |
|
---|
| 225 | UntypedExpr *dstIndex = new UntypedExpr( new NameExpr( "?[?]" ) );
|
---|
[1a5ad8c] | 226 | dstIndex->args.push_back( dstParam );
|
---|
| 227 | dstIndex->args.push_back( new VariableExpr( index ) );
|
---|
[2be1023] | 228 | dstParam = dstIndex;
|
---|
| 229 |
|
---|
[39f84a4] | 230 | // srcParam must keep track of the array indices to build the
|
---|
| 231 | // source parameter and/or array list initializer
|
---|
[1a5ad8c] | 232 | srcParam.addArrayIndex( new VariableExpr( index ), array->dimension->clone() );
|
---|
[39f84a4] | 233 |
|
---|
[2be1023] | 234 | // for stmt's body, eventually containing call
|
---|
[ba3706f] | 235 | CompoundStmt * body = new CompoundStmt();
|
---|
[1a5ad8c] | 236 | Statement * listInit = genCall( srcParam, dstParam, fname, back_inserter( body->kids ), array->base, addCast, forward );
|
---|
[2be1023] | 237 |
|
---|
| 238 | // block containing for stmt and index variable
|
---|
| 239 | std::list<Statement *> initList;
|
---|
[ba3706f] | 240 | CompoundStmt * block = new CompoundStmt();
|
---|
| 241 | block->push_back( new DeclStmt( index ) );
|
---|
[f9cebb5] | 242 | if ( listInit ) block->get_kids().push_back( listInit );
|
---|
[ba3706f] | 243 | block->push_back( new ForStmt( initList, cond, inc, body ) );
|
---|
[2be1023] | 244 |
|
---|
| 245 | *out++ = block;
|
---|
| 246 | }
|
---|
| 247 |
|
---|
[b8524ca] | 248 | /// Store in out a loop which calls fname on each element of the array with srcParam and
|
---|
| 249 | /// dstParam as arguments. If forward is true, loop goes from 0 to N-1, else N-1 to 0
|
---|
| 250 | template< typename OutIter >
|
---|
| 251 | void genArrayCall(
|
---|
| 252 | InitTweak::InitExpander_new & srcParam, const ast::Expr * dstParam,
|
---|
| 253 | const CodeLocation & loc, const std::string & fname, OutIter && out,
|
---|
| 254 | const ast::ArrayType * array, const ast::Type * addCast = nullptr,
|
---|
| 255 | LoopDirection forward = LoopForward
|
---|
| 256 | ) {
|
---|
| 257 | static UniqueName indexName( "_index" );
|
---|
| 258 |
|
---|
| 259 | // for a flexible array member nothing is done -- user must define own assignment
|
---|
| 260 | if ( ! array->dimension ) return;
|
---|
| 261 |
|
---|
| 262 | if ( addCast ) {
|
---|
| 263 | // peel off array layer from cast
|
---|
| 264 | addCast = strict_dynamic_cast< const ast::ArrayType * >( addCast )->base;
|
---|
| 265 | }
|
---|
| 266 |
|
---|
[417117e] | 267 | ast::ptr< ast::Expr > begin, end;
|
---|
| 268 | std::string cmp, update;
|
---|
[b8524ca] | 269 |
|
---|
| 270 | if ( forward ) {
|
---|
| 271 | // generate: for ( int i = 0; i < N; ++i )
|
---|
| 272 | begin = ast::ConstantExpr::from_int( loc, 0 );
|
---|
| 273 | end = array->dimension;
|
---|
[417117e] | 274 | cmp = "?<?";
|
---|
| 275 | update = "++?";
|
---|
[b8524ca] | 276 | } else {
|
---|
| 277 | // generate: for ( int i = N-1; i >= 0; --i )
|
---|
[417117e] | 278 | begin = ast::call(
|
---|
| 279 | loc, "?-?", array->dimension, ast::ConstantExpr::from_int( loc, 1 ) );
|
---|
[b8524ca] | 280 | end = ast::ConstantExpr::from_int( loc, 0 );
|
---|
[417117e] | 281 | cmp = "?>=?";
|
---|
| 282 | update = "--?";
|
---|
[b8524ca] | 283 | }
|
---|
| 284 |
|
---|
| 285 | ast::ptr< ast::DeclWithType > index = new ast::ObjectDecl{
|
---|
| 286 | loc, indexName.newName(), new ast::BasicType{ ast::BasicType::SignedInt },
|
---|
| 287 | new ast::SingleInit{ loc, begin } };
|
---|
[417117e] | 288 | ast::ptr< ast::Expr > indexVar = new ast::VariableExpr{ loc, index };
|
---|
[b8524ca] | 289 |
|
---|
[417117e] | 290 | ast::ptr< ast::Expr > cond = ast::call( loc, cmp, indexVar, end );
|
---|
[b8524ca] | 291 |
|
---|
[417117e] | 292 | ast::ptr< ast::Expr > inc = ast::call( loc, update, indexVar );
|
---|
[b8524ca] | 293 |
|
---|
[417117e] | 294 | ast::ptr< ast::Expr > dstIndex = ast::call( loc, "?[?]", dstParam, indexVar );
|
---|
[b8524ca] | 295 |
|
---|
| 296 | // srcParam must keep track of the array indices to build the source parameter and/or
|
---|
| 297 | // array list initializer
|
---|
[417117e] | 298 | srcParam.addArrayIndex( indexVar, array->dimension );
|
---|
[b8524ca] | 299 |
|
---|
| 300 | // for stmt's body, eventually containing call
|
---|
| 301 | ast::CompoundStmt * body = new ast::CompoundStmt{ loc };
|
---|
| 302 | ast::ptr< ast::Stmt > listInit = genCall(
|
---|
| 303 | srcParam, dstIndex, loc, fname, std::back_inserter( body->kids ), array->base, addCast,
|
---|
| 304 | forward );
|
---|
| 305 |
|
---|
| 306 | // block containing the stmt and index variable
|
---|
| 307 | ast::CompoundStmt * block = new ast::CompoundStmt{ loc };
|
---|
| 308 | block->push_back( new ast::DeclStmt{ loc, index } );
|
---|
| 309 | if ( listInit ) { block->push_back( listInit ); }
|
---|
| 310 | block->push_back( new ast::ForStmt{ loc, {}, cond, inc, body } );
|
---|
| 311 |
|
---|
| 312 | *out++ = block;
|
---|
| 313 | }
|
---|
| 314 |
|
---|
[2be1023] | 315 | template< typename OutputIterator >
|
---|
[b8524ca] | 316 | Statement * genCall( InitTweak::InitExpander_old & srcParam, Expression * dstParam, const std::string & fname, OutputIterator out, Type * type, Type * addCast, bool forward ) {
|
---|
[2be1023] | 317 | if ( ArrayType * at = dynamic_cast< ArrayType * >( type ) ) {
|
---|
[4d2434a] | 318 | genArrayCall( srcParam, dstParam, fname, out, at, addCast, forward );
|
---|
[f9cebb5] | 319 | return 0;
|
---|
[2be1023] | 320 | } else {
|
---|
[f9cebb5] | 321 | return genScalarCall( srcParam, dstParam, fname, out, type, addCast );
|
---|
[2be1023] | 322 | }
|
---|
| 323 | }
|
---|
| 324 |
|
---|
[b8524ca] | 325 | template< typename OutIter >
|
---|
| 326 | ast::ptr< ast::Stmt > genCall(
|
---|
| 327 | InitTweak::InitExpander_new & srcParam, const ast::Expr * dstParam,
|
---|
| 328 | const CodeLocation & loc, const std::string & fname, OutIter && out,
|
---|
| 329 | const ast::Type * type, const ast::Type * addCast, LoopDirection forward
|
---|
| 330 | ) {
|
---|
| 331 | if ( auto at = dynamic_cast< const ast::ArrayType * >( type ) ) {
|
---|
| 332 | genArrayCall(
|
---|
| 333 | srcParam, dstParam, loc, fname, std::forward< OutIter >(out), at, addCast,
|
---|
| 334 | forward );
|
---|
| 335 | return {};
|
---|
| 336 | } else {
|
---|
| 337 | return genScalarCall(
|
---|
| 338 | srcParam, dstParam, loc, fname, std::forward< OutIter >( out ), type, addCast );
|
---|
| 339 | }
|
---|
| 340 | }
|
---|
| 341 |
|
---|
[2be1023] | 342 | /// inserts into out a generated call expression to function fname with arguments dstParam
|
---|
| 343 | /// and srcParam. Intended to be used with generated ?=?, ?{}, and ^?{} calls. decl is the
|
---|
| 344 | /// object being constructed. The function wraps constructor and destructor calls in an
|
---|
| 345 | /// ImplicitCtorDtorStmt node.
|
---|
| 346 | template< typename OutputIterator >
|
---|
[b8524ca] | 347 | void genImplicitCall( InitTweak::InitExpander_old & srcParam, Expression * dstParam, const std::string & fname, OutputIterator out, DeclarationWithType * decl, bool forward = true ) {
|
---|
[2be1023] | 348 | ObjectDecl *obj = dynamic_cast<ObjectDecl *>( decl );
|
---|
| 349 | assert( obj );
|
---|
| 350 | // unnamed bit fields are not copied as they cannot be accessed
|
---|
| 351 | if ( isUnnamedBitfield( obj ) ) return;
|
---|
| 352 |
|
---|
[b95fe40] | 353 | Type * addCast = nullptr;
|
---|
| 354 | if ( (fname == "?{}" || fname == "^?{}") && ( !obj || ( obj && ! obj->get_bitfieldWidth() ) ) ) {
|
---|
| 355 | assert( dstParam->result );
|
---|
| 356 | addCast = dstParam->result;
|
---|
| 357 | }
|
---|
[2be1023] | 358 | std::list< Statement * > stmts;
|
---|
[1a5ad8c] | 359 | genCall( srcParam, dstParam, fname, back_inserter( stmts ), obj->type, addCast, forward );
|
---|
[2be1023] | 360 |
|
---|
[6cf27a07] | 361 | // currently genCall should produce at most one element, but if that changes then the next line needs to be updated to grab the statement which contains the call
|
---|
| 362 | assert( stmts.size() <= 1 );
|
---|
[f9cebb5] | 363 | if ( stmts.size() == 1 ) {
|
---|
| 364 | Statement * callStmt = stmts.front();
|
---|
| 365 | if ( addCast ) {
|
---|
| 366 | // implicitly generated ctor/dtor calls should be wrapped
|
---|
| 367 | // so that later passes are aware they were generated.
|
---|
| 368 | // xxx - don't mark as an implicit ctor/dtor if obj is a bitfield,
|
---|
| 369 | // because this causes the address to be taken at codegen, which is illegal in C.
|
---|
| 370 | callStmt = new ImplicitCtorDtorStmt( callStmt );
|
---|
| 371 | }
|
---|
| 372 | *out++ = callStmt;
|
---|
| 373 | }
|
---|
[2be1023] | 374 | }
|
---|
[b8524ca] | 375 |
|
---|
| 376 | static inline ast::ptr< ast::Stmt > genImplicitCall(
|
---|
| 377 | InitTweak::InitExpander_new & srcParam, const ast::Expr * dstParam,
|
---|
| 378 | const CodeLocation & loc, const std::string & fname, const ast::ObjectDecl * obj,
|
---|
| 379 | LoopDirection forward = LoopForward
|
---|
| 380 | ) {
|
---|
| 381 | // unnamed bit fields are not copied as they cannot be accessed
|
---|
| 382 | if ( isUnnamedBitfield( obj ) ) return {};
|
---|
| 383 |
|
---|
[417117e] | 384 | ast::ptr< ast::Type > addCast;
|
---|
[b8524ca] | 385 | if ( (fname == "?{}" || fname == "^?{}") && ( ! obj || ( obj && ! obj->bitfieldWidth ) ) ) {
|
---|
| 386 | assert( dstParam->result );
|
---|
| 387 | addCast = dstParam->result;
|
---|
| 388 | }
|
---|
| 389 |
|
---|
| 390 | std::vector< ast::ptr< ast::Stmt > > stmts;
|
---|
| 391 | genCall(
|
---|
| 392 | srcParam, dstParam, loc, fname, back_inserter( stmts ), obj->type, addCast, forward );
|
---|
| 393 |
|
---|
| 394 | if ( stmts.empty() ) {
|
---|
| 395 | return {};
|
---|
| 396 | } else if ( stmts.size() == 1 ) {
|
---|
| 397 | const ast::Stmt * callStmt = stmts.front();
|
---|
| 398 | if ( addCast ) {
|
---|
| 399 | // implicitly generated ctor/dtor calls should be wrapped so that later passes are
|
---|
| 400 | // aware they were generated.
|
---|
| 401 | callStmt = new ast::ImplicitCtorDtorStmt{ callStmt->location, callStmt };
|
---|
| 402 | }
|
---|
| 403 | return callStmt;
|
---|
| 404 | } else {
|
---|
| 405 | assert( false );
|
---|
| 406 | return {};
|
---|
| 407 | }
|
---|
| 408 | }
|
---|
[972e6f7] | 409 | } // namespace SymTab
|
---|
[6b0b624] | 410 |
|
---|
| 411 | // Local Variables: //
|
---|
| 412 | // tab-width: 4 //
|
---|
| 413 | // mode: c++ //
|
---|
| 414 | // compile-command: "make install" //
|
---|
| 415 | // End: //
|
---|
| 416 |
|
---|