[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/DeclMutator.h" // for DeclMutator
|
---|
| 32 | #include "GenPoly/ScopedSet.h" // for ScopedSet, ScopedSet<>::iterator
|
---|
[8b11840] | 33 | #include "InitTweak/GenInit.h" // for fixReturnStatements
|
---|
| 34 | #include "ResolvExpr/Resolver.h" // for resolveDecl
|
---|
[30f9072] | 35 | #include "SymTab/Mangler.h" // for Mangler
|
---|
[9236060] | 36 | #include "SynTree/Attribute.h" // For Attribute
|
---|
[30f9072] | 37 | #include "SynTree/Mutator.h" // for maybeMutate
|
---|
| 38 | #include "SynTree/Statement.h" // for CompoundStmt, ReturnStmt, ExprStmt
|
---|
| 39 | #include "SynTree/Type.h" // for FunctionType, Type, TypeInstType
|
---|
| 40 | #include "SynTree/Visitor.h" // for maybeAccept, Visitor, acceptAll
|
---|
| 41 |
|
---|
| 42 | class Attribute;
|
---|
[972e6f7] | 43 |
|
---|
| 44 | namespace SymTab {
|
---|
[5f98ce5] | 45 | Type * SizeType = 0;
|
---|
[207c7e1d] | 46 | typedef ScopedMap< std::string, bool > TypeMap;
|
---|
| 47 |
|
---|
| 48 | /// Data used to generate functions generically. Specifically, the name of the generated function, a function which generates the routine protoype, and a map which contains data to determine whether a function should be generated.
|
---|
| 49 | struct FuncData {
|
---|
| 50 | typedef FunctionType * (*TypeGen)( Type * );
|
---|
| 51 | FuncData( const std::string & fname, const TypeGen & genType, TypeMap & map ) : fname( fname ), genType( genType ), map( map ) {}
|
---|
| 52 | std::string fname;
|
---|
| 53 | TypeGen genType;
|
---|
| 54 | TypeMap & map;
|
---|
| 55 | };
|
---|
[5f98ce5] | 56 |
|
---|
[e4d6335] | 57 | struct AutogenerateRoutines final : public WithDeclsToAdd, public WithVisitorRef<AutogenerateRoutines>, public WithGuards, public WithShortCircuiting {
|
---|
[207c7e1d] | 58 | AutogenerateRoutines();
|
---|
| 59 |
|
---|
[e4d6335] | 60 | void previsit( EnumDecl * enumDecl );
|
---|
| 61 | void previsit( StructDecl * structDecl );
|
---|
| 62 | void previsit( UnionDecl * structDecl );
|
---|
| 63 | void previsit( TypeDecl * typeDecl );
|
---|
| 64 | void previsit( TraitDecl * traitDecl );
|
---|
| 65 | void previsit( FunctionDecl * functionDecl );
|
---|
[972e6f7] | 66 |
|
---|
[e4d6335] | 67 | void previsit( FunctionType * ftype );
|
---|
| 68 | void previsit( PointerType * ptype );
|
---|
[972e6f7] | 69 |
|
---|
[e4d6335] | 70 | void previsit( CompoundStmt * compoundStmt );
|
---|
[972e6f7] | 71 |
|
---|
[1486116] | 72 | private:
|
---|
[e4d6335] | 73 | GenPoly::ScopedSet< std::string > structsDone;
|
---|
[1486116] | 74 | unsigned int functionNesting = 0; // current level of nested functions
|
---|
[207c7e1d] | 75 | /// Note: the following maps could be ScopedSets, but it should be easier to work
|
---|
| 76 | /// deleted functions in if they are maps, since the value false can be inserted
|
---|
| 77 | /// at the current scope without affecting outer scopes or requiring copies.
|
---|
| 78 | TypeMap copyable, assignable, constructable, destructable;
|
---|
| 79 | std::vector< FuncData > data;
|
---|
[1486116] | 80 | };
|
---|
| 81 |
|
---|
| 82 | /// generates routines for tuple types.
|
---|
| 83 | /// Doesn't really need to be a mutator, but it's easier to reuse DeclMutator than it is to use AddVisit
|
---|
| 84 | /// or anything we currently have that supports adding new declarations for visitors
|
---|
| 85 | class AutogenTupleRoutines : public GenPoly::DeclMutator {
|
---|
| 86 | public:
|
---|
| 87 | typedef GenPoly::DeclMutator Parent;
|
---|
| 88 | using Parent::mutate;
|
---|
| 89 |
|
---|
| 90 | virtual DeclarationWithType * mutate( FunctionDecl *functionDecl );
|
---|
| 91 |
|
---|
| 92 | virtual Type * mutate( TupleType *tupleType );
|
---|
| 93 |
|
---|
| 94 | virtual CompoundStmt * mutate( CompoundStmt *compoundStmt );
|
---|
| 95 |
|
---|
| 96 | private:
|
---|
| 97 | unsigned int functionNesting = 0; // current level of nested functions
|
---|
| 98 | GenPoly::ScopedSet< std::string > seenTuples;
|
---|
[972e6f7] | 99 | };
|
---|
| 100 |
|
---|
| 101 | void autogenerateRoutines( std::list< Declaration * > &translationUnit ) {
|
---|
[e4d6335] | 102 | PassVisitor<AutogenerateRoutines> generator;
|
---|
| 103 | acceptAll( translationUnit, generator );
|
---|
[1486116] | 104 |
|
---|
| 105 | // needs to be done separately because AutogenerateRoutines skips types that appear as function arguments, etc.
|
---|
| 106 | // AutogenTupleRoutines tupleGenerator;
|
---|
| 107 | // tupleGenerator.mutateDeclarationList( translationUnit );
|
---|
[972e6f7] | 108 | }
|
---|
| 109 |
|
---|
[356189a] | 110 | bool isUnnamedBitfield( ObjectDecl * obj ) {
|
---|
[8b11840] | 111 | return obj != nullptr && obj->get_name() == "" && obj->get_bitfieldWidth() != nullptr;
|
---|
[356189a] | 112 | }
|
---|
| 113 |
|
---|
[186fd86] | 114 | /// inserts a forward declaration for functionDecl into declsToAdd
|
---|
| 115 | void addForwardDecl( FunctionDecl * functionDecl, std::list< Declaration * > & declsToAdd ) {
|
---|
| 116 | FunctionDecl * decl = functionDecl->clone();
|
---|
| 117 | delete decl->get_statements();
|
---|
[8b11840] | 118 | decl->set_statements( nullptr );
|
---|
[186fd86] | 119 | declsToAdd.push_back( decl );
|
---|
| 120 | decl->fixUniqueId();
|
---|
[972e6f7] | 121 | }
|
---|
| 122 |
|
---|
[186fd86] | 123 | /// given type T, generate type of default ctor/dtor, i.e. function type void (*) (T *)
|
---|
| 124 | FunctionType * genDefaultType( Type * paramType ) {
|
---|
| 125 | FunctionType *ftype = new FunctionType( Type::Qualifiers(), false );
|
---|
[49148d5] | 126 | ObjectDecl *dstParam = new ObjectDecl( "_dst", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new ReferenceType( Type::Qualifiers(), paramType->clone() ), nullptr );
|
---|
[186fd86] | 127 | ftype->get_parameters().push_back( dstParam );
|
---|
[972e6f7] | 128 |
|
---|
[186fd86] | 129 | return ftype;
|
---|
| 130 | }
|
---|
[d0f8b19] | 131 |
|
---|
[186fd86] | 132 | /// given type T, generate type of copy ctor, i.e. function type void (*) (T *, T)
|
---|
| 133 | FunctionType * genCopyType( Type * paramType ) {
|
---|
| 134 | FunctionType *ftype = genDefaultType( paramType );
|
---|
[68fe077a] | 135 | ObjectDecl *srcParam = new ObjectDecl( "_src", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, paramType->clone(), nullptr );
|
---|
[186fd86] | 136 | ftype->get_parameters().push_back( srcParam );
|
---|
| 137 | return ftype;
|
---|
| 138 | }
|
---|
[d0f8b19] | 139 |
|
---|
[186fd86] | 140 | /// given type T, generate type of assignment, i.e. function type T (*) (T *, T)
|
---|
[1486116] | 141 | FunctionType * genAssignType( Type * paramType ) {
|
---|
[186fd86] | 142 | FunctionType *ftype = genCopyType( paramType );
|
---|
[68fe077a] | 143 | ObjectDecl *returnVal = new ObjectDecl( "_ret", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, paramType->clone(), nullptr );
|
---|
[186fd86] | 144 | ftype->get_returnVals().push_back( returnVal );
|
---|
| 145 | return ftype;
|
---|
| 146 | }
|
---|
[972e6f7] | 147 |
|
---|
[186fd86] | 148 | /// generate a function decl from a name and type. Nesting depth determines whether
|
---|
| 149 | /// the declaration is static or not; optional paramter determines if declaration is intrinsic
|
---|
| 150 | FunctionDecl * genFunc( const std::string & fname, FunctionType * ftype, unsigned int functionNesting, bool isIntrinsic = false ) {
|
---|
| 151 | // Routines at global scope marked "static" to prevent multiple definitions in separate translation units
|
---|
[972e6f7] | 152 | // because each unit generates copies of the default routines for each aggregate.
|
---|
[68fe077a] | 153 | Type::StorageClasses scs = functionNesting > 0 ? Type::StorageClasses() : Type::StorageClasses( Type::Static );
|
---|
[186fd86] | 154 | LinkageSpec::Spec spec = isIntrinsic ? LinkageSpec::Intrinsic : LinkageSpec::AutoGen;
|
---|
[a7c90d4] | 155 | FunctionDecl * decl = new FunctionDecl( fname, scs, spec, ftype, new CompoundStmt( noLabels ),
|
---|
[ddfd945] | 156 | std::list< Attribute * >(), Type::FuncSpecifiers( Type::Inline ) );
|
---|
[186fd86] | 157 | decl->fixUniqueId();
|
---|
| 158 | return decl;
|
---|
| 159 | }
|
---|
[d0f8b19] | 160 |
|
---|
[207c7e1d] | 161 | /// inserts base type of first argument into map if pred(funcDecl) is true
|
---|
| 162 | void insert( FunctionDecl *funcDecl, TypeMap & map, FunctionDecl * (*pred)(Declaration *) ) {
|
---|
| 163 | // insert type into constructable, etc. map if appropriate
|
---|
| 164 | if ( pred( funcDecl ) ) {
|
---|
| 165 | FunctionType * ftype = funcDecl->get_functionType();
|
---|
| 166 | assert( ! ftype->get_parameters().empty() );
|
---|
[ce8c12f] | 167 | Type * t = InitTweak::getPointerBase( ftype->get_parameters().front()->get_type() );
|
---|
| 168 | assert( t );
|
---|
[207c7e1d] | 169 | map.insert( Mangler::mangleType( t ), true );
|
---|
| 170 | }
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | /// using map and t, determines if is constructable, etc.
|
---|
| 174 | bool lookup( const TypeMap & map, Type * t ) {
|
---|
[108f3cdb] | 175 | assertf( t, "Autogenerate lookup was given non-type: %s", toString( t ).c_str() );
|
---|
[207c7e1d] | 176 | if ( dynamic_cast< PointerType * >( t ) ) {
|
---|
| 177 | // will need more complicated checking if we want this to work with pointer types, since currently
|
---|
| 178 | return true;
|
---|
| 179 | } else if ( ArrayType * at = dynamic_cast< ArrayType * >( t ) ) {
|
---|
| 180 | // an array's constructor, etc. is generated on the fly based on the base type's constructor, etc.
|
---|
| 181 | return lookup( map, at->get_base() );
|
---|
| 182 | }
|
---|
| 183 | TypeMap::const_iterator it = map.find( Mangler::mangleType( t ) );
|
---|
| 184 | if ( it != map.end() ) return it->second;
|
---|
| 185 | // something that does not appear in the map is by default not constructable, etc.
|
---|
| 186 | return false;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | /// using map and aggr, examines each member to determine if constructor, etc. should be generated
|
---|
[108f3cdb] | 190 | template<typename Container>
|
---|
| 191 | bool shouldGenerate( const TypeMap & map, const Container & container ) {
|
---|
| 192 | for ( Type * t : container ) {
|
---|
| 193 | if ( ! lookup( map, t ) ) return false;
|
---|
[207c7e1d] | 194 | }
|
---|
| 195 | return true;
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | /// data structure for abstracting the generation of special functions
|
---|
[108f3cdb] | 199 | template< typename OutputIterator, typename Container >
|
---|
[207c7e1d] | 200 | struct FuncGenerator {
|
---|
[108f3cdb] | 201 | const Container & container;
|
---|
| 202 | Type *refType;
|
---|
[207c7e1d] | 203 | unsigned int functionNesting;
|
---|
| 204 | const std::list< TypeDecl* > & typeParams;
|
---|
| 205 | OutputIterator out;
|
---|
[108f3cdb] | 206 | FuncGenerator( const Container & container, Type *refType, unsigned int functionNesting, const std::list< TypeDecl* > & typeParams, OutputIterator out ) : container( container ), refType( refType ), functionNesting( functionNesting ), typeParams( typeParams ), out( out ) {}
|
---|
[207c7e1d] | 207 |
|
---|
| 208 | /// generates a function (?{}, ?=?, ^?{}) based on the data argument and members. If function is generated, inserts the type into the map.
|
---|
[bcda04c] | 209 | void gen( const FuncData & data, bool concurrent_type ) {
|
---|
[108f3cdb] | 210 | if ( ! shouldGenerate( data.map, container ) ) return;
|
---|
[207c7e1d] | 211 | FunctionType * ftype = data.genType( refType );
|
---|
[bcda04c] | 212 |
|
---|
[4e8949f] | 213 | if ( concurrent_type && CodeGen::isDestructor( data.fname ) ) {
|
---|
[108f3cdb] | 214 | ftype->parameters.front()->get_type()->set_mutex( true );
|
---|
[bcda04c] | 215 | }
|
---|
| 216 |
|
---|
[108f3cdb] | 217 | cloneAll( typeParams, ftype->forall );
|
---|
[207c7e1d] | 218 | *out++ = genFunc( data.fname, ftype, functionNesting );
|
---|
| 219 | data.map.insert( Mangler::mangleType( refType ), true );
|
---|
| 220 | }
|
---|
| 221 | };
|
---|
| 222 |
|
---|
[108f3cdb] | 223 | template< typename OutputIterator, typename Container >
|
---|
| 224 | FuncGenerator<OutputIterator, Container> makeFuncGenerator( const Container & container, Type *refType, unsigned int functionNesting, const std::list< TypeDecl* > & typeParams, OutputIterator out ) {
|
---|
| 225 | return FuncGenerator<OutputIterator, Container>( container, refType, functionNesting, typeParams, out );
|
---|
[207c7e1d] | 226 | }
|
---|
| 227 |
|
---|
[186fd86] | 228 | /// generates a single enumeration assignment expression
|
---|
| 229 | ApplicationExpr * genEnumAssign( FunctionType * ftype, FunctionDecl * assignDecl ) {
|
---|
[d0f8b19] | 230 | // enum copy construct and assignment is just C-style assignment.
|
---|
| 231 | // this looks like a bad recursive call, but code gen will turn it into
|
---|
| 232 | // a C-style assignment.
|
---|
| 233 | // This happens before function pointer type conversion, so need to do it manually here
|
---|
[186fd86] | 234 | // NOTE: ftype is not necessarily the functionType belonging to assignDecl - ftype is the
|
---|
| 235 | // type of the function that this expression is being generated for (so that the correct
|
---|
| 236 | // parameters) are using in the variable exprs
|
---|
| 237 | assert( ftype->get_parameters().size() == 2 );
|
---|
[e3e16bc] | 238 | ObjectDecl * dstParam = strict_dynamic_cast< ObjectDecl * >( ftype->get_parameters().front() );
|
---|
| 239 | ObjectDecl * srcParam = strict_dynamic_cast< ObjectDecl * >( ftype->get_parameters().back() );
|
---|
[186fd86] | 240 |
|
---|
[d0f8b19] | 241 | VariableExpr * assignVarExpr = new VariableExpr( assignDecl );
|
---|
[906e24d] | 242 | Type * assignVarExprType = assignVarExpr->get_result();
|
---|
[d0f8b19] | 243 | assignVarExprType = new PointerType( Type::Qualifiers(), assignVarExprType );
|
---|
[906e24d] | 244 | assignVarExpr->set_result( assignVarExprType );
|
---|
[d0f8b19] | 245 | ApplicationExpr * assignExpr = new ApplicationExpr( assignVarExpr );
|
---|
| 246 | assignExpr->get_args().push_back( new VariableExpr( dstParam ) );
|
---|
| 247 | assignExpr->get_args().push_back( new VariableExpr( srcParam ) );
|
---|
[186fd86] | 248 | return assignExpr;
|
---|
[972e6f7] | 249 | }
|
---|
| 250 |
|
---|
[186fd86] | 251 | // E ?=?(E volatile*, int),
|
---|
| 252 | // ?=?(E _Atomic volatile*, int);
|
---|
[d7dc824] | 253 | void makeEnumFunctions( EnumInstType *refType, unsigned int functionNesting, std::list< Declaration * > &declsToAdd ) {
|
---|
[972e6f7] | 254 |
|
---|
[186fd86] | 255 | // T ?=?(E *, E);
|
---|
| 256 | FunctionType *assignType = genAssignType( refType );
|
---|
[9554d9b] | 257 |
|
---|
[186fd86] | 258 | // void ?{}(E *); void ^?{}(E *);
|
---|
| 259 | FunctionType * ctorType = genDefaultType( refType->clone() );
|
---|
| 260 | FunctionType * dtorType = genDefaultType( refType->clone() );
|
---|
[9554d9b] | 261 |
|
---|
[186fd86] | 262 | // void ?{}(E *, E);
|
---|
| 263 | FunctionType *copyCtorType = genCopyType( refType->clone() );
|
---|
[9554d9b] | 264 |
|
---|
[46adb83] | 265 | // add unused attribute to parameters of default constructor and destructor
|
---|
| 266 | ctorType->get_parameters().front()->get_attributes().push_back( new Attribute( "unused" ) );
|
---|
| 267 | dtorType->get_parameters().front()->get_attributes().push_back( new Attribute( "unused" ) );
|
---|
| 268 |
|
---|
[186fd86] | 269 | // xxx - should we also generate void ?{}(E *, int) and E ?{}(E *, E)?
|
---|
| 270 | // right now these cases work, but that might change.
|
---|
[9554d9b] | 271 |
|
---|
[186fd86] | 272 | // xxx - Temporary: make these functions intrinsic so they codegen as C assignment.
|
---|
| 273 | // Really they're something of a cross between instrinsic and autogen, so should
|
---|
| 274 | // probably make a new linkage type
|
---|
| 275 | FunctionDecl *assignDecl = genFunc( "?=?", assignType, functionNesting, true );
|
---|
| 276 | FunctionDecl *ctorDecl = genFunc( "?{}", ctorType, functionNesting, true );
|
---|
| 277 | FunctionDecl *copyCtorDecl = genFunc( "?{}", copyCtorType, functionNesting, true );
|
---|
| 278 | FunctionDecl *dtorDecl = genFunc( "^?{}", dtorType, functionNesting, true );
|
---|
[9554d9b] | 279 |
|
---|
[186fd86] | 280 | // body is either return stmt or expr stmt
|
---|
| 281 | assignDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, genEnumAssign( assignType, assignDecl ) ) );
|
---|
| 282 | copyCtorDecl->get_statements()->get_kids().push_back( new ExprStmt( noLabels, genEnumAssign( copyCtorType, assignDecl ) ) );
|
---|
[9554d9b] | 283 |
|
---|
[186fd86] | 284 | declsToAdd.push_back( ctorDecl );
|
---|
| 285 | declsToAdd.push_back( copyCtorDecl );
|
---|
| 286 | declsToAdd.push_back( dtorDecl );
|
---|
[1486116] | 287 | declsToAdd.push_back( assignDecl ); // assignment should come last since it uses copy constructor in return
|
---|
[972e6f7] | 288 | }
|
---|
| 289 |
|
---|
[186fd86] | 290 | /// generates a single struct member operation (constructor call, destructor call, assignment call)
|
---|
[74b007ba] | 291 | void makeStructMemberOp( ObjectDecl * dstParam, Expression * src, DeclarationWithType * field, FunctionDecl * func, bool forward = true ) {
|
---|
[39f84a4] | 292 | InitTweak::InitExpander srcParam( src );
|
---|
| 293 |
|
---|
[49148d5] | 294 | // assign to destination
|
---|
[e3e16bc] | 295 | Expression *dstselect = new MemberExpr( field, new CastExpr( new VariableExpr( dstParam ), strict_dynamic_cast< ReferenceType* >( dstParam->get_type() )->get_base()->clone() ) );
|
---|
[39f84a4] | 296 | genImplicitCall( srcParam, dstselect, func->get_name(), back_inserter( func->get_statements()->get_kids() ), field, forward );
|
---|
[dac0aa9] | 297 | }
|
---|
| 298 |
|
---|
[186fd86] | 299 | /// 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
|
---|
[972e6f7] | 300 | template<typename Iterator>
|
---|
[74b007ba] | 301 | void makeStructFunctionBody( Iterator member, Iterator end, FunctionDecl * func, bool forward = true ) {
|
---|
[972e6f7] | 302 | for ( ; member != end; ++member ) {
|
---|
[dac0aa9] | 303 | if ( DeclarationWithType *field = dynamic_cast< DeclarationWithType * >( *member ) ) { // otherwise some form of type declaration, e.g. Aggregate
|
---|
[972e6f7] | 304 | // query the type qualifiers of this field and skip assigning it if it is marked const.
|
---|
| 305 | // If it is an array type, we need to strip off the array layers to find its qualifiers.
|
---|
[dac0aa9] | 306 | Type * type = field->get_type();
|
---|
[972e6f7] | 307 | while ( ArrayType * at = dynamic_cast< ArrayType * >( type ) ) {
|
---|
| 308 | type = at->get_base();
|
---|
| 309 | }
|
---|
| 310 |
|
---|
[615a096] | 311 | if ( type->get_const() && func->get_name() == "?=?" ) {
|
---|
[cad355a] | 312 | // don't assign const members, but do construct/destruct
|
---|
[972e6f7] | 313 | continue;
|
---|
| 314 | }
|
---|
| 315 |
|
---|
[fb24492] | 316 | if ( field->get_name() == "" ) {
|
---|
| 317 | // don't assign to anonymous members
|
---|
| 318 | // xxx - this is a temporary fix. Anonymous members tie into
|
---|
| 319 | // our inheritance model. I think the correct way to handle this is to
|
---|
| 320 | // cast the structure to the type of the member and let the resolver
|
---|
| 321 | // figure out whether it's valid and have a pass afterwards that fixes
|
---|
| 322 | // the assignment to use pointer arithmetic with the offset of the
|
---|
| 323 | // member, much like how generic type members are handled.
|
---|
| 324 | continue;
|
---|
| 325 | }
|
---|
| 326 |
|
---|
[972e6f7] | 327 | assert( ! func->get_functionType()->get_parameters().empty() );
|
---|
| 328 | ObjectDecl * dstParam = dynamic_cast<ObjectDecl*>( func->get_functionType()->get_parameters().front() );
|
---|
[8b11840] | 329 | ObjectDecl * srcParam = nullptr;
|
---|
[972e6f7] | 330 | if ( func->get_functionType()->get_parameters().size() == 2 ) {
|
---|
| 331 | srcParam = dynamic_cast<ObjectDecl*>( func->get_functionType()->get_parameters().back() );
|
---|
| 332 | }
|
---|
| 333 | // srcParam may be NULL, in which case we have default ctor/dtor
|
---|
| 334 | assert( dstParam );
|
---|
| 335 |
|
---|
[8b11840] | 336 | Expression *srcselect = srcParam ? new MemberExpr( field, new VariableExpr( srcParam ) ) : nullptr;
|
---|
[74b007ba] | 337 | makeStructMemberOp( dstParam, srcselect, field, func, forward );
|
---|
[972e6f7] | 338 | } // if
|
---|
| 339 | } // for
|
---|
| 340 | } // makeStructFunctionBody
|
---|
| 341 |
|
---|
[dac0aa9] | 342 | /// generate the body of a constructor which takes parameters that match fields, e.g.
|
---|
| 343 | /// void ?{}(A *, int) and void?{}(A *, int, int) for a struct A which has two int fields.
|
---|
| 344 | template<typename Iterator>
|
---|
[74b007ba] | 345 | void makeStructFieldCtorBody( Iterator member, Iterator end, FunctionDecl * func ) {
|
---|
[dac0aa9] | 346 | FunctionType * ftype = func->get_functionType();
|
---|
| 347 | std::list<DeclarationWithType*> & params = ftype->get_parameters();
|
---|
| 348 | assert( params.size() >= 2 ); // should not call this function for default ctor, etc.
|
---|
| 349 |
|
---|
| 350 | // skip 'this' parameter
|
---|
| 351 | ObjectDecl * dstParam = dynamic_cast<ObjectDecl*>( params.front() );
|
---|
| 352 | assert( dstParam );
|
---|
| 353 | std::list<DeclarationWithType*>::iterator parameter = params.begin()+1;
|
---|
| 354 | for ( ; member != end; ++member ) {
|
---|
| 355 | if ( DeclarationWithType * field = dynamic_cast<DeclarationWithType*>( *member ) ) {
|
---|
[0b4d93ab] | 356 | if ( isUnnamedBitfield( dynamic_cast< ObjectDecl * > ( field ) ) ) {
|
---|
| 357 | // don't make a function whose parameter is an unnamed bitfield
|
---|
| 358 | continue;
|
---|
| 359 | } else if ( field->get_name() == "" ) {
|
---|
| 360 | // don't assign to anonymous members
|
---|
| 361 | // xxx - this is a temporary fix. Anonymous members tie into
|
---|
| 362 | // our inheritance model. I think the correct way to handle this is to
|
---|
| 363 | // cast the structure to the type of the member and let the resolver
|
---|
| 364 | // figure out whether it's valid and have a pass afterwards that fixes
|
---|
| 365 | // the assignment to use pointer arithmetic with the offset of the
|
---|
| 366 | // member, much like how generic type members are handled.
|
---|
| 367 | continue;
|
---|
| 368 | } else if ( parameter != params.end() ) {
|
---|
[dac0aa9] | 369 | // matching parameter, initialize field with copy ctor
|
---|
| 370 | Expression *srcselect = new VariableExpr(*parameter);
|
---|
[74b007ba] | 371 | makeStructMemberOp( dstParam, srcselect, field, func );
|
---|
[dac0aa9] | 372 | ++parameter;
|
---|
| 373 | } else {
|
---|
| 374 | // no matching parameter, initialize field with default ctor
|
---|
[8b11840] | 375 | makeStructMemberOp( dstParam, nullptr, field, func );
|
---|
[dac0aa9] | 376 | }
|
---|
| 377 | }
|
---|
| 378 | }
|
---|
| 379 | }
|
---|
| 380 |
|
---|
[108f3cdb] | 381 | Type * declToType( Declaration * decl ) {
|
---|
| 382 | if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) {
|
---|
| 383 | return dwt->get_type();
|
---|
| 384 | }
|
---|
| 385 | return nullptr;
|
---|
| 386 | }
|
---|
| 387 |
|
---|
[186fd86] | 388 | /// generates struct constructors, destructor, and assignment functions
|
---|
[207c7e1d] | 389 | void makeStructFunctions( StructDecl *aggregateDecl, StructInstType *refType, unsigned int functionNesting, std::list< Declaration * > & declsToAdd, const std::vector< FuncData > & data ) {
|
---|
[fa4805f] | 390 | // Builtins do not use autogeneration.
|
---|
[8b11840] | 391 | if ( LinkageSpec::isBuiltin( aggregateDecl->get_linkage() ) ) {
|
---|
[fa4805f] | 392 | return;
|
---|
| 393 | }
|
---|
| 394 |
|
---|
[972e6f7] | 395 | // Make function polymorphic in same parameters as generic struct, if applicable
|
---|
[8b11840] | 396 | const std::list< TypeDecl * > & typeParams = aggregateDecl->get_parameters(); // List of type variables to be placed on the generated functions
|
---|
[972e6f7] | 397 |
|
---|
[207c7e1d] | 398 | // generate each of the functions based on the supplied FuncData objects
|
---|
| 399 | std::list< FunctionDecl * > newFuncs;
|
---|
[108f3cdb] | 400 | // structure that iterates aggregate decl members, returning their types
|
---|
| 401 | auto generator = makeFuncGenerator( lazy_map( aggregateDecl->members, declToType ), refType, functionNesting, typeParams, back_inserter( newFuncs ) );
|
---|
[207c7e1d] | 402 | for ( const FuncData & d : data ) {
|
---|
[bcda04c] | 403 | generator.gen( d, aggregateDecl->is_thread() || aggregateDecl->is_monitor() );
|
---|
[207c7e1d] | 404 | }
|
---|
[bcda04c] | 405 |
|
---|
[207c7e1d] | 406 | // field ctors are only generated if default constructor and copy constructor are both generated
|
---|
[bff227f] | 407 | unsigned numCtors = std::count_if( newFuncs.begin(), newFuncs.end(), [](FunctionDecl * dcl) { return CodeGen::isConstructor( dcl->get_name() ); } );
|
---|
[972e6f7] | 408 |
|
---|
[9f70ab57] | 409 | if ( functionNesting == 0 ) {
|
---|
| 410 | // forward declare if top-level struct, so that
|
---|
| 411 | // type is complete as soon as its body ends
|
---|
[1486116] | 412 | // Note: this is necessary if we want structs which contain
|
---|
| 413 | // generic (otype) structs as members.
|
---|
[207c7e1d] | 414 | for ( FunctionDecl * dcl : newFuncs ) {
|
---|
| 415 | addForwardDecl( dcl, declsToAdd );
|
---|
| 416 | }
|
---|
| 417 | }
|
---|
| 418 |
|
---|
| 419 | for ( FunctionDecl * dcl : newFuncs ) {
|
---|
| 420 | // generate appropriate calls to member ctor, assignment
|
---|
| 421 | // destructor needs to do everything in reverse, so pass "forward" based on whether the function is a destructor
|
---|
[bff227f] | 422 | if ( ! CodeGen::isDestructor( dcl->get_name() ) ) {
|
---|
[74b007ba] | 423 | makeStructFunctionBody( aggregateDecl->get_members().begin(), aggregateDecl->get_members().end(), dcl );
|
---|
[207c7e1d] | 424 | } else {
|
---|
[74b007ba] | 425 | makeStructFunctionBody( aggregateDecl->get_members().rbegin(), aggregateDecl->get_members().rend(), dcl, false );
|
---|
[207c7e1d] | 426 | }
|
---|
[bff227f] | 427 | if ( CodeGen::isAssignment( dcl->get_name() ) ) {
|
---|
[207c7e1d] | 428 | // assignment needs to return a value
|
---|
| 429 | FunctionType * assignType = dcl->get_functionType();
|
---|
| 430 | assert( assignType->get_parameters().size() == 2 );
|
---|
[e3e16bc] | 431 | ObjectDecl * srcParam = strict_dynamic_cast< ObjectDecl * >( assignType->get_parameters().back() );
|
---|
[207c7e1d] | 432 | dcl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) );
|
---|
| 433 | }
|
---|
| 434 | declsToAdd.push_back( dcl );
|
---|
[9f70ab57] | 435 | }
|
---|
| 436 |
|
---|
[dac0aa9] | 437 | // create constructors which take each member type as a parameter.
|
---|
[f5ef08c] | 438 | // for example, for struct A { int x, y; }; generate
|
---|
[207c7e1d] | 439 | // void ?{}(A *, int) and void ?{}(A *, int, int)
|
---|
| 440 | // Field constructors are only generated if default and copy constructor
|
---|
| 441 | // are generated, since they need access to both
|
---|
| 442 | if ( numCtors == 2 ) {
|
---|
| 443 | FunctionType * memCtorType = genDefaultType( refType );
|
---|
| 444 | cloneAll( typeParams, memCtorType->get_forall() );
|
---|
| 445 | for ( std::list<Declaration *>::iterator i = aggregateDecl->get_members().begin(); i != aggregateDecl->get_members().end(); ++i ) {
|
---|
| 446 | DeclarationWithType * member = dynamic_cast<DeclarationWithType *>( *i );
|
---|
| 447 | assert( member );
|
---|
| 448 | if ( isUnnamedBitfield( dynamic_cast< ObjectDecl * > ( member ) ) ) {
|
---|
| 449 | // don't make a function whose parameter is an unnamed bitfield
|
---|
| 450 | continue;
|
---|
| 451 | } else if ( member->get_name() == "" ) {
|
---|
| 452 | // don't assign to anonymous members
|
---|
| 453 | // xxx - this is a temporary fix. Anonymous members tie into
|
---|
| 454 | // our inheritance model. I think the correct way to handle this is to
|
---|
| 455 | // cast the structure to the type of the member and let the resolver
|
---|
[49148d5] | 456 | // figure out whether it's valid/choose the correct unnamed member
|
---|
[207c7e1d] | 457 | continue;
|
---|
| 458 | }
|
---|
[68fe077a] | 459 | memCtorType->get_parameters().push_back( new ObjectDecl( member->get_name(), Type::StorageClasses(), LinkageSpec::Cforall, 0, member->get_type()->clone(), 0 ) );
|
---|
[207c7e1d] | 460 | FunctionDecl * ctor = genFunc( "?{}", memCtorType->clone(), functionNesting );
|
---|
[74b007ba] | 461 | makeStructFieldCtorBody( aggregateDecl->get_members().begin(), aggregateDecl->get_members().end(), ctor );
|
---|
[207c7e1d] | 462 | declsToAdd.push_back( ctor );
|
---|
[356189a] | 463 | }
|
---|
[207c7e1d] | 464 | delete memCtorType;
|
---|
[dac0aa9] | 465 | }
|
---|
[972e6f7] | 466 | }
|
---|
| 467 |
|
---|
[186fd86] | 468 | /// generate a single union assignment expression (using memcpy)
|
---|
| 469 | template< typename OutputIterator >
|
---|
| 470 | void makeUnionFieldsAssignment( ObjectDecl * srcParam, ObjectDecl * dstParam, OutputIterator out ) {
|
---|
| 471 | UntypedExpr *copy = new UntypedExpr( new NameExpr( "__builtin_memcpy" ) );
|
---|
[49148d5] | 472 | copy->get_args().push_back( new AddressExpr( new VariableExpr( dstParam ) ) );
|
---|
[186fd86] | 473 | copy->get_args().push_back( new AddressExpr( new VariableExpr( srcParam ) ) );
|
---|
| 474 | copy->get_args().push_back( new SizeofExpr( srcParam->get_type()->clone() ) );
|
---|
| 475 | *out++ = new ExprStmt( noLabels, copy );
|
---|
| 476 | }
|
---|
[972e6f7] | 477 |
|
---|
[186fd86] | 478 | /// generates the body of a union assignment/copy constructor/field constructor
|
---|
[d7dc824] | 479 | void makeUnionAssignBody( FunctionDecl * funcDecl ) {
|
---|
[186fd86] | 480 | FunctionType * ftype = funcDecl->get_functionType();
|
---|
| 481 | assert( ftype->get_parameters().size() == 2 );
|
---|
[e3e16bc] | 482 | ObjectDecl * dstParam = strict_dynamic_cast< ObjectDecl * >( ftype->get_parameters().front() );
|
---|
| 483 | ObjectDecl * srcParam = strict_dynamic_cast< ObjectDecl * >( ftype->get_parameters().back() );
|
---|
[972e6f7] | 484 |
|
---|
[186fd86] | 485 | makeUnionFieldsAssignment( srcParam, dstParam, back_inserter( funcDecl->get_statements()->get_kids() ) );
|
---|
[bff227f] | 486 | if ( CodeGen::isAssignment( funcDecl->get_name() ) ) {
|
---|
[49148d5] | 487 | // also generate return statement in assignment
|
---|
[4b0f997] | 488 | funcDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) );
|
---|
[186fd86] | 489 | }
|
---|
| 490 | }
|
---|
[972e6f7] | 491 |
|
---|
[186fd86] | 492 | /// generates union constructors, destructors, and assignment operator
|
---|
| 493 | void makeUnionFunctions( UnionDecl *aggregateDecl, UnionInstType *refType, unsigned int functionNesting, std::list< Declaration * > & declsToAdd ) {
|
---|
| 494 | // Make function polymorphic in same parameters as generic union, if applicable
|
---|
| 495 | const std::list< TypeDecl* > & typeParams = aggregateDecl->get_parameters(); // List of type variables to be placed on the generated functions
|
---|
[49148d5] | 496 |
|
---|
[186fd86] | 497 | // default ctor/dtor need only first parameter
|
---|
| 498 | // void ?{}(T *); void ^?{}(T *);
|
---|
| 499 | FunctionType *ctorType = genDefaultType( refType );
|
---|
| 500 | FunctionType *dtorType = genDefaultType( refType );
|
---|
[972e6f7] | 501 |
|
---|
| 502 | // copy ctor needs both parameters
|
---|
[186fd86] | 503 | // void ?{}(T *, T);
|
---|
| 504 | FunctionType *copyCtorType = genCopyType( refType );
|
---|
[972e6f7] | 505 |
|
---|
| 506 | // assignment needs both and return value
|
---|
[186fd86] | 507 | // T ?=?(T *, T);
|
---|
| 508 | FunctionType *assignType = genAssignType( refType );
|
---|
[1486116] | 509 |
|
---|
| 510 | cloneAll( typeParams, ctorType->get_forall() );
|
---|
| 511 | cloneAll( typeParams, dtorType->get_forall() );
|
---|
| 512 | cloneAll( typeParams, copyCtorType->get_forall() );
|
---|
[186fd86] | 513 | cloneAll( typeParams, assignType->get_forall() );
|
---|
[972e6f7] | 514 |
|
---|
[46adb83] | 515 | // add unused attribute to parameters of default constructor and destructor
|
---|
| 516 | ctorType->get_parameters().front()->get_attributes().push_back( new Attribute( "unused" ) );
|
---|
| 517 | dtorType->get_parameters().front()->get_attributes().push_back( new Attribute( "unused" ) );
|
---|
| 518 |
|
---|
[972e6f7] | 519 | // Routines at global scope marked "static" to prevent multiple definitions is separate translation units
|
---|
| 520 | // because each unit generates copies of the default routines for each aggregate.
|
---|
[186fd86] | 521 | FunctionDecl *assignDecl = genFunc( "?=?", assignType, functionNesting );
|
---|
| 522 | FunctionDecl *ctorDecl = genFunc( "?{}", ctorType, functionNesting );
|
---|
| 523 | FunctionDecl *copyCtorDecl = genFunc( "?{}", copyCtorType, functionNesting );
|
---|
| 524 | FunctionDecl *dtorDecl = genFunc( "^?{}", dtorType, functionNesting );
|
---|
[972e6f7] | 525 |
|
---|
[d7dc824] | 526 | makeUnionAssignBody( assignDecl );
|
---|
[972e6f7] | 527 |
|
---|
| 528 | // body of assignment and copy ctor is the same
|
---|
[d7dc824] | 529 | makeUnionAssignBody( copyCtorDecl );
|
---|
[972e6f7] | 530 |
|
---|
[a465caff] | 531 | // create a constructor which takes the first member type as a parameter.
|
---|
| 532 | // for example, for Union A { int x; double y; }; generate
|
---|
| 533 | // void ?{}(A *, int)
|
---|
| 534 | // This is to mimic C's behaviour which initializes the first member of the union.
|
---|
| 535 | std::list<Declaration *> memCtors;
|
---|
| 536 | for ( Declaration * member : aggregateDecl->get_members() ) {
|
---|
| 537 | if ( DeclarationWithType * field = dynamic_cast< DeclarationWithType * >( member ) ) {
|
---|
[68fe077a] | 538 | ObjectDecl * srcParam = new ObjectDecl( "src", Type::StorageClasses(), LinkageSpec::Cforall, 0, field->get_type()->clone(), 0 );
|
---|
[a465caff] | 539 |
|
---|
| 540 | FunctionType * memCtorType = ctorType->clone();
|
---|
| 541 | memCtorType->get_parameters().push_back( srcParam );
|
---|
[186fd86] | 542 | FunctionDecl * ctor = genFunc( "?{}", memCtorType, functionNesting );
|
---|
[a465caff] | 543 |
|
---|
[d7dc824] | 544 | makeUnionAssignBody( ctor );
|
---|
[a465caff] | 545 | memCtors.push_back( ctor );
|
---|
| 546 | // only generate a ctor for the first field
|
---|
| 547 | break;
|
---|
| 548 | }
|
---|
| 549 | }
|
---|
| 550 |
|
---|
[972e6f7] | 551 | declsToAdd.push_back( ctorDecl );
|
---|
| 552 | declsToAdd.push_back( copyCtorDecl );
|
---|
| 553 | declsToAdd.push_back( dtorDecl );
|
---|
[1486116] | 554 | declsToAdd.push_back( assignDecl ); // assignment should come last since it uses copy constructor in return
|
---|
[a465caff] | 555 | declsToAdd.splice( declsToAdd.end(), memCtors );
|
---|
[972e6f7] | 556 | }
|
---|
| 557 |
|
---|
[207c7e1d] | 558 | AutogenerateRoutines::AutogenerateRoutines() {
|
---|
| 559 | // the order here determines the order that these functions are generated.
|
---|
| 560 | // assignment should come last since it uses copy constructor in return.
|
---|
[e4d6335] | 561 | data.emplace_back( "?{}", genDefaultType, constructable );
|
---|
| 562 | data.emplace_back( "?{}", genCopyType, copyable );
|
---|
| 563 | data.emplace_back( "^?{}", genDefaultType, destructable );
|
---|
| 564 | data.emplace_back( "?=?", genAssignType, assignable );
|
---|
[207c7e1d] | 565 | }
|
---|
| 566 |
|
---|
[8b11840] | 567 | void AutogenerateRoutines::previsit( EnumDecl * enumDecl ) {
|
---|
[e4d6335] | 568 | visit_children = false;
|
---|
[972e6f7] | 569 | if ( ! enumDecl->get_members().empty() ) {
|
---|
| 570 | EnumInstType *enumInst = new EnumInstType( Type::Qualifiers(), enumDecl->get_name() );
|
---|
| 571 | // enumInst->set_baseEnum( enumDecl );
|
---|
[d7dc824] | 572 | makeEnumFunctions( enumInst, functionNesting, declsToAddAfter );
|
---|
[972e6f7] | 573 | }
|
---|
| 574 | }
|
---|
| 575 |
|
---|
[8b11840] | 576 | void AutogenerateRoutines::previsit( StructDecl * structDecl ) {
|
---|
[e4d6335] | 577 | visit_children = false;
|
---|
[8b11840] | 578 | if ( structDecl->has_body() && structsDone.find( structDecl->name ) == structsDone.end() ) {
|
---|
| 579 | StructInstType structInst( Type::Qualifiers(), structDecl->name );
|
---|
| 580 | for ( TypeDecl * typeDecl : structDecl->parameters ) {
|
---|
[207c7e1d] | 581 | // need to visit assertions so that they are added to the appropriate maps
|
---|
[8b11840] | 582 | acceptAll( typeDecl->assertions, *visitor );
|
---|
| 583 | structInst.parameters.push_back( new TypeExpr( new TypeInstType( Type::Qualifiers(), typeDecl->name, typeDecl ) ) );
|
---|
[186fd86] | 584 | }
|
---|
[972e6f7] | 585 | structInst.set_baseStruct( structDecl );
|
---|
[c0aa336] | 586 | makeStructFunctions( structDecl, &structInst, functionNesting, declsToAddAfter, data );
|
---|
[8b11840] | 587 | structsDone.insert( structDecl->name );
|
---|
[972e6f7] | 588 | } // if
|
---|
| 589 | }
|
---|
| 590 |
|
---|
[8b11840] | 591 | void AutogenerateRoutines::previsit( UnionDecl * unionDecl ) {
|
---|
[e4d6335] | 592 | visit_children = false;
|
---|
[972e6f7] | 593 | if ( ! unionDecl->get_members().empty() ) {
|
---|
| 594 | UnionInstType unionInst( Type::Qualifiers(), unionDecl->get_name() );
|
---|
| 595 | unionInst.set_baseUnion( unionDecl );
|
---|
[186fd86] | 596 | for ( TypeDecl * typeDecl : unionDecl->get_parameters() ) {
|
---|
| 597 | unionInst.get_parameters().push_back( new TypeExpr( new TypeInstType( Type::Qualifiers(), typeDecl->get_name(), typeDecl ) ) );
|
---|
| 598 | }
|
---|
[c0aa336] | 599 | makeUnionFunctions( unionDecl, &unionInst, functionNesting, declsToAddAfter );
|
---|
[972e6f7] | 600 | } // if
|
---|
| 601 | }
|
---|
| 602 |
|
---|
[108f3cdb] | 603 | Type * declToTypeDeclBase( Declaration * decl ) {
|
---|
| 604 | if ( TypeDecl * td = dynamic_cast< TypeDecl * >( decl ) ) {
|
---|
| 605 | return td->base;
|
---|
| 606 | }
|
---|
| 607 | return nullptr;
|
---|
| 608 | }
|
---|
| 609 |
|
---|
| 610 | // generate ctor/dtors/assign for typedecls, e.g., otype T = int *;
|
---|
[8b11840] | 611 | void AutogenerateRoutines::previsit( TypeDecl * typeDecl ) {
|
---|
[e4d6335] | 612 | visit_children = false;
|
---|
[108f3cdb] | 613 | if ( ! typeDecl->base ) return;
|
---|
| 614 |
|
---|
| 615 | // generate each of the functions based on the supplied FuncData objects
|
---|
| 616 | std::list< FunctionDecl * > newFuncs;
|
---|
| 617 | std::list< Declaration * > tds { typeDecl };
|
---|
| 618 | std::list< TypeDecl * > typeParams;
|
---|
| 619 | TypeInstType refType( Type::Qualifiers(), typeDecl->name, typeDecl );
|
---|
| 620 | auto generator = makeFuncGenerator( lazy_map( tds, declToTypeDeclBase ), &refType, functionNesting, typeParams, back_inserter( newFuncs ) );
|
---|
| 621 | for ( const FuncData & d : data ) {
|
---|
| 622 | generator.gen( d, false );
|
---|
| 623 | }
|
---|
| 624 |
|
---|
| 625 | if ( functionNesting == 0 ) {
|
---|
| 626 | // forward declare if top-level struct, so that
|
---|
| 627 | // type is complete as soon as its body ends
|
---|
| 628 | // Note: this is necessary if we want structs which contain
|
---|
| 629 | // generic (otype) structs as members.
|
---|
| 630 | for ( FunctionDecl * dcl : newFuncs ) {
|
---|
| 631 | addForwardDecl( dcl, declsToAddAfter );
|
---|
| 632 | }
|
---|
| 633 | }
|
---|
| 634 |
|
---|
| 635 | for ( FunctionDecl * dcl : newFuncs ) {
|
---|
| 636 | FunctionType * ftype = dcl->type;
|
---|
| 637 | assertf( ftype->parameters.size() == 1 || ftype->parameters.size() == 2, "Incorrect number of parameters in autogenerated typedecl function: %zd", ftype->parameters.size() );
|
---|
| 638 | DeclarationWithType * dst = ftype->parameters.front();
|
---|
| 639 | DeclarationWithType * src = ftype->parameters.size() == 2 ? ftype->parameters.back() : nullptr;
|
---|
| 640 | // generate appropriate calls to member ctor, assignment
|
---|
| 641 | // destructor needs to do everything in reverse, so pass "forward" based on whether the function is a destructor
|
---|
| 642 | UntypedExpr * expr = new UntypedExpr( new NameExpr( dcl->name ) );
|
---|
| 643 | expr->args.push_back( new CastExpr( new VariableExpr( dst ), new ReferenceType( Type::Qualifiers(), typeDecl->base->clone() ) ) );
|
---|
| 644 | if ( src ) expr->args.push_back( new CastExpr( new VariableExpr( src ), typeDecl->base->clone() ) );
|
---|
| 645 | dcl->statements->kids.push_back( new ExprStmt( noLabels, expr ) );
|
---|
| 646 | if ( CodeGen::isAssignment( dcl->get_name() ) ) {
|
---|
| 647 | // assignment needs to return a value
|
---|
| 648 | FunctionType * assignType = dcl->type;
|
---|
| 649 | assert( assignType->parameters.size() == 2 );
|
---|
[982832e] | 650 | ObjectDecl * srcParam = strict_dynamic_cast< ObjectDecl * >( assignType->parameters.back() );
|
---|
[108f3cdb] | 651 | dcl->statements->kids.push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) );
|
---|
| 652 | }
|
---|
| 653 | declsToAddAfter.push_back( dcl );
|
---|
| 654 | }
|
---|
[972e6f7] | 655 | }
|
---|
| 656 |
|
---|
[e4d6335] | 657 | void AutogenerateRoutines::previsit( FunctionType *) {
|
---|
[972e6f7] | 658 | // ensure that we don't add assignment ops for types defined as part of the function
|
---|
[e4d6335] | 659 | visit_children = false;
|
---|
[972e6f7] | 660 | }
|
---|
| 661 |
|
---|
[e4d6335] | 662 | void AutogenerateRoutines::previsit( PointerType *) {
|
---|
[972e6f7] | 663 | // ensure that we don't add assignment ops for types defined as part of the pointer
|
---|
[e4d6335] | 664 | visit_children = false;
|
---|
[972e6f7] | 665 | }
|
---|
| 666 |
|
---|
[e4d6335] | 667 | void AutogenerateRoutines::previsit( TraitDecl * ) {
|
---|
[a5a71d0] | 668 | // ensure that we don't add assignment ops for types defined as part of the trait
|
---|
[e4d6335] | 669 | visit_children = false;
|
---|
[972e6f7] | 670 | }
|
---|
| 671 |
|
---|
[e4d6335] | 672 | void AutogenerateRoutines::previsit( FunctionDecl * functionDecl ) {
|
---|
| 673 | visit_children = false;
|
---|
[207c7e1d] | 674 | // record the existence of this function as appropriate
|
---|
| 675 | insert( functionDecl, constructable, InitTweak::isDefaultConstructor );
|
---|
| 676 | insert( functionDecl, assignable, InitTweak::isAssignment );
|
---|
| 677 | insert( functionDecl, copyable, InitTweak::isCopyConstructor );
|
---|
| 678 | insert( functionDecl, destructable, InitTweak::isDestructor );
|
---|
| 679 |
|
---|
[8b11840] | 680 | maybeAccept( functionDecl->type, *visitor );
|
---|
[972e6f7] | 681 | functionNesting += 1;
|
---|
[8b11840] | 682 | maybeAccept( functionDecl->statements, *visitor );
|
---|
[972e6f7] | 683 | functionNesting -= 1;
|
---|
| 684 | }
|
---|
| 685 |
|
---|
[e4d6335] | 686 | void AutogenerateRoutines::previsit( CompoundStmt * ) {
|
---|
| 687 | GuardScope( constructable );
|
---|
| 688 | GuardScope( assignable );
|
---|
| 689 | GuardScope( copyable );
|
---|
| 690 | GuardScope( destructable );
|
---|
| 691 | GuardScope( structsDone );
|
---|
[972e6f7] | 692 | }
|
---|
[1486116] | 693 |
|
---|
| 694 | void makeTupleFunctionBody( FunctionDecl * function ) {
|
---|
| 695 | FunctionType * ftype = function->get_functionType();
|
---|
| 696 | assertf( ftype->get_parameters().size() == 1 || ftype->get_parameters().size() == 2, "too many parameters in generated tuple function" );
|
---|
| 697 |
|
---|
| 698 | UntypedExpr * untyped = new UntypedExpr( new NameExpr( function->get_name() ) );
|
---|
| 699 |
|
---|
| 700 | /// xxx - &* is used to make this easier for later passes to handle
|
---|
| 701 | untyped->get_args().push_back( new AddressExpr( UntypedExpr::createDeref( new VariableExpr( ftype->get_parameters().front() ) ) ) );
|
---|
| 702 | if ( ftype->get_parameters().size() == 2 ) {
|
---|
| 703 | untyped->get_args().push_back( new VariableExpr( ftype->get_parameters().back() ) );
|
---|
| 704 | }
|
---|
| 705 | function->get_statements()->get_kids().push_back( new ExprStmt( noLabels, untyped ) );
|
---|
| 706 | function->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, UntypedExpr::createDeref( new VariableExpr( ftype->get_parameters().front() ) ) ) );
|
---|
| 707 | }
|
---|
| 708 |
|
---|
| 709 | Type * AutogenTupleRoutines::mutate( TupleType * tupleType ) {
|
---|
[e3e16bc] | 710 | tupleType = strict_dynamic_cast< TupleType * >( Parent::mutate( tupleType ) );
|
---|
[1486116] | 711 | std::string mangleName = SymTab::Mangler::mangleType( tupleType );
|
---|
| 712 | if ( seenTuples.find( mangleName ) != seenTuples.end() ) return tupleType;
|
---|
| 713 | seenTuples.insert( mangleName );
|
---|
| 714 |
|
---|
| 715 | // T ?=?(T *, T);
|
---|
| 716 | FunctionType *assignType = genAssignType( tupleType );
|
---|
| 717 |
|
---|
| 718 | // void ?{}(T *); void ^?{}(T *);
|
---|
| 719 | FunctionType *ctorType = genDefaultType( tupleType );
|
---|
| 720 | FunctionType *dtorType = genDefaultType( tupleType );
|
---|
| 721 |
|
---|
| 722 | // void ?{}(T *, T);
|
---|
| 723 | FunctionType *copyCtorType = genCopyType( tupleType );
|
---|
| 724 |
|
---|
| 725 | std::set< TypeDecl* > done;
|
---|
| 726 | std::list< TypeDecl * > typeParams;
|
---|
| 727 | for ( Type * t : *tupleType ) {
|
---|
| 728 | if ( TypeInstType * ty = dynamic_cast< TypeInstType * >( t ) ) {
|
---|
| 729 | if ( ! done.count( ty->get_baseType() ) ) {
|
---|
[68fe077a] | 730 | TypeDecl * newDecl = new TypeDecl( ty->get_baseType()->get_name(), Type::StorageClasses(), nullptr, TypeDecl::Any );
|
---|
[1486116] | 731 | TypeInstType * inst = new TypeInstType( Type::Qualifiers(), newDecl->get_name(), newDecl );
|
---|
[68fe077a] | 732 | newDecl->get_assertions().push_back( new FunctionDecl( "?=?", Type::StorageClasses(), LinkageSpec::Cforall, genAssignType( inst ), nullptr,
|
---|
[ddfd945] | 733 | std::list< Attribute * >(), Type::FuncSpecifiers( Type::Inline ) ) );
|
---|
[68fe077a] | 734 | newDecl->get_assertions().push_back( new FunctionDecl( "?{}", Type::StorageClasses(), LinkageSpec::Cforall, genDefaultType( inst ), nullptr,
|
---|
[ddfd945] | 735 | std::list< Attribute * >(), Type::FuncSpecifiers( Type::Inline ) ) );
|
---|
[68fe077a] | 736 | newDecl->get_assertions().push_back( new FunctionDecl( "?{}", Type::StorageClasses(), LinkageSpec::Cforall, genCopyType( inst ), nullptr,
|
---|
[ddfd945] | 737 | std::list< Attribute * >(), Type::FuncSpecifiers( Type::Inline ) ) );
|
---|
[68fe077a] | 738 | newDecl->get_assertions().push_back( new FunctionDecl( "^?{}", Type::StorageClasses(), LinkageSpec::Cforall, genDefaultType( inst ), nullptr,
|
---|
[ddfd945] | 739 | std::list< Attribute * >(), Type::FuncSpecifiers( Type::Inline ) ) );
|
---|
[1486116] | 740 | typeParams.push_back( newDecl );
|
---|
| 741 | done.insert( ty->get_baseType() );
|
---|
| 742 | }
|
---|
| 743 | }
|
---|
| 744 | }
|
---|
| 745 | cloneAll( typeParams, ctorType->get_forall() );
|
---|
| 746 | cloneAll( typeParams, dtorType->get_forall() );
|
---|
| 747 | cloneAll( typeParams, copyCtorType->get_forall() );
|
---|
| 748 | cloneAll( typeParams, assignType->get_forall() );
|
---|
| 749 |
|
---|
| 750 | FunctionDecl *assignDecl = genFunc( "?=?", assignType, functionNesting );
|
---|
| 751 | FunctionDecl *ctorDecl = genFunc( "?{}", ctorType, functionNesting );
|
---|
| 752 | FunctionDecl *copyCtorDecl = genFunc( "?{}", copyCtorType, functionNesting );
|
---|
| 753 | FunctionDecl *dtorDecl = genFunc( "^?{}", dtorType, functionNesting );
|
---|
| 754 |
|
---|
| 755 | makeTupleFunctionBody( assignDecl );
|
---|
| 756 | makeTupleFunctionBody( ctorDecl );
|
---|
| 757 | makeTupleFunctionBody( copyCtorDecl );
|
---|
| 758 | makeTupleFunctionBody( dtorDecl );
|
---|
| 759 |
|
---|
| 760 | addDeclaration( ctorDecl );
|
---|
| 761 | addDeclaration( copyCtorDecl );
|
---|
| 762 | addDeclaration( dtorDecl );
|
---|
| 763 | addDeclaration( assignDecl ); // assignment should come last since it uses copy constructor in return
|
---|
| 764 |
|
---|
| 765 | return tupleType;
|
---|
| 766 | }
|
---|
| 767 |
|
---|
| 768 | DeclarationWithType * AutogenTupleRoutines::mutate( FunctionDecl *functionDecl ) {
|
---|
| 769 | functionDecl->set_functionType( maybeMutate( functionDecl->get_functionType(), *this ) );
|
---|
| 770 | functionNesting += 1;
|
---|
| 771 | functionDecl->set_statements( maybeMutate( functionDecl->get_statements(), *this ) );
|
---|
| 772 | functionNesting -= 1;
|
---|
| 773 | return functionDecl;
|
---|
| 774 | }
|
---|
| 775 |
|
---|
| 776 | CompoundStmt * AutogenTupleRoutines::mutate( CompoundStmt *compoundStmt ) {
|
---|
| 777 | seenTuples.beginScope();
|
---|
[e3e16bc] | 778 | compoundStmt = strict_dynamic_cast< CompoundStmt * >( Parent::mutate( compoundStmt ) );
|
---|
[1486116] | 779 | seenTuples.endScope();
|
---|
| 780 | return compoundStmt;
|
---|
| 781 | }
|
---|
[972e6f7] | 782 | } // SymTab
|
---|
[c0aa336] | 783 |
|
---|
| 784 | // Local Variables: //
|
---|
| 785 | // tab-width: 4 //
|
---|
| 786 | // mode: c++ //
|
---|
| 787 | // compile-command: "make install" //
|
---|
| 788 | // End: //
|
---|