Changeset 5bf685f


Ignore:
Timestamp:
Jan 17, 2024, 3:13:56 PM (20 months ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
master
Children:
11f65b3
Parents:
e891349
Message:

Replayed maybeClone with maybeCopy, removed unused helppers in utility.h and pushed some includes out of headers.

Location:
src
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Decl.hpp

    re891349 r5bf685f  
    2929#include "StorageClasses.hpp"
    3030#include "Visitor.hpp"
    31 #include "Common/utility.h"
    3231
    3332// Must be included in *all* AST classes; should be #undef'd at the end of the file
  • src/AST/Pass.proto.hpp

    re891349 r5bf685f  
    1919#include "Common/Iterate.hpp"
    2020#include "Common/Stats/Heap.h"
     21#include "Common/utility.h"
    2122namespace ast {
    2223        template<typename core_t> class Pass;
  • src/CodeGen/CodeGenerator.hpp

    re891349 r5bf685f  
    2121#include "AST/Pass.hpp"          // for WithGuards, WithShortCircuiting, ...
    2222#include "CodeGen/Options.h"     // for Options
     23#include "Common/Indenter.h"     // for Indenter
    2324
    2425
  • src/Common/utility.h

    re891349 r5bf685f  
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Fri Feb 17 15:25:00 2023
    13 // Update Count     : 53
     12// Last Modified On : Wed Jan 17 14:40:00 2024
     13// Update Count     : 54
    1414//
    1515
     
    1717
    1818#include <cassert>
    19 #include <cctype>
    2019#include <algorithm>
    21 #include <iostream>
    2220#include <list>
    23 #include <memory>
    2421#include <string>
    2522#include <type_traits>
    2623#include <vector>
    27 #include <cstring>                                                                              // memcmp
    28 
    29 #include "Common/Indenter.h"
    30 
    31 class Expression;
    32 
    33 /// bring std::move into global scope
    34 using std::move;
    3524
    3625/// partner to move that copies any copyable type
    3726template<typename T>
    3827T copy( const T & x ) { return x; }
    39 
    40 template< typename T >
    41 static inline T * maybeClone( const T *orig ) {
    42         if ( orig ) {
    43                 return orig->clone();
    44         } else {
    45                 return 0;
    46         } // if
    47 }
    48 
    49 template< typename Input_iterator >
    50 void printEnums( Input_iterator begin, Input_iterator end, const char * const *name_array, std::ostream &os ) {
    51         for ( Input_iterator i = begin; i != end; ++i ) {
    52                 os << name_array[ *i ] << ' ';
    53         } // for
    54 }
    55 
    56 template< typename Container >
    57 void deleteAll( const Container &container ) {
    58         for ( const auto &i : container ) {
    59                 delete i;
    60         } // for
    61 }
    62 
    63 template< typename Container >
    64 void printAll( const Container &container, std::ostream &os, Indenter indent = {} ) {
    65         for ( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) {
    66                 if ( *i ) {
    67                         os << indent;
    68                         (*i)->print( os, indent );
    69                         // need an endl after each element because it's not easy to know when each individual item should end
    70                         os << std::endl;
    71                 } // if
    72         } // for
    73 }
    74 
    75 template< typename SrcContainer, typename DestContainer >
    76 void cloneAll( const SrcContainer &src, DestContainer &dest ) {
    77         typename SrcContainer::const_iterator in = src.begin();
    78         std::back_insert_iterator< DestContainer > out( dest );
    79         while ( in != src.end() ) {
    80                 *out++ = (*in++)->clone();
    81         } // while
    82 }
    83 
    84 template< typename SrcContainer, typename DestContainer, typename Predicate >
    85 void cloneAll_if( const SrcContainer &src, DestContainer &dest, Predicate pred ) {
    86         std::back_insert_iterator< DestContainer > out( dest );
    87         for ( auto x : src ) {
    88                 if ( pred(x) ) {
    89                         *out++ = x->clone();
    90                 }
    91         } // while
    92 }
    93 
    94 template< typename Container >
    95 void assertAll( const Container &container ) {
    96         int count = 0;
    97         for ( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) {
    98                 if ( !(*i) ) {
    99                         std::cerr << count << " is null" << std::endl;
    100                 } // if
    101         } // for
    102 }
    103 
    104 template < typename T >
    105 std::list<T> tail( std::list<T> l ) {
    106         if ( ! l.empty() ) {
    107                 std::list<T> ret(++(l.begin()), l.end());
    108                 return ret;
    109         } // if
    110 }
    111 
    112 template < typename T >
    113 std::list<T> flatten( std::list < std::list<T> > l) {
    114         typedef std::list <T> Ts;
    115 
    116         Ts ret;
    117 
    118         switch ( l.size() ) {
    119           case 0:
    120                 return ret;
    121           case 1:
    122                 return l.front();
    123           default:
    124                 ret = flatten(tail(l));
    125                 ret.insert(ret.begin(), l.front().begin(), l.front().end());
    126                 return ret;
    127         } // switch
    128 }
    12928
    13029/// Splice src onto the end of dst, clearing src
     
    14342}
    14443
    145 template< typename... Args >
    146 auto filter(Args&&... args) -> decltype(std::copy_if(std::forward<Args>(args)...)) {
    147   return std::copy_if(std::forward<Args>(args)...);
    148 }
    149 
    150 template <typename E, typename UnaryPredicate, template< typename, typename...> class Container, typename... Args >
    151 void filter( Container< E *, Args... > & container, UnaryPredicate pred, bool doDelete ) {
    152         auto i = begin( container );
    153         while ( i != end( container ) ) {
    154                 auto it = next( i );
    155                 if ( pred( *i ) ) {
    156                         if ( doDelete ) {
    157                                 delete *i;
    158                         } // if
    159                         container.erase( i );
    160                 } // if
    161                 i = it;
    162         } // while
    163 }
    164 
     44/// Remove elements that match pred from the container.
    16545template<typename Container, typename Pred>
    16646void erase_if( Container & cont, Pred && pred ) {
  • src/InitTweak/FixInit.cpp

    re891349 r5bf685f  
    581581        }
    582582
    583         if ( ! dtor->env ) dtor->env = maybeClone( env );
     583        if ( nullptr == dtor->env && nullptr != env ) {
     584                dtor->env = ast::shallowCopy( env );
     585        }
    584586        auto dtorFunc = getDtorFunc( ret, new ast::ExprStmt(loc, dtor ), stmtsToAddBefore );
    585587
  • src/Parser/DeclarationNode.cc

    re891349 r5bf685f  
    3535#include "Common/SemanticError.h"  // for SemanticError
    3636#include "Common/UniqueName.h"     // for UniqueName
    37 #include "Common/utility.h"        // for maybeClone
     37#include "Common/utility.h"        // for copy, spliceBegin
    3838#include "Parser/ExpressionNode.h" // for ExpressionNode
    3939#include "Parser/InitializerNode.h"// for InitializerNode
     
    4141#include "TypeData.h"              // for TypeData, TypeData::Aggregate_t
    4242#include "TypedefTable.h"          // for TypedefTable
    43 
    44 class Initializer;
    4543
    4644extern TypedefTable typedefTable;
     
    10199DeclarationNode * DeclarationNode::clone() const {
    102100        DeclarationNode * newnode = new DeclarationNode;
    103         newnode->set_next( maybeClone( get_next() ) );
     101        newnode->set_next( maybeCopy( get_next() ) );
    104102        newnode->name = name ? new string( *name ) : nullptr;
    105103
    106104        newnode->builtin = NoBuiltinType;
    107         newnode->type = maybeClone( type );
     105        newnode->type = maybeCopy( type );
    108106        newnode->inLine = inLine;
    109107        newnode->storageClasses = storageClasses;
    110108        newnode->funcSpecs = funcSpecs;
    111         newnode->bitfieldWidth = maybeClone( bitfieldWidth );
    112         newnode->enumeratorValue.reset( maybeClone( enumeratorValue.get() ) );
     109        newnode->bitfieldWidth = maybeCopy( bitfieldWidth );
     110        newnode->enumeratorValue.reset( maybeCopy( enumeratorValue.get() ) );
    113111        newnode->hasEllipsis = hasEllipsis;
    114112        newnode->linkage = linkage;
    115113        newnode->asmName = maybeCopy( asmName );
    116114        newnode->attributes = attributes;
    117         newnode->initializer = maybeClone( initializer );
     115        newnode->initializer = maybeCopy( initializer );
    118116        newnode->extension = extension;
    119         newnode->asmStmt = maybeClone( asmStmt );
     117        newnode->asmStmt = maybeCopy( asmStmt );
    120118        newnode->error = error;
    121119
    122120//      newnode->variable.name = variable.name ? new string( *variable.name ) : nullptr;
    123121        newnode->variable.tyClass = variable.tyClass;
    124         newnode->variable.assertions = maybeClone( variable.assertions );
    125         newnode->variable.initializer = maybeClone( variable.initializer );
    126 
    127         newnode->assert.condition = maybeClone( assert.condition );
     122        newnode->variable.assertions = maybeCopy( variable.assertions );
     123        newnode->variable.initializer = maybeCopy( variable.initializer );
     124
     125        newnode->assert.condition = maybeCopy( assert.condition );
    128126        newnode->assert.message = maybeCopy( assert.message );
    129127        return newnode;
     
    664662                                dst->base->aggInst.aggregate = src;
    665663                                if ( src->kind == TypeData::Aggregate ) {
    666                                         dst->base->aggInst.params = maybeClone( src->aggregate.actuals );
     664                                        dst->base->aggInst.params = maybeCopy( src->aggregate.actuals );
    667665                                } // if
    668666                                dst->base->qualifiers |= src->qualifiers;
     
    694692                                        if ( o->type->kind == TypeData::Aggregate ) {
    695693                                                type->aggInst.hoistType = o->type->aggregate.body;
    696                                                 type->aggInst.params = maybeClone( o->type->aggregate.actuals );
     694                                                type->aggInst.params = maybeCopy( o->type->aggregate.actuals );
    697695                                        } else {
    698696                                                type->aggInst.hoistType = o->type->enumeration.body;
     
    860858                                p->type->base->aggInst.aggregate = type;
    861859                                if ( type->kind == TypeData::Aggregate ) {
    862                                         p->type->base->aggInst.params = maybeClone( type->aggregate.actuals );
     860                                        p->type->base->aggInst.params = maybeCopy( type->aggregate.actuals );
    863861                                } // if
    864862                                p->type->base->qualifiers |= type->qualifiers;
     
    897895                        lastArray->base->aggInst.aggregate = type;
    898896                        if ( type->kind == TypeData::Aggregate ) {
    899                                 lastArray->base->aggInst.params = maybeClone( type->aggregate.actuals );
     897                                lastArray->base->aggInst.params = maybeCopy( type->aggregate.actuals );
    900898                        } // if
    901899                        lastArray->base->qualifiers |= type->qualifiers;
     
    950948DeclarationNode * DeclarationNode::cloneType( string * name ) {
    951949        DeclarationNode * newnode = newName( name );
    952         newnode->type = maybeClone( type );
     950        newnode->type = maybeCopy( type );
    953951        newnode->copySpecifiers( this );
    954952        return newnode;
     
    984982                } // if
    985983
    986                 newType->forall = maybeClone( type->forall );
     984                newType->forall = maybeCopy( type->forall );
    987985                if ( ! o->type ) {
    988986                        o->type = newType;
  • src/Parser/ParseNode.h

    re891349 r5bf685f  
    3030#include "Common/SemanticError.h"  // for SemanticError
    3131#include "Common/UniqueName.h"     // for UniqueName
    32 #include "Common/utility.h"        // for maybeClone
    3332#include "Parser/parserutility.h"  // for maybeBuild, maybeCopy
    3433
  • src/Parser/TypeData.cc

    re891349 r5bf685f  
    167167        TypeData * newtype = new TypeData( kind );
    168168        newtype->qualifiers = qualifiers;
    169         newtype->base = maybeClone( base );
    170         newtype->forall = maybeClone( forall );
     169        newtype->base = maybeCopy( base );
     170        newtype->forall = maybeCopy( forall );
    171171
    172172        switch ( kind ) {
     
    185185                break;
    186186        case Array:
    187                 newtype->array.dimension = maybeClone( array.dimension );
     187                newtype->array.dimension = maybeCopy( array.dimension );
    188188                newtype->array.isVarLen = array.isVarLen;
    189189                newtype->array.isStatic = array.isStatic;
    190190                break;
    191191        case Function:
    192                 newtype->function.params = maybeClone( function.params );
    193                 newtype->function.idList = maybeClone( function.idList );
    194                 newtype->function.oldDeclList = maybeClone( function.oldDeclList );
    195                 newtype->function.body = maybeClone( function.body );
    196                 newtype->function.withExprs = maybeClone( function.withExprs );
     192                newtype->function.params = maybeCopy( function.params );
     193                newtype->function.idList = maybeCopy( function.idList );
     194                newtype->function.oldDeclList = maybeCopy( function.oldDeclList );
     195                newtype->function.body = maybeCopy( function.body );
     196                newtype->function.withExprs = maybeCopy( function.withExprs );
    197197                break;
    198198        case Aggregate:
    199199                newtype->aggregate.kind = aggregate.kind;
    200200                newtype->aggregate.name = aggregate.name ? new string( *aggregate.name ) : nullptr;
    201                 newtype->aggregate.params = maybeClone( aggregate.params );
    202                 newtype->aggregate.actuals = maybeClone( aggregate.actuals );
    203                 newtype->aggregate.fields = maybeClone( aggregate.fields );
     201                newtype->aggregate.params = maybeCopy( aggregate.params );
     202                newtype->aggregate.actuals = maybeCopy( aggregate.actuals );
     203                newtype->aggregate.fields = maybeCopy( aggregate.fields );
    204204                newtype->aggregate.body = aggregate.body;
    205205                newtype->aggregate.anon = aggregate.anon;
     
    208208                break;
    209209        case AggregateInst:
    210                 newtype->aggInst.aggregate = maybeClone( aggInst.aggregate );
    211                 newtype->aggInst.params = maybeClone( aggInst.params );
     210                newtype->aggInst.aggregate = maybeCopy( aggInst.aggregate );
     211                newtype->aggInst.params = maybeCopy( aggInst.params );
    212212                newtype->aggInst.hoistType = aggInst.hoistType;
    213213                break;
    214214        case Enum:
    215215                newtype->enumeration.name = enumeration.name ? new string( *enumeration.name ) : nullptr;
    216                 newtype->enumeration.constants = maybeClone( enumeration.constants );
     216                newtype->enumeration.constants = maybeCopy( enumeration.constants );
    217217                newtype->enumeration.body = enumeration.body;
    218218                newtype->enumeration.anon = enumeration.anon;
     
    221221        case SymbolicInst:
    222222                newtype->symbolic.name = symbolic.name ? new string( *symbolic.name ) : nullptr;
    223                 newtype->symbolic.params = maybeClone( symbolic.params );
    224                 newtype->symbolic.actuals = maybeClone( symbolic.actuals );
    225                 newtype->symbolic.assertions = maybeClone( symbolic.assertions );
     223                newtype->symbolic.params = maybeCopy( symbolic.params );
     224                newtype->symbolic.actuals = maybeCopy( symbolic.actuals );
     225                newtype->symbolic.assertions = maybeCopy( symbolic.assertions );
    226226                newtype->symbolic.isTypedef = symbolic.isTypedef;
    227227                break;
    228228        case Tuple:
    229                 newtype->tuple = maybeClone( tuple );
     229                newtype->tuple = maybeCopy( tuple );
    230230                break;
    231231        case Typeof:
    232232        case Basetypeof:
    233                 newtype->typeexpr = maybeClone( typeexpr );
     233                newtype->typeexpr = maybeCopy( typeexpr );
    234234                break;
    235235        case Vtable:
     
    240240                break;
    241241        case Qualified:
    242                 newtype->qualified.parent = maybeClone( qualified.parent );
    243                 newtype->qualified.child = maybeClone( qualified.child );
     242                newtype->qualified.parent = maybeCopy( qualified.parent );
     243                newtype->qualified.child = maybeCopy( qualified.child );
    244244                break;
    245245        } // switch
  • src/Parser/parser.yy

    re891349 r5bf685f  
    19601960                        // Append the return type at the start (left-hand-side) to each identifier in the list.
    19611961                        DeclarationNode * ret = new DeclarationNode;
    1962                         ret->type = maybeClone( $1->type->base );
     1962                        ret->type = maybeCopy( $1->type->base );
    19631963                        $$ = $1->appendList( DeclarationNode::newFunction( $3, ret, $6, nullptr ) );
    19641964                }
  • src/Parser/parserutility.h

    re891349 r5bf685f  
    3636
    3737template<typename node_t>
    38 node_t * maybeCopy( node_t const * node ) {
     38static inline node_t * maybeCopy( node_t const * node ) {
    3939        return node ? ast::shallowCopy( node ) : nullptr;
    4040}
  • src/SymTab/GenImplicitCall.cpp

    re891349 r5bf685f  
    2525#include "CodeGen/OperatorTable.h"       // for isCtorDtor
    2626#include "Common/UniqueName.h"           // for UniqueName
     27#include "Common/utility.h"              // for splice
    2728
    2829namespace SymTab {
Note: See TracChangeset for help on using the changeset viewer.