source: src/AST/Pass.proto.hpp@ 0240cd69

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 0240cd69 was 7ff3e522, checked in by Andrew Beach <ajbeach@…>, 5 years ago

{pass_t Pass::pass; => core_t Pass::core;} To avoid confusion about which pass we are talking about.

  • Property mode set to 100644
File size: 14.7 KB
RevLine 
[04124c4]1//
2// Cforall Version 1.0.0 Copyright (C) 2019 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// Pass.impl.hpp --
8//
9// Author : Thierry Delisle
10// Created On : Thu May 09 15::37::05 2019
11// Last Modified By :
12// Last Modified On :
13// Update Count :
14//
15
[f47f887]16#pragma once
17// IWYU pragma: private, include "Pass.hpp"
18
[c15085d]19#include "Common/Stats/Heap.h"
20
[f47f887]21namespace ast {
[7ff3e522]22template<typename core_t>
[04124c4]23class Pass;
[f47f887]24
[04124c4]25namespace __pass {
26 typedef std::function<void( void * )> cleanup_func_t;
27 typedef std::function<void( cleanup_func_t, void * )> at_cleanup_t;
[f47f887]28
29
[04124c4]30 // boolean reference that may be null
31 // either refers to a boolean value or is null and returns true
32 class bool_ref {
33 public:
34 bool_ref() = default;
35 ~bool_ref() = default;
[f47f887]36
[04124c4]37 operator bool() { return m_ref ? *m_ref : true; }
38 bool operator=( bool val ) { assert(m_ref); return *m_ref = val; }
[f47f887]39
[04124c4]40 private:
[f47f887]41
[04124c4]42 friend class visit_children_guard;
[f47f887]43
[04124c4]44 bool * set( bool * val ) {
45 bool * prev = m_ref;
46 m_ref = val;
47 return prev;
48 }
[f47f887]49
[04124c4]50 bool * m_ref = nullptr;
51 };
[f47f887]52
[04124c4]53 // Implementation of the guard value
54 // Created inside the visit scope
55 class guard_value {
56 public:
57 /// Push onto the cleanup
58 guard_value( at_cleanup_t * at_cleanup ) {
59 if( at_cleanup ) {
60 *at_cleanup = [this]( cleanup_func_t && func, void* val ) {
61 push( std::move( func ), val );
62 };
[f47f887]63 }
[04124c4]64 }
[f47f887]65
[04124c4]66 ~guard_value() {
67 while( !cleanups.empty() ) {
68 auto& cleanup = cleanups.top();
69 cleanup.func( cleanup.val );
70 cleanups.pop();
[f47f887]71 }
[04124c4]72 }
[f47f887]73
[04124c4]74 void push( cleanup_func_t && func, void* val ) {
75 cleanups.emplace( std::move(func), val );
76 }
[f47f887]77
[04124c4]78 private:
79 struct cleanup_t {
80 cleanup_func_t func;
81 void * val;
[f47f887]82
[04124c4]83 cleanup_t( cleanup_func_t&& func, void * val ) : func(func), val(val) {}
[f47f887]84 };
85
[c408483]86 std::stack< cleanup_t, std::vector<cleanup_t> > cleanups;
[04124c4]87 };
[f47f887]88
[04124c4]89 // Guard structure implementation for whether or not children should be visited
90 class visit_children_guard {
91 public:
[f47f887]92
[04124c4]93 visit_children_guard( bool_ref * ref )
94 : m_val ( true )
95 , m_prev( ref ? ref->set( &m_val ) : nullptr )
96 , m_ref ( ref )
97 {}
98
99 ~visit_children_guard() {
100 if( m_ref ) {
101 m_ref->set( m_prev );
[f47f887]102 }
[04124c4]103 }
[f47f887]104
[04124c4]105 operator bool() { return m_val; }
[f47f887]106
[04124c4]107 private:
108 bool m_val;
109 bool * m_prev;
110 bool_ref * m_ref;
111 };
[f47f887]112
[e4b6cf78]113 /// "Short hand" to check if this is a valid previsit function
114 /// Mostly used to make the static_assert look (and print) prettier
[7ff3e522]115 template<typename core_t, typename node_t>
[dff6452]116 struct is_valid_previsit {
[7ff3e522]117 using ret_t = decltype( ((core_t*)nullptr)->previsit( (const node_t *)nullptr ) );
[dff6452]118
119 static constexpr bool value = std::is_void< ret_t >::value ||
120 std::is_base_of<const node_t, typename std::remove_pointer<ret_t>::type >::value;
121 };
122
[e4b6cf78]123 /// Used by previsit implementation
124 /// We need to reassign the result to 'node', unless the function
125 /// returns void, then we just leave 'node' unchanged
[0b8bf27]126 template<bool is_void>
127 struct __assign;
128
129 template<>
130 struct __assign<true> {
[7ff3e522]131 template<typename core_t, typename node_t>
132 static inline void result( core_t & core, const node_t * & node ) {
133 core.previsit( node );
[0b8bf27]134 }
135 };
136
137 template<>
138 struct __assign<false> {
[7ff3e522]139 template<typename core_t, typename node_t>
140 static inline void result( core_t & core, const node_t * & node ) {
141 node = core.previsit( node );
[0b8bf27]142 assertf(node, "Previsit must not return NULL");
143 }
144 };
145
[e4b6cf78]146 /// Used by postvisit implementation
147 /// We need to return the result unless the function
148 /// returns void, then we just return the original node
149 template<bool is_void>
150 struct __return;
151
152 template<>
153 struct __return<true> {
[7ff3e522]154 template<typename core_t, typename node_t>
155 static inline const node_t * result( core_t & core, const node_t * & node ) {
156 core.postvisit( node );
[e4b6cf78]157 return node;
158 }
159 };
160
161 template<>
162 struct __return<false> {
[7ff3e522]163 template<typename core_t, typename node_t>
164 static inline auto result( core_t & core, const node_t * & node ) {
165 return core.postvisit( node );
[e4b6cf78]166 }
167 };
168
[04124c4]169 //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
170 // Deep magic (a.k.a template meta programming) to make the templated visitor work
171 // Basically the goal is to make 2 previsit
172 // 1 - Use when a pass implements a valid previsit. This uses overloading which means the any overload of
173 // 'pass.previsit( node )' that compiles will be used for that node for that type
174 // This requires that this option only compile for passes that actually define an appropriate visit.
175 // SFINAE will make sure the compilation errors in this function don't halt the build.
176 // See http://en.cppreference.com/w/cpp/language/sfinae for details on SFINAE
177 // 2 - Since the first implementation might not be specilizable, the second implementation exists and does nothing.
178 // This is needed only to eliminate the need for passes to specify any kind of handlers.
179 // The second implementation only works because it has a lower priority. This is due to the bogus last parameter.
180 // The second implementation takes a long while the first takes an int. Since the caller always passes an literal 0
181 // the first implementation takes priority in regards to overloading.
182 //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
183 // PreVisit : may mutate the pointer passed in if the node is mutated in the previsit call
[7ff3e522]184 template<typename core_t, typename node_t>
185 static inline auto previsit( core_t & core, const node_t * & node, int ) -> decltype( core.previsit( node ), void() ) {
[b0abc8a0]186 static_assert(
[7ff3e522]187 is_valid_previsit<core_t, node_t>::value,
[dff6452]188 "Previsit may not change the type of the node. It must return its paremeter or void."
[b0abc8a0]189 );
[0b8bf27]190
191 __assign<
192 std::is_void<
[7ff3e522]193 decltype( core.previsit( node ) )
[0b8bf27]194 >::value
[7ff3e522]195 >::result( core, node );
[04124c4]196 }
197
[7ff3e522]198 template<typename core_t, typename node_t>
199 static inline auto previsit( core_t &, const node_t *, long ) {}
[04124c4]200
201 // PostVisit : never mutates the passed pointer but may return a different node
[7ff3e522]202 template<typename core_t, typename node_t>
203 static inline auto postvisit( core_t & core, const node_t * node, int ) ->
204 decltype( core.postvisit( node ), node->accept( *(Visitor*)nullptr ) )
[dff6452]205 {
[e4b6cf78]206 return __return<
207 std::is_void<
[7ff3e522]208 decltype( core.postvisit( node ) )
[e4b6cf78]209 >::value
[7ff3e522]210 >::result( core, node );
[04124c4]211 }
212
[7ff3e522]213 template<typename core_t, typename node_t>
214 static inline const node_t * postvisit( core_t &, const node_t * node, long ) { return node; }
[04124c4]215
216 //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
217 // Deep magic (a.k.a template meta programming) continued
218 // To make the templated visitor be more expressive, we allow 'accessories' : classes/structs the implementation can inherit
219 // from in order to get extra functionallity for example
220 // class ErrorChecker : WithShortCircuiting { ... };
221 // Pass<ErrorChecker> checker;
222 // this would define a pass that uses the templated visitor with the additionnal feature that it has short circuiting
223 // Note that in all cases the accessories are not required but guarantee the requirements of the feature is matched
224 //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
225 // For several accessories, the feature is enabled by detecting that a specific field is present
226 // Use a macro the encapsulate the logic of detecting a particular field
227 // The type is not strictly enforced but does match the accessory
228 #define FIELD_PTR( name, default_type ) \
[7ff3e522]229 template< typename core_t > \
230 static inline auto name( core_t & core, int ) -> decltype( &core.name ) { return &core.name; } \
[04124c4]231 \
[7ff3e522]232 template< typename core_t > \
233 static inline default_type * name( core_t &, long ) { return nullptr; }
[04124c4]234
235 // List of fields and their expected types
[6d51bd7]236 FIELD_PTR( env, const ast::TypeSubstitution * )
[04124c4]237 FIELD_PTR( stmtsToAddBefore, std::list< ast::ptr< ast::Stmt > > )
238 FIELD_PTR( stmtsToAddAfter , std::list< ast::ptr< ast::Stmt > > )
239 FIELD_PTR( declsToAddBefore, std::list< ast::ptr< ast::Decl > > )
240 FIELD_PTR( declsToAddAfter , std::list< ast::ptr< ast::Decl > > )
241 FIELD_PTR( visit_children, __pass::bool_ref )
242 FIELD_PTR( at_cleanup, __pass::at_cleanup_t )
[7ff3e522]243 FIELD_PTR( visitor, ast::Pass<core_t> * const )
[04124c4]244
245 // Remove the macro to make sure we don't clash
246 #undef FIELD_PTR
247
[7ff3e522]248 template< typename core_t >
249 static inline auto beginTrace(core_t &, int) -> decltype( core_t::traceId, void() ) {
250 // Stats::Heap::stacktrace_push(core_t::traceId);
[c15085d]251 }
252
[7ff3e522]253 template< typename core_t >
254 static inline auto endTrace(core_t &, int) -> decltype( core_t::traceId, void() ) {
[0d070ca]255 // Stats::Heap::stacktrace_pop();
[c15085d]256 }
257
[7ff3e522]258 template< typename core_t >
259 static void beginTrace(core_t &, long) {}
[c15085d]260
[7ff3e522]261 template< typename core_t >
262 static void endTrace(core_t &, long) {}
[c15085d]263
[04124c4]264 // Another feature of the templated visitor is that it calls beginScope()/endScope() for compound statement.
265 // All passes which have such functions are assumed desire this behaviour
266 // detect it using the same strategy
267 namespace scope {
[7ff3e522]268 template<typename core_t>
269 static inline auto enter( core_t & core, int ) -> decltype( core.beginScope(), void() ) {
270 core.beginScope();
[f47f887]271 }
272
[7ff3e522]273 template<typename core_t>
274 static inline void enter( core_t &, long ) {}
[f47f887]275
[7ff3e522]276 template<typename core_t>
277 static inline auto leave( core_t & core, int ) -> decltype( core.endScope(), void() ) {
278 core.endScope();
[f47f887]279 }
280
[7ff3e522]281 template<typename core_t>
282 static inline void leave( core_t &, long ) {}
[e0e9a0b]283 } // namespace scope
[f47f887]284
[e0e9a0b]285 // Certain passes desire an up to date symbol table automatically
[0e42794]286 // detect the presence of a member name `symtab` and call all the members appropriately
287 namespace symtab {
[04124c4]288 // Some simple scoping rules
[7ff3e522]289 template<typename core_t>
290 static inline auto enter( core_t & core, int ) -> decltype( core.symtab, void() ) {
291 core.symtab.enterScope();
[04124c4]292 }
[f47f887]293
[7ff3e522]294 template<typename core_t>
295 static inline auto enter( core_t &, long ) {}
[f47f887]296
[7ff3e522]297 template<typename core_t>
298 static inline auto leave( core_t & core, int ) -> decltype( core.symtab, void() ) {
299 core.symtab.leaveScope();
[04124c4]300 }
[f47f887]301
[7ff3e522]302 template<typename core_t>
303 static inline auto leave( core_t &, long ) {}
[f47f887]304
[0e42794]305 // The symbol table has 2 kind of functions mostly, 1 argument and 2 arguments
[04124c4]306 // Create macro to condense these common patterns
[0e42794]307 #define SYMTAB_FUNC1( func, type ) \
[7ff3e522]308 template<typename core_t> \
309 static inline auto func( core_t & core, int, type arg ) -> decltype( core.symtab.func( arg ), void() ) {\
310 core.symtab.func( arg ); \
[04124c4]311 } \
312 \
[7ff3e522]313 template<typename core_t> \
314 static inline void func( core_t &, long, type ) {}
[04124c4]315
[0e42794]316 #define SYMTAB_FUNC2( func, type1, type2 ) \
[7ff3e522]317 template<typename core_t> \
318 static inline auto func( core_t & core, int, type1 arg1, type2 arg2 ) -> decltype( core.symtab.func( arg1, arg2 ), void () ) {\
319 core.symtab.func( arg1, arg2 ); \
[04124c4]320 } \
[f47f887]321 \
[7ff3e522]322 template<typename core_t> \
323 static inline void func( core_t &, long, type1, type2 ) {}
[04124c4]324
[0e42794]325 SYMTAB_FUNC1( addId , const DeclWithType * );
326 SYMTAB_FUNC1( addType , const NamedTypeDecl * );
327 SYMTAB_FUNC1( addStruct , const StructDecl * );
328 SYMTAB_FUNC1( addEnum , const EnumDecl * );
329 SYMTAB_FUNC1( addUnion , const UnionDecl * );
330 SYMTAB_FUNC1( addTrait , const TraitDecl * );
[396b830]331 SYMTAB_FUNC2( addWith , const std::vector< ptr<Expr> > &, const Decl * );
[04124c4]332
333 // A few extra functions have more complicated behaviour, they are hand written
[7ff3e522]334 template<typename core_t>
335 static inline auto addStructFwd( core_t & core, int, const ast::StructDecl * decl ) -> decltype( core.symtab.addStruct( decl ), void() ) {
[6d51bd7]336 ast::StructDecl * fwd = new ast::StructDecl( decl->location, decl->name );
[87701b6]337 fwd->params = decl->params;
[7ff3e522]338 core.symtab.addStruct( fwd );
[6d51bd7]339 }
340
[7ff3e522]341 template<typename core_t>
342 static inline void addStructFwd( core_t &, long, const ast::StructDecl * ) {}
[6d51bd7]343
[7ff3e522]344 template<typename core_t>
345 static inline auto addUnionFwd( core_t & core, int, const ast::UnionDecl * decl ) -> decltype( core.symtab.addUnion( decl ), void() ) {
[6d51bd7]346 UnionDecl * fwd = new UnionDecl( decl->location, decl->name );
[87701b6]347 fwd->params = decl->params;
[7ff3e522]348 core.symtab.addUnion( fwd );
[6d51bd7]349 }
350
[7ff3e522]351 template<typename core_t>
352 static inline void addUnionFwd( core_t &, long, const ast::UnionDecl * ) {}
[6d51bd7]353
[7ff3e522]354 template<typename core_t>
355 static inline auto addStruct( core_t & core, int, const std::string & str ) -> decltype( core.symtab.addStruct( str ), void() ) {
356 if ( ! core.symtab.lookupStruct( str ) ) {
357 core.symtab.addStruct( str );
[6d51bd7]358 }
359 }
360
[7ff3e522]361 template<typename core_t>
362 static inline void addStruct( core_t &, long, const std::string & ) {}
[6d51bd7]363
[7ff3e522]364 template<typename core_t>
365 static inline auto addUnion( core_t & core, int, const std::string & str ) -> decltype( core.symtab.addUnion( str ), void() ) {
366 if ( ! core.symtab.lookupUnion( str ) ) {
367 core.symtab.addUnion( str );
[6d51bd7]368 }
369 }
370
[7ff3e522]371 template<typename core_t>
372 static inline void addUnion( core_t &, long, const std::string & ) {}
[04124c4]373
[0e42794]374 #undef SYMTAB_FUNC1
375 #undef SYMTAB_FUNC2
[e0e9a0b]376 } // namespace symtab
377
378 // Some passes need to mutate TypeDecl and properly update their pointing TypeInstType.
379 // Detect the presence of a member name `subs` and call all members appropriately
380 namespace forall {
381 // Some simple scoping rules
[7ff3e522]382 template<typename core_t>
383 static inline auto enter( core_t & core, int, const ast::ParameterizedType * type )
384 -> decltype( core.subs, void() ) {
385 if ( ! type->forall.empty() ) core.subs.beginScope();
[e0e9a0b]386 }
387
[7ff3e522]388 template<typename core_t>
389 static inline auto enter( core_t &, long, const ast::ParameterizedType * ) {}
[e0e9a0b]390
[7ff3e522]391 template<typename core_t>
392 static inline auto leave( core_t & core, int, const ast::ParameterizedType * type )
393 -> decltype( core.subs, void() ) {
394 if ( ! type->forall.empty() ) { core.subs.endScope(); }
[e0e9a0b]395 }
396
[7ff3e522]397 template<typename core_t>
398 static inline auto leave( core_t &, long, const ast::ParameterizedType * ) {}
[e0e9a0b]399
400 // Get the substitution table, if present
[7ff3e522]401 template<typename core_t>
402 static inline auto subs( core_t & core, int ) -> decltype( &core.subs ) {
403 return &core.subs;
[e0e9a0b]404 }
[396b830]405
[7ff3e522]406 template<typename core_t>
407 static inline ast::ForallSubstitutionTable * subs( core_t &, long ) { return nullptr; }
[e0e9a0b]408
409 // Replaces a TypeInstType's base TypeDecl according to the table
[7ff3e522]410 template<typename core_t>
411 static inline auto replace( core_t & core, int, const ast::TypeInstType *& inst )
412 -> decltype( core.subs, void() ) {
[396b830]413 inst = ast::mutate_field(
[7ff3e522]414 inst, &ast::TypeInstType::base, core.subs.replace( inst->base ) );
[e0e9a0b]415 }
416
[7ff3e522]417 template<typename core_t>
418 static inline auto replace( core_t &, long, const ast::TypeInstType *& ) {}
[e0e9a0b]419
420 } // namespace forall
421} // namespace __pass
[aebf5b0]422} // namespace ast
Note: See TracBrowser for help on using the repository browser.