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 | // ForallPointerDecay.cpp -- |
---|
8 | // |
---|
9 | // Author : Andrew Beach |
---|
10 | // Created On : Tue Dec 7 16:15:00 2021 |
---|
11 | // Last Modified By : Andrew Beach |
---|
12 | // Last Modified On : Fri Feb 11 10:59:00 2022 |
---|
13 | // Update Count : 0 |
---|
14 | // |
---|
15 | |
---|
16 | #include "ForallPointerDecay.hpp" |
---|
17 | |
---|
18 | #include "AST/Copy.hpp" |
---|
19 | #include "AST/Decl.hpp" |
---|
20 | #include "AST/DeclReplacer.hpp" |
---|
21 | #include "AST/Pass.hpp" |
---|
22 | #include "CodeGen/OperatorTable.h" |
---|
23 | #include "Common/CodeLocation.h" |
---|
24 | #include "SymTab/FixFunction.h" |
---|
25 | |
---|
26 | #include "AST/Print.hpp" |
---|
27 | |
---|
28 | namespace Validate { |
---|
29 | |
---|
30 | namespace { |
---|
31 | |
---|
32 | // Create a function type only using information on the FunctionDecl. |
---|
33 | ast::FunctionType * makeFuncType( const ast::FunctionDecl * decl ) { |
---|
34 | auto type = new ast::FunctionType( decl->type->isVarArgs ); |
---|
35 | for ( auto & param : decl->params ) { |
---|
36 | type->params.emplace_back( param->get_type() ); |
---|
37 | } |
---|
38 | for ( auto & ret : decl->returns ) { |
---|
39 | type->returns.emplace_back( ret->get_type() ); |
---|
40 | } |
---|
41 | for ( auto & type_param : decl->type_params ) { |
---|
42 | type->forall.emplace_back( |
---|
43 | new ast::TypeInstType( type_param->name, type_param ) ); |
---|
44 | } |
---|
45 | for ( auto & assertion : decl->assertions ) { |
---|
46 | type->assertions.emplace_back( new ast::VariableExpr( |
---|
47 | assertion->location, assertion ) ); |
---|
48 | } |
---|
49 | return type; |
---|
50 | } |
---|
51 | |
---|
52 | template<typename T> |
---|
53 | void append( std::vector<T> & dst, std::vector<T> & src ) { |
---|
54 | dst.reserve( dst.size() + src.size() ); |
---|
55 | for ( auto el : src ) { |
---|
56 | dst.emplace_back( std::move( el ) ); |
---|
57 | } |
---|
58 | src.clear(); |
---|
59 | } |
---|
60 | |
---|
61 | // Component Passes: |
---|
62 | /// Expand assertions from a trait instance, |
---|
63 | /// performing appropriate type variable substitutions. |
---|
64 | struct TraitExpander final { |
---|
65 | using AssertionList = std::vector<ast::ptr<ast::DeclWithType>>; |
---|
66 | |
---|
67 | static AssertionList expandTrait( const ast::TraitInstType * inst ) { |
---|
68 | assertf( inst->base, "Trait instance not linked to base trait: %s", |
---|
69 | toCString( inst ) ); |
---|
70 | AssertionList assertions; |
---|
71 | // Substitute trait decl parameters for instance parameters. |
---|
72 | ast::TypeSubstitution sub( inst->base->params, inst->params ); |
---|
73 | for ( const ast::ptr<ast::Decl> & decl : inst->base->members ) { |
---|
74 | ast::ptr<ast::DeclWithType> copy = |
---|
75 | ast::deepCopy( decl.strict_as<ast::DeclWithType>() ); |
---|
76 | |
---|
77 | int count = sub.apply( copy ); |
---|
78 | (void)count; |
---|
79 | |
---|
80 | // Update the type (type substution does not seem to cover it). |
---|
81 | if ( auto func = copy.as<ast::FunctionDecl>() ) { |
---|
82 | auto mut = ast::mutate( func ); |
---|
83 | mut->type = makeFuncType( func ); |
---|
84 | copy = mut; |
---|
85 | } |
---|
86 | assertions.push_back( copy ); |
---|
87 | } |
---|
88 | return assertions; |
---|
89 | } |
---|
90 | |
---|
91 | static AssertionList expandAssertions( const AssertionList & old ) { |
---|
92 | AssertionList assertions; |
---|
93 | for ( const ast::ptr<ast::DeclWithType> & decl : old ) { |
---|
94 | if ( auto traitInst = dynamic_cast<const ast::TraitInstType *>( |
---|
95 | decl->get_type() ) ) { |
---|
96 | auto moreAsserts = expandTrait( traitInst ); |
---|
97 | append( assertions, moreAsserts ); |
---|
98 | } else { |
---|
99 | assertions.push_back( decl ); |
---|
100 | } |
---|
101 | } |
---|
102 | return assertions; |
---|
103 | } |
---|
104 | |
---|
105 | using TypeDeclVec = std::vector<ast::ptr<ast::TypeDecl>>; |
---|
106 | |
---|
107 | static TypeDeclVec expandTypeDecls( const TypeDeclVec & old ) { |
---|
108 | TypeDeclVec typeDecls; |
---|
109 | for ( const ast::TypeDecl * typeDecl : old ) { |
---|
110 | typeDecls.push_back( ast::mutate_field( typeDecl, |
---|
111 | &ast::TypeDecl::assertions, |
---|
112 | expandAssertions( typeDecl->assertions ) ) ); |
---|
113 | } |
---|
114 | return typeDecls; |
---|
115 | } |
---|
116 | |
---|
117 | const ast::FunctionDecl * postvisit( const ast::FunctionDecl * decl ) { |
---|
118 | if ( decl->assertions.empty() ) { |
---|
119 | return decl; |
---|
120 | } |
---|
121 | auto mut = ast::mutate( decl ); |
---|
122 | mut->assertions = expandAssertions( decl->assertions ); |
---|
123 | // Update the assertion list on the type as well. |
---|
124 | auto mutType = ast::mutate( mut->type.get() ); |
---|
125 | mutType->assertions.clear(); |
---|
126 | for ( auto & assertion : mut->assertions ) { |
---|
127 | mutType->assertions.emplace_back( |
---|
128 | new ast::VariableExpr( mut->location, assertion ) ); |
---|
129 | } |
---|
130 | mut->type = mutType; |
---|
131 | return mut; |
---|
132 | } |
---|
133 | |
---|
134 | const ast::StructDecl * previsit( const ast::StructDecl * decl ) { |
---|
135 | if ( decl->params.empty() ) { |
---|
136 | return decl; |
---|
137 | } |
---|
138 | return ast::mutate_field( decl, &ast::StructDecl::params, |
---|
139 | expandTypeDecls( decl->params ) ); |
---|
140 | } |
---|
141 | |
---|
142 | const ast::UnionDecl * previsit( const ast::UnionDecl * decl ) { |
---|
143 | if ( decl->params.empty() ) { |
---|
144 | return decl; |
---|
145 | } |
---|
146 | return ast::mutate_field( decl, &ast::UnionDecl::params, |
---|
147 | expandTypeDecls( decl->params ) ); |
---|
148 | } |
---|
149 | }; |
---|
150 | |
---|
151 | std::vector<ast::ptr<ast::DeclWithType>> fixAssertionList( |
---|
152 | const ast::ParseNode * node, |
---|
153 | const std::vector<ast::ptr<ast::DeclWithType>> & assertions ) { |
---|
154 | std::vector<ast::ptr<ast::DeclWithType>> ret; |
---|
155 | for ( const auto & assn : assertions ) { |
---|
156 | bool isVoid = false; |
---|
157 | ret.push_back( SymTab::fixFunction( assn, isVoid ) ); |
---|
158 | if ( isVoid ) { |
---|
159 | SemanticError( node->location, node, |
---|
160 | "invalid type void in assertion of function " ); |
---|
161 | } |
---|
162 | } |
---|
163 | return ret; |
---|
164 | } |
---|
165 | |
---|
166 | std::vector<ast::ptr<ast::TypeDecl>> fixTypeDeclList( |
---|
167 | const ast::ParseNode * node, |
---|
168 | const std::vector<ast::ptr<ast::TypeDecl>> & type_params ) { |
---|
169 | std::vector<ast::ptr<ast::TypeDecl>> ret; |
---|
170 | ret.reserve( type_params.size() ); |
---|
171 | for ( const ast::TypeDecl * type_param : type_params ) { |
---|
172 | auto mutParam = ast::mutate( type_param ); |
---|
173 | mutParam->assertions = fixAssertionList( node, mutParam->assertions ); |
---|
174 | ret.push_back( mutParam ); |
---|
175 | } |
---|
176 | return ret; |
---|
177 | } |
---|
178 | |
---|
179 | struct AssertionFunctionFixer final { |
---|
180 | const ast::FunctionDecl * previsit( const ast::FunctionDecl * decl ) { |
---|
181 | if ( decl->assertions.empty() ) { |
---|
182 | return decl; |
---|
183 | } |
---|
184 | return ast::mutate_field( decl, &ast::FunctionDecl::assertions, |
---|
185 | fixAssertionList( decl, decl->assertions ) ); |
---|
186 | } |
---|
187 | |
---|
188 | const ast::StructDecl * previsit( const ast::StructDecl * decl ) { |
---|
189 | if ( decl->params.empty() ) { |
---|
190 | return decl; |
---|
191 | } |
---|
192 | return ast::mutate_field( decl, &ast::StructDecl::params, |
---|
193 | fixTypeDeclList( decl, decl->params ) ); |
---|
194 | } |
---|
195 | |
---|
196 | const ast::UnionDecl * previsit( const ast::UnionDecl * decl ) { |
---|
197 | if ( decl->params.empty() ) { |
---|
198 | return decl; |
---|
199 | } |
---|
200 | return ast::mutate_field( decl, &ast::UnionDecl::params, |
---|
201 | fixTypeDeclList( decl, decl->params ) ); |
---|
202 | } |
---|
203 | }; |
---|
204 | |
---|
205 | struct OberatorChecker final { |
---|
206 | void previsit( const ast::ObjectDecl * obj ) { |
---|
207 | if ( CodeGen::isOperator( obj->name ) ) { |
---|
208 | auto type = obj->type->stripDeclarator(); |
---|
209 | if ( ! dynamic_cast< const ast::FunctionType * >( type ) ) { |
---|
210 | SemanticError( obj->location, |
---|
211 | toCString( "operator ", obj->name.c_str(), " is not " |
---|
212 | "a function or function pointer." ) ); |
---|
213 | } |
---|
214 | } |
---|
215 | } |
---|
216 | }; |
---|
217 | |
---|
218 | struct UniqueFixCore final { |
---|
219 | const ast::DeclWithType * postvisit( const ast::DeclWithType * decl ) { |
---|
220 | if ( decl->uniqueId ) { |
---|
221 | return decl; |
---|
222 | } else { |
---|
223 | auto mut = ast::mutate( decl ); |
---|
224 | mut->fixUniqueId(); |
---|
225 | return mut; |
---|
226 | } |
---|
227 | } |
---|
228 | }; |
---|
229 | |
---|
230 | } // namespace |
---|
231 | |
---|
232 | void decayForallPointers( ast::TranslationUnit & transUnit ) { |
---|
233 | ast::Pass<TraitExpander>::run( transUnit ); |
---|
234 | ast::Pass<AssertionFunctionFixer>::run( transUnit ); |
---|
235 | ast::Pass<OberatorChecker>::run( transUnit ); |
---|
236 | ast::Pass<UniqueFixCore>::run( transUnit ); |
---|
237 | } |
---|
238 | |
---|
239 | } // namespace Validate |
---|
240 | |
---|
241 | // Local Variables: // |
---|
242 | // tab-width: 4 // |
---|
243 | // mode: c++ // |
---|
244 | // compile-command: "make install" // |
---|
245 | // End: // |
---|