source: src/GenPoly/InstantiateGeneric.cc @ b0b958a

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since b0b958a was b0b958a, checked in by Aaron Moss <a3moss@…>, 8 years ago

Switched InstantiateGeneric? pass over to use DeclMutator? base

  • Property mode set to 100644
File size: 8.5 KB
Line 
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// InstantiateGeneric.cc --
8//
9// Author           : Aaron B. Moss
10// Created On       : Wed Nov 11 14:55:01 2015
11// Last Modified By : Aaron B. Moss
12// Last Modified On : Wed Nov 11 14:55:01 2015
13// Update Count     : 1
14//
15
16#include <list>
17#include <map>
18#include <string>
19#include <utility>
20#include <vector>
21
22#include "InstantiateGeneric.h"
23#include "DeclMutator.h"
24
25#include "ResolvExpr/typeops.h"
26#include "SymTab/Indexer.h"
27#include "SynTree/Declaration.h"
28#include "SynTree/Mutator.h"
29#include "SynTree/Statement.h"
30#include "SynTree/Type.h"
31#include "SynTree/TypeSubstitution.h"
32
33#include "UniqueName.h"
34#include "utility.h"
35
36namespace GenPoly {
37
38        /// Key for a unique concrete type; generic base type paired with type parameter list
39        struct ConcreteType {
40                ConcreteType() : base(NULL), params() {}
41
42                ConcreteType(AggregateDecl *_base, const std::list< Type* >& _params) : base(_base), params() { cloneAll(_params, params); }
43
44                ConcreteType(const ConcreteType& that) : base(that.base), params() { cloneAll(that.params, params); }
45
46                /// Extracts types from a list of Expression* (which should be TypeExpr*)
47                ConcreteType(AggregateDecl *_base, const std::list< Expression* >& _params) : base(_base), params() {
48                        for ( std::list< Expression* >::const_iterator it = _params.begin(); it != _params.end(); ++it ) {
49                                TypeExpr *param = dynamic_cast< TypeExpr* >(*it);
50                                assert(param && "Aggregate parameters should be type expressions");
51                                params.push_back( param->get_type()->clone() );
52                        }
53                }
54
55                ConcreteType& operator= (const ConcreteType& that) {
56                        deleteAll( params );
57                        params.clear();
58
59                        base = that.base;
60                        cloneAll( that.params, params );
61
62                        return *this;
63                }
64
65                ~ConcreteType() { deleteAll( params ); }
66
67                bool operator== (const ConcreteType& that) const {
68                        SymTab::Indexer dummy;
69
70                        if ( base != that.base ) return false;
71                        if ( params.size() != that.params.size() ) return false;
72                        for ( std::list< Type* >::const_iterator it = params.begin(), jt = that.params.begin(); it != params.end(); ++it, ++jt ) {
73                                if ( ! ResolvExpr::typesCompatible( *it, *jt, dummy ) ) return false;
74                        }
75                        return true;
76                }
77
78                AggregateDecl *base;        ///< Base generic type
79                std::list< Type* > params;  ///< Instantiation parameters
80        };
81
82        /// Maps a concrete type to the instantiated struct type, accounting for scope
83        class InstantiationMap {
84                /// Pair of concrete type and declaration that instantiates it
85                typedef std::pair< ConcreteType, AggregateDecl* > Instantiation;
86                /// Map of generic types to instantiations of them
87                typedef std::map< AggregateDecl*, std::vector< Instantiation > > Scope;
88
89                std::vector< Scope > scopes;  ///< list of scopes, from outermost to innermost
90
91        public:
92                /// Starts a new scope
93                void beginScope() {
94                        Scope scope;
95                        scopes.push_back(scope);
96                }
97
98                /// Ends a scope
99                void endScope() {
100                        scopes.pop_back();
101                }
102
103                /// Default constructor initializes with one scope
104                InstantiationMap() { beginScope(); }
105
106        private:
107                /// Gets the declaration for the concrete instantiation of this type, assuming it has already been instantiated in the current scope.
108                /// Returns NULL on none such.
109                AggregateDecl* lookup( AggregateDecl *generic, std::list< Expression* >& params ) {
110                        ConcreteType key(generic, params);
111                        // scan scopes from innermost out
112                        for ( std::vector< Scope >::const_reverse_iterator scope = scopes.rbegin(); scope != scopes.rend(); ++scope ) {
113                                // skip scope if no instantiations of this generic type
114                                Scope::const_iterator insts = scope->find( generic );
115                                if ( insts == scope->end() ) continue;
116                                // look through instantiations for matches to concrete type
117                                for ( std::vector< Instantiation >::const_iterator inst = insts->second.begin(); inst != insts->second.end(); ++inst ) {
118                                        if ( inst->first == key ) return inst->second;
119                                }
120                        }
121                        // no matching instantiation found
122                        return NULL;
123                }
124        public:
125                StructDecl* lookup( StructInstType *inst ) { return (StructDecl*)lookup( inst->get_baseStruct(), inst->get_parameters() ); }
126                UnionDecl* lookup( UnionInstType *inst ) { return (UnionDecl*)lookup( inst->get_baseUnion(), inst->get_parameters() ); }
127
128        private:
129                /// Adds a declaration for a concrete type to the current scope
130                void insert( AggregateDecl *generic, std::list< Expression* >& params, AggregateDecl *decl ) {
131                        ConcreteType key(generic, params);
132                        scopes.back()[generic].push_back( std::make_pair( key, decl ) );
133                }
134        public:
135                void insert( StructInstType *inst, StructDecl *decl ) { insert( inst->get_baseStruct(), inst->get_parameters(), decl ); }
136                void insert( UnionInstType *inst, UnionDecl *decl ) { insert( inst->get_baseUnion(), inst->get_parameters(), decl ); }
137        };
138
139        /// Mutator pass that replaces concrete instantiations of generic types with actual struct declarations, scoped appropriately
140        class Instantiate : public DeclMutator {
141                InstantiationMap instantiations;
142                UniqueName typeNamer;
143
144        public:
145                Instantiate() : DeclMutator(), instantiations(), typeNamer("_conc_") {}
146
147                virtual Type* mutate( StructInstType *inst );
148                virtual Type* mutate( UnionInstType *inst );
149               
150                virtual void doBeginScope();
151                virtual void doEndScope();
152        };
153       
154        void instantiateGeneric( std::list< Declaration* >& translationUnit ) {
155                Instantiate instantiator;
156                instantiator.mutateDeclarationList( translationUnit );
157        }
158
159        /// Substitutes types of members of in according to baseParams => params, appending the result to out
160        void substituteMembers( const std::list< Declaration* >& in,
161                                                        const std::list< TypeDecl * >& baseParams, const std::list< Expression* >& params,
162                                                    std::list< Declaration* >& out ) {
163                // substitute types into new members
164                TypeSubstitution subs( baseParams.begin(), baseParams.end(), params.begin() );
165                for ( std::list< Declaration* >::const_iterator member = in.begin(); member != in.end(); ++member ) {
166                        Declaration *newMember = (*member)->clone();
167                        subs.apply(newMember);
168                        out.push_back( newMember );
169                }
170        }
171
172        Type* Instantiate::mutate( StructInstType *inst ) {
173                // mutate subtypes
174                Type *mutated = Mutator::mutate( inst );
175                inst = dynamic_cast< StructInstType* >( mutated );
176                if ( ! inst ) return mutated;
177
178                // exit early if no need for further mutation
179                if ( inst->get_parameters().empty() ) return inst;
180               
181                // make concrete instantiation of generic type
182                StructDecl *concDecl = instantiations.lookup( inst );
183                if ( ! concDecl ) {
184                        assert( inst->get_baseParameters() && "Base struct has parameters" );
185                        // set concDecl to new type, insert type declaration into statements to add
186                        concDecl = new StructDecl( typeNamer.newName( inst->get_name() ) );
187                        substituteMembers( inst->get_baseStruct()->get_members(),
188                                                                *inst->get_baseParameters(), inst->get_parameters(),
189                                                                concDecl->get_members() );
190                        DeclMutator::addDeclaration( concDecl );
191                        instantiations.insert( inst, concDecl );
192                }
193                StructInstType *newInst = new StructInstType( inst->get_qualifiers(), concDecl->get_name() );
194                newInst->set_baseStruct( concDecl );
195                delete inst;
196                return newInst;
197        }
198       
199        Type* Instantiate::mutate( UnionInstType *inst ) {
200                // mutate subtypes
201                Type *mutated = Mutator::mutate( inst );
202                inst = dynamic_cast< UnionInstType* >( mutated );
203                if ( ! inst ) return mutated;
204
205                // exit early if no need for further mutation
206                if ( inst->get_parameters().empty() ) return inst;
207
208                // make concrete instantiation of generic type
209                UnionDecl *concDecl = instantiations.lookup( inst );
210                if ( ! concDecl ) {
211                        // set concDecl to new type, insert type declaration into statements to add
212                        assert( inst->get_baseParameters() && "Base union has parameters" );
213                        concDecl = new UnionDecl( typeNamer.newName( inst->get_name() ) );
214                        substituteMembers( inst->get_baseUnion()->get_members(),
215                                                                *inst->get_baseParameters(), inst->get_parameters(),
216                                                                concDecl->get_members() );
217                        DeclMutator::addDeclaration( concDecl );
218                        instantiations.insert( inst, concDecl );
219                }
220                UnionInstType *newInst = new UnionInstType( inst->get_qualifiers(), concDecl->get_name() );
221                newInst->set_baseUnion( concDecl );
222                delete inst;
223                return newInst;
224        }
225       
226        void Instantiate::doBeginScope() {
227                DeclMutator::doBeginScope();
228                // push a new concrete type scope
229                instantiations.beginScope();
230        }
231
232        void Instantiate::doEndScope() {
233                DeclMutator::doEndScope();
234                // pop the last concrete type scope
235                instantiations.endScope();
236        }
237       
238}  // namespace GenPoly
239
240// Local Variables: //
241// tab-width: 4 //
242// mode: c++ //
243// compile-command: "make install" //
244// End: //
Note: See TracBrowser for help on using the repository browser.