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