Changeset 8f06277
- Timestamp:
- Feb 17, 2023, 3:40:15 PM (2 years ago)
- Branches:
- ADT, ast-experimental, master
- Children:
- 56bb2e1
- Parents:
- 14f6a3cb
- Location:
- src
- Files:
-
- 2 added
- 13 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified src/AST/Create.cpp ¶
r14f6a3cb r8f06277 20 20 #include "AST/Decl.hpp" 21 21 #include "AST/Type.hpp" 22 #include "Common/Iterate.hpp" 22 23 23 24 namespace ast { -
TabularUnified src/AST/Decl.cpp ¶
r14f6a3cb r8f06277 20 20 #include <unordered_map> 21 21 22 #include "Common/ utility.h"22 #include "Common/Eval.h" // for eval 23 23 24 24 #include "Fwd.hpp" // for UniqueId -
TabularUnified src/AST/Pass.impl.hpp ¶
r14f6a3cb r8f06277 22 22 #include "AST/TranslationUnit.hpp" 23 23 #include "AST/TypeSubstitution.hpp" 24 #include "Common/Iterate.hpp" 24 25 25 26 #define VISIT_START( node ) \ -
TabularUnified src/Common/Eval.cc ¶
r14f6a3cb r8f06277 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // utility.h --7 // Eval.cc -- Evaluate parts of the ast at compile time. 8 8 // 9 9 // Author : Richard C. Bilson … … 13 13 // Update Count : 119 14 14 // 15 16 #include "Eval.h" 15 17 16 18 #include <utility> // for pair -
TabularUnified src/Common/module.mk ¶
r14f6a3cb r8f06277 25 25 Common/ErrorObjects.h \ 26 26 Common/Eval.cc \ 27 Common/Eval.h \ 27 28 Common/Examine.cc \ 28 29 Common/Examine.h \ … … 30 31 Common/Indenter.h \ 31 32 Common/Indenter.cc \ 33 Common/Iterate.hpp \ 32 34 Common/PassVisitor.cc \ 33 35 Common/PassVisitor.h \ -
TabularUnified src/Common/utility.h ¶
r14f6a3cb r8f06277 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // utility.h -- 7 // utility.h -- General utilities used across the compiler. 8 8 // 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 11 // Last Modified By : Andrew Beach 12 // Last Modified On : Thr Feb 16 12:35:00 202313 // Update Count : 5 212 // Last Modified On : Fri Feb 17 15:25:00 2023 13 // Update Count : 53 14 14 // 15 15 … … 19 19 #include <cctype> 20 20 #include <algorithm> 21 #include <functional>22 21 #include <iostream> 23 #include <iterator>24 22 #include <list> 25 23 #include <memory> … … 27 25 #include <string> 28 26 #include <type_traits> 29 #include <utility>30 27 #include <vector> 31 28 #include <cstring> // memcmp … … 166 163 167 164 #define toCString( ... ) toString( __VA_ARGS__ ).c_str() 168 169 // replace element of list with all elements of another list170 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 element183 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 }188 165 189 166 template< typename... Args > … … 213 190 } 214 191 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 amount227 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 244 192 template< typename... Params > 245 193 void warn( const Params & ... params ) { 246 194 std::cerr << "Warning: "; 247 warn_single( params... ); 195 toString_single( std::cerr, params... ); 196 std::cerr << std::endl; 248 197 } 249 198 … … 251 200 static inline bool isPrefix( const std::string & str, const std::string & pref, unsigned int start = 0 ) { 252 201 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 } 279 204 280 205 // ----------------------------------------------------------------------------- … … 333 258 ~ValueGuardPtr() { if( ref ) { swap( *ref, old ); } } 334 259 }; 335 336 // -----------------------------------------------------------------------------337 // Helper struct and function to support338 // for ( val : reverseIterate( container ) ) {}339 // syntax to have a for each that iterates backwards340 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 can447 // 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 support512 // for ( val : lazy_map( container1, f ) ) {}513 // syntax to have a for each that iterates a container, mapping each element by applying f514 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 }541 260 542 261 // ----------------------------------------------------------------------------- … … 560 279 } // ilog2 561 280 562 // -----------------------------------------------------------------------------563 /// evaluates expr as a long long int. If second is false, expr could not be evaluated564 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 the574 /// 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 position584 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 position588 ++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::less601 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 606 281 // Local Variables: // 607 282 // tab-width: 4 // -
TabularUnified src/ResolvExpr/CurrentObject.cc ¶
r14f6a3cb r8f06277 26 26 #include "AST/Init.hpp" // for Designation 27 27 #include "AST/Node.hpp" // for readonly 28 #include "AST/Print.hpp" 28 #include "AST/Print.hpp" // for readonly 29 29 #include "AST/Type.hpp" 30 #include "Common/Eval.h" // for eval 30 31 #include "Common/Indenter.h" // for Indenter, operator<< 31 32 #include "Common/SemanticError.h" // for SemanticError -
TabularUnified src/ResolvExpr/ResolveAssertions.cc ¶
r14f6a3cb r8f06277 30 30 #include "Common/FilterCombos.h" // for filterCombos 31 31 #include "Common/Indenter.h" // for Indenter 32 #include "Common/utility.h" // for sort_mins33 32 #include "GenPoly/GenPoly.h" // for getFunctionType 34 33 #include "ResolvExpr/AlternativeFinder.h" // for computeConversionCost -
TabularUnified src/ResolvExpr/Resolver.cc ¶
r14f6a3cb r8f06277 38 38 #include "AST/SymbolTable.hpp" 39 39 #include "AST/Type.hpp" 40 #include "Common/Eval.h" // for eval 40 41 #include "Common/PassVisitor.h" // for PassVisitor 41 42 #include "Common/SemanticError.h" // for SemanticError -
TabularUnified src/ResolvExpr/Resolver.h ¶
r14f6a3cb r8f06277 34 34 class Decl; 35 35 class DeletedExpr; 36 class Expr; 36 37 class Init; 37 38 class StmtExpr; -
TabularUnified src/SynTree/AggregateDecl.cc ¶
r14f6a3cb r8f06277 19 19 20 20 #include "Attribute.h" // for Attribute 21 #include "Common/Eval.h" // for eval 21 22 #include "Common/utility.h" // for printAll, cloneAll, deleteAll 22 23 #include "Declaration.h" // for AggregateDecl, TypeDecl, Declaration -
TabularUnified src/SynTree/Type.h ¶
r14f6a3cb r8f06277 23 23 24 24 #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 26 27 #include "Mutator.h" // for Mutator 27 28 #include "SynTree.h" // for AST nodes -
TabularUnified src/Validate/HandleAttributes.cc ¶
r14f6a3cb r8f06277 17 17 18 18 #include "CompilationState.h" 19 #include "Common/Eval.h" 19 20 #include "Common/PassVisitor.h" 20 21 #include "Common/SemanticError.h"
Note: See TracChangeset
for help on using the changeset viewer.