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