Changeset 24d6572 for src/AST/Print.cpp


Ignore:
Timestamp:
Jun 12, 2023, 2:45:32 PM (2 years ago)
Author:
Fangren Yu <f37yu@…>
Branches:
ast-experimental, master
Children:
62d62db
Parents:
34b4268 (diff), 251ce80 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' into ast-experimental

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Print.cpp

    r34b4268 r24d6572  
    1616#include "Print.hpp"
    1717
     18#include "Attribute.hpp"
    1819#include "Decl.hpp"
    1920#include "Expr.hpp"
     21#include "Init.hpp"
    2022#include "Stmt.hpp"
    2123#include "Type.hpp"
    2224#include "TypeSubstitution.hpp"
    2325#include "CompilationState.h"
    24 
    25 #include "Common/utility.h" // for group_iterate
     26#include "Common/Iterate.hpp"
    2627
    2728using namespace std;
     
    2930namespace ast {
    3031
    31 template <typename C, typename... T>
    32 constexpr array<C,sizeof...(T)> make_array(T&&... values)
    33 {
    34         return array<C,sizeof...(T)>{
    35                 std::forward<T>(values)...
    36         };
     32namespace {
     33
     34template<typename C, typename... T>
     35constexpr array<C, sizeof...(T)> make_array( T&&... values ) {
     36        return array<C, sizeof...(T)>{ std::forward<T>( values )... };
     37}
     38
     39namespace Names {
     40        static constexpr auto FuncSpecifiers = make_array<const char*>(
     41                "inline", "_Noreturn", "fortran"
     42        );
     43
     44        static constexpr auto StorageClasses = make_array<const char*>(
     45                "extern", "static", "auto", "register", "__thread", "_Thread_local"
     46        );
     47
     48        static constexpr auto Qualifiers = make_array<const char*>(
     49                "const", "restrict", "volatile", "mutex", "_Atomic"
     50        );
     51}
     52
     53template<typename bits_t, size_t N>
     54void print( ostream & os, const bits_t & bits,
     55                const array<const char *, N> & names ) {
     56        if ( !bits.any() ) return;
     57        for ( size_t i = 0 ; i < N ; i += 1 ) {
     58                if ( bits[i] ) {
     59                        os << names[i] << ' ';
     60                }
     61        }
    3762}
    3863
     
    80105        static const char* Names[];
    81106
    82         struct Names {
    83                 static constexpr auto FuncSpecifiers = make_array<const char*>(
    84                         "inline", "_Noreturn", "fortran"
    85                 );
    86 
    87                 static constexpr auto StorageClasses = make_array<const char*>(
    88                         "extern", "static", "auto", "register", "__thread", "_Thread_local"
    89                 );
    90 
    91                 static constexpr auto Qualifiers = make_array<const char*>(
    92                         "const", "restrict", "volatile", "mutex", "_Atomic"
    93                 );
    94         };
    95 
    96         template<typename storage_t, size_t N>
    97         void print(const storage_t & storage, const array<const char *, N> & Names ) {
    98                 if ( storage.any() ) {
    99                         for ( size_t i = 0; i < Names.size(); i += 1 ) {
    100                                 if ( storage[i] ) {
    101                                         os << Names[i] << ' ';
    102                                 }
    103                         }
    104                 }
    105         }
    106 
    107         void print( const ast::Function::Specs & specs ) {
    108                 print(specs, Names::FuncSpecifiers);
    109         }
    110 
    111         void print( const ast::Storage::Classes & storage ) {
    112                 print(storage, Names::StorageClasses);
    113         }
    114 
    115         void print( const ast::CV::Qualifiers & qualifiers ) {
    116                 print(qualifiers, Names::Qualifiers);
    117         }
    118 
    119107        void print( const std::vector<ast::Label> & labels ) {
    120108                if ( labels.empty() ) return;
     
    221209        }
    222210
     211    void print( const ast::WaitStmt * node ) {
     212                if ( node->timeout_time ) {
     213                        os << indent-1 << "timeout of:" << endl;
     214                        node->timeout_time->accept( *this );
     215
     216                        if ( node->timeout_stmt ) {
     217                                os << indent-1 << "... with statment:" << endl;
     218                                node->timeout_stmt->accept( *this );
     219                        }
     220
     221                        if ( node->timeout_cond ) {
     222                                os << indent-1 << "... with condition:" << endl;
     223                                node->timeout_cond->accept( *this );
     224                        }
     225                }
     226
     227                if ( node->else_stmt ) {
     228                        os << indent-1 << "else:" << endl;
     229                        node->else_stmt->accept( *this );
     230
     231                        if ( node->else_cond ) {
     232                                os << indent-1 << "... with condition:" << endl;
     233                                node->else_cond->accept( *this );
     234                        }
     235                }
     236        }
     237
    223238        void preprint( const ast::NamedTypeDecl * node ) {
    224239                if ( ! node->name.empty() ) {
     
    230245                }
    231246
    232                 print( node->storage );
     247                ast::print( os, node->storage );
    233248                os << node->typeString();
    234249
     
    272287
    273288        void preprint( const ast::Type * node ) {
    274                 print( node->qualifiers );
     289                ast::print( os, node->qualifiers );
    275290        }
    276291
     
    278293                print( node->forall );
    279294                print( node->assertions );
    280                 print( node->qualifiers );
     295                ast::print( os, node->qualifiers );
    281296        }
    282297
    283298        void preprint( const ast::BaseInstType * node ) {
    284299                print( node->attributes );
    285                 print( node->qualifiers );
     300                ast::print( os, node->qualifiers );
    286301        }
    287302
     
    294309                }
    295310
    296                 print( node->storage );
     311                ast::print( os, node->storage );
    297312
    298313                if ( node->type ) {
     
    338353                if ( ! short_mode ) printAll( node->attributes );
    339354
    340                 print( node->storage );
    341                 print( node->funcSpec );
    342 
    343 
     355                ast::print( os, node->storage );
     356                ast::print( os, node->funcSpec );
    344357
    345358                if ( node->type && node->isTypeFixed ) {
     
    384397                                --indent;
    385398                        }
     399                }
     400
     401                if ( ! node->withExprs.empty() ) {
     402                        // Not with a clause, but the 'with clause'.
     403                        ++indent;
     404                        os << " with clause" << endl << indent;
     405                        printAll( node->withExprs );
     406                        --indent;
    386407                }
    387408
     
    746767        virtual const ast::Stmt * visit( const ast::SuspendStmt * node ) override final {
    747768                os << "Suspend Statement";
    748                 switch (node->type) {
    749                         case ast::SuspendStmt::None     : os << " with implicit target"; break;
    750                         case ast::SuspendStmt::Generator: os << " for generator"; break;
    751                         case ast::SuspendStmt::Coroutine: os << " for coroutine"; break;
     769                switch (node->kind) {
     770                case ast::SuspendStmt::None     : os << " with implicit target"; break;
     771                case ast::SuspendStmt::Generator: os << " for generator"; break;
     772                case ast::SuspendStmt::Coroutine: os << " for coroutine"; break;
    752773                }
    753774                os << endl;
     
    759780                }
    760781                ++indent;
     782
     783                return node;
     784        }
     785
     786        virtual const ast::WhenClause * visit( const ast::WhenClause * node ) override final {
     787                os << indent-1 << "target: ";
     788                safe_print( node->target );
     789
     790                if ( node->stmt ) {
     791                        os << indent-1 << "... with statment:" << endl;
     792                        node->stmt->accept( *this );
     793                }
     794
     795                if ( node->when_cond ) {
     796                        os << indent-1 << "... with when condition:" << endl;
     797                        node->when_cond->accept( *this );
     798                }
    761799
    762800                return node;
     
    800838        virtual const ast::WaitForClause * visit( const ast::WaitForClause * node ) override final {
    801839                os << indent-1 << "target function: ";
    802                 safe_print( node->target_func );
     840                safe_print( node->target );
    803841
    804842                if ( !node->target_args.empty() ) {
     
    814852                }
    815853
    816                 if ( node->cond ) {
     854                if ( node->when_cond ) {
    817855                        os << indent-1 << "... with condition:" << endl;
    818                         node->cond->accept( *this );
    819                 }
    820 
     856                        node->when_cond->accept( *this );
     857                }
     858
     859                return node;
     860        }
     861
     862    virtual const ast::Stmt * visit( const ast::WaitUntilStmt * node ) override final {
     863                os << "Waituntil Statement" << endl;
     864                indent += 2;
     865                for( const auto & clause : node->clauses ) {
     866                        clause->accept( *this );
     867                }
     868        print(node);    // calls print( const ast::WaitStmt * node )
    821869                return node;
    822870        }
     
    16271675};
    16281676
     1677} // namespace
     1678
    16291679void print( ostream & os, const ast::Node * node, Indenter indent ) {
    16301680        Printer printer { os, indent, false };
     
    16371687}
    16381688
    1639 // Annoyingly these needed to be defined out of line to avoid undefined references.
    1640 // The size here needs to be explicit but at least the compiler will produce an error
    1641 // if the wrong size is specified
    1642 constexpr array<const char*, 3> Printer::Names::FuncSpecifiers;
    1643 constexpr array<const char*, 6> Printer::Names::StorageClasses;
    1644 constexpr array<const char*, 5> Printer::Names::Qualifiers;
     1689void print( ostream & os, Function::Specs specs ) {
     1690        print( os, specs, Names::FuncSpecifiers );
    16451691}
     1692
     1693void print( ostream & os, Storage::Classes storage ) {
     1694        print( os, storage, Names::StorageClasses );
     1695}
     1696
     1697void print( ostream & os, CV::Qualifiers qualifiers ) {
     1698        print( os, qualifiers, Names::Qualifiers );
     1699}
     1700
     1701} // namespace ast
Note: See TracChangeset for help on using the changeset viewer.