| 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 |
|
|---|
| 16 | #pragma once
|
|---|
| 17 | // IWYU pragma: private, include "AST/Pass.hpp"
|
|---|
| 18 |
|
|---|
| 19 | #include <type_traits>
|
|---|
| 20 | #include <unordered_map>
|
|---|
| 21 |
|
|---|
| 22 | #include "AST/TypeSubstitution.hpp"
|
|---|
| 23 |
|
|---|
| 24 | #define VISIT_START( node ) \
|
|---|
| 25 | using namespace ast; \
|
|---|
| 26 | /* back-up the visit children */ \
|
|---|
| 27 | __attribute__((unused)) ast::__pass::visit_children_guard guard1( ast::__pass::visit_children(pass, 0) ); \
|
|---|
| 28 | /* setup the scope for passes that want to run code at exit */ \
|
|---|
| 29 | __attribute__((unused)) ast::__pass::guard_value guard2( ast::__pass::at_cleanup (pass, 0) ); \
|
|---|
| 30 | /* call the implementation of the previsit of this pass */ \
|
|---|
| 31 | __pass::previsit( pass, node, 0 );
|
|---|
| 32 |
|
|---|
| 33 | #define VISIT( code... ) \
|
|---|
| 34 | /* if this node should visit its children */ \
|
|---|
| 35 | if ( __visit_children() ) { \
|
|---|
| 36 | /* visit the children */ \
|
|---|
| 37 | code \
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | #define VISIT_END( type, node ) \
|
|---|
| 41 | /* call the implementation of the postvisit of this pass */ \
|
|---|
| 42 | auto __return = __pass::postvisit( pass, node, 0 ); \
|
|---|
| 43 | assertf(__return, "post visit should never return null"); \
|
|---|
| 44 | return __return;
|
|---|
| 45 |
|
|---|
| 46 | #ifdef PEDANTIC_PASS_ASSERT
|
|---|
| 47 | #define __pedantic_pass_assert(...) assert (__VA_ARGS__)
|
|---|
| 48 | #define __pedantic_pass_assertf(...) assertf(__VA_ARGS__)
|
|---|
| 49 | #else
|
|---|
| 50 | #define __pedantic_pass_assert(...)
|
|---|
| 51 | #define __pedantic_pass_assertf(...)
|
|---|
| 52 | #endif
|
|---|
| 53 |
|
|---|
| 54 | namespace ast {
|
|---|
| 55 | namespace __pass {
|
|---|
| 56 | // Check if this is either a null pointer or a pointer to an empty container
|
|---|
| 57 | template<typename T>
|
|---|
| 58 | static inline bool empty( T * ptr ) {
|
|---|
| 59 | return !ptr || ptr->empty();
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | //------------------------------
|
|---|
| 63 | template<typename it_t, template <class...> class container_t>
|
|---|
| 64 | static inline void take_all( it_t it, container_t<ast::ptr<ast::Decl>> * decls, bool * mutated = nullptr ) {
|
|---|
| 65 | if(empty(decls)) return;
|
|---|
| 66 |
|
|---|
| 67 | std::transform(decls->begin(), decls->end(), it, [](const ast::Decl * decl) -> auto {
|
|---|
| 68 | return new DeclStmt( decl->location, decl );
|
|---|
| 69 | });
|
|---|
| 70 | decls->clear();
|
|---|
| 71 | if(mutated) *mutated = true;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | template<typename it_t, template <class...> class container_t>
|
|---|
| 75 | static inline void take_all( it_t it, container_t<ast::ptr<ast::Stmt>> * decls, bool * mutated = nullptr ) {
|
|---|
| 76 | if(empty(decls)) return;
|
|---|
| 77 |
|
|---|
| 78 | std::move(decls->begin(), decls->end(), it);
|
|---|
| 79 | decls->clear();
|
|---|
| 80 | if(mutated) *mutated = true;
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | //------------------------------
|
|---|
| 84 | /// Check if should be skipped, different for pointers and containers
|
|---|
| 85 | template<typename node_t>
|
|---|
| 86 | bool skip( const ast::ptr<node_t> & val) {
|
|---|
| 87 | return !val;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | template< template <class...> class container_t, typename node_t >
|
|---|
| 91 | bool skip( const container_t<ast::ptr< node_t >> & val ) {
|
|---|
| 92 | return val.empty();
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | //------------------------------
|
|---|
| 96 | /// Get the value to visit, different for pointers and containers
|
|---|
| 97 | template<typename node_t>
|
|---|
| 98 | auto get( const ast::ptr<node_t> & val, int ) -> decltype(val.get()) {
|
|---|
| 99 | return val.get();
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | template<typename node_t>
|
|---|
| 103 | const node_t & get( const node_t & val, long) {
|
|---|
| 104 | return val;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 | //------------------------------
|
|---|
| 109 | /// Check if value was mutated, different for pointers and containers
|
|---|
| 110 | template<typename lhs_t, typename rhs_t>
|
|---|
| 111 | bool differs( const lhs_t * old_val, const rhs_t * new_val ) {
|
|---|
| 112 | return old_val != new_val;
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | template< template <class...> class container_t, typename node_t >
|
|---|
| 116 | bool differs( const container_t<ast::ptr< node_t >> &, const container_t<ast::ptr< node_t >> & new_val ) {
|
|---|
| 117 | return !new_val.empty();
|
|---|
| 118 | }
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | template< typename pass_t >
|
|---|
| 122 | template< typename node_t >
|
|---|
| 123 | auto Pass< pass_t >::call_accept( const node_t * node )
|
|---|
| 124 | -> typename std::enable_if<
|
|---|
| 125 | !std::is_base_of<ast::Expr, node_t>::value &&
|
|---|
| 126 | !std::is_base_of<ast::Stmt, node_t>::value
|
|---|
| 127 | , decltype( node->accept(*this) )
|
|---|
| 128 | >::type
|
|---|
| 129 |
|
|---|
| 130 | {
|
|---|
| 131 | __pedantic_pass_assert( __visit_children() );
|
|---|
| 132 | __pedantic_pass_assert( expr );
|
|---|
| 133 |
|
|---|
| 134 | static_assert( !std::is_base_of<ast::Expr, node_t>::value, "ERROR");
|
|---|
| 135 | static_assert( !std::is_base_of<ast::Stmt, node_t>::value, "ERROR");
|
|---|
| 136 |
|
|---|
| 137 | return node->accept( *this );
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | template< typename pass_t >
|
|---|
| 141 | const ast::Expr * Pass< pass_t >::call_accept( const ast::Expr * expr ) {
|
|---|
| 142 | __pedantic_pass_assert( __visit_children() );
|
|---|
| 143 | __pedantic_pass_assert( expr );
|
|---|
| 144 |
|
|---|
| 145 | const ast::TypeSubstitution ** env_ptr = __pass::env( pass, 0);
|
|---|
| 146 | if ( env_ptr && expr->env ) {
|
|---|
| 147 | *env_ptr = expr->env;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | return expr->accept( *this );
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | template< typename pass_t >
|
|---|
| 154 | const ast::Stmt * Pass< pass_t >::call_accept( const ast::Stmt * stmt ) {
|
|---|
| 155 | __pedantic_pass_assert( __visit_children() );
|
|---|
| 156 | __pedantic_pass_assert( stmt );
|
|---|
| 157 |
|
|---|
| 158 | // add a few useful symbols to the scope
|
|---|
| 159 | using __pass::empty;
|
|---|
| 160 |
|
|---|
| 161 | // get the stmts/decls that will need to be spliced in
|
|---|
| 162 | auto stmts_before = __pass::stmtsToAddBefore( pass, 0);
|
|---|
| 163 | auto stmts_after = __pass::stmtsToAddAfter ( pass, 0);
|
|---|
| 164 | auto decls_before = __pass::declsToAddBefore( pass, 0);
|
|---|
| 165 | auto decls_after = __pass::declsToAddAfter ( pass, 0);
|
|---|
| 166 |
|
|---|
| 167 | // These may be modified by subnode but most be restored once we exit this statemnet.
|
|---|
| 168 | ValueGuardPtr< const ast::TypeSubstitution * > __old_env ( __pass::env( pass, 0) );
|
|---|
| 169 | ValueGuardPtr< typename std::remove_pointer< decltype(stmts_before) >::type > __old_decls_before( stmts_before );
|
|---|
| 170 | ValueGuardPtr< typename std::remove_pointer< decltype(stmts_after ) >::type > __old_decls_after ( stmts_after );
|
|---|
| 171 | ValueGuardPtr< typename std::remove_pointer< decltype(decls_before) >::type > __old_stmts_before( decls_before );
|
|---|
| 172 | ValueGuardPtr< typename std::remove_pointer< decltype(decls_after ) >::type > __old_stmts_after ( decls_after );
|
|---|
| 173 |
|
|---|
| 174 | // Now is the time to actually visit the node
|
|---|
| 175 | const ast::Stmt * nstmt = stmt->accept( *this );
|
|---|
| 176 |
|
|---|
| 177 | // If the pass doesn't want to add anything then we are done
|
|---|
| 178 | if( empty(stmts_before) && empty(stmts_after) && empty(decls_before) && empty(decls_after) ) {
|
|---|
| 179 | return nstmt;
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | // Make sure that it is either adding statements or declartions but not both
|
|---|
| 183 | // this is because otherwise the order would be awkward to predict
|
|---|
| 184 | assert(( empty( stmts_before ) && empty( stmts_after ))
|
|---|
| 185 | || ( empty( decls_before ) && empty( decls_after )) );
|
|---|
| 186 |
|
|---|
| 187 | // Create a new Compound Statement to hold the new decls/stmts
|
|---|
| 188 | ast::CompoundStmt * compound = new ast::CompoundStmt( stmt->location );
|
|---|
| 189 |
|
|---|
| 190 | // Take all the declarations that go before
|
|---|
| 191 | __pass::take_all( std::back_inserter( compound->kids ), decls_before );
|
|---|
| 192 | __pass::take_all( std::back_inserter( compound->kids ), stmts_before );
|
|---|
| 193 |
|
|---|
| 194 | // Insert the original declaration
|
|---|
| 195 | compound->kids.emplace_back( nstmt );
|
|---|
| 196 |
|
|---|
| 197 | // Insert all the declarations that go before
|
|---|
| 198 | __pass::take_all( std::back_inserter( compound->kids ), decls_after );
|
|---|
| 199 | __pass::take_all( std::back_inserter( compound->kids ), stmts_after );
|
|---|
| 200 |
|
|---|
| 201 | return compound;
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 | template< typename pass_t >
|
|---|
| 205 | template< template <class...> class container_t >
|
|---|
| 206 | container_t< ptr<Stmt> > Pass< pass_t >::call_accept( const container_t< ptr<Stmt> > & statements ) {
|
|---|
| 207 | __pedantic_pass_assert( __visit_children() );
|
|---|
| 208 | if( statements.empty() ) return {};
|
|---|
| 209 |
|
|---|
| 210 | // We are going to aggregate errors for all these statements
|
|---|
| 211 | SemanticErrorException errors;
|
|---|
| 212 |
|
|---|
| 213 | // add a few useful symbols to the scope
|
|---|
| 214 | using __pass::empty;
|
|---|
| 215 |
|
|---|
| 216 | // get the stmts/decls that will need to be spliced in
|
|---|
| 217 | auto stmts_before = __pass::stmtsToAddBefore( pass, 0);
|
|---|
| 218 | auto stmts_after = __pass::stmtsToAddAfter ( pass, 0);
|
|---|
| 219 | auto decls_before = __pass::declsToAddBefore( pass, 0);
|
|---|
| 220 | auto decls_after = __pass::declsToAddAfter ( pass, 0);
|
|---|
| 221 |
|
|---|
| 222 | // These may be modified by subnode but most be restored once we exit this statemnet.
|
|---|
| 223 | ValueGuardPtr< typename std::remove_pointer< decltype(stmts_before) >::type > __old_decls_before( stmts_before );
|
|---|
| 224 | ValueGuardPtr< typename std::remove_pointer< decltype(stmts_after ) >::type > __old_decls_after ( stmts_after );
|
|---|
| 225 | ValueGuardPtr< typename std::remove_pointer< decltype(decls_before) >::type > __old_stmts_before( decls_before );
|
|---|
| 226 | ValueGuardPtr< typename std::remove_pointer< decltype(decls_after ) >::type > __old_stmts_after ( decls_after );
|
|---|
| 227 |
|
|---|
| 228 | // update pass statitistics
|
|---|
| 229 | pass_visitor_stats.depth++;
|
|---|
| 230 | pass_visitor_stats.max->push(pass_visitor_stats.depth);
|
|---|
| 231 | pass_visitor_stats.avg->push(pass_visitor_stats.depth);
|
|---|
| 232 |
|
|---|
| 233 | bool mutated = false;
|
|---|
| 234 | container_t< ptr<Stmt> > new_kids;
|
|---|
| 235 | for( const Stmt * stmt : statements ) {
|
|---|
| 236 | try {
|
|---|
| 237 | __pedantic_pass_assert( stmt );
|
|---|
| 238 | const ast::Stmt * new_stmt = stmt->accept( *this );
|
|---|
| 239 | assert( new_stmt );
|
|---|
| 240 | if(new_stmt != stmt ) mutated = true;
|
|---|
| 241 |
|
|---|
| 242 | // Make sure that it is either adding statements or declartions but not both
|
|---|
| 243 | // this is because otherwise the order would be awkward to predict
|
|---|
| 244 | assert(( empty( stmts_before ) && empty( stmts_after ))
|
|---|
| 245 | || ( empty( decls_before ) && empty( decls_after )) );
|
|---|
| 246 |
|
|---|
| 247 |
|
|---|
| 248 |
|
|---|
| 249 | // Take all the statements which should have gone after, N/A for first iteration
|
|---|
| 250 | __pass::take_all( std::back_inserter( new_kids ), decls_before, &mutated );
|
|---|
| 251 | __pass::take_all( std::back_inserter( new_kids ), stmts_before, &mutated );
|
|---|
| 252 |
|
|---|
| 253 | // Now add the statement if there is one
|
|---|
| 254 | new_kids.emplace_back( new_stmt );
|
|---|
| 255 |
|
|---|
| 256 | // Take all the declarations that go before
|
|---|
| 257 | __pass::take_all( std::back_inserter( new_kids ), decls_after, &mutated );
|
|---|
| 258 | __pass::take_all( std::back_inserter( new_kids ), stmts_after, &mutated );
|
|---|
| 259 | }
|
|---|
| 260 | catch ( SemanticErrorException &e ) {
|
|---|
| 261 | errors.append( e );
|
|---|
| 262 | }
|
|---|
| 263 | }
|
|---|
| 264 | pass_visitor_stats.depth--;
|
|---|
| 265 | if ( !errors.isEmpty() ) { throw errors; }
|
|---|
| 266 |
|
|---|
| 267 | return mutated ? new_kids : container_t< ptr<Stmt> >();
|
|---|
| 268 | }
|
|---|
| 269 |
|
|---|
| 270 | template< typename pass_t >
|
|---|
| 271 | template< template <class...> class container_t, typename node_t >
|
|---|
| 272 | container_t< ast::ptr<node_t> > Pass< pass_t >::call_accept( const container_t< ast::ptr<node_t> > & container ) {
|
|---|
| 273 | __pedantic_pass_assert( __visit_children() );
|
|---|
| 274 | if( container.empty() ) return {};
|
|---|
| 275 | SemanticErrorException errors;
|
|---|
| 276 |
|
|---|
| 277 | pass_visitor_stats.depth++;
|
|---|
| 278 | pass_visitor_stats.max->push(pass_visitor_stats.depth);
|
|---|
| 279 | pass_visitor_stats.avg->push(pass_visitor_stats.depth);
|
|---|
| 280 |
|
|---|
| 281 | bool mutated = false;
|
|---|
| 282 | container_t< ast::ptr<node_t> > new_kids;
|
|---|
| 283 | for ( const node_t * node : container ) {
|
|---|
| 284 | try {
|
|---|
| 285 | __pedantic_pass_assert( node );
|
|---|
| 286 | const node_t * new_stmt = strict_dynamic_cast< const node_t * >( node->accept( *this ) );
|
|---|
| 287 | if(new_stmt != node ) mutated = true;
|
|---|
| 288 |
|
|---|
| 289 | new_kids.emplace_back( new_stmt );
|
|---|
| 290 | }
|
|---|
| 291 | catch( SemanticErrorException &e ) {
|
|---|
| 292 | errors.append( e );
|
|---|
| 293 | }
|
|---|
| 294 | }
|
|---|
| 295 | pass_visitor_stats.depth--;
|
|---|
| 296 | if ( ! errors.isEmpty() ) { throw errors; }
|
|---|
| 297 |
|
|---|
| 298 | return mutated ? new_kids : container_t< ast::ptr<node_t> >();
|
|---|
| 299 | }
|
|---|
| 300 |
|
|---|
| 301 | template< typename pass_t >
|
|---|
| 302 | template<typename node_t, typename parent_t, typename child_t>
|
|---|
| 303 | void Pass< pass_t >::maybe_accept(
|
|---|
| 304 | const node_t * & parent,
|
|---|
| 305 | child_t parent_t::*child
|
|---|
| 306 | ) {
|
|---|
| 307 | static_assert( std::is_base_of<parent_t, node_t>::value, "Error deducing member object" );
|
|---|
| 308 |
|
|---|
| 309 | if(__pass::skip(parent->*child)) return;
|
|---|
| 310 | const auto & old_val = __pass::get(parent->*child, 0);
|
|---|
| 311 |
|
|---|
| 312 | static_assert( !std::is_same<const ast::Node * &, decltype(old_val)>::value, "ERROR");
|
|---|
| 313 |
|
|---|
| 314 | auto new_val = call_accept( old_val );
|
|---|
| 315 |
|
|---|
| 316 | static_assert( !std::is_same<const ast::Node *, decltype(new_val)>::value || std::is_same<int, decltype(old_val)>::value, "ERROR");
|
|---|
| 317 |
|
|---|
| 318 | if( __pass::differs(old_val, new_val) ) {
|
|---|
| 319 | auto new_parent = mutate(parent);
|
|---|
| 320 | new_parent->*child = new_val;
|
|---|
| 321 | parent = new_parent;
|
|---|
| 322 | }
|
|---|
| 323 | }
|
|---|
| 324 |
|
|---|
| 325 | }
|
|---|
| 326 |
|
|---|
| 327 | //------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
|---|
| 328 | //========================================================================================================================================================================
|
|---|
| 329 | //========================================================================================================================================================================
|
|---|
| 330 | //========================================================================================================================================================================
|
|---|
| 331 | //========================================================================================================================================================================
|
|---|
| 332 | //========================================================================================================================================================================
|
|---|
| 333 | //------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
|---|
| 334 |
|
|---|
| 335 | template< typename pass_t >
|
|---|
| 336 | inline void ast::accept_all( std::list< ast::ptr<ast::Decl> > & decls, ast::Pass< pass_t > & visitor ) {
|
|---|
| 337 | // We are going to aggregate errors for all these statements
|
|---|
| 338 | SemanticErrorException errors;
|
|---|
| 339 |
|
|---|
| 340 | // add a few useful symbols to the scope
|
|---|
| 341 | using __pass::empty;
|
|---|
| 342 |
|
|---|
| 343 | // get the stmts/decls that will need to be spliced in
|
|---|
| 344 | auto decls_before = __pass::declsToAddBefore( visitor.pass, 0);
|
|---|
| 345 | auto decls_after = __pass::declsToAddAfter ( visitor.pass, 0);
|
|---|
| 346 |
|
|---|
| 347 | // update pass statitistics
|
|---|
| 348 | pass_visitor_stats.depth++;
|
|---|
| 349 | pass_visitor_stats.max->push(pass_visitor_stats.depth);
|
|---|
| 350 | pass_visitor_stats.avg->push(pass_visitor_stats.depth);
|
|---|
| 351 |
|
|---|
| 352 | for ( std::list< ast::ptr<ast::Decl> >::iterator i = decls.begin(); ; ++i ) {
|
|---|
| 353 | // splice in new declarations after previous decl
|
|---|
| 354 | if ( !empty( decls_after ) ) { decls.splice( i, *decls_after ); }
|
|---|
| 355 |
|
|---|
| 356 | if ( i == decls.end() ) break;
|
|---|
| 357 |
|
|---|
| 358 | try {
|
|---|
| 359 | // run visitor on declaration
|
|---|
| 360 | ast::ptr<ast::Decl> & node = *i;
|
|---|
| 361 | assert( node );
|
|---|
| 362 | node = node->accept( visitor );
|
|---|
| 363 | }
|
|---|
| 364 | catch( SemanticErrorException &e ) {
|
|---|
| 365 | errors.append( e );
|
|---|
| 366 | }
|
|---|
| 367 |
|
|---|
| 368 | // splice in new declarations before current decl
|
|---|
| 369 | if ( !empty( decls_before ) ) { decls.splice( i, *decls_before ); }
|
|---|
| 370 | }
|
|---|
| 371 | pass_visitor_stats.depth--;
|
|---|
| 372 | if ( !errors.isEmpty() ) { throw errors; }
|
|---|
| 373 | }
|
|---|
| 374 |
|
|---|
| 375 | // A NOTE ON THE ORDER OF TRAVERSAL
|
|---|
| 376 | //
|
|---|
| 377 | // Types and typedefs have their base types visited before they are added to the type table. This is ok, since there is
|
|---|
| 378 | // no such thing as a recursive type or typedef.
|
|---|
| 379 | //
|
|---|
| 380 | // typedef struct { T *x; } T; // never allowed
|
|---|
| 381 | //
|
|---|
| 382 | // for structs/unions, it is possible to have recursion, so the decl should be added as if it's incomplete to begin, the
|
|---|
| 383 | // members are traversed, and then the complete type should be added (assuming the type is completed by this particular
|
|---|
| 384 | // declaration).
|
|---|
| 385 | //
|
|---|
| 386 | // struct T { struct T *x; }; // allowed
|
|---|
| 387 | //
|
|---|
| 388 | // It is important to add the complete type to the symbol table *after* the members/base has been traversed, since that
|
|---|
| 389 | // traversal may modify the definition of the type and these modifications should be visible when the symbol table is
|
|---|
| 390 | // queried later in this pass.
|
|---|
| 391 |
|
|---|
| 392 | //--------------------------------------------------------------------------
|
|---|
| 393 | // ObjectDecl
|
|---|
| 394 | template< typename pass_t >
|
|---|
| 395 | const ast::DeclWithType * ast::Pass< pass_t >::visit( const ast::ObjectDecl * node ) {
|
|---|
| 396 | VISIT_START( node );
|
|---|
| 397 |
|
|---|
| 398 | VISIT(
|
|---|
| 399 | {
|
|---|
| 400 | guard_indexer guard { *this };
|
|---|
| 401 | maybe_accept( node, &ObjectDecl::type );
|
|---|
| 402 | }
|
|---|
| 403 | maybe_accept( node, &ObjectDecl::init );
|
|---|
| 404 | maybe_accept( node, &ObjectDecl::bitfieldWidth );
|
|---|
| 405 | maybe_accept( node, &ObjectDecl::attributes );
|
|---|
| 406 | )
|
|---|
| 407 |
|
|---|
| 408 | __pass::indexer::addId( pass, 0, node );
|
|---|
| 409 |
|
|---|
| 410 | VISIT_END( DeclWithType, node );
|
|---|
| 411 | }
|
|---|
| 412 |
|
|---|
| 413 | //--------------------------------------------------------------------------
|
|---|
| 414 | // FunctionDecl
|
|---|
| 415 | template< typename pass_t >
|
|---|
| 416 | const ast::DeclWithType * ast::Pass< pass_t >::visit( const ast::FunctionDecl * node ) {
|
|---|
| 417 | VISIT_START( node );
|
|---|
| 418 |
|
|---|
| 419 | __pass::indexer::addId( pass, 0, node );
|
|---|
| 420 |
|
|---|
| 421 | VISIT(maybe_accept( node, &FunctionDecl::withExprs );)
|
|---|
| 422 | {
|
|---|
| 423 | // with clause introduces a level of scope (for the with expression members).
|
|---|
| 424 | // with clause exprs are added to the indexer before parameters so that parameters
|
|---|
| 425 | // shadow with exprs and not the other way around.
|
|---|
| 426 | guard_indexer guard { *this };
|
|---|
| 427 | __pass::indexer::addWith( pass, 0, node->withExprs, node );
|
|---|
| 428 | {
|
|---|
| 429 | guard_indexer guard { *this };
|
|---|
| 430 | // implicit add __func__ identifier as specified in the C manual 6.4.2.2
|
|---|
| 431 | static ast::ObjectDecl func(
|
|---|
| 432 | node->location, "__func__",
|
|---|
| 433 | new ast::ArrayType(
|
|---|
| 434 | new ast::BasicType( ast::BasicType::Char, ast::CV::Qualifiers( ast::CV::Const ) ),
|
|---|
| 435 | nullptr, VariableLen, DynamicDim
|
|---|
| 436 | )
|
|---|
| 437 | );
|
|---|
| 438 | __pass::indexer::addId( pass, 0, &func );
|
|---|
| 439 | VISIT(
|
|---|
| 440 | maybe_accept( node, &FunctionDecl::type );
|
|---|
| 441 | // function body needs to have the same scope as parameters - CompoundStmt will not enter
|
|---|
| 442 | // a new scope if inFunction is true
|
|---|
| 443 | ValueGuard< bool > oldInFunction( inFunction );
|
|---|
| 444 | inFunction = true;
|
|---|
| 445 | maybe_accept( node, &FunctionDecl::stmts );
|
|---|
| 446 | maybe_accept( node, &FunctionDecl::attributes );
|
|---|
| 447 | )
|
|---|
| 448 | }
|
|---|
| 449 | }
|
|---|
| 450 |
|
|---|
| 451 | VISIT_END( DeclWithType, node );
|
|---|
| 452 | }
|
|---|
| 453 |
|
|---|
| 454 | //--------------------------------------------------------------------------
|
|---|
| 455 | // StructDecl
|
|---|
| 456 | template< typename pass_t >
|
|---|
| 457 | const ast::Decl * ast::Pass< pass_t >::visit( const ast::StructDecl * node ) {
|
|---|
| 458 | VISIT_START( node );
|
|---|
| 459 |
|
|---|
| 460 | // make up a forward declaration and add it before processing the members
|
|---|
| 461 | // needs to be on the heap because addStruct saves the pointer
|
|---|
| 462 | __pass::indexer::addStructFwd( pass, 0, node );
|
|---|
| 463 |
|
|---|
| 464 | VISIT({
|
|---|
| 465 | guard_indexer guard { * this };
|
|---|
| 466 | maybe_accept( node, &StructDecl::params );
|
|---|
| 467 | maybe_accept( node, &StructDecl::members );
|
|---|
| 468 | })
|
|---|
| 469 |
|
|---|
| 470 | // this addition replaces the forward declaration
|
|---|
| 471 | __pass::indexer::addStruct( pass, 0, node );
|
|---|
| 472 |
|
|---|
| 473 | VISIT_END( Decl, node );
|
|---|
| 474 | }
|
|---|
| 475 |
|
|---|
| 476 | //--------------------------------------------------------------------------
|
|---|
| 477 | // UnionDecl
|
|---|
| 478 | template< typename pass_t >
|
|---|
| 479 | const ast::Decl * ast::Pass< pass_t >::visit( const ast::UnionDecl * node ) {
|
|---|
| 480 | VISIT_START( node );
|
|---|
| 481 |
|
|---|
| 482 | // make up a forward declaration and add it before processing the members
|
|---|
| 483 | __pass::indexer::addUnionFwd( pass, 0, node );
|
|---|
| 484 |
|
|---|
| 485 | VISIT({
|
|---|
| 486 | guard_indexer guard { * this };
|
|---|
| 487 | maybe_accept( node, &UnionDecl::params );
|
|---|
| 488 | maybe_accept( node, &UnionDecl::members );
|
|---|
| 489 | })
|
|---|
| 490 |
|
|---|
| 491 | __pass::indexer::addUnion( pass, 0, node );
|
|---|
| 492 |
|
|---|
| 493 | VISIT_END( Decl, node );
|
|---|
| 494 | }
|
|---|
| 495 |
|
|---|
| 496 | //--------------------------------------------------------------------------
|
|---|
| 497 | // EnumDecl
|
|---|
| 498 | template< typename pass_t >
|
|---|
| 499 | const ast::Decl * ast::Pass< pass_t >::visit( const ast::EnumDecl * node ) {
|
|---|
| 500 | VISIT_START( node );
|
|---|
| 501 |
|
|---|
| 502 | __pass::indexer::addEnum( pass, 0, node );
|
|---|
| 503 |
|
|---|
| 504 | VISIT(
|
|---|
| 505 | // unlike structs, traits, and unions, enums inject their members into the global scope
|
|---|
| 506 | maybe_accept( node, &EnumDecl::params );
|
|---|
| 507 | maybe_accept( node, &EnumDecl::members );
|
|---|
| 508 | )
|
|---|
| 509 |
|
|---|
| 510 | VISIT_END( Decl, node );
|
|---|
| 511 | }
|
|---|
| 512 |
|
|---|
| 513 | //--------------------------------------------------------------------------
|
|---|
| 514 | // TraitDecl
|
|---|
| 515 | template< typename pass_t >
|
|---|
| 516 | const ast::Decl * ast::Pass< pass_t >::visit( const ast::TraitDecl * node ) {
|
|---|
| 517 | VISIT_START( node );
|
|---|
| 518 |
|
|---|
| 519 | VISIT({
|
|---|
| 520 | guard_indexer guard { *this };
|
|---|
| 521 | maybe_accept( node, &TraitDecl::params );
|
|---|
| 522 | maybe_accept( node, &TraitDecl::members );
|
|---|
| 523 | })
|
|---|
| 524 |
|
|---|
| 525 | __pass::indexer::addTrait( pass, 0, node );
|
|---|
| 526 |
|
|---|
| 527 | VISIT_END( Decl, node );
|
|---|
| 528 | }
|
|---|
| 529 |
|
|---|
| 530 | //--------------------------------------------------------------------------
|
|---|
| 531 | // TypeDecl
|
|---|
| 532 | template< typename pass_t >
|
|---|
| 533 | const ast::Decl * ast::Pass< pass_t >::visit( const ast::TypeDecl * node ) {
|
|---|
| 534 | VISIT_START( node );
|
|---|
| 535 |
|
|---|
| 536 | VISIT({
|
|---|
| 537 | guard_indexer guard { *this };
|
|---|
| 538 | maybe_accept( node, &TypeDecl::params );
|
|---|
| 539 | maybe_accept( node, &TypeDecl::base );
|
|---|
| 540 | })
|
|---|
| 541 |
|
|---|
| 542 | // see A NOTE ON THE ORDER OF TRAVERSAL, above
|
|---|
| 543 | // note that assertions come after the type is added to the symtab, since they are not part of the type proper
|
|---|
| 544 | // and may depend on the type itself
|
|---|
| 545 | __pass::indexer::addType( pass, 0, node );
|
|---|
| 546 |
|
|---|
| 547 | VISIT(
|
|---|
| 548 | maybe_accept( node, &TypeDecl::assertions );
|
|---|
| 549 |
|
|---|
| 550 | {
|
|---|
| 551 | guard_indexer guard { *this };
|
|---|
| 552 | maybe_accept( node, &TypeDecl::init );
|
|---|
| 553 | }
|
|---|
| 554 | )
|
|---|
| 555 |
|
|---|
| 556 | VISIT_END( Decl, node );
|
|---|
| 557 | }
|
|---|
| 558 |
|
|---|
| 559 | //--------------------------------------------------------------------------
|
|---|
| 560 | // TypedefDecl
|
|---|
| 561 | template< typename pass_t >
|
|---|
| 562 | const ast::Decl * ast::Pass< pass_t >::visit( const ast::TypedefDecl * node ) {
|
|---|
| 563 | VISIT_START( node );
|
|---|
| 564 |
|
|---|
| 565 | VISIT({
|
|---|
| 566 | guard_indexer guard { *this };
|
|---|
| 567 | maybe_accept( node, &TypedefDecl::params );
|
|---|
| 568 | maybe_accept( node, &TypedefDecl::base );
|
|---|
| 569 | })
|
|---|
| 570 |
|
|---|
| 571 | __pass::indexer::addType( pass, 0, node );
|
|---|
| 572 |
|
|---|
| 573 | maybe_accept( node, &TypedefDecl::assertions );
|
|---|
| 574 |
|
|---|
| 575 | VISIT_END( Decl, node );
|
|---|
| 576 | }
|
|---|
| 577 |
|
|---|
| 578 | //--------------------------------------------------------------------------
|
|---|
| 579 | // AsmDecl
|
|---|
| 580 | template< typename pass_t >
|
|---|
| 581 | const ast::AsmDecl * ast::Pass< pass_t >::visit( const ast::AsmDecl * node ) {
|
|---|
| 582 | VISIT_START( node );
|
|---|
| 583 |
|
|---|
| 584 | VISIT(
|
|---|
| 585 | maybe_accept( node, &AsmDecl::stmt );
|
|---|
| 586 | )
|
|---|
| 587 |
|
|---|
| 588 | VISIT_END( AsmDecl, node );
|
|---|
| 589 | }
|
|---|
| 590 |
|
|---|
| 591 | //--------------------------------------------------------------------------
|
|---|
| 592 | // StaticAssertDecl
|
|---|
| 593 | template< typename pass_t >
|
|---|
| 594 | const ast::StaticAssertDecl * ast::Pass< pass_t >::visit( const ast::StaticAssertDecl * node ) {
|
|---|
| 595 | VISIT_START( node );
|
|---|
| 596 |
|
|---|
| 597 | VISIT(
|
|---|
| 598 | maybe_accept( node, &StaticAssertDecl::condition );
|
|---|
| 599 | maybe_accept( node, &StaticAssertDecl::msg );
|
|---|
| 600 | )
|
|---|
| 601 |
|
|---|
| 602 | VISIT_END( StaticAssertDecl, node );
|
|---|
| 603 | }
|
|---|
| 604 |
|
|---|
| 605 | //--------------------------------------------------------------------------
|
|---|
| 606 | // CompoundStmt
|
|---|
| 607 | template< typename pass_t >
|
|---|
| 608 | const ast::CompoundStmt * ast::Pass< pass_t >::visit( const ast::CompoundStmt * node ) {
|
|---|
| 609 | VISIT_START( node );
|
|---|
| 610 | VISIT({
|
|---|
| 611 | // do not enter a new scope if inFunction is true - needs to check old state before the assignment
|
|---|
| 612 | auto guard1 = makeFuncGuard( [this, inFunction = this->inFunction]() {
|
|---|
| 613 | if ( ! inFunction ) __pass::indexer::enter(pass, 0);
|
|---|
| 614 | }, [this, inFunction = this->inFunction]() {
|
|---|
| 615 | if ( ! inFunction ) __pass::indexer::leave(pass, 0);
|
|---|
| 616 | });
|
|---|
| 617 | ValueGuard< bool > guard2( inFunction );
|
|---|
| 618 | guard_scope guard3 { *this };
|
|---|
| 619 | inFunction = false;
|
|---|
| 620 | maybe_accept( node, &CompoundStmt::kids );
|
|---|
| 621 | })
|
|---|
| 622 | VISIT_END( CompoundStmt, node );
|
|---|
| 623 | }
|
|---|
| 624 |
|
|---|
| 625 | //--------------------------------------------------------------------------
|
|---|
| 626 | // ExprStmt
|
|---|
| 627 | template< typename pass_t >
|
|---|
| 628 | const ast::Stmt * ast::Pass< pass_t >::visit( const ExprStmt * node ) {
|
|---|
| 629 | VISIT_START( node );
|
|---|
| 630 |
|
|---|
| 631 | VISIT(
|
|---|
| 632 | maybe_accept( node, &ExprStmt::expr );
|
|---|
| 633 | )
|
|---|
| 634 |
|
|---|
| 635 | VISIT_END( Stmt, node );
|
|---|
| 636 | }
|
|---|
| 637 |
|
|---|
| 638 | //--------------------------------------------------------------------------
|
|---|
| 639 | // AsmStmt
|
|---|
| 640 | template< typename pass_t >
|
|---|
| 641 | const ast::Stmt * ast::Pass< pass_t >::visit( const ast::AsmStmt * node ) {
|
|---|
| 642 | VISIT_START( node )
|
|---|
| 643 |
|
|---|
| 644 | VISIT(
|
|---|
| 645 | maybe_accept( node, &AsmStmt::instruction );
|
|---|
| 646 | maybe_accept( node, &AsmStmt::output );
|
|---|
| 647 | maybe_accept( node, &AsmStmt::input );
|
|---|
| 648 | maybe_accept( node, &AsmStmt::clobber );
|
|---|
| 649 | )
|
|---|
| 650 |
|
|---|
| 651 | VISIT_END( Stmt, node );
|
|---|
| 652 | }
|
|---|
| 653 |
|
|---|
| 654 | //--------------------------------------------------------------------------
|
|---|
| 655 | // DirectiveStmt
|
|---|
| 656 | template< typename pass_t >
|
|---|
| 657 | const ast::Stmt * ast::Pass< pass_t >::visit( const ast::DirectiveStmt * node ) {
|
|---|
| 658 | VISIT_START( node )
|
|---|
| 659 |
|
|---|
| 660 | VISIT_END( Stmt, node );
|
|---|
| 661 | }
|
|---|
| 662 |
|
|---|
| 663 | //--------------------------------------------------------------------------
|
|---|
| 664 | // IfStmt
|
|---|
| 665 | template< typename pass_t >
|
|---|
| 666 | const ast::Stmt * ast::Pass< pass_t >::visit( const ast::IfStmt * node ) {
|
|---|
| 667 | VISIT_START( node );
|
|---|
| 668 | VISIT({
|
|---|
| 669 | // if statements introduce a level of scope (for the initialization)
|
|---|
| 670 | guard_indexer guard { *this };
|
|---|
| 671 | maybe_accept( node, &IfStmt::inits );
|
|---|
| 672 | maybe_accept( node, &IfStmt::cond );
|
|---|
| 673 | maybe_accept( node, &IfStmt::thenPart );
|
|---|
| 674 | maybe_accept( node, &IfStmt::elsePart );
|
|---|
| 675 | })
|
|---|
| 676 | VISIT_END( Stmt, node );
|
|---|
| 677 | }
|
|---|
| 678 |
|
|---|
| 679 | //--------------------------------------------------------------------------
|
|---|
| 680 | // WhileStmt
|
|---|
| 681 | template< typename pass_t >
|
|---|
| 682 | const ast::Stmt * ast::Pass< pass_t >::visit( const WhileStmt * node ) {
|
|---|
| 683 | VISIT_START( node );
|
|---|
| 684 |
|
|---|
| 685 | VISIT({
|
|---|
| 686 | // while statements introduce a level of scope (for the initialization)
|
|---|
| 687 | guard_indexer guard { *this };
|
|---|
| 688 | maybe_accept( node, &WhileStmt::inits );
|
|---|
| 689 | maybe_accept( node, &WhileStmt::cond );
|
|---|
| 690 | maybe_accept( node, &WhileStmt::body );
|
|---|
| 691 | })
|
|---|
| 692 |
|
|---|
| 693 | VISIT_END( Stmt, node );
|
|---|
| 694 | }
|
|---|
| 695 |
|
|---|
| 696 | //--------------------------------------------------------------------------
|
|---|
| 697 | // SingleInit
|
|---|
| 698 | template< typename pass_t >
|
|---|
| 699 | const ast::Init * ast::Pass< pass_t >::visit( const ast::SingleInit * node ) {
|
|---|
| 700 | VISIT_START( node );
|
|---|
| 701 |
|
|---|
| 702 | VISIT(
|
|---|
| 703 | maybe_accept( node, &SingleInit::value );
|
|---|
| 704 | )
|
|---|
| 705 |
|
|---|
| 706 | VISIT_END( Init, node );
|
|---|
| 707 | }
|
|---|
| 708 |
|
|---|
| 709 | //--------------------------------------------------------------------------
|
|---|
| 710 | // ListInit
|
|---|
| 711 | template< typename pass_t >
|
|---|
| 712 | const ast::Init * ast::Pass< pass_t >::visit( const ast::ListInit * node ) {
|
|---|
| 713 | VISIT_START( node );
|
|---|
| 714 |
|
|---|
| 715 | VISIT(
|
|---|
| 716 | maybe_accept( node, &ListInit::designations );
|
|---|
| 717 | maybe_accept( node, &ListInit::initializers );
|
|---|
| 718 | )
|
|---|
| 719 |
|
|---|
| 720 | VISIT_END( Init, node );
|
|---|
| 721 | }
|
|---|
| 722 |
|
|---|
| 723 | //--------------------------------------------------------------------------
|
|---|
| 724 | // ConstructorInit
|
|---|
| 725 | template< typename pass_t >
|
|---|
| 726 | const ast::Init * ast::Pass< pass_t >::visit( const ast::ConstructorInit * node ) {
|
|---|
| 727 | VISIT_START( node );
|
|---|
| 728 |
|
|---|
| 729 | VISIT(
|
|---|
| 730 | maybe_accept( node, &ConstructorInit::ctor );
|
|---|
| 731 | maybe_accept( node, &ConstructorInit::dtor );
|
|---|
| 732 | maybe_accept( node, &ConstructorInit::init );
|
|---|
| 733 | )
|
|---|
| 734 |
|
|---|
| 735 | VISIT_END( Init, node );
|
|---|
| 736 | }
|
|---|
| 737 |
|
|---|
| 738 | //--------------------------------------------------------------------------
|
|---|
| 739 | // Attribute
|
|---|
| 740 | template< typename pass_t >
|
|---|
| 741 | const ast::Attribute * ast::Pass< pass_t >::visit( const ast::Attribute * node ) {
|
|---|
| 742 | VISIT_START( node );
|
|---|
| 743 |
|
|---|
| 744 | VISIT(
|
|---|
| 745 | maybe_accept( node, &Attribute::parameters );
|
|---|
| 746 | )
|
|---|
| 747 |
|
|---|
| 748 | VISIT_END( Attribute, node );
|
|---|
| 749 | }
|
|---|
| 750 |
|
|---|
| 751 | //--------------------------------------------------------------------------
|
|---|
| 752 | // TypeSubstitution
|
|---|
| 753 | template< typename pass_t >
|
|---|
| 754 | const ast::TypeSubstitution * ast::Pass< pass_t >::visit( const ast::TypeSubstitution * node ) {
|
|---|
| 755 | VISIT_START( node );
|
|---|
| 756 |
|
|---|
| 757 | VISIT(
|
|---|
| 758 | {
|
|---|
| 759 | bool mutated = false;
|
|---|
| 760 | std::unordered_map< std::string, ast::ptr< ast::Type > > new_map;
|
|---|
| 761 | for ( const auto & p : node->typeEnv ) {
|
|---|
| 762 | guard_indexer guard { *this };
|
|---|
| 763 | auto new_node = p.second->accept( *this );
|
|---|
| 764 | if (new_node != p.second) mutated = false;
|
|---|
| 765 | new_map.insert({ p.first, new_node });
|
|---|
| 766 | }
|
|---|
| 767 | if (mutated) {
|
|---|
| 768 | auto new_node = mutate( node );
|
|---|
| 769 | new_node->typeEnv.swap( new_map );
|
|---|
| 770 | node = new_node;
|
|---|
| 771 | }
|
|---|
| 772 | }
|
|---|
| 773 |
|
|---|
| 774 | {
|
|---|
| 775 | bool mutated = false;
|
|---|
| 776 | std::unordered_map< std::string, ast::ptr< ast::Expr > > new_map;
|
|---|
| 777 | for ( const auto & p : node->varEnv ) {
|
|---|
| 778 | guard_indexer guard { *this };
|
|---|
| 779 | auto new_node = p.second->accept( *this );
|
|---|
| 780 | if (new_node != p.second) mutated = false;
|
|---|
| 781 | new_map.insert({ p.first, new_node });
|
|---|
| 782 | }
|
|---|
| 783 | if (mutated) {
|
|---|
| 784 | auto new_node = mutate( node );
|
|---|
| 785 | new_node->varEnv.swap( new_map );
|
|---|
| 786 | node = new_node;
|
|---|
| 787 | }
|
|---|
| 788 | }
|
|---|
| 789 | )
|
|---|
| 790 |
|
|---|
| 791 | VISIT_END( TypeSubstitution, node );
|
|---|
| 792 | }
|
|---|
| 793 |
|
|---|
| 794 | #undef VISIT_START
|
|---|
| 795 | #undef VISIT
|
|---|
| 796 | #undef VISIT_END
|
|---|