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