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