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