Changeset 257a8f5 for src


Ignore:
Timestamp:
Feb 22, 2023, 5:47:11 PM (14 months ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
ADT, ast-experimental, master
Children:
aca0d2f
Parents:
640b3df
Message:

Made some of the AST/Print helpers public.

Location:
src/AST
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Fwd.hpp

    r640b3df r257a8f5  
    1515
    1616#pragma once
     17
     18template<typename> struct bitfield;
    1719
    1820#include "AST/Node.hpp"
     
    147149class TranslationGlobal;
    148150
     151// For the following types, only use the using type.
     152namespace CV {
     153        struct qualifier_flags;
     154        using Qualifiers = bitfield<qualifier_flags>;
    149155}
     156namespace Function {
     157        struct spec_flags;
     158        using Specs = bitfield<spec_flags>;
     159}
     160namespace Storage {
     161        struct class_flags;
     162        using Classes = bitfield<class_flags>;
     163}
     164namespace Linkage {
     165        struct spec_flags;
     166        using Spec = bitfield<spec_flags>;
     167}
     168
     169}
  • src/AST/Print.cpp

    r640b3df r257a8f5  
    2929namespace ast {
    3030
    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         };
     31namespace {
     32
     33template<typename C, typename... T>
     34constexpr array<C, sizeof...(T)> make_array( T&&... values ) {
     35        return array<C, sizeof...(T)>{ std::forward<T>( values )... };
     36}
     37
     38namespace Names {
     39        static constexpr auto FuncSpecifiers = make_array<const char*>(
     40                "inline", "_Noreturn", "fortran"
     41        );
     42
     43        static constexpr auto StorageClasses = make_array<const char*>(
     44                "extern", "static", "auto", "register", "__thread", "_Thread_local"
     45        );
     46
     47        static constexpr auto Qualifiers = make_array<const char*>(
     48                "const", "restrict", "volatile", "mutex", "_Atomic"
     49        );
     50}
     51
     52template<typename bits_t, size_t N>
     53void print( ostream & os, const bits_t & bits,
     54                const array<const char *, N> & names ) {
     55        if ( !bits.any() ) return;
     56        for ( size_t i = 0 ; i < N ; i += 1 ) {
     57                if ( bits[i] ) {
     58                        os << names[i] << ' ';
     59                }
     60        }
    3761}
    3862
     
    80104        static const char* Names[];
    81105
    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 
    119106        void print( const std::vector<ast::Label> & labels ) {
    120107                if ( labels.empty() ) return;
     
    230217                }
    231218
    232                 print( node->storage );
     219                ast::print( os, node->storage );
    233220                os << node->typeString();
    234221
     
    272259
    273260        void preprint( const ast::Type * node ) {
    274                 print( node->qualifiers );
     261                ast::print( os, node->qualifiers );
    275262        }
    276263
     
    278265                print( node->forall );
    279266                print( node->assertions );
    280                 print( node->qualifiers );
     267                ast::print( os, node->qualifiers );
    281268        }
    282269
    283270        void preprint( const ast::BaseInstType * node ) {
    284271                print( node->attributes );
    285                 print( node->qualifiers );
     272                ast::print( os, node->qualifiers );
    286273        }
    287274
     
    294281                }
    295282
    296                 print( node->storage );
     283                ast::print( os, node->storage );
    297284
    298285                if ( node->type ) {
     
    338325                if ( ! short_mode ) printAll( node->attributes );
    339326
    340                 print( node->storage );
    341                 print( node->funcSpec );
    342 
    343 
     327                ast::print( os, node->storage );
     328                ast::print( os, node->funcSpec );
    344329
    345330                if ( node->type && node->isTypeFixed ) {
     
    16271612};
    16281613
     1614} // namespace
     1615
    16291616void print( ostream & os, const ast::Node * node, Indenter indent ) {
    16301617        Printer printer { os, indent, false };
     
    16371624}
    16381625
    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;
     1626void print( ostream & os, Function::Specs specs ) {
     1627        print( os, specs, Names::FuncSpecifiers );
    16451628}
     1629
     1630void print( ostream & os, Storage::Classes storage ) {
     1631        print( os, storage, Names::StorageClasses );
     1632}
     1633
     1634void print( ostream & os, CV::Qualifiers qualifiers ) {
     1635        print( os, qualifiers, Names::Qualifiers );
     1636}
     1637
     1638} // namespace ast
  • src/AST/Print.hpp

    r640b3df r257a8f5  
    1616#pragma once
    1717
    18 #include <iostream>
    19 #include <utility>   // for forward
     18#include <iosfwd>
    2019
    21 #include "AST/Node.hpp"
     20#include "AST/Fwd.hpp"
    2221#include "Common/Indenter.h"
    2322
    2423namespace ast {
    25 
    26 class Decl;
    2724
    2825/// Print a node with the given indenter
     
    4441}
    4542
     43/// Print each cv-qualifier used in the set, followed by a space.
     44void print( std::ostream & os, CV::Qualifiers );
     45/// Print each function specifier used in the set, followed by a space.
     46void print( std::ostream & os, Function::Specs );
     47/// Print each storage class used in the set, followed by a space.
     48void print( std::ostream & os, Storage::Classes );
     49
    4650}
Note: See TracChangeset for help on using the changeset viewer.