Changeset 6a625de


Ignore:
Timestamp:
May 8, 2019, 4:19:13 PM (5 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
2ed1d50
Parents:
b1d3ee1
Message:

Some clean-up, some more assertions to check assumptions and changes to node.hpp

Location:
src
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Node.hpp

    rb1d3ee1 r6a625de  
    3232                }
    3333
     34                template<typename node_t>
     35                friend auto mutate(const node_t * node);
     36
    3437        private:
    3538                size_t strong_ref = 0;
     
    3740        };
    3841
    39         template< typename node_t, enum  Node::ref_type ref_t>
     42        // Mutate a node, non-member function to avoid static type
     43        // problems and be able to use auto return
     44        template<typename node_t>
     45        auto mutate(const node_t * node) {
     46                assertf(
     47                        node->strong_count >= 1,
     48                        "Error: attempting to mutate a node that appears to have been linked"
     49                );
     50                if (node->strong_count == 1) {
     51                        return const_cast<node_t *>(node);
     52                }
     53
     54                assertf(
     55                        node->weak_count == 0,
     56                        "Error: mutating node with weak references to it will invalided some references"
     57                );
     58                return node->clone();
     59        }
     60
     61        // All accept routines should look as follows :
     62        // virtual void accept( Visitor &v ) override {
     63        //      return v.visit(this);
     64        // }
     65        // Using the following wrapper to handle the node type
     66        template< typename node_t >
     67        auto visit_this( visitor & v, node_t * node ) {
     68                ptr<node_t> p;
     69                p.node = node;
     70                auto r = v.visit(p);
     71                p.node = nullptr;
     72                return r;
     73        }
     74
     75        // Base class for the smart pointer types
     76        // should never really be used.
     77        template< typename node_t, enum Node::ref_type ref_t>
    4078        class ptr_base {
    4179        public:
     
    87125
    88126        template< typename node_t >
    89         class ptr : public ptr_base< node_t, Node::ref_type::strong > {
    90         public:
    91                 typedef ptr_base< node_t, Node::ref_type::strong > base_t;
    92 
    93                 ptr() = default;
    94                 ptr( node_t node ) : base_t( node ) {}
    95                 ~ptr() = default;
    96 
    97                 template< enum  Node::ref_type ref_t >
    98                 ptr( const ptr_base<node_t, ref_t> & o ) : base_t(o) {}
    99 
    100                 template< enum  Node::ref_type ref_t >
    101                 ptr( ptr_base<node_t, ref_t> && o ) : base_t( std::move(o) ) {}
    102 
    103                 template< enum  Node::ref_type o_ref_t >
    104                 ptr & operator=( const ptr_base<node_t, o_ref_t> & o ) {
    105                         base_t::operator=(o);
    106                         return *this;
    107                 }
    108 
    109                 template< enum  Node::ref_type o_ref_t >
    110                 ptr & operator=( ptr_base<node_t, o_ref_t> && o ) {
    111                         base_t::operator=(std::move(o));
    112                         return *this;
    113                 }
    114 
    115                 node_t * mutate() {
    116                         using base_t::node;
    117                         assert(node->strong_count);
    118                         if (node->strong_count == 1) {
    119                                 return node;
    120                         }
    121 
    122                         assertf(node->weak_count == 0, "Error: mutating node with weak references to it will invalided some references");
    123                         auto n = new node_t(*node);
    124                         assign(n);
    125                         return node;
    126                 }
    127         };
     127        using ptr = ptr_base< node_t, Node::ref_type::strong >;
    128128
    129129        template< typename node_t >
    130         class readonly : public ptr_base< node_t, Node::ref_type::weak > {
    131         public:
    132                 typedef ptr_base< node_t, Node::ref_type::strong > base_t;
    133 
    134                 readonly() = default;
    135                 readonly( node_t node ) : base_t( node ) {}
    136                 ~readonly() = default;
    137 
    138                 template< enum  Node::ref_type ref_t >
    139                 readonly( const ptr_base<node_t, ref_t> & o ) : base_t(o) {}
    140 
    141                 template< enum  Node::ref_type ref_t >
    142                 readonly( ptr_base<node_t, ref_t> && o ) : base_t( std::move(o) ) {}
    143 
    144                 template< enum  Node::ref_type o_ref_t >
    145                 readonly & operator=( const ptr_base<node_t, o_ref_t> & o ) {
    146                         base_t::operator=(o);
    147                         return *this;
    148                 }
    149 
    150                 template< enum  Node::ref_type o_ref_t >
    151                 readonly & operator=( ptr_base<node_t, o_ref_t> && o ) {
    152                         base_t::operator=(std::move(o));
    153                         return *this;
    154                 }
    155         };
     130        using readonly = ptr_base< node_t, Node::ref_type::weak >;
    156131}
  • src/Common/Assert.cc

    rb1d3ee1 r6a625de  
    3939}
    4040
     41void abort(const char *fmt, ... ) noexcept __attribute__((noreturn, format(printf, 1, 2)));
     42void abort(const char *fmt, ... ) noexcept {
     43        va_list args;
     44        va_start( args, fmt );
     45        vfprintf( stderr, fmt, args );
     46        va_end( args );
     47        fprintf( stderr, "\n" );
     48        abort();
     49}
     50
    4151// Local Variables: //
    4252// tab-width: 4 //
  • src/Common/PassVisitor.impl.h

    rb1d3ee1 r6a625de  
    2020
    2121#define MUTATE_END( type, node )                \
    22         return call_postmutate< type * >( node ); \
     22        auto __return = call_postmutate< type * >( node ); \
     23        assert( __return ); \
     24        return __return;
    2325
    2426
  • src/Common/PassVisitor.proto.h

    rb1d3ee1 r6a625de  
    174174FIELD_PTR( PassVisitor<pass_type> * const, visitor )
    175175
     176#undef FIELD_PTR
     177
    176178//---------------------------------------------------------
    177179// Indexer
  • src/ControlStruct/ExceptTranslate.cc

    rb1d3ee1 r6a625de  
    617617                                return create_terminate_rethrow( throwStmt );
    618618                        } else {
    619                                 assertf(false, "Invalid throw in %s at %i\n",
     619                                abort("Invalid throw in %s at %i\n",
    620620                                        throwStmt->location.filename.c_str(),
    621621                                        throwStmt->location.first_line);
    622                                 return nullptr;
    623622                        }
    624623                } else {
     
    628627                                return create_resume_rethrow( throwStmt );
    629628                        } else {
    630                                 assertf(false, "Invalid throwResume in %s at %i\n",
     629                                abort("Invalid throwResume in %s at %i\n",
    631630                                        throwStmt->location.filename.c_str(),
    632631                                        throwStmt->location.first_line);
    633                                 return nullptr;
    634632                        }
    635633                }
  • src/include/cassert

    rb1d3ee1 r6a625de  
    4545}
    4646
     47extern void abort(const char *fmt, ...  ) noexcept __attribute__((noreturn, format(printf, 1, 2)));
    4748// Local Variables: //
    4849// tab-width: 4 //
  • src/main.cc

    rb1d3ee1 r6a625de  
    4040#include "Common/Stats.h"
    4141#include "Common/PassVisitor.h"
     42// #include "AST/Pass.hpp"
    4243#include "Common/SemanticError.h"           // for SemanticError
    4344#include "Common/UnimplementedError.h"      // for UnimplementedError
Note: See TracChangeset for help on using the changeset viewer.