[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( FunctionType * ftype );
|
---|
| 65 | void previsit( PointerType * ptype );
|
---|
[972e6f7] | 66 |
|
---|
[e4d6335] | 67 | void previsit( CompoundStmt * compoundStmt );
|
---|
[972e6f7] | 68 |
|
---|
[1486116] | 69 | private:
|
---|
[a117f7c] | 70 |
|
---|
[e4d6335] | 71 | GenPoly::ScopedSet< std::string > structsDone;
|
---|
[1486116] | 72 | unsigned int functionNesting = 0; // current level of nested functions
|
---|
[a117f7c] | 73 |
|
---|
| 74 | InitTweak::ManagedTypes managedTypes;
|
---|
[207c7e1d] | 75 | std::vector< FuncData > data;
|
---|
[1486116] | 76 | };
|
---|
| 77 |
|
---|
| 78 | /// generates routines for tuple types.
|
---|
[ac74057] | 79 | struct AutogenTupleRoutines : public WithDeclsToAdd, public WithVisitorRef<AutogenTupleRoutines>, public WithGuards, public WithShortCircuiting {
|
---|
[a117f7c] | 80 | void previsit( FunctionDecl * functionDecl );
|
---|
[1486116] | 81 |
|
---|
[a117f7c] | 82 | void postvisit( TupleType * tupleType );
|
---|
[1486116] | 83 |
|
---|
[a117f7c] | 84 | void previsit( CompoundStmt * compoundStmt );
|
---|
[1486116] | 85 |
|
---|
| 86 | private:
|
---|
| 87 | unsigned int functionNesting = 0; // current level of nested functions
|
---|
| 88 | GenPoly::ScopedSet< std::string > seenTuples;
|
---|
[972e6f7] | 89 | };
|
---|
| 90 |
|
---|
| 91 | void autogenerateRoutines( std::list< Declaration * > &translationUnit ) {
|
---|
[e4d6335] | 92 | PassVisitor<AutogenerateRoutines> generator;
|
---|
| 93 | acceptAll( translationUnit, generator );
|
---|
[1486116] | 94 |
|
---|
| 95 | // needs to be done separately because AutogenerateRoutines skips types that appear as function arguments, etc.
|
---|
| 96 | // AutogenTupleRoutines tupleGenerator;
|
---|
[ac74057] | 97 | // acceptAll( translationUnit, tupleGenerator );
|
---|
[972e6f7] | 98 | }
|
---|
| 99 |
|
---|
[a117f7c] | 100 | //=============================================================================================
|
---|
| 101 | // FuncGenerator definitions
|
---|
| 102 | //=============================================================================================
|
---|
| 103 | class FuncGenerator {
|
---|
| 104 | public:
|
---|
| 105 | std::list< Declaration * > definitions, forwards;
|
---|
| 106 |
|
---|
[0a267c1] | 107 | FuncGenerator( Type * type, const std::vector< FuncData > & data, unsigned int functionNesting, SymTab::Indexer & indexer ) : type( type ), data( data ), functionNesting( functionNesting ), indexer( indexer ) {}
|
---|
[a117f7c] | 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;
|
---|
[0a267c1] | 115 | unsigned int functionNesting;
|
---|
[a117f7c] | 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:
|
---|
[0a267c1] | 128 | StructFuncGenerator( StructDecl * aggregateDecl, StructInstType * refType, const std::vector< FuncData > & data, unsigned int functionNesting, SymTab::Indexer & indexer ) : FuncGenerator( refType, data, functionNesting, indexer ), aggregateDecl( aggregateDecl) {}
|
---|
[a117f7c] | 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 |
|
---|
[0a267c1] | 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 |
|
---|
[18ca28e] | 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 |
|
---|
[a117f7c] | 189 | class TypeFuncGenerator : public FuncGenerator {
|
---|
| 190 | TypeDecl * typeDecl;
|
---|
| 191 | public:
|
---|
[0a267c1] | 192 | TypeFuncGenerator( TypeDecl * typeDecl, TypeInstType * refType, const std::vector<FuncData> & data, unsigned int functionNesting, SymTab::Indexer & indexer ) : FuncGenerator( refType, data, functionNesting, indexer ), typeDecl( typeDecl ) {}
|
---|
[a117f7c] | 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 |
|
---|
[356189a] | 214 | bool isUnnamedBitfield( ObjectDecl * obj ) {
|
---|
[f30b2610] | 215 | return obj != nullptr && obj->name == "" && obj->bitfieldWidth != nullptr;
|
---|
[356189a] | 216 | }
|
---|
| 217 |
|
---|
[186fd86] | 218 | /// inserts a forward declaration for functionDecl into declsToAdd
|
---|
| 219 | void addForwardDecl( FunctionDecl * functionDecl, std::list< Declaration * > & declsToAdd ) {
|
---|
| 220 | FunctionDecl * decl = functionDecl->clone();
|
---|
[f30b2610] | 221 | delete decl->statements;
|
---|
| 222 | decl->statements = nullptr;
|
---|
[186fd86] | 223 | declsToAdd.push_back( decl );
|
---|
| 224 | decl->fixUniqueId();
|
---|
[972e6f7] | 225 | }
|
---|
| 226 |
|
---|
[f30b2610] | 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 |
|
---|
[186fd86] | 237 | /// given type T, generate type of default ctor/dtor, i.e. function type void (*) (T *)
|
---|
| 238 | FunctionType * genDefaultType( Type * paramType ) {
|
---|
[f30b2610] | 239 | const auto & typeParams = getGenericParams( paramType );
|
---|
[186fd86] | 240 | FunctionType *ftype = new FunctionType( Type::Qualifiers(), false );
|
---|
[f30b2610] | 241 | cloneAll( typeParams, ftype->forall );
|
---|
[49148d5] | 242 | ObjectDecl *dstParam = new ObjectDecl( "_dst", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new ReferenceType( Type::Qualifiers(), paramType->clone() ), nullptr );
|
---|
[f30b2610] | 243 | ftype->parameters.push_back( dstParam );
|
---|
[186fd86] | 244 | return ftype;
|
---|
| 245 | }
|
---|
[d0f8b19] | 246 |
|
---|
[186fd86] | 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 );
|
---|
[68fe077a] | 250 | ObjectDecl *srcParam = new ObjectDecl( "_src", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, paramType->clone(), nullptr );
|
---|
[f30b2610] | 251 | ftype->parameters.push_back( srcParam );
|
---|
[186fd86] | 252 | return ftype;
|
---|
| 253 | }
|
---|
[d0f8b19] | 254 |
|
---|
[186fd86] | 255 | /// given type T, generate type of assignment, i.e. function type T (*) (T *, T)
|
---|
[1486116] | 256 | FunctionType * genAssignType( Type * paramType ) {
|
---|
[186fd86] | 257 | FunctionType *ftype = genCopyType( paramType );
|
---|
[68fe077a] | 258 | ObjectDecl *returnVal = new ObjectDecl( "_ret", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, paramType->clone(), nullptr );
|
---|
[f30b2610] | 259 | ftype->returnVals.push_back( returnVal );
|
---|
[186fd86] | 260 | return ftype;
|
---|
| 261 | }
|
---|
[972e6f7] | 262 |
|
---|
[186fd86] | 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
|
---|
[972e6f7] | 267 | // because each unit generates copies of the default routines for each aggregate.
|
---|
[68fe077a] | 268 | Type::StorageClasses scs = functionNesting > 0 ? Type::StorageClasses() : Type::StorageClasses( Type::Static );
|
---|
[186fd86] | 269 | LinkageSpec::Spec spec = isIntrinsic ? LinkageSpec::Intrinsic : LinkageSpec::AutoGen;
|
---|
[a7c90d4] | 270 | FunctionDecl * decl = new FunctionDecl( fname, scs, spec, ftype, new CompoundStmt( noLabels ),
|
---|
[ddfd945] | 271 | std::list< Attribute * >(), Type::FuncSpecifiers( Type::Inline ) );
|
---|
[186fd86] | 272 | decl->fixUniqueId();
|
---|
| 273 | return decl;
|
---|
| 274 | }
|
---|
[d0f8b19] | 275 |
|
---|
[a117f7c] | 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 );
|
---|
[f232977] | 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 | }
|
---|
[a117f7c] | 307 | resolve( dcl );
|
---|
| 308 | }
|
---|
| 309 | }
|
---|
| 310 |
|
---|
| 311 | void FuncGenerator::generatePrototypes( std::list< FunctionDecl * > & newFuncs ) {
|
---|
| 312 | bool concurrent_type = isConcurrentType();
|
---|
[e220391] | 313 | for ( const FuncData & d : data ) {
|
---|
[a117f7c] | 314 | // generate a function (?{}, ?=?, ^?{}) based on the current FuncData.
|
---|
[e220391] | 315 | FunctionType * ftype = d.genType( type );
|
---|
[a117f7c] | 316 |
|
---|
| 317 | // destructor for concurrent type must be mutex
|
---|
[e220391] | 318 | if ( concurrent_type && CodeGen::isDestructor( d.fname ) ) {
|
---|
[a117f7c] | 319 | ftype->parameters.front()->get_type()->set_mutex( true );
|
---|
| 320 | }
|
---|
| 321 |
|
---|
[e220391] | 322 | newFuncs.push_back( genFunc( d.fname, ftype, functionNesting ) );
|
---|
[a117f7c] | 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
|
---|
[f30b2610] | 353 | if ( ! CodeGen::isDestructor( dcl->name ) ) {
|
---|
[a117f7c] | 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
|
---|
[f30b2610] | 362 | unsigned numCtors = std::count_if( definitions.begin(), definitions.end(), [](Declaration * dcl) { return CodeGen::isConstructor( dcl->name ); } );
|
---|
[a117f7c] | 363 |
|
---|
[0a267c1] | 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 |
|
---|
[a117f7c] | 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)
|
---|
[0a267c1] | 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;
|
---|
[a117f7c] | 377 | }
|
---|
[0a267c1] | 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 );
|
---|
[a117f7c] | 382 | }
|
---|
[0a267c1] | 383 | delete memCtorType;
|
---|
[a117f7c] | 384 | }
|
---|
| 385 |
|
---|
| 386 | void StructFuncGenerator::makeMemberOp( ObjectDecl * dstParam, Expression * src, DeclarationWithType * field, FunctionDecl * func, bool forward ) {
|
---|
[39f84a4] | 387 | InitTweak::InitExpander srcParam( src );
|
---|
| 388 |
|
---|
[49148d5] | 389 | // assign to destination
|
---|
[f30b2610] | 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 );
|
---|
[dac0aa9] | 392 | }
|
---|
| 393 |
|
---|
[972e6f7] | 394 | template<typename Iterator>
|
---|
[a117f7c] | 395 | void StructFuncGenerator::makeFunctionBody( Iterator member, Iterator end, FunctionDecl * func, bool forward ) {
|
---|
[972e6f7] | 396 | for ( ; member != end; ++member ) {
|
---|
[dac0aa9] | 397 | if ( DeclarationWithType *field = dynamic_cast< DeclarationWithType * >( *member ) ) { // otherwise some form of type declaration, e.g. Aggregate
|
---|
[972e6f7] | 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.
|
---|
[dac0aa9] | 400 | Type * type = field->get_type();
|
---|
[972e6f7] | 401 | while ( ArrayType * at = dynamic_cast< ArrayType * >( type ) ) {
|
---|
| 402 | type = at->get_base();
|
---|
| 403 | }
|
---|
| 404 |
|
---|
[6fc5c14] | 405 | if ( type->get_const() && CodeGen::isAssignment( func->name ) ) {
|
---|
[cad355a] | 406 | // don't assign const members, but do construct/destruct
|
---|
[972e6f7] | 407 | continue;
|
---|
| 408 | }
|
---|
| 409 |
|
---|
| 410 | assert( ! func->get_functionType()->get_parameters().empty() );
|
---|
| 411 | ObjectDecl * dstParam = dynamic_cast<ObjectDecl*>( func->get_functionType()->get_parameters().front() );
|
---|
[8b11840] | 412 | ObjectDecl * srcParam = nullptr;
|
---|
[972e6f7] | 413 | if ( func->get_functionType()->get_parameters().size() == 2 ) {
|
---|
| 414 | srcParam = dynamic_cast<ObjectDecl*>( func->get_functionType()->get_parameters().back() );
|
---|
| 415 | }
|
---|
[1a5ad8c] | 416 |
|
---|
[972e6f7] | 417 | // srcParam may be NULL, in which case we have default ctor/dtor
|
---|
| 418 | assert( dstParam );
|
---|
| 419 |
|
---|
[8b11840] | 420 | Expression *srcselect = srcParam ? new MemberExpr( field, new VariableExpr( srcParam ) ) : nullptr;
|
---|
[a117f7c] | 421 | makeMemberOp( dstParam, srcselect, field, func, forward );
|
---|
[972e6f7] | 422 | } // if
|
---|
| 423 | } // for
|
---|
[a117f7c] | 424 | } // makeFunctionBody
|
---|
[972e6f7] | 425 |
|
---|
[dac0aa9] | 426 | template<typename Iterator>
|
---|
[a117f7c] | 427 | void StructFuncGenerator::makeFieldCtorBody( Iterator member, Iterator end, FunctionDecl * func ) {
|
---|
| 428 | FunctionType * ftype = func->type;
|
---|
| 429 | std::list<DeclarationWithType*> & params = ftype->parameters;
|
---|
[dac0aa9] | 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 ) ) {
|
---|
[0b4d93ab] | 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() ) {
|
---|
[dac0aa9] | 442 | // matching parameter, initialize field with copy ctor
|
---|
| 443 | Expression *srcselect = new VariableExpr(*parameter);
|
---|
[a117f7c] | 444 | makeMemberOp( dstParam, srcselect, field, func );
|
---|
[dac0aa9] | 445 | ++parameter;
|
---|
| 446 | } else {
|
---|
| 447 | // no matching parameter, initialize field with default ctor
|
---|
[a117f7c] | 448 | makeMemberOp( dstParam, nullptr, field, func );
|
---|
[189d800] | 449 | }
|
---|
[356189a] | 450 | }
|
---|
[dac0aa9] | 451 | }
|
---|
[972e6f7] | 452 | }
|
---|
| 453 |
|
---|
[0a267c1] | 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 |
|
---|
[186fd86] | 462 | /// generate a single union assignment expression (using memcpy)
|
---|
| 463 | template< typename OutputIterator >
|
---|
[0a267c1] | 464 | void UnionFuncGenerator::makeMemberOp( ObjectDecl * srcParam, ObjectDecl * dstParam, OutputIterator out ) {
|
---|
[186fd86] | 465 | UntypedExpr *copy = new UntypedExpr( new NameExpr( "__builtin_memcpy" ) );
|
---|
[0a267c1] | 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() ) );
|
---|
[186fd86] | 469 | *out++ = new ExprStmt( noLabels, copy );
|
---|
| 470 | }
|
---|
[972e6f7] | 471 |
|
---|
[186fd86] | 472 | /// generates the body of a union assignment/copy constructor/field constructor
|
---|
[0a267c1] | 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" ) );
|
---|
[186fd86] | 485 | }
|
---|
| 486 | }
|
---|
[972e6f7] | 487 |
|
---|
[0a267c1] | 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() ); } );
|
---|
[972e6f7] | 493 |
|
---|
[0a267c1] | 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;
|
---|
[972e6f7] | 497 |
|
---|
[a465caff] | 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.
|
---|
[0a267c1] | 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
|
---|
[a465caff] | 507 | break;
|
---|
| 508 | }
|
---|
[0a267c1] | 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() );
|
---|
[582ee28] | 512 | srcParam->fixUniqueId();
|
---|
[0a267c1] | 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;
|
---|
[a465caff] | 518 | }
|
---|
[0a267c1] | 519 | delete memCtorType;
|
---|
[972e6f7] | 520 | }
|
---|
| 521 |
|
---|
[18ca28e] | 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 |
|
---|
[a117f7c] | 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
|
---|
[18ca28e] | 572 | void TypeFuncGenerator::genFieldCtors() {};
|
---|
[a117f7c] | 573 |
|
---|
| 574 | //=============================================================================================
|
---|
| 575 | // Visitor definitions
|
---|
| 576 | //=============================================================================================
|
---|
[207c7e1d] | 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.
|
---|
[a117f7c] | 580 | data.emplace_back( "?{}", genDefaultType );
|
---|
| 581 | data.emplace_back( "?{}", genCopyType );
|
---|
| 582 | data.emplace_back( "^?{}", genDefaultType );
|
---|
| 583 | data.emplace_back( "?=?", genAssignType );
|
---|
[207c7e1d] | 584 | }
|
---|
| 585 |
|
---|
[8b11840] | 586 | void AutogenerateRoutines::previsit( EnumDecl * enumDecl ) {
|
---|
[189d800] | 587 | // must visit children (enum constants) to add them to the indexer
|
---|
| 588 | if ( enumDecl->has_body() ) {
|
---|
[18ca28e] | 589 | EnumInstType enumInst( Type::Qualifiers(), enumDecl->get_name() );
|
---|
| 590 | enumInst.set_baseEnum( enumDecl );
|
---|
| 591 | EnumFuncGenerator gen( &enumInst, data, functionNesting, indexer );
|
---|
| 592 | generateFunctions( gen, declsToAddAfter );
|
---|
[972e6f7] | 593 | }
|
---|
| 594 | }
|
---|
| 595 |
|
---|
[8b11840] | 596 | void AutogenerateRoutines::previsit( StructDecl * structDecl ) {
|
---|
[e4d6335] | 597 | visit_children = false;
|
---|
[a117f7c] | 598 | if ( structDecl->has_body() ) {
|
---|
[8b11840] | 599 | StructInstType structInst( Type::Qualifiers(), structDecl->name );
|
---|
[a117f7c] | 600 | structInst.set_baseStruct( structDecl );
|
---|
[8b11840] | 601 | for ( TypeDecl * typeDecl : structDecl->parameters ) {
|
---|
| 602 | structInst.parameters.push_back( new TypeExpr( new TypeInstType( Type::Qualifiers(), typeDecl->name, typeDecl ) ) );
|
---|
[186fd86] | 603 | }
|
---|
[a117f7c] | 604 | StructFuncGenerator gen( structDecl, &structInst, data, functionNesting, indexer );
|
---|
| 605 | generateFunctions( gen, declsToAddAfter );
|
---|
[972e6f7] | 606 | } // if
|
---|
| 607 | }
|
---|
| 608 |
|
---|
[8b11840] | 609 | void AutogenerateRoutines::previsit( UnionDecl * unionDecl ) {
|
---|
[e4d6335] | 610 | visit_children = false;
|
---|
[a117f7c] | 611 | if ( unionDecl->has_body() ) {
|
---|
[972e6f7] | 612 | UnionInstType unionInst( Type::Qualifiers(), unionDecl->get_name() );
|
---|
| 613 | unionInst.set_baseUnion( unionDecl );
|
---|
[186fd86] | 614 | for ( TypeDecl * typeDecl : unionDecl->get_parameters() ) {
|
---|
| 615 | unionInst.get_parameters().push_back( new TypeExpr( new TypeInstType( Type::Qualifiers(), typeDecl->get_name(), typeDecl ) ) );
|
---|
| 616 | }
|
---|
[0a267c1] | 617 | UnionFuncGenerator gen( unionDecl, &unionInst, data, functionNesting, indexer );
|
---|
| 618 | generateFunctions( gen, declsToAddAfter );
|
---|
[972e6f7] | 619 | } // if
|
---|
| 620 | }
|
---|
| 621 |
|
---|
[108f3cdb] | 622 | // generate ctor/dtors/assign for typedecls, e.g., otype T = int *;
|
---|
[8b11840] | 623 | void AutogenerateRoutines::previsit( TypeDecl * typeDecl ) {
|
---|
[e4d6335] | 624 | visit_children = false;
|
---|
[108f3cdb] | 625 | if ( ! typeDecl->base ) return;
|
---|
| 626 |
|
---|
| 627 | TypeInstType refType( Type::Qualifiers(), typeDecl->name, typeDecl );
|
---|
[0a267c1] | 628 | TypeFuncGenerator gen( typeDecl, &refType, data, functionNesting, indexer );
|
---|
[a117f7c] | 629 | generateFunctions( gen, declsToAddAfter );
|
---|
[972e6f7] | 630 | }
|
---|
| 631 |
|
---|
[e4d6335] | 632 | void AutogenerateRoutines::previsit( FunctionType *) {
|
---|
[972e6f7] | 633 | // ensure that we don't add assignment ops for types defined as part of the function
|
---|
[e4d6335] | 634 | visit_children = false;
|
---|
[972e6f7] | 635 | }
|
---|
| 636 |
|
---|
[e4d6335] | 637 | void AutogenerateRoutines::previsit( PointerType *) {
|
---|
[972e6f7] | 638 | // ensure that we don't add assignment ops for types defined as part of the pointer
|
---|
[e4d6335] | 639 | visit_children = false;
|
---|
[972e6f7] | 640 | }
|
---|
| 641 |
|
---|
[e4d6335] | 642 | void AutogenerateRoutines::previsit( TraitDecl * ) {
|
---|
[a5a71d0] | 643 | // ensure that we don't add assignment ops for types defined as part of the trait
|
---|
[e4d6335] | 644 | visit_children = false;
|
---|
[972e6f7] | 645 | }
|
---|
| 646 |
|
---|
[e4d6335] | 647 | void AutogenerateRoutines::previsit( FunctionDecl * functionDecl ) {
|
---|
| 648 | visit_children = false;
|
---|
[207c7e1d] | 649 | // record the existence of this function as appropriate
|
---|
[a117f7c] | 650 | managedTypes.handleDWT( functionDecl );
|
---|
[207c7e1d] | 651 |
|
---|
[8b11840] | 652 | maybeAccept( functionDecl->type, *visitor );
|
---|
[972e6f7] | 653 | functionNesting += 1;
|
---|
[8b11840] | 654 | maybeAccept( functionDecl->statements, *visitor );
|
---|
[972e6f7] | 655 | functionNesting -= 1;
|
---|
| 656 | }
|
---|
| 657 |
|
---|
[e4d6335] | 658 | void AutogenerateRoutines::previsit( CompoundStmt * ) {
|
---|
[a117f7c] | 659 | GuardScope( managedTypes );
|
---|
[e4d6335] | 660 | GuardScope( structsDone );
|
---|
[972e6f7] | 661 | }
|
---|
[1486116] | 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 |
|
---|
[ac74057] | 678 | void AutogenTupleRoutines::postvisit( TupleType * tupleType ) {
|
---|
[1486116] | 679 | std::string mangleName = SymTab::Mangler::mangleType( tupleType );
|
---|
[ac74057] | 680 | if ( seenTuples.find( mangleName ) != seenTuples.end() ) return;
|
---|
[1486116] | 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() ) ) {
|
---|
[f0ecf9b] | 698 | TypeDecl * newDecl = new TypeDecl( ty->get_baseType()->get_name(), Type::StorageClasses(), nullptr, TypeDecl::Dtype, true );
|
---|
[1486116] | 699 | TypeInstType * inst = new TypeInstType( Type::Qualifiers(), newDecl->get_name(), newDecl );
|
---|
[68fe077a] | 700 | newDecl->get_assertions().push_back( new FunctionDecl( "?=?", Type::StorageClasses(), LinkageSpec::Cforall, genAssignType( inst ), nullptr,
|
---|
[ddfd945] | 701 | std::list< Attribute * >(), Type::FuncSpecifiers( Type::Inline ) ) );
|
---|
[68fe077a] | 702 | newDecl->get_assertions().push_back( new FunctionDecl( "?{}", Type::StorageClasses(), LinkageSpec::Cforall, genDefaultType( inst ), nullptr,
|
---|
[ddfd945] | 703 | std::list< Attribute * >(), Type::FuncSpecifiers( Type::Inline ) ) );
|
---|
[68fe077a] | 704 | newDecl->get_assertions().push_back( new FunctionDecl( "?{}", Type::StorageClasses(), LinkageSpec::Cforall, genCopyType( inst ), nullptr,
|
---|
[ddfd945] | 705 | std::list< Attribute * >(), Type::FuncSpecifiers( Type::Inline ) ) );
|
---|
[68fe077a] | 706 | newDecl->get_assertions().push_back( new FunctionDecl( "^?{}", Type::StorageClasses(), LinkageSpec::Cforall, genDefaultType( inst ), nullptr,
|
---|
[ddfd945] | 707 | std::list< Attribute * >(), Type::FuncSpecifiers( Type::Inline ) ) );
|
---|
[1486116] | 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 |
|
---|
[ac74057] | 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
|
---|
[1486116] | 732 | }
|
---|
| 733 |
|
---|
[ac74057] | 734 | void AutogenTupleRoutines::previsit( FunctionDecl *functionDecl ) {
|
---|
| 735 | visit_children = false;
|
---|
[a139c11] | 736 | maybeAccept( functionDecl->type, *visitor );
|
---|
[1486116] | 737 | functionNesting += 1;
|
---|
[a139c11] | 738 | maybeAccept( functionDecl->statements, *visitor );
|
---|
[1486116] | 739 | functionNesting -= 1;
|
---|
| 740 | }
|
---|
| 741 |
|
---|
[ac74057] | 742 | void AutogenTupleRoutines::previsit( CompoundStmt * ) {
|
---|
| 743 | GuardScope( seenTuples );
|
---|
[1486116] | 744 | }
|
---|
[972e6f7] | 745 | } // SymTab
|
---|
[c0aa336] | 746 |
|
---|
| 747 | // Local Variables: //
|
---|
| 748 | // tab-width: 4 //
|
---|
| 749 | // mode: c++ //
|
---|
| 750 | // compile-command: "make install" //
|
---|
| 751 | // End: //
|
---|