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