Changeset 8631c84 for src


Ignore:
Timestamp:
Mar 25, 2022, 11:47:51 AM (2 years ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
ADT, ast-experimental, enum, master, pthread-emulation, qualifiedEnum
Children:
f5bace8
Parents:
94fa946
Message:

Made ast::ptr_base swappable.

Location:
src/AST
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Node.cpp

    r94fa946 r8631c84  
    99// Author           : Thierry Delisle
    1010// Created On       : Thu May 16 14:16:00 2019
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Feb  1 09:09:39 2022
    13 // Update Count     : 3
     11// Last Modified By : Andrew Beach
     12// Last Modified On : Fri Mar 25 10:30:00 2022
     13// Update Count     : 4
    1414//
    1515
     
    1919#include <csignal>  // MEMORY DEBUG -- for raise
    2020#include <iostream>
     21#include <utility>
    2122
    2223#include "Attribute.hpp"
     
    7677void ast::ptr_base<node_t, ref_t>::_check() const {
    7778        // if(node) assert(node->was_ever_strong == false || node->strong_count > 0);
     79}
     80
     81template< typename node_t, enum ast::Node::ref_type ref_t >
     82void ast::ptr_base<node_t, ref_t>::swap( ptr_base & other ) noexcept {
     83        std::swap( this->node, other.node );
     84        _trap( this->node );
     85        _trap( other.node );
    7886}
    7987
  • src/AST/Node.hpp

    r94fa946 r8631c84  
    1010// Created On       : Wed May 8 10:27:04 2019
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Fri Jun 5 9:47:00 2020
    13 // Update Count     : 6
     12// Last Modified On : Fri Mar 25 10:33:00 2022
     13// Update Count     : 7
    1414//
    1515
     
    230230        }
    231231
     232        /// Swaps the nodes contained within two pointers.
     233        void swap( ptr_base & other ) noexcept;
     234
    232235        const node_t * get() const { _check(); return  node; }
    233236        const node_t * operator->() const { _check(); return  node; }
     
    292295template< typename node_t >
    293296using readonly = ptr_base< node_t, Node::ref_type::weak >;
     297
     298/// Non-member swap that an participate in overload resolution.
     299template< typename node_t, enum Node::ref_type ref_t >
     300void swap( ptr_base< node_t, ref_t > & l, ptr_base< node_t, ref_t > & r ) {
     301        l.swap( r );
     302}
     303
    294304}
    295305
  • src/AST/Pass.impl.hpp

    r94fa946 r8631c84  
    354354                        // Take all the elements that are different in 'values'
    355355                        // and swap them into 'container'
    356                         if( values[i] != nullptr ) std::swap(container[i], values[i]);
     356                        if( values[i] != nullptr ) swap(container[i], values[i]);
    357357                }
    358358
     
    10541054                        auto n = __pass::mutate<core_t>(node);
    10551055                        for(size_t i = 0; i < new_clauses.size(); i++) {
    1056                                 if(new_clauses.at(i).target.func != nullptr) std::swap(n->clauses.at(i).target.func, new_clauses.at(i).target.func);
     1056                                if(new_clauses.at(i).target.func != nullptr) swap(n->clauses.at(i).target.func, new_clauses.at(i).target.func);
    10571057
    10581058                                for(size_t j = 0; j < new_clauses.at(i).target.args.size(); j++) {
    1059                                         if(new_clauses.at(i).target.args.at(j) != nullptr) std::swap(n->clauses.at(i).target.args.at(j), new_clauses.at(i).target.args.at(j));
     1059                                        if(new_clauses.at(i).target.args.at(j) != nullptr) swap(n->clauses.at(i).target.args.at(j), new_clauses.at(i).target.args.at(j));
    10601060                                }
    10611061
    1062                                 if(new_clauses.at(i).stmt != nullptr) std::swap(n->clauses.at(i).stmt, new_clauses.at(i).stmt);
    1063                                 if(new_clauses.at(i).cond != nullptr) std::swap(n->clauses.at(i).cond, new_clauses.at(i).cond);
     1062                                if(new_clauses.at(i).stmt != nullptr) swap(n->clauses.at(i).stmt, new_clauses.at(i).stmt);
     1063                                if(new_clauses.at(i).cond != nullptr) swap(n->clauses.at(i).cond, new_clauses.at(i).cond);
    10641064                        }
    10651065                        node = n;
     
    21512151
    21522152        if ( __visit_children() ) {
    2153                 {
    2154                         bool mutated = false;
    2155                         std::unordered_map< ast::TypeInstType::TypeEnvKey, ast::ptr< ast::Type > > new_map;
    2156                         for ( const auto & p : node->typeEnv ) {
    2157                                 guard_symtab guard { *this };
    2158                                 auto new_node = p.second->accept( *this );
    2159                                 if (new_node != p.second) mutated = true;
    2160                                 new_map.insert({ p.first, new_node });
    2161                         }
    2162                         if (mutated) {
    2163                                 auto new_node = __pass::mutate<core_t>( node );
    2164                                 new_node->typeEnv.swap( new_map );
    2165                                 node = new_node;
    2166                         }
     2153                bool mutated = false;
     2154                std::unordered_map< ast::TypeInstType::TypeEnvKey, ast::ptr< ast::Type > > new_map;
     2155                for ( const auto & p : node->typeEnv ) {
     2156                        guard_symtab guard { *this };
     2157                        auto new_node = p.second->accept( *this );
     2158                        if (new_node != p.second) mutated = true;
     2159                        new_map.insert({ p.first, new_node });
     2160                }
     2161                if (mutated) {
     2162                        auto new_node = __pass::mutate<core_t>( node );
     2163                        new_node->typeEnv.swap( new_map );
     2164                        node = new_node;
    21672165                }
    21682166        }
Note: See TracChangeset for help on using the changeset viewer.