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