[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.cc -- |
---|
| 8 | // |
---|
| 9 | // Author : Rob Schluntz |
---|
| 10 | // Created On : Thu Mar 03 15:45:56 2016 |
---|
[6926a6d] | 11 | // Last Modified By : Peter A. Buhr |
---|
| 12 | // Last Modified On : Fri Apr 27 14:39:06 2018 |
---|
| 13 | // Update Count : 63 |
---|
[972e6f7] | 14 | // |
---|
| 15 | |
---|
| 16 | #include "Autogen.h" |
---|
[30f9072] | 17 | |
---|
| 18 | #include <algorithm> // for count_if |
---|
[e3e16bc] | 19 | #include <cassert> // for strict_dynamic_cast, assert, assertf |
---|
[30f9072] | 20 | #include <iterator> // for back_insert_iterator, back_inserter |
---|
| 21 | #include <list> // for list, _List_iterator, list<>::iter... |
---|
| 22 | #include <set> // for set, _Rb_tree_const_iterator |
---|
[be9288a] | 23 | #include <utility> // for pair |
---|
[30f9072] | 24 | #include <vector> // for vector |
---|
| 25 | |
---|
[9236060] | 26 | #include "CodeGen/OperatorTable.h" // for isCtorDtor, isCtorDtorAssign |
---|
[e4d6335] | 27 | #include "Common/PassVisitor.h" // for PassVisitor |
---|
[30f9072] | 28 | #include "Common/ScopedMap.h" // for ScopedMap<>::const_iterator, Scope... |
---|
| 29 | #include "Common/utility.h" // for cloneAll, operator+ |
---|
| 30 | #include "GenPoly/ScopedSet.h" // for ScopedSet, ScopedSet<>::iterator |
---|
[8b11840] | 31 | #include "InitTweak/GenInit.h" // for fixReturnStatements |
---|
| 32 | #include "ResolvExpr/Resolver.h" // for resolveDecl |
---|
[30f9072] | 33 | #include "SymTab/Mangler.h" // for Mangler |
---|
[9236060] | 34 | #include "SynTree/Attribute.h" // For Attribute |
---|
[30f9072] | 35 | #include "SynTree/Mutator.h" // for maybeMutate |
---|
| 36 | #include "SynTree/Statement.h" // for CompoundStmt, ReturnStmt, ExprStmt |
---|
| 37 | #include "SynTree/Type.h" // for FunctionType, Type, TypeInstType |
---|
| 38 | #include "SynTree/Visitor.h" // for maybeAccept, Visitor, acceptAll |
---|
| 39 | |
---|
| 40 | class Attribute; |
---|
[972e6f7] | 41 | |
---|
| 42 | namespace SymTab { |
---|
[a117f7c] | 43 | /// Data used to generate functions generically. Specifically, the name of the generated function and a function which generates the routine protoype |
---|
[207c7e1d] | 44 | struct FuncData { |
---|
[837ce06] | 45 | typedef FunctionType * (*TypeGen)( Type *, bool ); |
---|
[a117f7c] | 46 | FuncData( const std::string & fname, const TypeGen & genType ) : fname( fname ), genType( genType ) {} |
---|
[207c7e1d] | 47 | std::string fname; |
---|
| 48 | TypeGen genType; |
---|
| 49 | }; |
---|
[5f98ce5] | 50 | |
---|
[189d800] | 51 | struct AutogenerateRoutines final : public WithDeclsToAdd, public WithVisitorRef<AutogenerateRoutines>, public WithGuards, public WithShortCircuiting, public WithIndexer { |
---|
[207c7e1d] | 52 | AutogenerateRoutines(); |
---|
| 53 | |
---|
[e4d6335] | 54 | void previsit( EnumDecl * enumDecl ); |
---|
| 55 | void previsit( StructDecl * structDecl ); |
---|
| 56 | void previsit( UnionDecl * structDecl ); |
---|
| 57 | void previsit( TypeDecl * typeDecl ); |
---|
| 58 | void previsit( TraitDecl * traitDecl ); |
---|
| 59 | void previsit( FunctionDecl * functionDecl ); |
---|
[972e6f7] | 60 | |
---|
[e4d6335] | 61 | void previsit( CompoundStmt * compoundStmt ); |
---|
[972e6f7] | 62 | |
---|
[1486116] | 63 | private: |
---|
[a117f7c] | 64 | |
---|
[e4d6335] | 65 | GenPoly::ScopedSet< std::string > structsDone; |
---|
[1486116] | 66 | unsigned int functionNesting = 0; // current level of nested functions |
---|
[a117f7c] | 67 | |
---|
[207c7e1d] | 68 | std::vector< FuncData > data; |
---|
[1486116] | 69 | }; |
---|
| 70 | |
---|
| 71 | /// generates routines for tuple types. |
---|
[ac74057] | 72 | struct AutogenTupleRoutines : public WithDeclsToAdd, public WithVisitorRef<AutogenTupleRoutines>, public WithGuards, public WithShortCircuiting { |
---|
[a117f7c] | 73 | void previsit( FunctionDecl * functionDecl ); |
---|
[1486116] | 74 | |
---|
[a117f7c] | 75 | void postvisit( TupleType * tupleType ); |
---|
[1486116] | 76 | |
---|
[a117f7c] | 77 | void previsit( CompoundStmt * compoundStmt ); |
---|
[1486116] | 78 | |
---|
| 79 | private: |
---|
| 80 | unsigned int functionNesting = 0; // current level of nested functions |
---|
| 81 | GenPoly::ScopedSet< std::string > seenTuples; |
---|
[972e6f7] | 82 | }; |
---|
| 83 | |
---|
| 84 | void autogenerateRoutines( std::list< Declaration * > &translationUnit ) { |
---|
[e4d6335] | 85 | PassVisitor<AutogenerateRoutines> generator; |
---|
| 86 | acceptAll( translationUnit, generator ); |
---|
[1486116] | 87 | |
---|
| 88 | // needs to be done separately because AutogenerateRoutines skips types that appear as function arguments, etc. |
---|
| 89 | // AutogenTupleRoutines tupleGenerator; |
---|
[ac74057] | 90 | // acceptAll( translationUnit, tupleGenerator ); |
---|
[972e6f7] | 91 | } |
---|
| 92 | |
---|
[a117f7c] | 93 | //============================================================================================= |
---|
| 94 | // FuncGenerator definitions |
---|
| 95 | //============================================================================================= |
---|
| 96 | class FuncGenerator { |
---|
| 97 | public: |
---|
| 98 | std::list< Declaration * > definitions, forwards; |
---|
| 99 | |
---|
[0a267c1] | 100 | FuncGenerator( Type * type, const std::vector< FuncData > & data, unsigned int functionNesting, SymTab::Indexer & indexer ) : type( type ), data( data ), functionNesting( functionNesting ), indexer( indexer ) {} |
---|
[a117f7c] | 101 | |
---|
| 102 | virtual bool shouldAutogen() const = 0; |
---|
| 103 | void genStandardFuncs(); |
---|
| 104 | virtual void genFieldCtors() = 0; |
---|
| 105 | protected: |
---|
| 106 | Type * type; |
---|
| 107 | const std::vector< FuncData > & data; |
---|
[0a267c1] | 108 | unsigned int functionNesting; |
---|
[a117f7c] | 109 | SymTab::Indexer & indexer; |
---|
| 110 | |
---|
| 111 | virtual void genFuncBody( FunctionDecl * dcl ) = 0; |
---|
| 112 | virtual bool isConcurrentType() const = 0; |
---|
| 113 | |
---|
| 114 | void resolve( FunctionDecl * dcl ); |
---|
| 115 | void generatePrototypes( std::list< FunctionDecl * > & newFuncs ); |
---|
| 116 | }; |
---|
| 117 | |
---|
| 118 | class StructFuncGenerator : public FuncGenerator { |
---|
| 119 | StructDecl * aggregateDecl; |
---|
| 120 | public: |
---|
[0a267c1] | 121 | StructFuncGenerator( StructDecl * aggregateDecl, StructInstType * refType, const std::vector< FuncData > & data, unsigned int functionNesting, SymTab::Indexer & indexer ) : FuncGenerator( refType, data, functionNesting, indexer ), aggregateDecl( aggregateDecl) {} |
---|
[a117f7c] | 122 | |
---|
| 123 | virtual bool shouldAutogen() const override; |
---|
| 124 | virtual bool isConcurrentType() const override; |
---|
| 125 | |
---|
| 126 | virtual void genFuncBody( FunctionDecl * dcl ) override; |
---|
| 127 | virtual void genFieldCtors() override; |
---|
| 128 | |
---|
| 129 | private: |
---|
| 130 | /// generates a single struct member operation (constructor call, destructor call, assignment call) |
---|
| 131 | void makeMemberOp( ObjectDecl * dstParam, Expression * src, DeclarationWithType * field, FunctionDecl * func, bool forward = true ); |
---|
| 132 | |
---|
| 133 | /// generates the body of a struct function by iterating the struct members (via parameters) - generates default ctor, copy ctor, assignment, and dtor bodies, but NOT field ctor bodies |
---|
| 134 | template<typename Iterator> |
---|
| 135 | void makeFunctionBody( Iterator member, Iterator end, FunctionDecl * func, bool forward = true ); |
---|
| 136 | |
---|
| 137 | /// generate the body of a constructor which takes parameters that match fields, e.g. |
---|
| 138 | /// void ?{}(A *, int) and void?{}(A *, int, int) for a struct A which has two int fields. |
---|
| 139 | template<typename Iterator> |
---|
| 140 | void makeFieldCtorBody( Iterator member, Iterator end, FunctionDecl * func ); |
---|
| 141 | }; |
---|
| 142 | |
---|
[0a267c1] | 143 | class UnionFuncGenerator : public FuncGenerator { |
---|
| 144 | UnionDecl * aggregateDecl; |
---|
| 145 | public: |
---|
| 146 | UnionFuncGenerator( UnionDecl * aggregateDecl, UnionInstType * refType, const std::vector< FuncData > & data, unsigned int functionNesting, SymTab::Indexer & indexer ) : FuncGenerator( refType, data, functionNesting, indexer ), aggregateDecl( aggregateDecl) {} |
---|
| 147 | |
---|
| 148 | virtual bool shouldAutogen() const override; |
---|
| 149 | virtual bool isConcurrentType() const override; |
---|
| 150 | |
---|
| 151 | virtual void genFuncBody( FunctionDecl * dcl ) override; |
---|
| 152 | virtual void genFieldCtors() override; |
---|
| 153 | |
---|
| 154 | private: |
---|
| 155 | /// generates a single struct member operation (constructor call, destructor call, assignment call) |
---|
| 156 | template<typename OutputIterator> |
---|
| 157 | void makeMemberOp( ObjectDecl * srcParam, ObjectDecl * dstParam, OutputIterator out ); |
---|
| 158 | |
---|
| 159 | /// generates the body of a struct function by iterating the struct members (via parameters) - generates default ctor, copy ctor, assignment, and dtor bodies, but NOT field ctor bodies |
---|
| 160 | template<typename Iterator> |
---|
| 161 | void makeFunctionBody( Iterator member, Iterator end, FunctionDecl * func, bool forward = true ); |
---|
| 162 | |
---|
| 163 | /// generate the body of a constructor which takes parameters that match fields, e.g. |
---|
| 164 | /// void ?{}(A *, int) and void?{}(A *, int, int) for a struct A which has two int fields. |
---|
| 165 | template<typename Iterator> |
---|
| 166 | void makeFieldCtorBody( Iterator member, Iterator end, FunctionDecl * func ); |
---|
| 167 | }; |
---|
| 168 | |
---|
[18ca28e] | 169 | class EnumFuncGenerator : public FuncGenerator { |
---|
| 170 | public: |
---|
| 171 | EnumFuncGenerator( EnumInstType * refType, const std::vector< FuncData > & data, unsigned int functionNesting, SymTab::Indexer & indexer ) : FuncGenerator( refType, data, functionNesting, indexer ) {} |
---|
| 172 | |
---|
| 173 | virtual bool shouldAutogen() const override; |
---|
| 174 | virtual bool isConcurrentType() const override; |
---|
| 175 | |
---|
| 176 | virtual void genFuncBody( FunctionDecl * dcl ) override; |
---|
| 177 | virtual void genFieldCtors() override; |
---|
| 178 | |
---|
| 179 | private: |
---|
| 180 | }; |
---|
| 181 | |
---|
[a117f7c] | 182 | class TypeFuncGenerator : public FuncGenerator { |
---|
| 183 | TypeDecl * typeDecl; |
---|
| 184 | public: |
---|
[0a267c1] | 185 | TypeFuncGenerator( TypeDecl * typeDecl, TypeInstType * refType, const std::vector<FuncData> & data, unsigned int functionNesting, SymTab::Indexer & indexer ) : FuncGenerator( refType, data, functionNesting, indexer ), typeDecl( typeDecl ) {} |
---|
[a117f7c] | 186 | |
---|
| 187 | virtual bool shouldAutogen() const override; |
---|
| 188 | virtual void genFuncBody( FunctionDecl * dcl ) override; |
---|
| 189 | virtual bool isConcurrentType() const override; |
---|
| 190 | virtual void genFieldCtors() override; |
---|
| 191 | }; |
---|
| 192 | |
---|
| 193 | //============================================================================================= |
---|
| 194 | // helper functions |
---|
| 195 | //============================================================================================= |
---|
| 196 | void generateFunctions( FuncGenerator & gen, std::list< Declaration * > & declsToAdd ) { |
---|
| 197 | if ( ! gen.shouldAutogen() ) return; |
---|
| 198 | |
---|
| 199 | // generate each of the functions based on the supplied FuncData objects |
---|
| 200 | gen.genStandardFuncs(); |
---|
| 201 | gen.genFieldCtors(); |
---|
| 202 | |
---|
| 203 | declsToAdd.splice( declsToAdd.end(), gen.forwards ); |
---|
| 204 | declsToAdd.splice( declsToAdd.end(), gen.definitions ); |
---|
| 205 | } |
---|
| 206 | |
---|
[356189a] | 207 | bool isUnnamedBitfield( ObjectDecl * obj ) { |
---|
[f30b2610] | 208 | return obj != nullptr && obj->name == "" && obj->bitfieldWidth != nullptr; |
---|
[356189a] | 209 | } |
---|
| 210 | |
---|
[186fd86] | 211 | /// inserts a forward declaration for functionDecl into declsToAdd |
---|
| 212 | void addForwardDecl( FunctionDecl * functionDecl, std::list< Declaration * > & declsToAdd ) { |
---|
| 213 | FunctionDecl * decl = functionDecl->clone(); |
---|
[f30b2610] | 214 | delete decl->statements; |
---|
| 215 | decl->statements = nullptr; |
---|
[186fd86] | 216 | declsToAdd.push_back( decl ); |
---|
| 217 | decl->fixUniqueId(); |
---|
[972e6f7] | 218 | } |
---|
| 219 | |
---|
[f30b2610] | 220 | const std::list< TypeDecl * > getGenericParams( Type * t ) { |
---|
| 221 | std::list< TypeDecl * > * ret = nullptr; |
---|
| 222 | if ( StructInstType * inst = dynamic_cast< StructInstType * > ( t ) ) { |
---|
| 223 | ret = inst->get_baseParameters(); |
---|
| 224 | } else if ( UnionInstType * inst = dynamic_cast< UnionInstType * >( t ) ) { |
---|
| 225 | ret = inst->get_baseParameters(); |
---|
| 226 | } |
---|
| 227 | return ret ? *ret : std::list< TypeDecl * >(); |
---|
| 228 | } |
---|
| 229 | |
---|
[186fd86] | 230 | /// given type T, generate type of default ctor/dtor, i.e. function type void (*) (T *) |
---|
[837ce06] | 231 | FunctionType * genDefaultType( Type * paramType, bool maybePolymorphic ) { |
---|
[186fd86] | 232 | FunctionType *ftype = new FunctionType( Type::Qualifiers(), false ); |
---|
[837ce06] | 233 | if ( maybePolymorphic ) { |
---|
| 234 | // only copy in |
---|
| 235 | const auto & typeParams = getGenericParams( paramType ); |
---|
| 236 | cloneAll( typeParams, ftype->forall ); |
---|
| 237 | } |
---|
[49148d5] | 238 | ObjectDecl *dstParam = new ObjectDecl( "_dst", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new ReferenceType( Type::Qualifiers(), paramType->clone() ), nullptr ); |
---|
[f30b2610] | 239 | ftype->parameters.push_back( dstParam ); |
---|
[186fd86] | 240 | return ftype; |
---|
| 241 | } |
---|
[d0f8b19] | 242 | |
---|
[186fd86] | 243 | /// given type T, generate type of copy ctor, i.e. function type void (*) (T *, T) |
---|
[837ce06] | 244 | FunctionType * genCopyType( Type * paramType, bool maybePolymorphic ) { |
---|
| 245 | FunctionType *ftype = genDefaultType( paramType, maybePolymorphic ); |
---|
[68fe077a] | 246 | ObjectDecl *srcParam = new ObjectDecl( "_src", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, paramType->clone(), nullptr ); |
---|
[f30b2610] | 247 | ftype->parameters.push_back( srcParam ); |
---|
[186fd86] | 248 | return ftype; |
---|
| 249 | } |
---|
[d0f8b19] | 250 | |
---|
[186fd86] | 251 | /// given type T, generate type of assignment, i.e. function type T (*) (T *, T) |
---|
[837ce06] | 252 | FunctionType * genAssignType( Type * paramType, bool maybePolymorphic ) { |
---|
| 253 | FunctionType *ftype = genCopyType( paramType, maybePolymorphic ); |
---|
[68fe077a] | 254 | ObjectDecl *returnVal = new ObjectDecl( "_ret", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, paramType->clone(), nullptr ); |
---|
[f30b2610] | 255 | ftype->returnVals.push_back( returnVal ); |
---|
[186fd86] | 256 | return ftype; |
---|
| 257 | } |
---|
[972e6f7] | 258 | |
---|
[186fd86] | 259 | /// generate a function decl from a name and type. Nesting depth determines whether |
---|
| 260 | /// the declaration is static or not; optional paramter determines if declaration is intrinsic |
---|
| 261 | FunctionDecl * genFunc( const std::string & fname, FunctionType * ftype, unsigned int functionNesting, bool isIntrinsic = false ) { |
---|
| 262 | // Routines at global scope marked "static" to prevent multiple definitions in separate translation units |
---|
[972e6f7] | 263 | // because each unit generates copies of the default routines for each aggregate. |
---|
[68fe077a] | 264 | Type::StorageClasses scs = functionNesting > 0 ? Type::StorageClasses() : Type::StorageClasses( Type::Static ); |
---|
[186fd86] | 265 | LinkageSpec::Spec spec = isIntrinsic ? LinkageSpec::Intrinsic : LinkageSpec::AutoGen; |
---|
[ba3706f] | 266 | FunctionDecl * decl = new FunctionDecl( fname, scs, spec, ftype, new CompoundStmt(), |
---|
[ddfd945] | 267 | std::list< Attribute * >(), Type::FuncSpecifiers( Type::Inline ) ); |
---|
[186fd86] | 268 | decl->fixUniqueId(); |
---|
| 269 | return decl; |
---|
| 270 | } |
---|
[d0f8b19] | 271 | |
---|
[a117f7c] | 272 | Type * declToType( Declaration * decl ) { |
---|
| 273 | if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) { |
---|
| 274 | return dwt->get_type(); |
---|
| 275 | } |
---|
| 276 | return nullptr; |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | Type * declToTypeDeclBase( Declaration * decl ) { |
---|
| 280 | if ( TypeDecl * td = dynamic_cast< TypeDecl * >( decl ) ) { |
---|
| 281 | return td->base; |
---|
| 282 | } |
---|
| 283 | return nullptr; |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | //============================================================================================= |
---|
| 287 | // FuncGenerator member definitions |
---|
| 288 | //============================================================================================= |
---|
| 289 | void FuncGenerator::genStandardFuncs() { |
---|
| 290 | std::list< FunctionDecl * > newFuncs; |
---|
| 291 | generatePrototypes( newFuncs ); |
---|
| 292 | |
---|
| 293 | for ( FunctionDecl * dcl : newFuncs ) { |
---|
| 294 | genFuncBody( dcl ); |
---|
[f232977] | 295 | if ( CodeGen::isAssignment( dcl->name ) ) { |
---|
| 296 | // assignment needs to return a value |
---|
| 297 | FunctionType * assignType = dcl->type; |
---|
| 298 | assert( assignType->parameters.size() == 2 ); |
---|
| 299 | assert( assignType->returnVals.size() == 1 ); |
---|
| 300 | ObjectDecl * dstParam = strict_dynamic_cast< ObjectDecl * >( assignType->parameters.front() ); |
---|
[ba3706f] | 301 | dcl->statements->push_back( new ReturnStmt( new VariableExpr( dstParam ) ) ); |
---|
[f232977] | 302 | } |
---|
[a117f7c] | 303 | resolve( dcl ); |
---|
| 304 | } |
---|
| 305 | } |
---|
| 306 | |
---|
| 307 | void FuncGenerator::generatePrototypes( std::list< FunctionDecl * > & newFuncs ) { |
---|
| 308 | bool concurrent_type = isConcurrentType(); |
---|
[e220391] | 309 | for ( const FuncData & d : data ) { |
---|
[a117f7c] | 310 | // generate a function (?{}, ?=?, ^?{}) based on the current FuncData. |
---|
[4ee1efb] | 311 | FunctionType * ftype = d.genType( type, true ); |
---|
[a117f7c] | 312 | |
---|
| 313 | // destructor for concurrent type must be mutex |
---|
[e220391] | 314 | if ( concurrent_type && CodeGen::isDestructor( d.fname ) ) { |
---|
[a117f7c] | 315 | ftype->parameters.front()->get_type()->set_mutex( true ); |
---|
| 316 | } |
---|
| 317 | |
---|
[e220391] | 318 | newFuncs.push_back( genFunc( d.fname, ftype, functionNesting ) ); |
---|
[a117f7c] | 319 | } |
---|
| 320 | } |
---|
| 321 | |
---|
| 322 | void FuncGenerator::resolve( FunctionDecl * dcl ) { |
---|
| 323 | try { |
---|
| 324 | ResolvExpr::resolveDecl( dcl, indexer ); |
---|
| 325 | if ( functionNesting == 0 ) { |
---|
| 326 | // forward declare if top-level struct, so that |
---|
| 327 | // type is complete as soon as its body ends |
---|
| 328 | // Note: this is necessary if we want structs which contain |
---|
| 329 | // generic (otype) structs as members. |
---|
| 330 | addForwardDecl( dcl, forwards ); |
---|
| 331 | } |
---|
| 332 | definitions.push_back( dcl ); |
---|
| 333 | indexer.addId( dcl ); |
---|
[6926a6d] | 334 | } catch ( SemanticErrorException & ) { |
---|
[a117f7c] | 335 | // okay if decl does not resolve - that means the function should not be generated |
---|
| 336 | delete dcl; |
---|
| 337 | } |
---|
| 338 | } |
---|
| 339 | |
---|
| 340 | bool StructFuncGenerator::shouldAutogen() const { |
---|
| 341 | // Builtins do not use autogeneration. |
---|
| 342 | return ! aggregateDecl->linkage.is_builtin; |
---|
| 343 | } |
---|
| 344 | bool StructFuncGenerator::isConcurrentType() const { return aggregateDecl->is_thread() || aggregateDecl->is_monitor(); } |
---|
| 345 | |
---|
| 346 | void StructFuncGenerator::genFuncBody( FunctionDecl * dcl ) { |
---|
| 347 | // generate appropriate calls to member ctor, assignment |
---|
| 348 | // destructor needs to do everything in reverse, so pass "forward" based on whether the function is a destructor |
---|
[f30b2610] | 349 | if ( ! CodeGen::isDestructor( dcl->name ) ) { |
---|
[a117f7c] | 350 | makeFunctionBody( aggregateDecl->members.begin(), aggregateDecl->members.end(), dcl ); |
---|
| 351 | } else { |
---|
| 352 | makeFunctionBody( aggregateDecl->members.rbegin(), aggregateDecl->members.rend(), dcl, false ); |
---|
| 353 | } |
---|
| 354 | } |
---|
| 355 | |
---|
| 356 | void StructFuncGenerator::genFieldCtors() { |
---|
| 357 | // field ctors are only generated if default constructor and copy constructor are both generated |
---|
[f30b2610] | 358 | unsigned numCtors = std::count_if( definitions.begin(), definitions.end(), [](Declaration * dcl) { return CodeGen::isConstructor( dcl->name ); } ); |
---|
[a117f7c] | 359 | |
---|
[0a267c1] | 360 | // Field constructors are only generated if default and copy constructor |
---|
| 361 | // are generated, since they need access to both |
---|
| 362 | if ( numCtors != 2 ) return; |
---|
| 363 | |
---|
[a117f7c] | 364 | // create constructors which take each member type as a parameter. |
---|
| 365 | // for example, for struct A { int x, y; }; generate |
---|
| 366 | // void ?{}(A *, int) and void ?{}(A *, int, int) |
---|
[0a267c1] | 367 | FunctionType * memCtorType = genDefaultType( type ); |
---|
| 368 | for ( Declaration * member : aggregateDecl->members ) { |
---|
| 369 | DeclarationWithType * field = strict_dynamic_cast<DeclarationWithType *>( member ); |
---|
| 370 | if ( isUnnamedBitfield( dynamic_cast< ObjectDecl * > ( field ) ) ) { |
---|
| 371 | // don't make a function whose parameter is an unnamed bitfield |
---|
| 372 | continue; |
---|
[a117f7c] | 373 | } |
---|
[1f370451] | 374 | // do not carry over field's attributes to parameter type |
---|
| 375 | Type * paramType = field->get_type()->clone(); |
---|
| 376 | deleteAll( paramType->attributes ); |
---|
| 377 | paramType->attributes.clear(); |
---|
| 378 | // add a parameter corresponding to this field |
---|
[54c9000] | 379 | ObjectDecl * param = new ObjectDecl( field->name, Type::StorageClasses(), LinkageSpec::Cforall, nullptr, paramType, nullptr ); |
---|
| 380 | cloneAll_if( field->attributes, param->attributes, [](Attribute * attr) { return attr->isValidOnFuncParam(); } ); |
---|
| 381 | memCtorType->parameters.push_back( param ); |
---|
[0a267c1] | 382 | FunctionDecl * ctor = genFunc( "?{}", memCtorType->clone(), functionNesting ); |
---|
| 383 | makeFieldCtorBody( aggregateDecl->members.begin(), aggregateDecl->members.end(), ctor ); |
---|
| 384 | resolve( ctor ); |
---|
[a117f7c] | 385 | } |
---|
[0a267c1] | 386 | delete memCtorType; |
---|
[a117f7c] | 387 | } |
---|
| 388 | |
---|
| 389 | void StructFuncGenerator::makeMemberOp( ObjectDecl * dstParam, Expression * src, DeclarationWithType * field, FunctionDecl * func, bool forward ) { |
---|
[39f84a4] | 390 | InitTweak::InitExpander srcParam( src ); |
---|
| 391 | |
---|
[49148d5] | 392 | // assign to destination |
---|
[f30b2610] | 393 | Expression *dstselect = new MemberExpr( field, new CastExpr( new VariableExpr( dstParam ), strict_dynamic_cast< ReferenceType* >( dstParam->get_type() )->base->clone() ) ); |
---|
| 394 | genImplicitCall( srcParam, dstselect, func->name, back_inserter( func->statements->kids ), field, forward ); |
---|
[dac0aa9] | 395 | } |
---|
| 396 | |
---|
[972e6f7] | 397 | template<typename Iterator> |
---|
[a117f7c] | 398 | void StructFuncGenerator::makeFunctionBody( Iterator member, Iterator end, FunctionDecl * func, bool forward ) { |
---|
[972e6f7] | 399 | for ( ; member != end; ++member ) { |
---|
[dac0aa9] | 400 | if ( DeclarationWithType *field = dynamic_cast< DeclarationWithType * >( *member ) ) { // otherwise some form of type declaration, e.g. Aggregate |
---|
[972e6f7] | 401 | // query the type qualifiers of this field and skip assigning it if it is marked const. |
---|
| 402 | // If it is an array type, we need to strip off the array layers to find its qualifiers. |
---|
[dac0aa9] | 403 | Type * type = field->get_type(); |
---|
[972e6f7] | 404 | while ( ArrayType * at = dynamic_cast< ArrayType * >( type ) ) { |
---|
| 405 | type = at->get_base(); |
---|
| 406 | } |
---|
| 407 | |
---|
[6fc5c14] | 408 | if ( type->get_const() && CodeGen::isAssignment( func->name ) ) { |
---|
[cad355a] | 409 | // don't assign const members, but do construct/destruct |
---|
[972e6f7] | 410 | continue; |
---|
| 411 | } |
---|
| 412 | |
---|
| 413 | assert( ! func->get_functionType()->get_parameters().empty() ); |
---|
| 414 | ObjectDecl * dstParam = dynamic_cast<ObjectDecl*>( func->get_functionType()->get_parameters().front() ); |
---|
[8b11840] | 415 | ObjectDecl * srcParam = nullptr; |
---|
[972e6f7] | 416 | if ( func->get_functionType()->get_parameters().size() == 2 ) { |
---|
| 417 | srcParam = dynamic_cast<ObjectDecl*>( func->get_functionType()->get_parameters().back() ); |
---|
| 418 | } |
---|
[1a5ad8c] | 419 | |
---|
[972e6f7] | 420 | // srcParam may be NULL, in which case we have default ctor/dtor |
---|
| 421 | assert( dstParam ); |
---|
| 422 | |
---|
[8b11840] | 423 | Expression *srcselect = srcParam ? new MemberExpr( field, new VariableExpr( srcParam ) ) : nullptr; |
---|
[a117f7c] | 424 | makeMemberOp( dstParam, srcselect, field, func, forward ); |
---|
[972e6f7] | 425 | } // if |
---|
| 426 | } // for |
---|
[a117f7c] | 427 | } // makeFunctionBody |
---|
[972e6f7] | 428 | |
---|
[dac0aa9] | 429 | template<typename Iterator> |
---|
[a117f7c] | 430 | void StructFuncGenerator::makeFieldCtorBody( Iterator member, Iterator end, FunctionDecl * func ) { |
---|
| 431 | FunctionType * ftype = func->type; |
---|
| 432 | std::list<DeclarationWithType*> & params = ftype->parameters; |
---|
[dac0aa9] | 433 | assert( params.size() >= 2 ); // should not call this function for default ctor, etc. |
---|
| 434 | |
---|
| 435 | // skip 'this' parameter |
---|
| 436 | ObjectDecl * dstParam = dynamic_cast<ObjectDecl*>( params.front() ); |
---|
| 437 | assert( dstParam ); |
---|
| 438 | std::list<DeclarationWithType*>::iterator parameter = params.begin()+1; |
---|
| 439 | for ( ; member != end; ++member ) { |
---|
| 440 | if ( DeclarationWithType * field = dynamic_cast<DeclarationWithType*>( *member ) ) { |
---|
[0b4d93ab] | 441 | if ( isUnnamedBitfield( dynamic_cast< ObjectDecl * > ( field ) ) ) { |
---|
| 442 | // don't make a function whose parameter is an unnamed bitfield |
---|
| 443 | continue; |
---|
| 444 | } else if ( parameter != params.end() ) { |
---|
[dac0aa9] | 445 | // matching parameter, initialize field with copy ctor |
---|
| 446 | Expression *srcselect = new VariableExpr(*parameter); |
---|
[a117f7c] | 447 | makeMemberOp( dstParam, srcselect, field, func ); |
---|
[dac0aa9] | 448 | ++parameter; |
---|
| 449 | } else { |
---|
| 450 | // no matching parameter, initialize field with default ctor |
---|
[a117f7c] | 451 | makeMemberOp( dstParam, nullptr, field, func ); |
---|
[189d800] | 452 | } |
---|
[356189a] | 453 | } |
---|
[dac0aa9] | 454 | } |
---|
[972e6f7] | 455 | } |
---|
| 456 | |
---|
[0a267c1] | 457 | bool UnionFuncGenerator::shouldAutogen() const { |
---|
| 458 | // Builtins do not use autogeneration. |
---|
| 459 | return ! aggregateDecl->linkage.is_builtin; |
---|
| 460 | } |
---|
| 461 | |
---|
| 462 | // xxx - is this right? |
---|
| 463 | bool UnionFuncGenerator::isConcurrentType() const { return false; }; |
---|
| 464 | |
---|
[186fd86] | 465 | /// generate a single union assignment expression (using memcpy) |
---|
| 466 | template< typename OutputIterator > |
---|
[0a267c1] | 467 | void UnionFuncGenerator::makeMemberOp( ObjectDecl * srcParam, ObjectDecl * dstParam, OutputIterator out ) { |
---|
[186fd86] | 468 | UntypedExpr *copy = new UntypedExpr( new NameExpr( "__builtin_memcpy" ) ); |
---|
[0a267c1] | 469 | copy->args.push_back( new AddressExpr( new VariableExpr( dstParam ) ) ); |
---|
| 470 | copy->args.push_back( new AddressExpr( new VariableExpr( srcParam ) ) ); |
---|
| 471 | copy->args.push_back( new SizeofExpr( srcParam->get_type()->clone() ) ); |
---|
[ba3706f] | 472 | *out++ = new ExprStmt( copy ); |
---|
[186fd86] | 473 | } |
---|
[972e6f7] | 474 | |
---|
[186fd86] | 475 | /// generates the body of a union assignment/copy constructor/field constructor |
---|
[0a267c1] | 476 | void UnionFuncGenerator::genFuncBody( FunctionDecl * funcDecl ) { |
---|
| 477 | FunctionType * ftype = funcDecl->type; |
---|
| 478 | if ( InitTweak::isCopyConstructor( funcDecl ) || InitTweak::isAssignment( funcDecl ) ) { |
---|
| 479 | assert( ftype->parameters.size() == 2 ); |
---|
| 480 | ObjectDecl * dstParam = strict_dynamic_cast< ObjectDecl * >( ftype->parameters.front() ); |
---|
| 481 | ObjectDecl * srcParam = strict_dynamic_cast< ObjectDecl * >( ftype->parameters.back() ); |
---|
| 482 | makeMemberOp( srcParam, dstParam, back_inserter( funcDecl->statements->kids ) ); |
---|
| 483 | } else { |
---|
| 484 | // default ctor/dtor body is empty - add unused attribute to parameter to silence warnings |
---|
| 485 | assert( ftype->parameters.size() == 1 ); |
---|
| 486 | ObjectDecl * dstParam = strict_dynamic_cast< ObjectDecl * >( ftype->parameters.front() ); |
---|
| 487 | dstParam->attributes.push_back( new Attribute( "unused" ) ); |
---|
[186fd86] | 488 | } |
---|
| 489 | } |
---|
[972e6f7] | 490 | |
---|
[0a267c1] | 491 | /// generate the body of a constructor which takes parameters that match fields, e.g. |
---|
| 492 | /// void ?{}(A *, int) and void?{}(A *, int, int) for a struct A which has two int fields. |
---|
| 493 | void UnionFuncGenerator::genFieldCtors() { |
---|
| 494 | // field ctors are only generated if default constructor and copy constructor are both generated |
---|
| 495 | unsigned numCtors = std::count_if( definitions.begin(), definitions.end(), [](Declaration * dcl) { return CodeGen::isConstructor( dcl->get_name() ); } ); |
---|
[972e6f7] | 496 | |
---|
[0a267c1] | 497 | // Field constructors are only generated if default and copy constructor |
---|
| 498 | // are generated, since they need access to both |
---|
| 499 | if ( numCtors != 2 ) return; |
---|
[972e6f7] | 500 | |
---|
[a465caf] | 501 | // create a constructor which takes the first member type as a parameter. |
---|
| 502 | // for example, for Union A { int x; double y; }; generate |
---|
| 503 | // void ?{}(A *, int) |
---|
| 504 | // This is to mimic C's behaviour which initializes the first member of the union. |
---|
[0a267c1] | 505 | FunctionType * memCtorType = genDefaultType( type ); |
---|
| 506 | for ( Declaration * member : aggregateDecl->members ) { |
---|
| 507 | DeclarationWithType * field = strict_dynamic_cast<DeclarationWithType *>( member ); |
---|
| 508 | if ( isUnnamedBitfield( dynamic_cast< ObjectDecl * > ( field ) ) ) { |
---|
| 509 | // don't make a function whose parameter is an unnamed bitfield |
---|
[a465caf] | 510 | break; |
---|
| 511 | } |
---|
[1f370451] | 512 | // do not carry over field's attributes to parameter type |
---|
| 513 | Type * paramType = field->get_type()->clone(); |
---|
| 514 | deleteAll( paramType->attributes ); |
---|
| 515 | paramType->attributes.clear(); |
---|
| 516 | // add a parameter corresponding to this field |
---|
| 517 | memCtorType->parameters.push_back( new ObjectDecl( field->name, Type::StorageClasses(), LinkageSpec::Cforall, nullptr, paramType, nullptr ) ); |
---|
[0a267c1] | 518 | FunctionDecl * ctor = genFunc( "?{}", memCtorType->clone(), functionNesting ); |
---|
| 519 | ObjectDecl * srcParam = strict_dynamic_cast<ObjectDecl *>( ctor->type->parameters.back() ); |
---|
[582ee28] | 520 | srcParam->fixUniqueId(); |
---|
[0a267c1] | 521 | ObjectDecl * dstParam = InitTweak::getParamThis( ctor->type ); |
---|
| 522 | makeMemberOp( srcParam, dstParam, back_inserter( ctor->statements->kids ) ); |
---|
| 523 | resolve( ctor ); |
---|
| 524 | // only generate one field ctor for unions |
---|
| 525 | break; |
---|
[a465caf] | 526 | } |
---|
[0a267c1] | 527 | delete memCtorType; |
---|
[972e6f7] | 528 | } |
---|
| 529 | |
---|
[18ca28e] | 530 | void EnumFuncGenerator::genFuncBody( FunctionDecl * funcDecl ) { |
---|
| 531 | // xxx - Temporary: make these functions intrinsic so they codegen as C assignment. |
---|
| 532 | // Really they're something of a cross between instrinsic and autogen, so should |
---|
| 533 | // probably make a new linkage type |
---|
| 534 | funcDecl->linkage = LinkageSpec::Intrinsic; |
---|
| 535 | FunctionType * ftype = funcDecl->type; |
---|
| 536 | if ( InitTweak::isCopyConstructor( funcDecl ) || InitTweak::isAssignment( funcDecl ) ) { |
---|
| 537 | assert( ftype->parameters.size() == 2 ); |
---|
| 538 | ObjectDecl * dstParam = strict_dynamic_cast< ObjectDecl * >( ftype->parameters.front() ); |
---|
| 539 | ObjectDecl * srcParam = strict_dynamic_cast< ObjectDecl * >( ftype->parameters.back() ); |
---|
| 540 | |
---|
| 541 | // enum copy construct and assignment is just C-style assignment. |
---|
| 542 | // this looks like a bad recursive call, but code gen will turn it into |
---|
| 543 | // a C-style assignment. |
---|
| 544 | // This happens before function pointer type conversion, so need to do it manually here |
---|
| 545 | ApplicationExpr * callExpr = new ApplicationExpr( VariableExpr::functionPointer( funcDecl ) ); |
---|
| 546 | callExpr->get_args().push_back( new VariableExpr( dstParam ) ); |
---|
| 547 | callExpr->get_args().push_back( new VariableExpr( srcParam ) ); |
---|
[ba3706f] | 548 | funcDecl->statements->push_back( new ExprStmt( callExpr ) ); |
---|
[18ca28e] | 549 | } else { |
---|
| 550 | // default ctor/dtor body is empty - add unused attribute to parameter to silence warnings |
---|
| 551 | assert( ftype->parameters.size() == 1 ); |
---|
| 552 | ObjectDecl * dstParam = strict_dynamic_cast< ObjectDecl * >( ftype->parameters.front() ); |
---|
| 553 | dstParam->attributes.push_back( new Attribute( "unused" ) ); |
---|
| 554 | } |
---|
| 555 | } |
---|
| 556 | |
---|
| 557 | bool EnumFuncGenerator::shouldAutogen() const { return true; } |
---|
| 558 | bool EnumFuncGenerator::isConcurrentType() const { return false; } |
---|
| 559 | // enums do not have field constructors |
---|
| 560 | void EnumFuncGenerator::genFieldCtors() {} |
---|
| 561 | |
---|
[a117f7c] | 562 | bool TypeFuncGenerator::shouldAutogen() const { return true; }; |
---|
| 563 | |
---|
| 564 | void TypeFuncGenerator::genFuncBody( FunctionDecl * dcl ) { |
---|
| 565 | FunctionType * ftype = dcl->type; |
---|
| 566 | assertf( ftype->parameters.size() == 1 || ftype->parameters.size() == 2, "Incorrect number of parameters in autogenerated typedecl function: %zd", ftype->parameters.size() ); |
---|
| 567 | DeclarationWithType * dst = ftype->parameters.front(); |
---|
| 568 | DeclarationWithType * src = ftype->parameters.size() == 2 ? ftype->parameters.back() : nullptr; |
---|
| 569 | // generate appropriate calls to member ctor, assignment |
---|
| 570 | UntypedExpr * expr = new UntypedExpr( new NameExpr( dcl->name ) ); |
---|
| 571 | expr->args.push_back( new CastExpr( new VariableExpr( dst ), new ReferenceType( Type::Qualifiers(), typeDecl->base->clone() ) ) ); |
---|
| 572 | if ( src ) expr->args.push_back( new CastExpr( new VariableExpr( src ), typeDecl->base->clone() ) ); |
---|
[ba3706f] | 573 | dcl->statements->kids.push_back( new ExprStmt( expr ) ); |
---|
[a117f7c] | 574 | }; |
---|
| 575 | |
---|
| 576 | // xxx - should reach in and determine if base type is concurrent? |
---|
| 577 | bool TypeFuncGenerator::isConcurrentType() const { return false; }; |
---|
| 578 | |
---|
| 579 | // opaque types do not have field constructors |
---|
[18ca28e] | 580 | void TypeFuncGenerator::genFieldCtors() {}; |
---|
[a117f7c] | 581 | |
---|
| 582 | //============================================================================================= |
---|
| 583 | // Visitor definitions |
---|
| 584 | //============================================================================================= |
---|
[207c7e1d] | 585 | AutogenerateRoutines::AutogenerateRoutines() { |
---|
| 586 | // the order here determines the order that these functions are generated. |
---|
| 587 | // assignment should come last since it uses copy constructor in return. |
---|
[a117f7c] | 588 | data.emplace_back( "?{}", genDefaultType ); |
---|
| 589 | data.emplace_back( "?{}", genCopyType ); |
---|
| 590 | data.emplace_back( "^?{}", genDefaultType ); |
---|
| 591 | data.emplace_back( "?=?", genAssignType ); |
---|
[207c7e1d] | 592 | } |
---|
| 593 | |
---|
[8b11840] | 594 | void AutogenerateRoutines::previsit( EnumDecl * enumDecl ) { |
---|
[189d800] | 595 | // must visit children (enum constants) to add them to the indexer |
---|
| 596 | if ( enumDecl->has_body() ) { |
---|
[18ca28e] | 597 | EnumInstType enumInst( Type::Qualifiers(), enumDecl->get_name() ); |
---|
| 598 | enumInst.set_baseEnum( enumDecl ); |
---|
| 599 | EnumFuncGenerator gen( &enumInst, data, functionNesting, indexer ); |
---|
| 600 | generateFunctions( gen, declsToAddAfter ); |
---|
[972e6f7] | 601 | } |
---|
| 602 | } |
---|
| 603 | |
---|
[8b11840] | 604 | void AutogenerateRoutines::previsit( StructDecl * structDecl ) { |
---|
[e4d6335] | 605 | visit_children = false; |
---|
[a117f7c] | 606 | if ( structDecl->has_body() ) { |
---|
[8b11840] | 607 | StructInstType structInst( Type::Qualifiers(), structDecl->name ); |
---|
[a117f7c] | 608 | structInst.set_baseStruct( structDecl ); |
---|
[8b11840] | 609 | for ( TypeDecl * typeDecl : structDecl->parameters ) { |
---|
| 610 | structInst.parameters.push_back( new TypeExpr( new TypeInstType( Type::Qualifiers(), typeDecl->name, typeDecl ) ) ); |
---|
[186fd86] | 611 | } |
---|
[a117f7c] | 612 | StructFuncGenerator gen( structDecl, &structInst, data, functionNesting, indexer ); |
---|
| 613 | generateFunctions( gen, declsToAddAfter ); |
---|
[972e6f7] | 614 | } // if |
---|
| 615 | } |
---|
| 616 | |
---|
[8b11840] | 617 | void AutogenerateRoutines::previsit( UnionDecl * unionDecl ) { |
---|
[e4d6335] | 618 | visit_children = false; |
---|
[a117f7c] | 619 | if ( unionDecl->has_body() ) { |
---|
[972e6f7] | 620 | UnionInstType unionInst( Type::Qualifiers(), unionDecl->get_name() ); |
---|
| 621 | unionInst.set_baseUnion( unionDecl ); |
---|
[186fd86] | 622 | for ( TypeDecl * typeDecl : unionDecl->get_parameters() ) { |
---|
| 623 | unionInst.get_parameters().push_back( new TypeExpr( new TypeInstType( Type::Qualifiers(), typeDecl->get_name(), typeDecl ) ) ); |
---|
| 624 | } |
---|
[0a267c1] | 625 | UnionFuncGenerator gen( unionDecl, &unionInst, data, functionNesting, indexer ); |
---|
| 626 | generateFunctions( gen, declsToAddAfter ); |
---|
[972e6f7] | 627 | } // if |
---|
| 628 | } |
---|
| 629 | |
---|
[108f3cdb] | 630 | // generate ctor/dtors/assign for typedecls, e.g., otype T = int *; |
---|
[8b11840] | 631 | void AutogenerateRoutines::previsit( TypeDecl * typeDecl ) { |
---|
[108f3cdb] | 632 | if ( ! typeDecl->base ) return; |
---|
| 633 | |
---|
| 634 | TypeInstType refType( Type::Qualifiers(), typeDecl->name, typeDecl ); |
---|
[0a267c1] | 635 | TypeFuncGenerator gen( typeDecl, &refType, data, functionNesting, indexer ); |
---|
[a117f7c] | 636 | generateFunctions( gen, declsToAddAfter ); |
---|
[972e6f7] | 637 | |
---|
| 638 | } |
---|
| 639 | |
---|
[e4d6335] | 640 | void AutogenerateRoutines::previsit( TraitDecl * ) { |
---|
[a5a71d0] | 641 | // ensure that we don't add assignment ops for types defined as part of the trait |
---|
[e4d6335] | 642 | visit_children = false; |
---|
[972e6f7] | 643 | } |
---|
| 644 | |
---|
[f203a7a] | 645 | void AutogenerateRoutines::previsit( FunctionDecl * ) { |
---|
| 646 | // Track whether we're currently in a function. |
---|
| 647 | // Can ignore function type idiosyncrasies, because function type can never |
---|
| 648 | // declare a new type. |
---|
[972e6f7] | 649 | functionNesting += 1; |
---|
[f203a7a] | 650 | GuardAction( [this]() { functionNesting -= 1; } ); |
---|
[972e6f7] | 651 | } |
---|
| 652 | |
---|
[e4d6335] | 653 | void AutogenerateRoutines::previsit( CompoundStmt * ) { |
---|
| 654 | GuardScope( structsDone ); |
---|
[972e6f7] | 655 | } |
---|
[1486116] | 656 | |
---|
| 657 | void makeTupleFunctionBody( FunctionDecl * function ) { |
---|
| 658 | FunctionType * ftype = function->get_functionType(); |
---|
| 659 | assertf( ftype->get_parameters().size() == 1 || ftype->get_parameters().size() == 2, "too many parameters in generated tuple function" ); |
---|
| 660 | |
---|
| 661 | UntypedExpr * untyped = new UntypedExpr( new NameExpr( function->get_name() ) ); |
---|
| 662 | |
---|
| 663 | /// xxx - &* is used to make this easier for later passes to handle |
---|
| 664 | untyped->get_args().push_back( new AddressExpr( UntypedExpr::createDeref( new VariableExpr( ftype->get_parameters().front() ) ) ) ); |
---|
| 665 | if ( ftype->get_parameters().size() == 2 ) { |
---|
| 666 | untyped->get_args().push_back( new VariableExpr( ftype->get_parameters().back() ) ); |
---|
| 667 | } |
---|
[ba3706f] | 668 | function->get_statements()->get_kids().push_back( new ExprStmt( untyped ) ); |
---|
| 669 | function->get_statements()->get_kids().push_back( new ReturnStmt( UntypedExpr::createDeref( new VariableExpr( ftype->get_parameters().front() ) ) ) ); |
---|
[1486116] | 670 | } |
---|
| 671 | |
---|
[ac74057] | 672 | void AutogenTupleRoutines::postvisit( TupleType * tupleType ) { |
---|
[1486116] | 673 | std::string mangleName = SymTab::Mangler::mangleType( tupleType ); |
---|
[ac74057] | 674 | if ( seenTuples.find( mangleName ) != seenTuples.end() ) return; |
---|
[1486116] | 675 | seenTuples.insert( mangleName ); |
---|
| 676 | |
---|
| 677 | // T ?=?(T *, T); |
---|
| 678 | FunctionType *assignType = genAssignType( tupleType ); |
---|
| 679 | |
---|
| 680 | // void ?{}(T *); void ^?{}(T *); |
---|
| 681 | FunctionType *ctorType = genDefaultType( tupleType ); |
---|
| 682 | FunctionType *dtorType = genDefaultType( tupleType ); |
---|
| 683 | |
---|
| 684 | // void ?{}(T *, T); |
---|
| 685 | FunctionType *copyCtorType = genCopyType( tupleType ); |
---|
| 686 | |
---|
| 687 | std::set< TypeDecl* > done; |
---|
| 688 | std::list< TypeDecl * > typeParams; |
---|
| 689 | for ( Type * t : *tupleType ) { |
---|
| 690 | if ( TypeInstType * ty = dynamic_cast< TypeInstType * >( t ) ) { |
---|
| 691 | if ( ! done.count( ty->get_baseType() ) ) { |
---|
[f0ecf9b] | 692 | TypeDecl * newDecl = new TypeDecl( ty->get_baseType()->get_name(), Type::StorageClasses(), nullptr, TypeDecl::Dtype, true ); |
---|
[1486116] | 693 | TypeInstType * inst = new TypeInstType( Type::Qualifiers(), newDecl->get_name(), newDecl ); |
---|
[68fe077a] | 694 | newDecl->get_assertions().push_back( new FunctionDecl( "?=?", Type::StorageClasses(), LinkageSpec::Cforall, genAssignType( inst ), nullptr, |
---|
[ddfd945] | 695 | std::list< Attribute * >(), Type::FuncSpecifiers( Type::Inline ) ) ); |
---|
[68fe077a] | 696 | newDecl->get_assertions().push_back( new FunctionDecl( "?{}", Type::StorageClasses(), LinkageSpec::Cforall, genDefaultType( inst ), nullptr, |
---|
[ddfd945] | 697 | std::list< Attribute * >(), Type::FuncSpecifiers( Type::Inline ) ) ); |
---|
[68fe077a] | 698 | newDecl->get_assertions().push_back( new FunctionDecl( "?{}", Type::StorageClasses(), LinkageSpec::Cforall, genCopyType( inst ), nullptr, |
---|
[ddfd945] | 699 | std::list< Attribute * >(), Type::FuncSpecifiers( Type::Inline ) ) ); |
---|
[68fe077a] | 700 | newDecl->get_assertions().push_back( new FunctionDecl( "^?{}", Type::StorageClasses(), LinkageSpec::Cforall, genDefaultType( inst ), nullptr, |
---|
[ddfd945] | 701 | std::list< Attribute * >(), Type::FuncSpecifiers( Type::Inline ) ) ); |
---|
[1486116] | 702 | typeParams.push_back( newDecl ); |
---|
| 703 | done.insert( ty->get_baseType() ); |
---|
| 704 | } |
---|
| 705 | } |
---|
| 706 | } |
---|
| 707 | cloneAll( typeParams, ctorType->get_forall() ); |
---|
| 708 | cloneAll( typeParams, dtorType->get_forall() ); |
---|
| 709 | cloneAll( typeParams, copyCtorType->get_forall() ); |
---|
| 710 | cloneAll( typeParams, assignType->get_forall() ); |
---|
| 711 | |
---|
| 712 | FunctionDecl *assignDecl = genFunc( "?=?", assignType, functionNesting ); |
---|
| 713 | FunctionDecl *ctorDecl = genFunc( "?{}", ctorType, functionNesting ); |
---|
| 714 | FunctionDecl *copyCtorDecl = genFunc( "?{}", copyCtorType, functionNesting ); |
---|
| 715 | FunctionDecl *dtorDecl = genFunc( "^?{}", dtorType, functionNesting ); |
---|
| 716 | |
---|
| 717 | makeTupleFunctionBody( assignDecl ); |
---|
| 718 | makeTupleFunctionBody( ctorDecl ); |
---|
| 719 | makeTupleFunctionBody( copyCtorDecl ); |
---|
| 720 | makeTupleFunctionBody( dtorDecl ); |
---|
| 721 | |
---|
[ac74057] | 722 | declsToAddBefore.push_back( ctorDecl ); |
---|
| 723 | declsToAddBefore.push_back( copyCtorDecl ); |
---|
| 724 | declsToAddBefore.push_back( dtorDecl ); |
---|
| 725 | declsToAddBefore.push_back( assignDecl ); // assignment should come last since it uses copy constructor in return |
---|
[1486116] | 726 | } |
---|
| 727 | |
---|
[ac74057] | 728 | void AutogenTupleRoutines::previsit( FunctionDecl *functionDecl ) { |
---|
| 729 | visit_children = false; |
---|
[a139c11] | 730 | maybeAccept( functionDecl->type, *visitor ); |
---|
[1486116] | 731 | functionNesting += 1; |
---|
[a139c11] | 732 | maybeAccept( functionDecl->statements, *visitor ); |
---|
[1486116] | 733 | functionNesting -= 1; |
---|
| 734 | } |
---|
| 735 | |
---|
[ac74057] | 736 | void AutogenTupleRoutines::previsit( CompoundStmt * ) { |
---|
| 737 | GuardScope( seenTuples ); |
---|
[1486116] | 738 | } |
---|
[972e6f7] | 739 | } // SymTab |
---|
[c0aa336] | 740 | |
---|
| 741 | // Local Variables: // |
---|
| 742 | // tab-width: 4 // |
---|
| 743 | // mode: c++ // |
---|
| 744 | // compile-command: "make install" // |
---|
| 745 | // End: // |
---|