source: src/Validate/HoistStruct.cpp@ 0dffe91

Last change on this file since 0dffe91 was 0153dbd, checked in by Andrew Beach <ajbeach@…>, 17 months ago

Updated hoistStruct so the nested mangled names are human readable. This does require more code to update instances, but I think it is reasonable in complexity and runtime.

  • Property mode set to 100644
File size: 7.1 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2018 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// HoistStruct.cpp -- Flattens nested type declarations.
8//
9// Author : Andrew Beach
10// Created On : Thr Apr 21 10:34:00 2022
11// Last Modified By : Andrew Beach
12// Last Modified On : Thr Apr 21 10:34:00 2022
13// Update Count : 0
14//
15
16#include "Validate/HoistStruct.hpp"
17
18#include <sstream>
19
20#include "AST/DeclReplacer.hpp"
21#include "AST/Pass.hpp"
22#include "AST/TranslationUnit.hpp"
23#include "AST/Vector.hpp"
24
25namespace Validate {
26
27namespace {
28
29/// Is this a declaration can appear in a struct/union and should be hoisted?
30bool shouldHoist( ast::Decl const * decl ) {
31 return dynamic_cast< ast::StructDecl const * >( decl )
32 || dynamic_cast< ast::UnionDecl const * >( decl )
33 || dynamic_cast< ast::EnumDecl const * >( decl )
34 || dynamic_cast< ast::StaticAssertDecl const * >( decl );
35}
36
37/// Helper that updates an InstType if the base name could be updated.
38template<typename InstType>
39InstType const * preInstType( InstType const * type ) {
40 assert( type->base );
41 if ( nullptr == type->base->parent ) return type;
42 auto mut = ast::mutate( type );
43 mut->name = mut->base->name;
44 return mut;
45}
46
47/// Update StructInstType and UnionInstType names.
48struct NameUpdater {
49 ast::StructInstType const * previsit( ast::StructInstType const * type ) {
50 return preInstType( type );
51 }
52
53 ast::UnionInstType const * previsit( ast::UnionInstType const * type ) {
54 return preInstType( type );
55 }
56};
57
58ast::Decl const * updateNames( ast::Decl const * decl ) {
59 ast::Pass<NameUpdater> visitor;
60 return decl->accept( visitor );
61}
62
63/* This pass hoists from structs/unions. Hoisted declarations should always
64 * appear before the declaration they are hoisted out of and if two types are
65 * nested in the same declaration their order should not change.
66 * It also sets up parent relationships, does name mangling of hoisted types
67 * and updates instance types of the hoisted types.
68 */
69struct HoistStructCore final :
70 public ast::WithDeclsToAdd<>, public ast::WithGuards {
71 ast::StructDecl const * previsit( ast::StructDecl const * decl );
72 ast::StructDecl const * postvisit( ast::StructDecl const * decl );
73 ast::UnionDecl const * previsit( ast::UnionDecl const * decl );
74 ast::UnionDecl const * postvisit( ast::UnionDecl const * decl );
75 ast::StructInstType const * previsit( ast::StructInstType const * type );
76 ast::UnionInstType const * previsit( ast::UnionInstType const * type );
77 ast::EnumInstType const * previsit( ast::EnumInstType const * type );
78
79private:
80 template<typename AggrDecl>
81 AggrDecl const * preAggregate( AggrDecl const * );
82 template<typename AggrDecl>
83 AggrDecl const * postAggregate( AggrDecl const * );
84 template<typename InstType>
85 InstType const * preCollectionInstType( InstType const * type );
86
87 ast::AggregateDecl const * parent = nullptr;
88};
89
90void qualifiedName( ast::AggregateDecl const * decl, std::ostringstream & ss ) {
91 if ( decl->parent ) {
92 qualifiedName( decl->parent, ss );
93 }
94 ss << "__" << decl->name;
95}
96
97std::string qualifiedName( ast::AggregateDecl const * decl ) {
98 std::ostringstream ss;
99 qualifiedName( decl, ss );
100 return ss.str();
101}
102
103void extendParams( ast::vector<ast::TypeDecl> & dstParams,
104 ast::vector<ast::TypeDecl> const & srcParams ) {
105 if ( srcParams.empty() ) return;
106
107 ast::DeclReplacer::TypeMap newToOld;
108 ast::vector<ast::TypeDecl> params;
109 for ( ast::ptr<ast::TypeDecl> const & srcParam : srcParams ) {
110 ast::TypeDecl * dstParam = ast::deepCopy( srcParam.get() );
111 dstParam->init = nullptr;
112 newToOld.emplace( srcParam, dstParam );
113 for ( auto assertion : dstParam->assertions ) {
114 assertion = ast::DeclReplacer::replace( assertion, newToOld );
115 }
116 params.emplace_back( dstParam );
117 }
118 spliceBegin( dstParams, params );
119}
120
121template<typename AggrDecl>
122AggrDecl const * HoistStructCore::preAggregate( AggrDecl const * decl ) {
123 if ( parent ) {
124 auto mut = ast::mutate( decl );
125 mut->parent = parent;
126 extendParams( mut->params, parent->params );
127 decl = mut;
128 }
129 GuardValue( parent ) = decl;
130 return decl;
131}
132
133template<typename AggrDecl>
134AggrDecl const * HoistStructCore::postAggregate( AggrDecl const * decl ) {
135 auto mut = ast::mutate( decl );
136 for ( auto it = mut->members.begin() ; it != mut->members.end() ; ) {
137 if ( shouldHoist( *it ) ) {
138 // This is the place where the actual hoisting happens.
139 declsToAddBefore.push_back( it->get() );
140 it = mut->members.erase( it );
141 } else {
142 ++it;
143 }
144 }
145 // Is this a nested type? Then update the name, after the parent's name
146 // has been updated (hence the post visit).
147 if ( mut->parent ) {
148 mut->name = qualifiedName( mut );
149 // Top level type that has hoisted? Then do a second pass subpass to make
150 // sure we update instance type names after the declaration is renamed.
151 } else if ( !declsToAddBefore.empty() ) {
152 for ( ast::ptr<ast::Decl> & member : mut->members ) {
153 member = updateNames( member.get() );
154 }
155 for ( ast::ptr<ast::Decl> & declToAdd : declsToAddBefore ) {
156 declToAdd = updateNames( declToAdd );
157 }
158 }
159 return mut;
160}
161
162ast::StructDecl const * HoistStructCore::previsit( ast::StructDecl const * decl ) {
163 return preAggregate( decl );
164}
165
166ast::StructDecl const * HoistStructCore::postvisit( ast::StructDecl const * decl ) {
167 return postAggregate( decl );
168}
169
170ast::UnionDecl const * HoistStructCore::previsit( ast::UnionDecl const * decl ) {
171 return preAggregate( decl );
172}
173
174ast::UnionDecl const * HoistStructCore::postvisit( ast::UnionDecl const * decl ) {
175 return postAggregate( decl );
176}
177
178ast::AggregateDecl const * commonParent(
179 ast::AggregateDecl const * lhs, ast::AggregateDecl const * rhs ) {
180 for ( auto outer = lhs ; outer ; outer = outer->parent ) {
181 for ( auto inner = rhs ; inner ; inner = inner->parent ) {
182 if ( outer == inner ) {
183 return outer;
184 }
185 }
186 }
187 return nullptr;
188}
189
190template<typename InstType>
191InstType const * HoistStructCore::preCollectionInstType( InstType const * type ) {
192 if ( !type->base->parent ) return type;
193 if ( type->base->params.empty() ) return type;
194
195 InstType * mut = ast::mutate( type );
196 ast::AggregateDecl const * parent =
197 commonParent( this->parent, mut->base->parent );
198 assert( parent );
199
200 std::vector<ast::ptr<ast::Expr>> args;
201 for ( const ast::ptr<ast::TypeDecl> & param : parent->params ) {
202 args.emplace_back( new ast::TypeExpr( param->location,
203 new ast::TypeInstType( param )
204 ) );
205 }
206 spliceBegin( mut->params, args );
207 return mut;
208}
209
210ast::StructInstType const * HoistStructCore::previsit( ast::StructInstType const * type ) {
211 return preInstType( preCollectionInstType( type ) );
212}
213
214ast::UnionInstType const * HoistStructCore::previsit( ast::UnionInstType const * type ) {
215 return preInstType( preCollectionInstType( type ) );
216}
217
218ast::EnumInstType const * HoistStructCore::previsit( ast::EnumInstType const * type ) {
219 return preInstType( type );
220}
221
222} // namespace
223
224void hoistStruct( ast::TranslationUnit & translationUnit ) {
225 ast::Pass<HoistStructCore>::run( translationUnit );
226}
227
228} // namespace Validate
229
230// Local Variables: //
231// tab-width: 4 //
232// mode: c++ //
233// compile-command: "make install" //
234// End: //
Note: See TracBrowser for help on using the repository browser.