[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 |
---|
| 12 | // Last Modified On : Tue Jul 12 17:47:17 2016 |
---|
| 13 | // Update Count : 2 |
---|
[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" |
---|
| 26 | |
---|
| 27 | namespace SymTab { |
---|
[5f98ce5] | 28 | Type * SizeType = 0; |
---|
| 29 | |
---|
[972e6f7] | 30 | class AutogenerateRoutines : public Visitor { |
---|
| 31 | public: |
---|
| 32 | std::list< Declaration * > &get_declsToAdd() { return declsToAdd; } |
---|
| 33 | |
---|
| 34 | virtual void visit( EnumDecl *enumDecl ); |
---|
| 35 | virtual void visit( StructDecl *structDecl ); |
---|
| 36 | virtual void visit( UnionDecl *structDecl ); |
---|
| 37 | virtual void visit( TypeDecl *typeDecl ); |
---|
[a5a71d0] | 38 | virtual void visit( TraitDecl *ctxDecl ); |
---|
[972e6f7] | 39 | virtual void visit( FunctionDecl *functionDecl ); |
---|
| 40 | |
---|
| 41 | virtual void visit( FunctionType *ftype ); |
---|
| 42 | virtual void visit( PointerType *ftype ); |
---|
| 43 | |
---|
| 44 | virtual void visit( CompoundStmt *compoundStmt ); |
---|
| 45 | virtual void visit( SwitchStmt *switchStmt ); |
---|
| 46 | |
---|
| 47 | AutogenerateRoutines() : functionNesting( 0 ) {} |
---|
| 48 | private: |
---|
| 49 | template< typename StmtClass > void visitStatement( StmtClass *stmt ); |
---|
| 50 | |
---|
| 51 | std::list< Declaration * > declsToAdd; |
---|
| 52 | std::set< std::string > structsDone; |
---|
| 53 | unsigned int functionNesting; // current level of nested functions |
---|
| 54 | }; |
---|
| 55 | |
---|
| 56 | void autogenerateRoutines( std::list< Declaration * > &translationUnit ) { |
---|
| 57 | AutogenerateRoutines visitor; |
---|
| 58 | acceptAndAdd( translationUnit, visitor, false ); |
---|
| 59 | } |
---|
| 60 | |
---|
[356189a] | 61 | bool isUnnamedBitfield( ObjectDecl * obj ) { |
---|
| 62 | return obj != NULL && obj->get_name() == "" && obj->get_bitfieldWidth() != NULL; |
---|
| 63 | } |
---|
| 64 | |
---|
[972e6f7] | 65 | template< typename OutputIterator > |
---|
[9b4c936] | 66 | void makeUnionFieldsAssignment( ObjectDecl *srcParam, ObjectDecl *dstParam, OutputIterator out ) { |
---|
[972e6f7] | 67 | UntypedExpr *copy = new UntypedExpr( new NameExpr( "__builtin_memcpy" ) ); |
---|
| 68 | copy->get_args().push_back( new VariableExpr( dstParam ) ); |
---|
| 69 | copy->get_args().push_back( new AddressExpr( new VariableExpr( srcParam ) ) ); |
---|
[a465caf] | 70 | copy->get_args().push_back( new SizeofExpr( srcParam->get_type()->clone() ) ); |
---|
[972e6f7] | 71 | |
---|
| 72 | *out++ = new ExprStmt( noLabels, copy ); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | //E ?=?(E volatile*, int), |
---|
| 76 | // ?=?(E _Atomic volatile*, int); |
---|
[4cc4286] | 77 | void makeEnumFunctions( EnumDecl *enumDecl, EnumInstType *refType, unsigned int functionNesting, std::list< Declaration * > &declsToAdd ) { |
---|
[972e6f7] | 78 | FunctionType *assignType = new FunctionType( Type::Qualifiers(), false ); |
---|
| 79 | |
---|
[d0f8b19] | 80 | ObjectDecl *dstParam = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), refType ), 0 ); |
---|
[972e6f7] | 81 | assignType->get_parameters().push_back( dstParam ); |
---|
| 82 | |
---|
[d0f8b19] | 83 | // void ?{}(E *); void ^?{}(E *); |
---|
| 84 | FunctionType * ctorType = assignType->clone(); |
---|
| 85 | FunctionType * dtorType = assignType->clone(); |
---|
| 86 | |
---|
| 87 | ObjectDecl *srcParam = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, refType->clone(), 0 ); |
---|
[972e6f7] | 88 | assignType->get_parameters().push_back( srcParam ); |
---|
[d0f8b19] | 89 | // void ?{}(E *, E); |
---|
| 90 | FunctionType *copyCtorType = assignType->clone(); |
---|
| 91 | |
---|
| 92 | // T ?=?(E *, E); |
---|
| 93 | ObjectDecl *returnVal = new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, refType->clone(), 0 ); |
---|
| 94 | assignType->get_returnVals().push_back( returnVal ); |
---|
[972e6f7] | 95 | |
---|
[d0f8b19] | 96 | // xxx - should we also generate void ?{}(E *, int) and E ?{}(E *, E)? |
---|
| 97 | // right now these cases work, but that might change. |
---|
[972e6f7] | 98 | |
---|
| 99 | // Routines at global scope marked "static" to prevent multiple definitions is separate translation units |
---|
| 100 | // because each unit generates copies of the default routines for each aggregate. |
---|
[d0f8b19] | 101 | // xxx - Temporary: make these functions intrinsic so they codegen as C assignment. |
---|
| 102 | // Really they're something of a cross between instrinsic and autogen, so should |
---|
| 103 | // probably make a new linkage type |
---|
| 104 | FunctionDecl *assignDecl = new FunctionDecl( "?=?", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::Intrinsic, assignType, new CompoundStmt( noLabels ), true, false ); |
---|
| 105 | FunctionDecl *ctorDecl = new FunctionDecl( "?{}", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::Intrinsic, ctorType, new CompoundStmt( noLabels ), true, false ); |
---|
| 106 | FunctionDecl *copyCtorDecl = new FunctionDecl( "?{}", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::Intrinsic, copyCtorType, new CompoundStmt( noLabels ), true, false ); |
---|
| 107 | FunctionDecl *dtorDecl = new FunctionDecl( "^?{}", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::Intrinsic, dtorType, new CompoundStmt( noLabels ), true, false ); |
---|
[972e6f7] | 108 | assignDecl->fixUniqueId(); |
---|
[d0f8b19] | 109 | ctorDecl->fixUniqueId(); |
---|
| 110 | copyCtorDecl->fixUniqueId(); |
---|
| 111 | dtorDecl->fixUniqueId(); |
---|
| 112 | |
---|
| 113 | // enum copy construct and assignment is just C-style assignment. |
---|
| 114 | // this looks like a bad recursive call, but code gen will turn it into |
---|
| 115 | // a C-style assignment. |
---|
| 116 | // This happens before function pointer type conversion, so need to do it manually here |
---|
| 117 | VariableExpr * assignVarExpr = new VariableExpr( assignDecl ); |
---|
| 118 | Type *& assignVarExprType = assignVarExpr->get_results().front(); |
---|
| 119 | assignVarExprType = new PointerType( Type::Qualifiers(), assignVarExprType ); |
---|
| 120 | ApplicationExpr * assignExpr = new ApplicationExpr( assignVarExpr ); |
---|
| 121 | assignExpr->get_args().push_back( new VariableExpr( dstParam ) ); |
---|
| 122 | assignExpr->get_args().push_back( new VariableExpr( srcParam ) ); |
---|
| 123 | |
---|
| 124 | // body is either return stmt or expr stmt |
---|
| 125 | assignDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, assignExpr ) ); |
---|
| 126 | copyCtorDecl->get_statements()->get_kids().push_back( new ExprStmt( noLabels, assignExpr->clone() ) ); |
---|
| 127 | |
---|
| 128 | declsToAdd.push_back( assignDecl ); |
---|
| 129 | declsToAdd.push_back( ctorDecl ); |
---|
| 130 | declsToAdd.push_back( copyCtorDecl ); |
---|
| 131 | declsToAdd.push_back( dtorDecl ); |
---|
[972e6f7] | 132 | } |
---|
| 133 | |
---|
| 134 | /// Clones a reference type, replacing any parameters it may have with a clone of the provided list |
---|
| 135 | template< typename GenericInstType > |
---|
| 136 | GenericInstType *cloneWithParams( GenericInstType *refType, const std::list< Expression* >& params ) { |
---|
| 137 | GenericInstType *clone = refType->clone(); |
---|
| 138 | clone->get_parameters().clear(); |
---|
| 139 | cloneAll( params, clone->get_parameters() ); |
---|
| 140 | return clone; |
---|
| 141 | } |
---|
| 142 | |
---|
[d8ba086] | 143 | /// Creates a new type decl that's the same as src, but renamed and with only the ?=?, ?{} (default and copy), and ^?{} assertions (for complete types only) |
---|
[972e6f7] | 144 | TypeDecl *cloneAndRename( TypeDecl *src, const std::string &name ) { |
---|
[9554d9b] | 145 | // TypeDecl *dst = new TypeDecl( name, src->get_storageClass(), 0, src->get_kind() ); |
---|
| 146 | |
---|
| 147 | // if ( src->get_kind() == TypeDecl::Any ) { |
---|
| 148 | // TypeInstType *opParamType = new TypeInstType( Type::Qualifiers(), name, dst ); |
---|
| 149 | // FunctionType *opFunctionType = new FunctionType( Type::Qualifiers(), false ); |
---|
| 150 | // opFunctionType->get_parameters().push_back( |
---|
| 151 | // new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), opParamType->clone() ), 0 ) ); |
---|
| 152 | // FunctionDecl *ctorAssert = new FunctionDecl( "?{}", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, opFunctionType->clone(), 0, false, false ); |
---|
| 153 | // FunctionDecl *dtorAssert = new FunctionDecl( "^?{}", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, opFunctionType->clone(), 0, false, false ); |
---|
| 154 | |
---|
| 155 | // opFunctionType->get_parameters().push_back( |
---|
| 156 | // new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, opParamType, 0 ) ); |
---|
| 157 | // FunctionDecl *copyCtorAssert = new FunctionDecl( "?{}", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, opFunctionType->clone(), 0, false, false ); |
---|
| 158 | |
---|
| 159 | // opFunctionType->get_returnVals().push_back( |
---|
| 160 | // new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, opParamType->clone(), 0 ) ); |
---|
| 161 | // FunctionDecl *assignAssert = new FunctionDecl( "?=?", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, opFunctionType, 0, false, false ); |
---|
| 162 | |
---|
| 163 | |
---|
| 164 | // dst->get_assertions().push_back( assignAssert ); |
---|
| 165 | // dst->get_assertions().push_back( ctorAssert ); |
---|
| 166 | // dst->get_assertions().push_back( dtorAssert ); |
---|
| 167 | // dst->get_assertions().push_back( copyCtorAssert ); |
---|
| 168 | // } |
---|
| 169 | |
---|
[2e3a379] | 170 | TypeDecl *dst = new TypeDecl( src->get_name(), src->get_storageClass(), 0, src->get_kind() ); |
---|
| 171 | cloneAll(src->get_assertions(), dst->get_assertions()); |
---|
[972e6f7] | 172 | return dst; |
---|
| 173 | } |
---|
| 174 | |
---|
[a2f920f] | 175 | void makeStructMemberOp( ObjectDecl * dstParam, Expression * src, DeclarationWithType * field, FunctionDecl * func, TypeSubstitution & genericSubs, bool isDynamicLayout, bool forward = true ) { |
---|
[3bb195cb] | 176 | // if ( isDynamicLayout && src ) { |
---|
| 177 | // genericSubs.apply( src ); |
---|
| 178 | // } |
---|
[dac0aa9] | 179 | |
---|
| 180 | ObjectDecl * returnVal = NULL; |
---|
| 181 | if ( ! func->get_functionType()->get_returnVals().empty() ) { |
---|
| 182 | returnVal = dynamic_cast<ObjectDecl*>( func->get_functionType()->get_returnVals().front() ); |
---|
| 183 | } |
---|
| 184 | |
---|
[39f84a4] | 185 | InitTweak::InitExpander srcParam( src ); |
---|
| 186 | |
---|
[dac0aa9] | 187 | // assign to destination (and return value if generic) |
---|
[2be1023] | 188 | UntypedExpr *derefExpr = new UntypedExpr( new NameExpr( "*?" ) ); |
---|
| 189 | derefExpr->get_args().push_back( new VariableExpr( dstParam ) ); |
---|
| 190 | Expression *dstselect = new MemberExpr( field, derefExpr ); |
---|
[39f84a4] | 191 | genImplicitCall( srcParam, dstselect, func->get_name(), back_inserter( func->get_statements()->get_kids() ), field, forward ); |
---|
[2be1023] | 192 | |
---|
[c331406] | 193 | if ( isDynamicLayout && returnVal ) { |
---|
[2be1023] | 194 | UntypedExpr *derefRet = new UntypedExpr( new NameExpr( "*?" ) ); |
---|
| 195 | derefRet->get_args().push_back( new VariableExpr( returnVal ) ); |
---|
| 196 | Expression *retselect = new MemberExpr( field, derefRet ); |
---|
[39f84a4] | 197 | genImplicitCall( srcParam, retselect, func->get_name(), back_inserter( func->get_statements()->get_kids() ), field, forward ); |
---|
[dac0aa9] | 198 | } // if |
---|
| 199 | } |
---|
| 200 | |
---|
[972e6f7] | 201 | template<typename Iterator> |
---|
[a2f920f] | 202 | void makeStructFunctionBody( Iterator member, Iterator end, FunctionDecl * func, TypeSubstitution & genericSubs, bool isDynamicLayout, bool forward = true ) { |
---|
[972e6f7] | 203 | for ( ; member != end; ++member ) { |
---|
[dac0aa9] | 204 | if ( DeclarationWithType *field = dynamic_cast< DeclarationWithType * >( *member ) ) { // otherwise some form of type declaration, e.g. Aggregate |
---|
[972e6f7] | 205 | // query the type qualifiers of this field and skip assigning it if it is marked const. |
---|
| 206 | // If it is an array type, we need to strip off the array layers to find its qualifiers. |
---|
[dac0aa9] | 207 | Type * type = field->get_type(); |
---|
[972e6f7] | 208 | while ( ArrayType * at = dynamic_cast< ArrayType * >( type ) ) { |
---|
| 209 | type = at->get_base(); |
---|
| 210 | } |
---|
| 211 | |
---|
[cad355a] | 212 | if ( type->get_qualifiers().isConst && func->get_name() == "?=?" ) { |
---|
| 213 | // don't assign const members, but do construct/destruct |
---|
[972e6f7] | 214 | continue; |
---|
| 215 | } |
---|
| 216 | |
---|
[fb24492] | 217 | if ( field->get_name() == "" ) { |
---|
| 218 | // don't assign to anonymous members |
---|
| 219 | // xxx - this is a temporary fix. Anonymous members tie into |
---|
| 220 | // our inheritance model. I think the correct way to handle this is to |
---|
| 221 | // cast the structure to the type of the member and let the resolver |
---|
| 222 | // figure out whether it's valid and have a pass afterwards that fixes |
---|
| 223 | // the assignment to use pointer arithmetic with the offset of the |
---|
| 224 | // member, much like how generic type members are handled. |
---|
| 225 | continue; |
---|
| 226 | } |
---|
| 227 | |
---|
[972e6f7] | 228 | assert( ! func->get_functionType()->get_parameters().empty() ); |
---|
| 229 | ObjectDecl * dstParam = dynamic_cast<ObjectDecl*>( func->get_functionType()->get_parameters().front() ); |
---|
| 230 | ObjectDecl * srcParam = NULL; |
---|
| 231 | if ( func->get_functionType()->get_parameters().size() == 2 ) { |
---|
| 232 | srcParam = dynamic_cast<ObjectDecl*>( func->get_functionType()->get_parameters().back() ); |
---|
| 233 | } |
---|
| 234 | // srcParam may be NULL, in which case we have default ctor/dtor |
---|
| 235 | assert( dstParam ); |
---|
| 236 | |
---|
[dac0aa9] | 237 | Expression *srcselect = srcParam ? new MemberExpr( field, new VariableExpr( srcParam ) ) : NULL; |
---|
[a2f920f] | 238 | makeStructMemberOp( dstParam, srcselect, field, func, genericSubs, isDynamicLayout, forward ); |
---|
[972e6f7] | 239 | } // if |
---|
| 240 | } // for |
---|
| 241 | } // makeStructFunctionBody |
---|
| 242 | |
---|
[dac0aa9] | 243 | /// generate the body of a constructor which takes parameters that match fields, e.g. |
---|
| 244 | /// void ?{}(A *, int) and void?{}(A *, int, int) for a struct A which has two int fields. |
---|
| 245 | template<typename Iterator> |
---|
[a2f920f] | 246 | void makeStructFieldCtorBody( Iterator member, Iterator end, FunctionDecl * func, TypeSubstitution & genericSubs, bool isDynamicLayout ) { |
---|
[dac0aa9] | 247 | FunctionType * ftype = func->get_functionType(); |
---|
| 248 | std::list<DeclarationWithType*> & params = ftype->get_parameters(); |
---|
| 249 | assert( params.size() >= 2 ); // should not call this function for default ctor, etc. |
---|
| 250 | |
---|
| 251 | // skip 'this' parameter |
---|
| 252 | ObjectDecl * dstParam = dynamic_cast<ObjectDecl*>( params.front() ); |
---|
| 253 | assert( dstParam ); |
---|
| 254 | std::list<DeclarationWithType*>::iterator parameter = params.begin()+1; |
---|
| 255 | for ( ; member != end; ++member ) { |
---|
| 256 | if ( DeclarationWithType * field = dynamic_cast<DeclarationWithType*>( *member ) ) { |
---|
[0b4d93ab] | 257 | if ( isUnnamedBitfield( dynamic_cast< ObjectDecl * > ( field ) ) ) { |
---|
| 258 | // don't make a function whose parameter is an unnamed bitfield |
---|
| 259 | continue; |
---|
| 260 | } else if ( field->get_name() == "" ) { |
---|
| 261 | // don't assign to anonymous members |
---|
| 262 | // xxx - this is a temporary fix. Anonymous members tie into |
---|
| 263 | // our inheritance model. I think the correct way to handle this is to |
---|
| 264 | // cast the structure to the type of the member and let the resolver |
---|
| 265 | // figure out whether it's valid and have a pass afterwards that fixes |
---|
| 266 | // the assignment to use pointer arithmetic with the offset of the |
---|
| 267 | // member, much like how generic type members are handled. |
---|
| 268 | continue; |
---|
| 269 | } else if ( parameter != params.end() ) { |
---|
[dac0aa9] | 270 | // matching parameter, initialize field with copy ctor |
---|
| 271 | Expression *srcselect = new VariableExpr(*parameter); |
---|
[a2f920f] | 272 | makeStructMemberOp( dstParam, srcselect, field, func, genericSubs, isDynamicLayout ); |
---|
[dac0aa9] | 273 | ++parameter; |
---|
| 274 | } else { |
---|
| 275 | // no matching parameter, initialize field with default ctor |
---|
[a2f920f] | 276 | makeStructMemberOp( dstParam, NULL, field, func, genericSubs, isDynamicLayout ); |
---|
[dac0aa9] | 277 | } |
---|
| 278 | } |
---|
| 279 | } |
---|
| 280 | } |
---|
| 281 | |
---|
[9f70ab57] | 282 | void addForwardDecl( FunctionDecl * functionDecl, std::list< Declaration * > & declsToAdd ) { |
---|
| 283 | FunctionDecl * decl = functionDecl->clone(); |
---|
| 284 | delete decl->get_statements(); |
---|
| 285 | decl->set_statements( NULL ); |
---|
| 286 | declsToAdd.push_back( decl ); |
---|
| 287 | decl->fixUniqueId(); |
---|
| 288 | } |
---|
| 289 | |
---|
[972e6f7] | 290 | void makeStructFunctions( StructDecl *aggregateDecl, StructInstType *refType, unsigned int functionNesting, std::list< Declaration * > & declsToAdd ) { |
---|
| 291 | FunctionType *assignType = new FunctionType( Type::Qualifiers(), false ); |
---|
| 292 | |
---|
| 293 | // Make function polymorphic in same parameters as generic struct, if applicable |
---|
[a2f920f] | 294 | bool isDynamicLayout = false; // NOTE this flag is an incredibly ugly kludge; we should fix the assignment signature instead (ditto for union) |
---|
[972e6f7] | 295 | std::list< TypeDecl* >& genericParams = aggregateDecl->get_parameters(); |
---|
| 296 | std::list< Expression* > structParams; // List of matching parameters to put on types |
---|
| 297 | TypeSubstitution genericSubs; // Substitutions to make to member types of struct |
---|
| 298 | for ( std::list< TypeDecl* >::const_iterator param = genericParams.begin(); param != genericParams.end(); ++param ) { |
---|
[5070fe4] | 299 | if ( (*param)->get_kind() == TypeDecl::Any ) isDynamicLayout = true; |
---|
[972e6f7] | 300 | TypeDecl *typeParam = cloneAndRename( *param, "_autoassign_" + aggregateDecl->get_name() + "_" + (*param)->get_name() ); |
---|
| 301 | assignType->get_forall().push_back( typeParam ); |
---|
| 302 | TypeInstType *newParamType = new TypeInstType( Type::Qualifiers(), typeParam->get_name(), typeParam ); |
---|
| 303 | genericSubs.add( (*param)->get_name(), newParamType ); |
---|
| 304 | structParams.push_back( new TypeExpr( newParamType ) ); |
---|
| 305 | } |
---|
| 306 | |
---|
| 307 | ObjectDecl *dstParam = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), cloneWithParams( refType, structParams ) ), 0 ); |
---|
| 308 | assignType->get_parameters().push_back( dstParam ); |
---|
| 309 | |
---|
| 310 | // void ?{}(T *); void ^?{}(T *); |
---|
| 311 | FunctionType *ctorType = assignType->clone(); |
---|
| 312 | FunctionType *dtorType = assignType->clone(); |
---|
| 313 | |
---|
| 314 | ObjectDecl *srcParam = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, cloneWithParams( refType, structParams ), 0 ); |
---|
| 315 | assignType->get_parameters().push_back( srcParam ); |
---|
| 316 | |
---|
| 317 | // void ?{}(T *, T); |
---|
| 318 | FunctionType *copyCtorType = assignType->clone(); |
---|
| 319 | |
---|
| 320 | // T ?=?(T *, T); |
---|
| 321 | ObjectDecl *returnVal = new ObjectDecl( "_ret", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, cloneWithParams( refType, structParams ), 0 ); |
---|
| 322 | assignType->get_returnVals().push_back( returnVal ); |
---|
| 323 | |
---|
| 324 | // Routines at global scope marked "static" to prevent multiple definitions is separate translation units |
---|
| 325 | // because each unit generates copies of the default routines for each aggregate. |
---|
| 326 | FunctionDecl *assignDecl = new FunctionDecl( "?=?", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, assignType, new CompoundStmt( noLabels ), true, false ); |
---|
| 327 | FunctionDecl *ctorDecl = new FunctionDecl( "?{}", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, ctorType, new CompoundStmt( noLabels ), true, false ); |
---|
| 328 | FunctionDecl *copyCtorDecl = new FunctionDecl( "?{}", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, copyCtorType, new CompoundStmt( noLabels ), true, false ); |
---|
| 329 | FunctionDecl *dtorDecl = new FunctionDecl( "^?{}", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, dtorType, new CompoundStmt( noLabels ), true, false ); |
---|
| 330 | assignDecl->fixUniqueId(); |
---|
| 331 | ctorDecl->fixUniqueId(); |
---|
| 332 | copyCtorDecl->fixUniqueId(); |
---|
| 333 | dtorDecl->fixUniqueId(); |
---|
| 334 | |
---|
[9f70ab57] | 335 | if ( functionNesting == 0 ) { |
---|
| 336 | // forward declare if top-level struct, so that |
---|
| 337 | // type is complete as soon as its body ends |
---|
| 338 | addForwardDecl( assignDecl, declsToAdd ); |
---|
| 339 | addForwardDecl( ctorDecl, declsToAdd ); |
---|
| 340 | addForwardDecl( copyCtorDecl, declsToAdd ); |
---|
| 341 | addForwardDecl( dtorDecl, declsToAdd ); |
---|
| 342 | } |
---|
| 343 | |
---|
[dac0aa9] | 344 | // create constructors which take each member type as a parameter. |
---|
[f5ef08c] | 345 | // for example, for struct A { int x, y; }; generate |
---|
| 346 | // void ?{}(A *, int) and void ?{}(A *, int, int) |
---|
[dac0aa9] | 347 | std::list<Declaration *> memCtors; |
---|
| 348 | FunctionType * memCtorType = ctorType->clone(); |
---|
| 349 | for ( std::list<Declaration *>::iterator i = aggregateDecl->get_members().begin(); i != aggregateDecl->get_members().end(); ++i ) { |
---|
| 350 | DeclarationWithType * member = dynamic_cast<DeclarationWithType *>( *i ); |
---|
| 351 | assert( member ); |
---|
[356189a] | 352 | if ( isUnnamedBitfield( dynamic_cast< ObjectDecl * > ( member ) ) ) { |
---|
| 353 | // don't make a function whose parameter is an unnamed bitfield |
---|
| 354 | continue; |
---|
[fb24492] | 355 | } else if ( member->get_name() == "" ) { |
---|
| 356 | // don't assign to anonymous members |
---|
| 357 | // xxx - this is a temporary fix. Anonymous members tie into |
---|
| 358 | // our inheritance model. I think the correct way to handle this is to |
---|
| 359 | // cast the structure to the type of the member and let the resolver |
---|
| 360 | // figure out whether it's valid and have a pass afterwards that fixes |
---|
| 361 | // the assignment to use pointer arithmetic with the offset of the |
---|
| 362 | // member, much like how generic type members are handled. |
---|
| 363 | continue; |
---|
[356189a] | 364 | } |
---|
[dac0aa9] | 365 | memCtorType->get_parameters().push_back( new ObjectDecl( member->get_name(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, member->get_type()->clone(), 0 ) ); |
---|
| 366 | FunctionDecl * ctor = new FunctionDecl( "?{}", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, memCtorType->clone(), new CompoundStmt( noLabels ), true, false ); |
---|
| 367 | ctor->fixUniqueId(); |
---|
[a2f920f] | 368 | makeStructFieldCtorBody( aggregateDecl->get_members().begin(), aggregateDecl->get_members().end(), ctor, genericSubs, isDynamicLayout ); |
---|
[dac0aa9] | 369 | memCtors.push_back( ctor ); |
---|
| 370 | } |
---|
| 371 | delete memCtorType; |
---|
| 372 | |
---|
[972e6f7] | 373 | // generate appropriate calls to member ctor, assignment |
---|
[a2f920f] | 374 | makeStructFunctionBody( aggregateDecl->get_members().begin(), aggregateDecl->get_members().end(), assignDecl, genericSubs, isDynamicLayout ); |
---|
| 375 | makeStructFunctionBody( aggregateDecl->get_members().begin(), aggregateDecl->get_members().end(), ctorDecl, genericSubs, isDynamicLayout ); |
---|
| 376 | makeStructFunctionBody( aggregateDecl->get_members().begin(), aggregateDecl->get_members().end(), copyCtorDecl, genericSubs, isDynamicLayout ); |
---|
[972e6f7] | 377 | // needs to do everything in reverse, so pass "forward" as false |
---|
[a2f920f] | 378 | makeStructFunctionBody( aggregateDecl->get_members().rbegin(), aggregateDecl->get_members().rend(), dtorDecl, genericSubs, isDynamicLayout, false ); |
---|
[972e6f7] | 379 | |
---|
[a2f920f] | 380 | if ( ! isDynamicLayout ) assignDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) ); |
---|
[972e6f7] | 381 | |
---|
| 382 | declsToAdd.push_back( assignDecl ); |
---|
| 383 | declsToAdd.push_back( ctorDecl ); |
---|
| 384 | declsToAdd.push_back( copyCtorDecl ); |
---|
| 385 | declsToAdd.push_back( dtorDecl ); |
---|
[dac0aa9] | 386 | declsToAdd.splice( declsToAdd.end(), memCtors ); |
---|
[972e6f7] | 387 | } |
---|
| 388 | |
---|
| 389 | void makeUnionFunctions( UnionDecl *aggregateDecl, UnionInstType *refType, unsigned int functionNesting, std::list< Declaration * > & declsToAdd ) { |
---|
| 390 | FunctionType *assignType = new FunctionType( Type::Qualifiers(), false ); |
---|
| 391 | |
---|
| 392 | // Make function polymorphic in same parameters as generic union, if applicable |
---|
[5070fe4] | 393 | bool isDynamicLayout = false; // NOTE this flag is an incredibly ugly kludge; we should fix the assignment signature instead (ditto for struct) |
---|
[972e6f7] | 394 | std::list< TypeDecl* >& genericParams = aggregateDecl->get_parameters(); |
---|
| 395 | std::list< Expression* > unionParams; // List of matching parameters to put on types |
---|
| 396 | for ( std::list< TypeDecl* >::const_iterator param = genericParams.begin(); param != genericParams.end(); ++param ) { |
---|
[5070fe4] | 397 | if ( (*param)->get_kind() == TypeDecl::Any ) isDynamicLayout = true; |
---|
[972e6f7] | 398 | TypeDecl *typeParam = cloneAndRename( *param, "_autoassign_" + aggregateDecl->get_name() + "_" + (*param)->get_name() ); |
---|
| 399 | assignType->get_forall().push_back( typeParam ); |
---|
| 400 | unionParams.push_back( new TypeExpr( new TypeInstType( Type::Qualifiers(), typeParam->get_name(), typeParam ) ) ); |
---|
| 401 | } |
---|
| 402 | |
---|
| 403 | ObjectDecl *dstParam = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), cloneWithParams( refType, unionParams ) ), 0 ); |
---|
| 404 | assignType->get_parameters().push_back( dstParam ); |
---|
| 405 | |
---|
| 406 | // default ctor/dtor need only first parameter |
---|
| 407 | FunctionType * ctorType = assignType->clone(); |
---|
| 408 | FunctionType * dtorType = assignType->clone(); |
---|
| 409 | |
---|
| 410 | ObjectDecl *srcParam = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, cloneWithParams( refType, unionParams ), 0 ); |
---|
| 411 | assignType->get_parameters().push_back( srcParam ); |
---|
| 412 | |
---|
| 413 | // copy ctor needs both parameters |
---|
| 414 | FunctionType * copyCtorType = assignType->clone(); |
---|
| 415 | |
---|
| 416 | // assignment needs both and return value |
---|
| 417 | ObjectDecl *returnVal = new ObjectDecl( "_ret", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, cloneWithParams( refType, unionParams ), 0 ); |
---|
| 418 | assignType->get_returnVals().push_back( returnVal ); |
---|
| 419 | |
---|
| 420 | // Routines at global scope marked "static" to prevent multiple definitions is separate translation units |
---|
| 421 | // because each unit generates copies of the default routines for each aggregate. |
---|
| 422 | FunctionDecl *assignDecl = new FunctionDecl( "?=?", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, assignType, new CompoundStmt( noLabels ), true, false ); |
---|
| 423 | FunctionDecl *ctorDecl = new FunctionDecl( "?{}", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, ctorType, new CompoundStmt( noLabels ), true, false ); |
---|
| 424 | FunctionDecl *copyCtorDecl = new FunctionDecl( "?{}", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, copyCtorType, NULL, true, false ); |
---|
| 425 | FunctionDecl *dtorDecl = new FunctionDecl( "^?{}", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, dtorType, new CompoundStmt( noLabels ), true, false ); |
---|
| 426 | |
---|
| 427 | assignDecl->fixUniqueId(); |
---|
| 428 | ctorDecl->fixUniqueId(); |
---|
| 429 | copyCtorDecl->fixUniqueId(); |
---|
| 430 | dtorDecl->fixUniqueId(); |
---|
| 431 | |
---|
[9b4c936] | 432 | makeUnionFieldsAssignment( srcParam, dstParam, back_inserter( assignDecl->get_statements()->get_kids() ) ); |
---|
| 433 | if ( isDynamicLayout ) makeUnionFieldsAssignment( srcParam, returnVal, back_inserter( assignDecl->get_statements()->get_kids() ) ); |
---|
[a465caf] | 434 | else assignDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) ); |
---|
[972e6f7] | 435 | |
---|
| 436 | // body of assignment and copy ctor is the same |
---|
| 437 | copyCtorDecl->set_statements( assignDecl->get_statements()->clone() ); |
---|
| 438 | |
---|
[a465caf] | 439 | // create a constructor which takes the first member type as a parameter. |
---|
| 440 | // for example, for Union A { int x; double y; }; generate |
---|
| 441 | // void ?{}(A *, int) |
---|
| 442 | // This is to mimic C's behaviour which initializes the first member of the union. |
---|
| 443 | std::list<Declaration *> memCtors; |
---|
| 444 | for ( Declaration * member : aggregateDecl->get_members() ) { |
---|
| 445 | if ( DeclarationWithType * field = dynamic_cast< DeclarationWithType * >( member ) ) { |
---|
| 446 | ObjectDecl * srcParam = new ObjectDecl( "src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, field->get_type()->clone(), 0 ); |
---|
| 447 | |
---|
| 448 | FunctionType * memCtorType = ctorType->clone(); |
---|
| 449 | memCtorType->get_parameters().push_back( srcParam ); |
---|
| 450 | FunctionDecl * ctor = new FunctionDecl( "?{}", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, memCtorType, new CompoundStmt( noLabels ), true, false ); |
---|
| 451 | ctor->fixUniqueId(); |
---|
| 452 | |
---|
[9b4c936] | 453 | makeUnionFieldsAssignment( srcParam, dstParam, back_inserter( ctor->get_statements()->get_kids() ) ); |
---|
[a465caf] | 454 | memCtors.push_back( ctor ); |
---|
| 455 | // only generate a ctor for the first field |
---|
| 456 | break; |
---|
| 457 | } |
---|
| 458 | } |
---|
| 459 | |
---|
[972e6f7] | 460 | declsToAdd.push_back( assignDecl ); |
---|
| 461 | declsToAdd.push_back( ctorDecl ); |
---|
| 462 | declsToAdd.push_back( copyCtorDecl ); |
---|
| 463 | declsToAdd.push_back( dtorDecl ); |
---|
[a465caf] | 464 | declsToAdd.splice( declsToAdd.end(), memCtors ); |
---|
[972e6f7] | 465 | } |
---|
| 466 | |
---|
| 467 | void AutogenerateRoutines::visit( EnumDecl *enumDecl ) { |
---|
| 468 | if ( ! enumDecl->get_members().empty() ) { |
---|
| 469 | EnumInstType *enumInst = new EnumInstType( Type::Qualifiers(), enumDecl->get_name() ); |
---|
| 470 | // enumInst->set_baseEnum( enumDecl ); |
---|
| 471 | // declsToAdd.push_back( |
---|
[4cc4286] | 472 | makeEnumFunctions( enumDecl, enumInst, functionNesting, declsToAdd ); |
---|
[972e6f7] | 473 | } |
---|
| 474 | } |
---|
| 475 | |
---|
| 476 | void AutogenerateRoutines::visit( StructDecl *structDecl ) { |
---|
| 477 | if ( ! structDecl->get_members().empty() && structsDone.find( structDecl->get_name() ) == structsDone.end() ) { |
---|
| 478 | StructInstType structInst( Type::Qualifiers(), structDecl->get_name() ); |
---|
| 479 | structInst.set_baseStruct( structDecl ); |
---|
| 480 | makeStructFunctions( structDecl, &structInst, functionNesting, declsToAdd ); |
---|
| 481 | structsDone.insert( structDecl->get_name() ); |
---|
| 482 | } // if |
---|
| 483 | } |
---|
| 484 | |
---|
| 485 | void AutogenerateRoutines::visit( UnionDecl *unionDecl ) { |
---|
| 486 | if ( ! unionDecl->get_members().empty() ) { |
---|
| 487 | UnionInstType unionInst( Type::Qualifiers(), unionDecl->get_name() ); |
---|
| 488 | unionInst.set_baseUnion( unionDecl ); |
---|
| 489 | makeUnionFunctions( unionDecl, &unionInst, functionNesting, declsToAdd ); |
---|
| 490 | } // if |
---|
| 491 | } |
---|
| 492 | |
---|
| 493 | void AutogenerateRoutines::visit( TypeDecl *typeDecl ) { |
---|
| 494 | CompoundStmt *stmts = 0; |
---|
| 495 | TypeInstType *typeInst = new TypeInstType( Type::Qualifiers(), typeDecl->get_name(), false ); |
---|
| 496 | typeInst->set_baseType( typeDecl ); |
---|
| 497 | ObjectDecl *src = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, typeInst->clone(), 0 ); |
---|
| 498 | ObjectDecl *dst = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), typeInst->clone() ), 0 ); |
---|
| 499 | if ( typeDecl->get_base() ) { |
---|
[c738ca4] | 500 | // xxx - generate ctor/dtors for typedecls, e.g. |
---|
| 501 | // otype T = int *; |
---|
[972e6f7] | 502 | stmts = new CompoundStmt( std::list< Label >() ); |
---|
| 503 | UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) ); |
---|
| 504 | assign->get_args().push_back( new CastExpr( new VariableExpr( dst ), new PointerType( Type::Qualifiers(), typeDecl->get_base()->clone() ) ) ); |
---|
| 505 | assign->get_args().push_back( new CastExpr( new VariableExpr( src ), typeDecl->get_base()->clone() ) ); |
---|
| 506 | stmts->get_kids().push_back( new ReturnStmt( std::list< Label >(), assign ) ); |
---|
| 507 | } // if |
---|
| 508 | FunctionType *type = new FunctionType( Type::Qualifiers(), false ); |
---|
| 509 | type->get_returnVals().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, typeInst, 0 ) ); |
---|
| 510 | type->get_parameters().push_back( dst ); |
---|
| 511 | type->get_parameters().push_back( src ); |
---|
[2f6b7c9] | 512 | FunctionDecl *func = new FunctionDecl( "?=?", DeclarationNode::NoStorageClass, LinkageSpec::AutoGen, type, stmts, true, false ); |
---|
[972e6f7] | 513 | declsToAdd.push_back( func ); |
---|
| 514 | } |
---|
| 515 | |
---|
| 516 | void addDecls( std::list< Declaration * > &declsToAdd, std::list< Statement * > &statements, std::list< Statement * >::iterator i ) { |
---|
| 517 | for ( std::list< Declaration * >::iterator decl = declsToAdd.begin(); decl != declsToAdd.end(); ++decl ) { |
---|
| 518 | statements.insert( i, new DeclStmt( noLabels, *decl ) ); |
---|
| 519 | } // for |
---|
| 520 | declsToAdd.clear(); |
---|
| 521 | } |
---|
| 522 | |
---|
| 523 | void AutogenerateRoutines::visit( FunctionType *) { |
---|
| 524 | // ensure that we don't add assignment ops for types defined as part of the function |
---|
| 525 | } |
---|
| 526 | |
---|
| 527 | void AutogenerateRoutines::visit( PointerType *) { |
---|
| 528 | // ensure that we don't add assignment ops for types defined as part of the pointer |
---|
| 529 | } |
---|
| 530 | |
---|
[a5a71d0] | 531 | void AutogenerateRoutines::visit( TraitDecl *) { |
---|
| 532 | // ensure that we don't add assignment ops for types defined as part of the trait |
---|
[972e6f7] | 533 | } |
---|
| 534 | |
---|
| 535 | template< typename StmtClass > |
---|
| 536 | inline void AutogenerateRoutines::visitStatement( StmtClass *stmt ) { |
---|
| 537 | std::set< std::string > oldStructs = structsDone; |
---|
| 538 | addVisit( stmt, *this ); |
---|
| 539 | structsDone = oldStructs; |
---|
| 540 | } |
---|
| 541 | |
---|
| 542 | void AutogenerateRoutines::visit( FunctionDecl *functionDecl ) { |
---|
| 543 | maybeAccept( functionDecl->get_functionType(), *this ); |
---|
| 544 | acceptAll( functionDecl->get_oldDecls(), *this ); |
---|
| 545 | functionNesting += 1; |
---|
| 546 | maybeAccept( functionDecl->get_statements(), *this ); |
---|
| 547 | functionNesting -= 1; |
---|
| 548 | } |
---|
| 549 | |
---|
| 550 | void AutogenerateRoutines::visit( CompoundStmt *compoundStmt ) { |
---|
| 551 | visitStatement( compoundStmt ); |
---|
| 552 | } |
---|
| 553 | |
---|
| 554 | void AutogenerateRoutines::visit( SwitchStmt *switchStmt ) { |
---|
| 555 | visitStatement( switchStmt ); |
---|
| 556 | } |
---|
| 557 | } // SymTab |
---|