Changeset 8f06277 for src


Ignore:
Timestamp:
Feb 17, 2023, 3:40:15 PM (15 months ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
ADT, ast-experimental, master
Children:
56bb2e1
Parents:
14f6a3cb
Message:

Some clean-up in Common/utility.h. Deleted some unused declarations and moved others to one of two new headers.

Location:
src
Files:
2 added
13 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Create.cpp

    r14f6a3cb r8f06277  
    2020#include "AST/Decl.hpp"
    2121#include "AST/Type.hpp"
     22#include "Common/Iterate.hpp"
    2223
    2324namespace ast {
  • src/AST/Decl.cpp

    r14f6a3cb r8f06277  
    2020#include <unordered_map>
    2121
    22 #include "Common/utility.h"
     22#include "Common/Eval.h"       // for eval
    2323
    2424#include "Fwd.hpp"             // for UniqueId
  • src/AST/Pass.impl.hpp

    r14f6a3cb r8f06277  
    2222#include "AST/TranslationUnit.hpp"
    2323#include "AST/TypeSubstitution.hpp"
     24#include "Common/Iterate.hpp"
    2425
    2526#define VISIT_START( node ) \
  • src/Common/Eval.cc

    r14f6a3cb r8f06277  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // utility.h --
     7// Eval.cc -- Evaluate parts of the ast at compile time.
    88//
    99// Author           : Richard C. Bilson
     
    1313// Update Count     : 119
    1414//
     15
     16#include "Eval.h"
    1517
    1618#include <utility> // for pair
  • src/Common/module.mk

    r14f6a3cb r8f06277  
    2525        Common/ErrorObjects.h \
    2626        Common/Eval.cc \
     27        Common/Eval.h \
    2728        Common/Examine.cc \
    2829        Common/Examine.h \
     
    3031        Common/Indenter.h \
    3132        Common/Indenter.cc \
     33        Common/Iterate.hpp \
    3234        Common/PassVisitor.cc \
    3335        Common/PassVisitor.h \
  • src/Common/utility.h

    r14f6a3cb r8f06277  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // utility.h --
     7// utility.h -- General utilities used across the compiler.
    88//
    99// Author           : Richard C. Bilson
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Thr Feb 16 12:35:00 2023
    13 // Update Count     : 52
     12// Last Modified On : Fri Feb 17 15:25:00 2023
     13// Update Count     : 53
    1414//
    1515
     
    1919#include <cctype>
    2020#include <algorithm>
    21 #include <functional>
    2221#include <iostream>
    23 #include <iterator>
    2422#include <list>
    2523#include <memory>
     
    2725#include <string>
    2826#include <type_traits>
    29 #include <utility>
    3027#include <vector>
    3128#include <cstring>                                                                              // memcmp
     
    166163
    167164#define toCString( ... ) toString( __VA_ARGS__ ).c_str()
    168 
    169 // replace element of list with all elements of another list
    170 template< typename T >
    171 void replace( std::list< T > &org, typename std::list< T >::iterator pos, std::list< T > &with ) {
    172         typename std::list< T >::iterator next = pos; advance( next, 1 );
    173 
    174         //if ( next != org.end() ) {
    175         org.erase( pos );
    176         org.splice( next, with );
    177         //}
    178 
    179         return;
    180 }
    181 
    182 // replace range of a list with a single element
    183 template< typename T >
    184 void replace( std::list< T > &org, typename std::list< T >::iterator begin, typename std::list< T >::iterator end, const T & with ) {
    185         org.insert( begin, with );
    186         org.erase( begin, end );
    187 }
    188165
    189166template< typename... Args >
     
    213190}
    214191
    215 template< typename... Args >
    216 auto zip(Args&&... args) -> decltype(zipWith(std::forward<Args>(args)..., std::make_pair)) {
    217   return zipWith(std::forward<Args>(args)..., std::make_pair);
    218 }
    219 
    220 template< class InputIterator1, class InputIterator2, class OutputIterator, class BinFunction >
    221 void zipWith( InputIterator1 b1, InputIterator1 e1, InputIterator2 b2, InputIterator2 e2, OutputIterator out, BinFunction func ) {
    222         while ( b1 != e1 && b2 != e2 )
    223                 *out++ = func(*b1++, *b2++);
    224 }
    225 
    226 // it's nice to actually be able to increment iterators by an arbitrary amount
    227 template< class InputIt, class Distance >
    228 InputIt operator+( InputIt it, Distance n ) {
    229         advance(it, n);
    230         return it;
    231 }
    232 
    233 template< typename T >
    234 void warn_single( const T & arg ) {
    235         std::cerr << arg << std::endl;
    236 }
    237 
    238 template< typename T, typename... Params >
    239 void warn_single(const T & arg, const Params & ... params ) {
    240         std::cerr << arg;
    241         warn_single( params... );
    242 }
    243 
    244192template< typename... Params >
    245193void warn( const Params & ... params ) {
    246194        std::cerr << "Warning: ";
    247         warn_single( params... );
     195        toString_single( std::cerr, params... );
     196        std::cerr << std::endl;
    248197}
    249198
     
    251200static inline bool isPrefix( const std::string & str, const std::string & pref, unsigned int start = 0 ) {
    252201        if ( pref.size() > str.size() ) return false;
    253     return 0 == memcmp( str.c_str() + start, pref.c_str(), pref.size() );
    254         // return prefix == full.substr(0, prefix.size()); // for future, requires c++17
    255 }
    256 
    257 // -----------------------------------------------------------------------------
    258 // Ref Counted Singleton class
    259 // Objects that inherit from this class will have at most one reference to it
    260 // but if all references die, the object will be deleted.
    261 
    262 template< typename ThisType >
    263 class RefCountSingleton {
    264   public:
    265         static std::shared_ptr<ThisType> get() {
    266                 if( global_instance.expired() ) {
    267                         std::shared_ptr<ThisType> new_instance = std::make_shared<ThisType>();
    268                         global_instance = new_instance;
    269                         return std::move(new_instance);
    270                 }
    271                 return global_instance.lock();
    272         }
    273   private:
    274         static std::weak_ptr<ThisType> global_instance;
    275 };
    276 
    277 template< typename ThisType >
    278 std::weak_ptr<ThisType> RefCountSingleton<ThisType>::global_instance;
     202        return pref == str.substr(start, pref.size());
     203}
    279204
    280205// -----------------------------------------------------------------------------
     
    333258        ~ValueGuardPtr() { if( ref ) { swap( *ref, old ); } }
    334259};
    335 
    336 // -----------------------------------------------------------------------------
    337 // Helper struct and function to support
    338 // for ( val : reverseIterate( container ) ) {}
    339 // syntax to have a for each that iterates backwards
    340 
    341 template< typename T >
    342 struct reverse_iterate_t {
    343         T& ref;
    344 
    345         reverse_iterate_t( T & ref ) : ref(ref) {}
    346 
    347         // this does NOT work on const T!!!
    348         // typedef typename T::reverse_iterator iterator;
    349         auto begin() { return ref.rbegin(); }
    350         auto end() { return ref.rend(); }
    351 };
    352 
    353 template< typename T >
    354 reverse_iterate_t< T > reverseIterate( T & ref ) {
    355         return reverse_iterate_t< T >( ref );
    356 }
    357 
    358 template< typename T >
    359 struct enumerate_t {
    360         template<typename val_t>
    361         struct value_t {
    362                 val_t & val;
    363                 size_t idx;
    364         };
    365 
    366         template< typename iter_t, typename val_t >
    367         struct iterator_t {
    368                 iter_t it;
    369                 size_t idx;
    370 
    371                 iterator_t( iter_t _it, size_t _idx ) : it(_it), idx(_idx) {}
    372 
    373                 value_t<val_t> operator*() const { return value_t<val_t>{ *it, idx }; }
    374 
    375                 bool operator==(const iterator_t & o) const { return o.it == it; }
    376                 bool operator!=(const iterator_t & o) const { return o.it != it; }
    377 
    378                 iterator_t & operator++() {
    379                         it++;
    380                         idx++;
    381                         return *this;
    382                 }
    383 
    384                 using difference_type   = typename std::iterator_traits< iter_t >::difference_type;
    385                 using value_type        = value_t<val_t>;
    386                 using pointer           = value_t<val_t> *;
    387                 using reference         = value_t<val_t> &;
    388                 using iterator_category = std::forward_iterator_tag;
    389         };
    390 
    391         T & ref;
    392 
    393         using iterator = iterator_t< typename T::iterator, typename T::value_type >;
    394         using const_iterator = iterator_t< typename T::const_iterator, const typename T::value_type >;
    395 
    396         iterator begin() { return iterator( ref.begin(), 0 ); }
    397         iterator end()   { return iterator( ref.end(), ref.size() ); }
    398 
    399         const_iterator begin() const { return const_iterator( ref.cbegin(), 0 ); }
    400         const_iterator end()   const { return const_iterator( ref.cend(), ref.size() ); }
    401 
    402         const_iterator cbegin() const { return const_iterator( ref.cbegin(), 0 ); }
    403         const_iterator cend()   const { return const_iterator( ref.cend(), ref.size() ); }
    404 };
    405 
    406 template< typename T >
    407 enumerate_t<T> enumerate( T & ref ) {
    408         return enumerate_t< T >{ ref };
    409 }
    410 
    411 template< typename T >
    412 const enumerate_t< const T > enumerate( const T & ref ) {
    413         return enumerate_t< const T >{ ref };
    414 }
    415 
    416 template< typename OutType, typename Range, typename Functor >
    417 OutType map_range( const Range& range, Functor&& functor ) {
    418         OutType out;
    419 
    420         std::transform(
    421                 begin( range ),
    422                 end( range ),
    423                 std::back_inserter( out ),
    424                 std::forward< Functor >( functor )
    425         );
    426 
    427         return out;
    428 }
    429 
    430 // -----------------------------------------------------------------------------
    431 // Helper struct and function to support:
    432 // for ( auto val : group_iterate( container1, container2, ... ) ) { ... }
    433 // This iteraters through multiple containers of the same size.
    434 
    435 template<typename... Args>
    436 class group_iterate_t {
    437         using Iterables = std::tuple<Args...>;
    438         Iterables iterables;
    439 
    440         // Getting the iterator and value types this way preserves const.
    441         template<size_t I> using Iter = decltype(std::get<I>(iterables).begin());
    442         template<size_t I> using Data = decltype(*std::get<I>(iterables).begin());
    443         template<typename> struct base_iterator;
    444 
    445         // This inner template puts the sequence of `0, 1, ... sizeof...(Args)-1`
    446         // into a pack. These are the indexes into the tuples, so unpacking can
    447         // go over each element of the tuple.
    448         // The std::integer_sequence is just used to build that sequence.
    449         // A library reference will probably explain it better than I can.
    450         template<std::size_t... Indices>
    451         struct base_iterator<std::integer_sequence<std::size_t, Indices...>> {
    452                 using value_type = std::tuple< Data<Indices>... >;
    453                 std::tuple<Iter<Indices>...> iterators;
    454 
    455                 base_iterator( Iter<Indices>... is ) : iterators( is... ) {}
    456                 base_iterator operator++() {
    457                         return base_iterator( ++std::get<Indices>( iterators )... );
    458                 }
    459                 bool operator!=( const base_iterator& other ) const {
    460                         return iterators != other.iterators;
    461                 }
    462                 value_type operator*() const {
    463                         return std::tie( *std::get<Indices>( iterators )... );
    464                 }
    465 
    466                 static base_iterator make_begin( Iterables & data ) {
    467                         return base_iterator( std::get<Indices>( data ).begin()... );
    468                 }
    469                 static base_iterator make_end( Iterables & data ) {
    470                         return base_iterator( std::get<Indices>( data ).end()... );
    471                 }
    472         };
    473 
    474 public:
    475         group_iterate_t( const Args &... args ) : iterables( args... ) {}
    476 
    477         using iterator = base_iterator<decltype(
    478                 std::make_integer_sequence<std::size_t, sizeof...(Args)>())>;
    479 
    480         iterator begin() { return iterator::make_begin( iterables ); }
    481         iterator end() { return iterator::make_end( iterables ); }
    482 };
    483 
    484 // Helpers for the bounds checks (the non-varatic part of group_iterate):
    485 static inline void runGroupBoundsCheck(size_t size0, size_t size1) {
    486         assertf( size0 == size1,
    487                 "group iteration requires containers of the same size: <%zd, %zd>.",
    488                 size0, size1 );
    489 }
    490 
    491 static inline void runGroupBoundsCheck(size_t size0, size_t size1, size_t size2) {
    492         assertf( size0 == size1 && size1 == size2,
    493                 "group iteration requires containers of the same size: <%zd, %zd, %zd>.",
    494                 size0, size1, size2 );
    495 }
    496 
    497 /// Performs bounds check to ensure that all arguments are of the same length.
    498 template< typename... Args >
    499 group_iterate_t<Args...> group_iterate( Args &&... args ) {
    500         runGroupBoundsCheck( args.size()... );
    501         return group_iterate_t<Args...>( std::forward<Args>( args )... );
    502 }
    503 
    504 /// Does not perform a bounds check - requires user to ensure that iteration terminates when appropriate.
    505 template< typename... Args >
    506 group_iterate_t<Args...> unsafe_group_iterate( Args &&... args ) {
    507         return group_iterate_t<Args...>( std::forward<Args>( args )... );
    508 }
    509 
    510 // -----------------------------------------------------------------------------
    511 // Helper struct and function to support
    512 // for ( val : lazy_map( container1, f ) ) {}
    513 // syntax to have a for each that iterates a container, mapping each element by applying f
    514 template< typename T, typename Func >
    515 struct lambda_iterate_t {
    516         const T & ref;
    517         std::function<Func> f;
    518 
    519         struct iterator {
    520                 typedef decltype(begin(ref)) Iter;
    521                 Iter it;
    522                 std::function<Func> f;
    523                 iterator( Iter it, std::function<Func> f ) : it(it), f(f) {}
    524                 iterator & operator++() {
    525                         ++it; return *this;
    526                 }
    527                 bool operator!=( const iterator &other ) const { return it != other.it; }
    528                 auto operator*() const -> decltype(f(*it)) { return f(*it); }
    529         };
    530 
    531         lambda_iterate_t( const T & ref, std::function<Func> f ) : ref(ref), f(f) {}
    532 
    533         auto begin() const -> decltype(iterator(std::begin(ref), f)) { return iterator(std::begin(ref), f); }
    534         auto end() const   -> decltype(iterator(std::end(ref), f)) { return iterator(std::end(ref), f); }
    535 };
    536 
    537 template< typename... Args >
    538 lambda_iterate_t<Args...> lazy_map( const Args &... args ) {
    539         return lambda_iterate_t<Args...>( args...);
    540 }
    541260
    542261// -----------------------------------------------------------------------------
     
    560279} // ilog2
    561280
    562 // -----------------------------------------------------------------------------
    563 /// evaluates expr as a long long int. If second is false, expr could not be evaluated
    564 std::pair<long long int, bool> eval(const Expression * expr);
    565 
    566 namespace ast {
    567         class Expr;
    568 }
    569 
    570 std::pair<long long int, bool> eval(const ast::Expr * expr);
    571 
    572 // -----------------------------------------------------------------------------
    573 /// Reorders the input range in-place so that the minimal-value elements according to the
    574 /// comparator are in front;
    575 /// returns the iterator after the last minimal-value element.
    576 template<typename Iter, typename Compare>
    577 Iter sort_mins( Iter begin, Iter end, Compare& lt ) {
    578         if ( begin == end ) return end;
    579 
    580         Iter min_pos = begin;
    581         for ( Iter i = begin + 1; i != end; ++i ) {
    582                 if ( lt( *i, *min_pos ) ) {
    583                         // new minimum cost; swap into first position
    584                         min_pos = begin;
    585                         std::iter_swap( min_pos, i );
    586                 } else if ( ! lt( *min_pos, *i ) ) {
    587                         // duplicate minimum cost; swap into next minimum position
    588                         ++min_pos;
    589                         std::iter_swap( min_pos, i );
    590                 }
    591         }
    592         return ++min_pos;
    593 }
    594 
    595 template<typename Iter, typename Compare>
    596 inline Iter sort_mins( Iter begin, Iter end, Compare&& lt ) {
    597         return sort_mins( begin, end, lt );
    598 }
    599 
    600 /// sort_mins defaulted to use std::less
    601 template<typename Iter>
    602 inline Iter sort_mins( Iter begin, Iter end ) {
    603         return sort_mins( begin, end, std::less<typename std::iterator_traits<Iter>::value_type>{} );
    604 }
    605 
    606281// Local Variables: //
    607282// tab-width: 4 //
  • src/ResolvExpr/CurrentObject.cc

    r14f6a3cb r8f06277  
    2626#include "AST/Init.hpp"                // for Designation
    2727#include "AST/Node.hpp"                // for readonly
    28 #include "AST/Print.hpp"                // for readonly
     28#include "AST/Print.hpp"               // for readonly
    2929#include "AST/Type.hpp"
     30#include "Common/Eval.h"               // for eval
    3031#include "Common/Indenter.h"           // for Indenter, operator<<
    3132#include "Common/SemanticError.h"      // for SemanticError
  • src/ResolvExpr/ResolveAssertions.cc

    r14f6a3cb r8f06277  
    3030#include "Common/FilterCombos.h"    // for filterCombos
    3131#include "Common/Indenter.h"        // for Indenter
    32 #include "Common/utility.h"         // for sort_mins
    3332#include "GenPoly/GenPoly.h"        // for getFunctionType
    3433#include "ResolvExpr/AlternativeFinder.h"  // for computeConversionCost
  • src/ResolvExpr/Resolver.cc

    r14f6a3cb r8f06277  
    3838#include "AST/SymbolTable.hpp"
    3939#include "AST/Type.hpp"
     40#include "Common/Eval.h"                 // for eval
    4041#include "Common/PassVisitor.h"          // for PassVisitor
    4142#include "Common/SemanticError.h"        // for SemanticError
  • src/ResolvExpr/Resolver.h

    r14f6a3cb r8f06277  
    3434        class Decl;
    3535        class DeletedExpr;
     36        class Expr;
    3637        class Init;
    3738        class StmtExpr;
  • src/SynTree/AggregateDecl.cc

    r14f6a3cb r8f06277  
    1919
    2020#include "Attribute.h"           // for Attribute
     21#include "Common/Eval.h"         // for eval
    2122#include "Common/utility.h"      // for printAll, cloneAll, deleteAll
    2223#include "Declaration.h"         // for AggregateDecl, TypeDecl, Declaration
  • src/SynTree/Type.h

    r14f6a3cb r8f06277  
    2323
    2424#include "BaseSyntaxNode.h"  // for BaseSyntaxNode
    25 #include "Common/utility.h"  // for operator+
     25#include "Common/Iterate.hpp"// for operator+
     26#include "Common/utility.h"  // for toCString
    2627#include "Mutator.h"         // for Mutator
    2728#include "SynTree.h"         // for AST nodes
  • src/Validate/HandleAttributes.cc

    r14f6a3cb r8f06277  
    1717
    1818#include "CompilationState.h"
     19#include "Common/Eval.h"
    1920#include "Common/PassVisitor.h"
    2021#include "Common/SemanticError.h"
Note: See TracChangeset for help on using the changeset viewer.