source: src/Validate/ForallPointerDecay.cpp@ 70056ed

ADT ast-experimental
Last change on this file since 70056ed was 9feb34b, checked in by Andrew Beach <ajbeach@…>, 2 years ago

Moved toString and toCString to a new header. Updated includes. cassert was somehow getting instances of toString before but that stopped working so I embedded the new smaller include.

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