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