Changeset c6a1e8a
- Timestamp:
- Jun 5, 2019, 8:36:48 PM (5 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- 866545b
- Parents:
- 67130fe (diff), 3cd5fdd (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - Location:
- src
- Files:
-
- 5 added
- 23 edited
Legend:
- Unmodified
- Added
- Removed
-
src/AST/Bitfield.hpp
r67130fe rc6a1e8a 9 9 // Author : Aaron B. Moss 10 10 // Created On : Thu May 9 10:00:00 2019 11 // Last Modified By : A aron B. Moss12 // Last Modified On : Thu May 910:00:00 201913 // Update Count : 111 // Last Modified By : Andrew Beach 12 // Last Modified On : Wed Jun 5 10:00:00 2019 13 // Update Count : 2 14 14 // 15 15 16 16 #pragma once 17 17 18 #include <strings.h> // for ffs 18 #include <strings.h> // for ffs 19 #include <type_traits> // for is_unsigned 19 20 20 21 /// Make a type a bitfield. … … 24 25 template<typename T> 25 26 struct bitfield : public T { 26 static_assert(sizeof(T) == sizeof(unsigned int), "Type has incorrect size");27 27 using T::val; 28 28 using val_t = decltype(val); 29 static_assert(sizeof(T) == sizeof(unsigned int), "Type has incorrect size"); 30 static_assert(std::is_unsigned<val_t>::value, "Bitfield val field is not unsigned."); 29 31 30 32 constexpr bitfield() : T( 0 ) {} -
src/AST/Convert.cpp
r67130fe rc6a1e8a 749 749 break; 750 750 case ast::ConstantExpr::String: 751 rslt = new ConstantExpr{Constant::from_string( node->rep )}; 751 rslt = new ConstantExpr{Constant{ 752 get<Type>().accept1( node->result ), 753 node->rep, 754 (long long unsigned int)0 755 }}; 752 756 break; 753 757 } … … 2150 2154 GET_ACCEPT_1(result, Type), 2151 2155 old->constant.get_value(), 2152 (unsigned long long) old->intValue() 2156 (unsigned long long) old->intValue(), 2157 ast::ConstantExpr::Kind::Integer 2153 2158 ); 2154 2159 } else if (isFloatlikeConstantType(old->result)) { … … 2160 2165 ); 2161 2166 } else if (isStringlikeConstantType(old->result)) { 2162 rslt = ast::ConstantExpr::from_string( 2163 old->location, 2164 old->constant.get_value() 2167 rslt = new ast::ConstantExpr( 2168 old->location, 2169 GET_ACCEPT_1(result, Type), 2170 old->constant.get_value(), 2171 0, 2172 ast::ConstantExpr::Kind::String 2165 2173 ); 2166 2174 } -
src/AST/Decl.hpp
r67130fe rc6a1e8a 103 103 104 104 ObjectDecl( const CodeLocation & loc, const std::string & name, const Type * type, 105 Init * init = nullptr, Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::C,106 Expr * bitWd = nullptr, std::vector< ptr<Attribute> > && attrs = {},107 Function::Specs fs = {} )105 const Init * init = nullptr, Storage::Classes storage = {}, 106 Linkage::Spec linkage = Linkage::C, const Expr * bitWd = nullptr, 107 std::vector< ptr<Attribute> > && attrs = {}, Function::Specs fs = {} ) 108 108 : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), type( type ), 109 109 init( init ), bitfieldWidth( bitWd ) {} -
src/AST/Node.cpp
r67130fe rc6a1e8a 34 34 template< typename node_t, enum ast::Node::ref_type ref_t > 35 35 void ast::ptr_base<node_t, ref_t>::_dec( const node_t * node ) { node->decrement(ref_t); } 36 37 template< typename node_t, enum ast::Node::ref_type ref_t > 38 void ast::ptr_base<node_t, ref_t>::_check() const { if(node) assert(node->was_ever_strong == false || node->strong_count > 0); } 36 39 37 40 template< typename node_t, enum ast::Node::ref_type ref_t > -
src/AST/Node.hpp
r67130fe rc6a1e8a 46 46 }; 47 47 48 bool unique() const { return strong_count == 1; } 49 48 50 private: 49 51 /// Make a copy of this node; should be overridden in subclass with more precise return type … … 56 58 mutable size_t strong_count = 0; 57 59 mutable size_t weak_count = 0; 60 mutable bool was_ever_strong = false; 58 61 59 62 void increment(ref_type ref) const { 60 63 switch (ref) { 61 case ref_type::strong: strong_count++; break;64 case ref_type::strong: strong_count++; was_ever_strong = true; break; 62 65 case ref_type::weak : weak_count ++; break; 63 66 } … … 176 179 } 177 180 178 const node_t * get() const { return node; }179 const node_t * operator->() const { return node; }180 const node_t & operator* () const { return *node; }181 explicit operator bool() const { return node; }182 operator const node_t * () const { return node; }181 const node_t * get() const { _check(); return node; } 182 const node_t * operator->() const { _check(); return node; } 183 const node_t & operator* () const { _check(); return *node; } 184 explicit operator bool() const { _check(); return node; } 185 operator const node_t * () const { _check(); return node; } 183 186 184 187 /// wrapper for convenient access to dynamic_cast 185 188 template<typename o_node_t> 186 const o_node_t * as() const { return dynamic_cast<const o_node_t *>(node); }189 const o_node_t * as() const { _check(); return dynamic_cast<const o_node_t *>(node); } 187 190 188 191 /// wrapper for convenient access to strict_dynamic_cast … … 208 211 void _inc( const node_t * other ); 209 212 void _dec( const node_t * other ); 213 void _check() const; 210 214 211 215 protected: -
src/AST/Pass.hpp
r67130fe rc6a1e8a 33 33 #include "AST/Visitor.hpp" 34 34 35 #include " SymTab/Indexer.h"35 #include "AST/SymbolTable.hpp" 36 36 37 37 // Private prelude header, needed for some of the magic tricks this class pulls off … … 61 61 // postvisit/postmutate teminates. 62 62 // | WithVisitorRef - provides an pointer to the templated visitor wrapper 63 // | With Indexer - provides indexer functionality (i.e. up-to-date symbol table)63 // | WithSymbolTable - provides symbol table functionality 64 64 //------------------------------------------------------------------------------------------------- 65 65 template< typename pass_t > … … 206 206 207 207 private: 208 /// Internal RAII guard for indexerfeatures209 struct guard_ indexer{210 guard_ indexer( Pass<pass_t> & pass ): pass( pass ) { __pass::indexer::enter(pass, 0); }211 ~guard_ indexer() { __pass::indexer::leave(pass, 0); }208 /// Internal RAII guard for symbol table features 209 struct guard_symtab { 210 guard_symtab( Pass<pass_t> & pass ): pass( pass ) { __pass::symtab::enter(pass, 0); } 211 ~guard_symtab() { __pass::symtab::leave(pass, 0); } 212 212 Pass<pass_t> & pass; 213 213 }; … … 294 294 }; 295 295 296 /// Use when the templated visitor should update the indexer297 struct With Indexer{298 Sym Tab::Indexer indexer;296 /// Use when the templated visitor should update the symbol table 297 struct WithSymbolTable { 298 SymbolTable symtab; 299 299 }; 300 300 } -
src/AST/Pass.impl.hpp
r67130fe rc6a1e8a 398 398 VISIT( 399 399 { 400 guard_ indexerguard { *this };400 guard_symtab guard { *this }; 401 401 maybe_accept( node, &ObjectDecl::type ); 402 402 } … … 406 406 ) 407 407 408 __pass:: indexer::addId( pass, 0, node );408 __pass::symtab::addId( pass, 0, node ); 409 409 410 410 VISIT_END( DeclWithType, node ); … … 417 417 VISIT_START( node ); 418 418 419 __pass:: indexer::addId( pass, 0, node );419 __pass::symtab::addId( pass, 0, node ); 420 420 421 421 VISIT(maybe_accept( node, &FunctionDecl::withExprs );) 422 422 { 423 423 // with clause introduces a level of scope (for the with expression members). 424 // with clause exprs are added to the indexerbefore parameters so that parameters424 // with clause exprs are added to the symbol table before parameters so that parameters 425 425 // shadow with exprs and not the other way around. 426 guard_ indexerguard { *this };427 __pass:: indexer::addWith( pass, 0, node->withExprs, node );426 guard_symtab guard { *this }; 427 __pass::symtab::addWith( pass, 0, node->withExprs, node ); 428 428 { 429 guard_ indexerguard { *this };429 guard_symtab guard { *this }; 430 430 // implicit add __func__ identifier as specified in the C manual 6.4.2.2 431 431 static ast::ObjectDecl func( … … 436 436 ) 437 437 ); 438 __pass:: indexer::addId( pass, 0, &func );438 __pass::symtab::addId( pass, 0, &func ); 439 439 VISIT( 440 440 maybe_accept( node, &FunctionDecl::type ); … … 460 460 // make up a forward declaration and add it before processing the members 461 461 // needs to be on the heap because addStruct saves the pointer 462 __pass:: indexer::addStructFwd( pass, 0, node );463 464 VISIT({ 465 guard_ indexerguard { * this };462 __pass::symtab::addStructFwd( pass, 0, node ); 463 464 VISIT({ 465 guard_symtab guard { * this }; 466 466 maybe_accept( node, &StructDecl::params ); 467 467 maybe_accept( node, &StructDecl::members ); … … 469 469 470 470 // this addition replaces the forward declaration 471 __pass:: indexer::addStruct( pass, 0, node );471 __pass::symtab::addStruct( pass, 0, node ); 472 472 473 473 VISIT_END( Decl, node ); … … 481 481 482 482 // make up a forward declaration and add it before processing the members 483 __pass:: indexer::addUnionFwd( pass, 0, node );484 485 VISIT({ 486 guard_ indexerguard { * this };483 __pass::symtab::addUnionFwd( pass, 0, node ); 484 485 VISIT({ 486 guard_symtab guard { * this }; 487 487 maybe_accept( node, &UnionDecl::params ); 488 488 maybe_accept( node, &UnionDecl::members ); 489 489 }) 490 490 491 __pass:: indexer::addUnion( pass, 0, node );491 __pass::symtab::addUnion( pass, 0, node ); 492 492 493 493 VISIT_END( Decl, node ); … … 500 500 VISIT_START( node ); 501 501 502 __pass:: indexer::addEnum( pass, 0, node );502 __pass::symtab::addEnum( pass, 0, node ); 503 503 504 504 VISIT( … … 518 518 519 519 VISIT({ 520 guard_ indexerguard { *this };520 guard_symtab guard { *this }; 521 521 maybe_accept( node, &TraitDecl::params ); 522 522 maybe_accept( node, &TraitDecl::members ); 523 523 }) 524 524 525 __pass:: indexer::addTrait( pass, 0, node );525 __pass::symtab::addTrait( pass, 0, node ); 526 526 527 527 VISIT_END( Decl, node ); … … 535 535 536 536 VISIT({ 537 guard_ indexerguard { *this };537 guard_symtab guard { *this }; 538 538 maybe_accept( node, &TypeDecl::params ); 539 539 maybe_accept( node, &TypeDecl::base ); … … 543 543 // note that assertions come after the type is added to the symtab, since they are not part of the type proper 544 544 // and may depend on the type itself 545 __pass:: indexer::addType( pass, 0, node );545 __pass::symtab::addType( pass, 0, node ); 546 546 547 547 VISIT( … … 549 549 550 550 { 551 guard_ indexerguard { *this };551 guard_symtab guard { *this }; 552 552 maybe_accept( node, &TypeDecl::init ); 553 553 } … … 564 564 565 565 VISIT({ 566 guard_ indexerguard { *this };566 guard_symtab guard { *this }; 567 567 maybe_accept( node, &TypedefDecl::params ); 568 568 maybe_accept( node, &TypedefDecl::base ); 569 569 }) 570 570 571 __pass:: indexer::addType( pass, 0, node );571 __pass::symtab::addType( pass, 0, node ); 572 572 573 573 VISIT( maybe_accept( node, &TypedefDecl::assertions ); ) … … 611 611 // do not enter a new scope if inFunction is true - needs to check old state before the assignment 612 612 auto guard1 = makeFuncGuard( [this, inFunction = this->inFunction]() { 613 if ( ! inFunction ) __pass:: indexer::enter(pass, 0);613 if ( ! inFunction ) __pass::symtab::enter(pass, 0); 614 614 }, [this, inFunction = this->inFunction]() { 615 if ( ! inFunction ) __pass:: indexer::leave(pass, 0);615 if ( ! inFunction ) __pass::symtab::leave(pass, 0); 616 616 }); 617 617 ValueGuard< bool > guard2( inFunction ); … … 669 669 VISIT({ 670 670 // if statements introduce a level of scope (for the initialization) 671 guard_ indexerguard { *this };671 guard_symtab guard { *this }; 672 672 maybe_accept( node, &IfStmt::inits ); 673 673 maybe_accept( node, &IfStmt::cond ); … … 687 687 VISIT({ 688 688 // while statements introduce a level of scope (for the initialization) 689 guard_ indexerguard { *this };689 guard_symtab guard { *this }; 690 690 maybe_accept( node, &WhileStmt::inits ); 691 691 maybe_accept( node, &WhileStmt::cond ); … … 704 704 VISIT({ 705 705 // for statements introduce a level of scope (for the initialization) 706 guard_ indexerguard { *this };706 guard_symtab guard { *this }; 707 707 maybe_accept( node, &ForStmt::inits ); 708 708 maybe_accept( node, &ForStmt::cond ); … … 800 800 VISIT({ 801 801 // catch statements introduce a level of scope (for the caught exception) 802 guard_ indexerguard { *this };802 guard_symtab guard { *this }; 803 803 maybe_accept( node, &CatchStmt::decl ); 804 804 maybe_accept( node, &CatchStmt::cond ); … … 901 901 { 902 902 // catch statements introduce a level of scope (for the caught exception) 903 guard_ indexerguard { *this };904 __pass:: indexer::addWith( pass, 0, node->exprs, node );903 guard_symtab guard { *this }; 904 __pass::symtab::addWith( pass, 0, node->exprs, node ); 905 905 maybe_accept( node, &WithStmt::stmt ); 906 906 } … … 953 953 VISIT( 954 954 { 955 guard_ indexerguard { *this };955 guard_symtab guard { *this }; 956 956 maybe_accept( node, &ApplicationExpr::result ); 957 957 } … … 971 971 VISIT( 972 972 { 973 guard_ indexerguard { *this };973 guard_symtab guard { *this }; 974 974 maybe_accept( node, &UntypedExpr::result ); 975 975 } … … 988 988 989 989 VISIT({ 990 guard_ indexerguard { *this };990 guard_symtab guard { *this }; 991 991 maybe_accept( node, &NameExpr::result ); 992 992 }) … … 1002 1002 1003 1003 VISIT({ 1004 guard_ indexerguard { *this };1004 guard_symtab guard { *this }; 1005 1005 maybe_accept( node, &CastExpr::result ); 1006 1006 } … … 1018 1018 1019 1019 VISIT({ 1020 guard_ indexerguard { *this };1020 guard_symtab guard { *this }; 1021 1021 maybe_accept( node, &KeywordCastExpr::result ); 1022 1022 } … … 1034 1034 1035 1035 VISIT({ 1036 guard_ indexerguard { *this };1036 guard_symtab guard { *this }; 1037 1037 maybe_accept( node, &VirtualCastExpr::result ); 1038 1038 } … … 1050 1050 1051 1051 VISIT({ 1052 guard_ indexerguard { *this };1052 guard_symtab guard { *this }; 1053 1053 maybe_accept( node, &AddressExpr::result ); 1054 1054 } … … 1066 1066 1067 1067 VISIT({ 1068 guard_ indexerguard { *this };1068 guard_symtab guard { *this }; 1069 1069 maybe_accept( node, &LabelAddressExpr::result ); 1070 1070 }) … … 1080 1080 1081 1081 VISIT({ 1082 guard_ indexerguard { *this };1082 guard_symtab guard { *this }; 1083 1083 maybe_accept( node, &UntypedMemberExpr::result ); 1084 1084 } … … 1097 1097 1098 1098 VISIT({ 1099 guard_ indexerguard { *this };1099 guard_symtab guard { *this }; 1100 1100 maybe_accept( node, &MemberExpr::result ); 1101 1101 } … … 1113 1113 1114 1114 VISIT({ 1115 guard_ indexerguard { *this };1115 guard_symtab guard { *this }; 1116 1116 maybe_accept( node, &VariableExpr::result ); 1117 1117 }) … … 1127 1127 1128 1128 VISIT({ 1129 guard_ indexerguard { *this };1129 guard_symtab guard { *this }; 1130 1130 maybe_accept( node, &ConstantExpr::result ); 1131 1131 }) … … 1141 1141 1142 1142 VISIT({ 1143 guard_ indexerguard { *this };1143 guard_symtab guard { *this }; 1144 1144 maybe_accept( node, &SizeofExpr::result ); 1145 1145 } … … 1161 1161 1162 1162 VISIT({ 1163 guard_ indexerguard { *this };1163 guard_symtab guard { *this }; 1164 1164 maybe_accept( node, &AlignofExpr::result ); 1165 1165 } … … 1181 1181 1182 1182 VISIT({ 1183 guard_ indexerguard { *this };1183 guard_symtab guard { *this }; 1184 1184 maybe_accept( node, &UntypedOffsetofExpr::result ); 1185 1185 } … … 1197 1197 1198 1198 VISIT({ 1199 guard_ indexerguard { *this };1199 guard_symtab guard { *this }; 1200 1200 maybe_accept( node, &OffsetofExpr::result ); 1201 1201 } … … 1213 1213 1214 1214 VISIT({ 1215 guard_ indexerguard { *this };1215 guard_symtab guard { *this }; 1216 1216 maybe_accept( node, &OffsetPackExpr::result ); 1217 1217 } … … 1229 1229 1230 1230 VISIT({ 1231 guard_ indexerguard { *this };1231 guard_symtab guard { *this }; 1232 1232 maybe_accept( node, &LogicalExpr::result ); 1233 1233 } … … 1246 1246 1247 1247 VISIT({ 1248 guard_ indexerguard { *this };1248 guard_symtab guard { *this }; 1249 1249 maybe_accept( node, &ConditionalExpr::result ); 1250 1250 } … … 1264 1264 1265 1265 VISIT({ 1266 guard_ indexerguard { *this };1266 guard_symtab guard { *this }; 1267 1267 maybe_accept( node, &CommaExpr::result ); 1268 1268 } … … 1281 1281 1282 1282 VISIT({ 1283 guard_ indexerguard { *this };1283 guard_symtab guard { *this }; 1284 1284 maybe_accept( node, &TypeExpr::result ); 1285 1285 } … … 1297 1297 1298 1298 VISIT({ 1299 guard_ indexerguard { *this };1299 guard_symtab guard { *this }; 1300 1300 maybe_accept( node, &AsmExpr::result ); 1301 1301 } … … 1315 1315 1316 1316 VISIT({ 1317 guard_ indexerguard { *this };1317 guard_symtab guard { *this }; 1318 1318 maybe_accept( node, &ImplicitCopyCtorExpr::result ); 1319 1319 } … … 1331 1331 1332 1332 VISIT({ 1333 guard_ indexerguard { *this };1333 guard_symtab guard { *this }; 1334 1334 maybe_accept( node, &ConstructorExpr::result ); 1335 1335 } … … 1347 1347 1348 1348 VISIT({ 1349 guard_ indexerguard { *this };1349 guard_symtab guard { *this }; 1350 1350 maybe_accept( node, &CompoundLiteralExpr::result ); 1351 1351 } … … 1363 1363 1364 1364 VISIT({ 1365 guard_ indexerguard { *this };1365 guard_symtab guard { *this }; 1366 1366 maybe_accept( node, &RangeExpr::result ); 1367 1367 } … … 1380 1380 1381 1381 VISIT({ 1382 guard_ indexerguard { *this };1382 guard_symtab guard { *this }; 1383 1383 maybe_accept( node, &UntypedTupleExpr::result ); 1384 1384 } … … 1396 1396 1397 1397 VISIT({ 1398 guard_ indexerguard { *this };1398 guard_symtab guard { *this }; 1399 1399 maybe_accept( node, &TupleExpr::result ); 1400 1400 } … … 1412 1412 1413 1413 VISIT({ 1414 guard_ indexerguard { *this };1414 guard_symtab guard { *this }; 1415 1415 maybe_accept( node, &TupleIndexExpr::result ); 1416 1416 } … … 1428 1428 1429 1429 VISIT({ 1430 guard_ indexerguard { *this };1430 guard_symtab guard { *this }; 1431 1431 maybe_accept( node, &TupleAssignExpr::result ); 1432 1432 } … … 1454 1454 1455 1455 { 1456 guard_ indexerguard { *this };1456 guard_symtab guard { *this }; 1457 1457 maybe_accept( node, &StmtExpr::result ); 1458 1458 } … … 1472 1472 1473 1473 VISIT({ 1474 guard_ indexerguard { *this };1474 guard_symtab guard { *this }; 1475 1475 maybe_accept( node, &UniqueExpr::result ); 1476 1476 } … … 1488 1488 1489 1489 VISIT({ 1490 guard_ indexerguard { *this };1490 guard_symtab guard { *this }; 1491 1491 maybe_accept( node, &UntypedInitExpr::result ); 1492 1492 } … … 1505 1505 1506 1506 VISIT({ 1507 guard_ indexerguard { *this };1507 guard_symtab guard { *this }; 1508 1508 maybe_accept( node, &InitExpr::result ); 1509 1509 } … … 1522 1522 1523 1523 VISIT({ 1524 guard_ indexerguard { *this };1524 guard_symtab guard { *this }; 1525 1525 maybe_accept( node, &DeletedExpr::result ); 1526 1526 } … … 1539 1539 1540 1540 VISIT({ 1541 guard_ indexerguard { *this };1541 guard_symtab guard { *this }; 1542 1542 maybe_accept( node, &DefaultArgExpr::result ); 1543 1543 } … … 1555 1555 1556 1556 VISIT({ 1557 guard_ indexerguard { *this };1557 guard_symtab guard { *this }; 1558 1558 maybe_accept( node, &GenericExpr::result ); 1559 1559 } … … 1566 1566 const Type * type = nullptr; 1567 1567 if( assoc.type ) { 1568 guard_ indexerguard { *this };1568 guard_symtab guard { *this }; 1569 1569 type = assoc.type->accept( *this ); 1570 1570 if( type != assoc.type ) mutated = true; … … 1682 1682 VISIT_START( node ); 1683 1683 1684 __pass:: indexer::addStruct( pass, 0, node->name );1685 1686 VISIT({ 1687 guard_ indexerguard { *this };1684 __pass::symtab::addStruct( pass, 0, node->name ); 1685 1686 VISIT({ 1687 guard_symtab guard { *this }; 1688 1688 maybe_accept( node, &StructInstType::forall ); 1689 1689 maybe_accept( node, &StructInstType::params ); … … 1699 1699 VISIT_START( node ); 1700 1700 1701 __pass:: indexer::addStruct( pass, 0, node->name );1701 __pass::symtab::addStruct( pass, 0, node->name ); 1702 1702 1703 1703 { 1704 guard_ indexerguard { *this };1704 guard_symtab guard { *this }; 1705 1705 maybe_accept( node, &UnionInstType::forall ); 1706 1706 maybe_accept( node, &UnionInstType::params ); … … 1893 1893 std::unordered_map< std::string, ast::ptr< ast::Type > > new_map; 1894 1894 for ( const auto & p : node->typeEnv ) { 1895 guard_ indexerguard { *this };1895 guard_symtab guard { *this }; 1896 1896 auto new_node = p.second->accept( *this ); 1897 1897 if (new_node != p.second) mutated = false; … … 1909 1909 std::unordered_map< std::string, ast::ptr< ast::Expr > > new_map; 1910 1910 for ( const auto & p : node->varEnv ) { 1911 guard_ indexerguard { *this };1911 guard_symtab guard { *this }; 1912 1912 auto new_node = p.second->accept( *this ); 1913 1913 if (new_node != p.second) mutated = false; -
src/AST/Pass.proto.hpp
r67130fe rc6a1e8a 265 265 }; 266 266 267 // Finally certain pass desire an up to date indexerautomatically268 // detect the presence of a member name indexerand call all the members appropriately269 namespace indexer{267 // Finally certain pass desire an up to date symbol table automatically 268 // detect the presence of a member name `symtab` and call all the members appropriately 269 namespace symtab { 270 270 // Some simple scoping rules 271 271 template<typename pass_t> 272 static inline auto enter( pass_t & pass, int ) -> decltype( pass. indexer.enterScope(), void() ) {273 pass. indexer.enterScope();272 static inline auto enter( pass_t & pass, int ) -> decltype( pass.symtab.enterScope(), void() ) { 273 pass.symtab.enterScope(); 274 274 } 275 275 … … 278 278 279 279 template<typename pass_t> 280 static inline auto leave( pass_t & pass, int ) -> decltype( pass. indexer.leaveScope(), void() ) {281 pass. indexer.leaveScope();280 static inline auto leave( pass_t & pass, int ) -> decltype( pass.symtab.leaveScope(), void() ) { 281 pass.symtab.leaveScope(); 282 282 } 283 283 … … 285 285 static inline auto leave( pass_t &, long ) {} 286 286 287 // The indexerhas 2 kind of functions mostly, 1 argument and 2 arguments287 // The symbol table has 2 kind of functions mostly, 1 argument and 2 arguments 288 288 // Create macro to condense these common patterns 289 #define INDEXER_FUNC1( func, type ) \289 #define SYMTAB_FUNC1( func, type ) \ 290 290 template<typename pass_t> \ 291 static inline auto func( pass_t & pass, int, type arg ) -> decltype( pass. indexer.func( arg ), void() ) {\292 pass. indexer.func( arg ); \291 static inline auto func( pass_t & pass, int, type arg ) -> decltype( pass.symtab.func( arg ), void() ) {\ 292 pass.symtab.func( arg ); \ 293 293 } \ 294 294 \ … … 296 296 static inline void func( pass_t &, long, type ) {} 297 297 298 #define INDEXER_FUNC2( func, type1, type2 ) \298 #define SYMTAB_FUNC2( func, type1, type2 ) \ 299 299 template<typename pass_t> \ 300 static inline auto func( pass_t & pass, int, type1 arg1, type2 arg2 ) -> decltype( pass. indexer.func( arg1, arg2 ), void () ) {\301 pass. indexer.func( arg1, arg2 ); \300 static inline auto func( pass_t & pass, int, type1 arg1, type2 arg2 ) -> decltype( pass.symtab.func( arg1, arg2 ), void () ) {\ 301 pass.symtab.func( arg1, arg2 ); \ 302 302 } \ 303 303 \ … … 305 305 static inline void func( pass_t &, long, type1, type2 ) {} 306 306 307 INDEXER_FUNC1( addId , const DeclWithType * );308 INDEXER_FUNC1( addType , const NamedTypeDecl * );309 INDEXER_FUNC1( addStruct , const StructDecl * );310 INDEXER_FUNC1( addEnum , const EnumDecl * );311 INDEXER_FUNC1( addUnion , const UnionDecl * );312 INDEXER_FUNC1( addTrait , const TraitDecl * );313 INDEXER_FUNC2( addWith , const std::vector< ptr<Expr> > &, const Node * );307 SYMTAB_FUNC1( addId , const DeclWithType * ); 308 SYMTAB_FUNC1( addType , const NamedTypeDecl * ); 309 SYMTAB_FUNC1( addStruct , const StructDecl * ); 310 SYMTAB_FUNC1( addEnum , const EnumDecl * ); 311 SYMTAB_FUNC1( addUnion , const UnionDecl * ); 312 SYMTAB_FUNC1( addTrait , const TraitDecl * ); 313 SYMTAB_FUNC2( addWith , const std::vector< ptr<Expr> > &, const Node * ); 314 314 315 315 // A few extra functions have more complicated behaviour, they are hand written 316 316 template<typename pass_t> 317 static inline auto addStructFwd( pass_t & pass, int, const ast::StructDecl * decl ) -> decltype( pass. indexer.addStruct( decl ), void() ) {317 static inline auto addStructFwd( pass_t & pass, int, const ast::StructDecl * decl ) -> decltype( pass.symtab.addStruct( decl ), void() ) { 318 318 ast::StructDecl * fwd = new ast::StructDecl( decl->location, decl->name ); 319 319 fwd->params = decl->params; 320 pass. indexer.addStruct( fwd );320 pass.symtab.addStruct( fwd ); 321 321 } 322 322 … … 325 325 326 326 template<typename pass_t> 327 static inline auto addUnionFwd( pass_t & pass, int, const ast::UnionDecl * decl ) -> decltype( pass. indexer.addUnion( decl ), void() ) {327 static inline auto addUnionFwd( pass_t & pass, int, const ast::UnionDecl * decl ) -> decltype( pass.symtab.addUnion( decl ), void() ) { 328 328 UnionDecl * fwd = new UnionDecl( decl->location, decl->name ); 329 329 fwd->params = decl->params; 330 pass. indexer.addUnion( fwd );330 pass.symtab.addUnion( fwd ); 331 331 } 332 332 … … 335 335 336 336 template<typename pass_t> 337 static inline auto addStruct( pass_t & pass, int, const std::string & str ) -> decltype( pass. indexer.addStruct( str ), void() ) {338 if ( ! pass. indexer.lookupStruct( str ) ) {339 pass. indexer.addStruct( str );337 static inline auto addStruct( pass_t & pass, int, const std::string & str ) -> decltype( pass.symtab.addStruct( str ), void() ) { 338 if ( ! pass.symtab.lookupStruct( str ) ) { 339 pass.symtab.addStruct( str ); 340 340 } 341 341 } … … 345 345 346 346 template<typename pass_t> 347 static inline auto addUnion( pass_t & pass, int, const std::string & str ) -> decltype( pass. indexer.addUnion( str ), void() ) {348 if ( ! pass. indexer.lookupUnion( str ) ) {349 pass. indexer.addUnion( str );347 static inline auto addUnion( pass_t & pass, int, const std::string & str ) -> decltype( pass.symtab.addUnion( str ), void() ) { 348 if ( ! pass.symtab.lookupUnion( str ) ) { 349 pass.symtab.addUnion( str ); 350 350 } 351 351 } … … 354 354 static inline void addUnion( pass_t &, long, const std::string & ) {} 355 355 356 #undef INDEXER_FUNC1357 #undef INDEXER_FUNC2356 #undef SYMTAB_FUNC1 357 #undef SYMTAB_FUNC2 358 358 }; 359 359 }; -
src/AST/Print.hpp
r67130fe rc6a1e8a 29 29 void print( std::ostream & os, const ast::Node * node, Indenter indent = {} ); 30 30 31 /// Wrap any standard format printer (matching above) with integer Indenter constructor32 template<typename T>33 inline void print( std::ostream & os, T && x, unsigned int indent ) {34 print( os, std::forward<T>(x), Indenter{ Indenter::tabsize, indent });35 }36 37 31 /// Print a declaration in its short form 38 32 void printShort( std::ostream & os, const ast::Decl * node, Indenter indent = {} ); 39 33 40 34 inline void printShort( std::ostream & os, const ast::Decl * node, unsigned int indent ) { 41 printShort( os, node, Indenter{ Indenter::tabsize,indent } );35 printShort( os, node, Indenter{ indent } ); 42 36 } 43 37 -
src/AST/SymbolTable.hpp
r67130fe rc6a1e8a 85 85 86 86 public: 87 explicitSymbolTable();87 SymbolTable(); 88 88 ~SymbolTable(); 89 89 … … 123 123 void addType( const NamedTypeDecl * decl ); 124 124 /// Adds a struct declaration to the symbol table by name 125 void addStruct( const std::string & id );125 void addStruct( const std::string & id ); 126 126 /// Adds a struct declaration to the symbol table 127 127 void addStruct( const StructDecl * decl ); 128 128 /// Adds an enum declaration to the symbol table 129 void addEnum( const EnumDecl * decl );129 void addEnum( const EnumDecl * decl ); 130 130 /// Adds a union declaration to the symbol table by name 131 void addUnion( const std::string & id );131 void addUnion( const std::string & id ); 132 132 /// Adds a union declaration to the symbol table 133 133 void addUnion( const UnionDecl * decl ); -
src/AST/porting.md
r67130fe rc6a1e8a 109 109 * `SymTab::Indexer` => `ast::SymbolTable` 110 110 * `SymTab/Indexer.{h,cc}` => `AST/SymbolTable.{hpp,cpp}` 111 * **TODO**`WithIndexer` => `WithSymbolTable`111 * `WithIndexer` => `WithSymbolTable` 112 112 * `indexer` => `symTab` 113 113 * `IdData::deleteStmt` => `IdData::deleter` 114 114 * `lookupId()` now returns a vector rather than an out-param list 115 116 115 * To avoid name collisions: 116 * `SymTab::Mangler` => `Mangle` 117 117 * `ResolvExpr::TypeEnvironment` => `ast::TypeEnvironment` 118 118 * in `AST/TypeEnvironment.hpp` … … 295 295 * changed `WidenMode widenMode` => `WidenMode widen` 296 296 297 `Alternative` => `Candidate` 298 * `openVars` => `open` 299 297 300 [1] https://gcc.gnu.org/onlinedocs/gcc-9.1.0/gcc/Type-Attributes.html#Type-Attributes 298 301 -
src/Common/Indenter.h
r67130fe rc6a1e8a 18 18 19 19 struct Indenter { 20 static unsigned tabsize; 20 static unsigned tabsize; ///< default number of spaces in one level of indentation 21 21 22 Indenter( unsigned int amt = tabsize, unsigned int indent = 0 ) : amt( amt ), indent( indent ) {} 23 unsigned int amt; // amount 1 level increases indent by (i.e. how much to increase by in operator++) 24 unsigned int indent; 22 unsigned int indent; ///< number of spaces to indent 23 unsigned int amt; ///< spaces in one level of indentation 25 24 25 Indenter( unsigned int indent = 0, unsigned int amt = tabsize ) 26 : indent( indent*amt ), amt( amt ) {} 27 26 28 Indenter & operator+=(int nlevels) { indent += amt*nlevels; return *this; } 27 29 Indenter & operator-=(int nlevels) { indent -= amt*nlevels; return *this; } -
src/InitTweak/FixInit.cc
r67130fe rc6a1e8a 715 715 stmtsToAddBefore.push_back( new DeclStmt( ret ) ); 716 716 717 if(!stmtExpr->resultExpr) { 718 SemanticError(stmtExpr, "Statment-Expression should have a resulting expression"); 719 } 717 assertf( 718 stmtExpr->resultExpr, 719 "Statement-Expression should have a resulting expression at %s:%d", 720 stmtExpr->location.filename.c_str(), 721 stmtExpr->location.first_line 722 ); 723 720 724 ExprStmt * last = stmtExpr->resultExpr; 721 725 try { 722 726 last->expr = makeCtorDtor( "?{}", ret, last->expr ); 723 727 } catch(...) { 724 std::cerr << "=======================" << std::endl; 725 std::cerr << "ERROR, can't resolve" << std::endl; 726 ret->print(std::cerr); 727 std::cerr << std::endl << "---" << std::endl; 728 last->expr->print(std::cerr); 728 std::cerr << "*CFA internal error: "; 729 std::cerr << "can't resolve implicit constructor"; 730 std::cerr << " at " << stmtExpr->location.filename; 731 std::cerr << ":" << stmtExpr->location.first_line << std::endl; 729 732 730 733 abort(); -
src/Makefile.in
r67130fe rc6a1e8a 189 189 ResolvExpr/Alternative.$(OBJEXT) \ 190 190 ResolvExpr/AlternativeFinder.$(OBJEXT) \ 191 ResolvExpr/Candidate.$(OBJEXT) \ 192 ResolvExpr/CandidateFinder.$(OBJEXT) \ 191 193 ResolvExpr/CastCost.$(OBJEXT) ResolvExpr/CommonType.$(OBJEXT) \ 192 194 ResolvExpr/ConversionCost.$(OBJEXT) \ … … 622 624 ResolvExpr/Alternative.cc \ 623 625 ResolvExpr/AlternativeFinder.cc \ 626 ResolvExpr/Candidate.cpp \ 627 ResolvExpr/CandidateFinder.cpp \ 624 628 ResolvExpr/CastCost.cc \ 625 629 ResolvExpr/CommonType.cc \ … … 872 876 ResolvExpr/AlternativeFinder.$(OBJEXT): ResolvExpr/$(am__dirstamp) \ 873 877 ResolvExpr/$(DEPDIR)/$(am__dirstamp) 878 ResolvExpr/Candidate.$(OBJEXT): ResolvExpr/$(am__dirstamp) \ 879 ResolvExpr/$(DEPDIR)/$(am__dirstamp) 880 ResolvExpr/CandidateFinder.$(OBJEXT): ResolvExpr/$(am__dirstamp) \ 881 ResolvExpr/$(DEPDIR)/$(am__dirstamp) 874 882 ResolvExpr/CastCost.$(OBJEXT): ResolvExpr/$(am__dirstamp) \ 875 883 ResolvExpr/$(DEPDIR)/$(am__dirstamp) … … 1254 1262 @AMDEP_TRUE@@am__include@ @am__quote@ResolvExpr/$(DEPDIR)/AlternativeFinder.Po@am__quote@ 1255 1263 @AMDEP_TRUE@@am__include@ @am__quote@ResolvExpr/$(DEPDIR)/AlternativePrinter.Po@am__quote@ 1264 @AMDEP_TRUE@@am__include@ @am__quote@ResolvExpr/$(DEPDIR)/Candidate.Po@am__quote@ 1265 @AMDEP_TRUE@@am__include@ @am__quote@ResolvExpr/$(DEPDIR)/CandidateFinder.Po@am__quote@ 1256 1266 @AMDEP_TRUE@@am__include@ @am__quote@ResolvExpr/$(DEPDIR)/CastCost.Po@am__quote@ 1257 1267 @AMDEP_TRUE@@am__include@ @am__quote@ResolvExpr/$(DEPDIR)/CommonType.Po@am__quote@ -
src/ResolvExpr/AlternativeFinder.cc
r67130fe rc6a1e8a 136 136 137 137 void printAlts( const AltList &list, std::ostream &os, unsigned int indentAmt ) { 138 Indenter indent = { Indenter::tabsize,indentAmt };138 Indenter indent = { indentAmt }; 139 139 for ( AltList::const_iterator i = list.begin(); i != list.end(); ++i ) { 140 140 i->print( os, indent ); -
src/ResolvExpr/ResolveAssertions.cc
r67130fe rc6a1e8a 365 365 // fail early if any assertion is not resolvable 366 366 if ( ! resolveAssertion( assn, resn ) ) { 367 Indenter tabs{ Indenter::tabsize,3 };367 Indenter tabs{ 3 }; 368 368 std::ostringstream ss; 369 369 ss << tabs << "Unsatisfiable alternative:\n"; … … 391 391 // fail early if no mutually-compatible assertion satisfaction 392 392 if ( compatible.empty() ) { 393 Indenter tabs{ Indenter::tabsize,3 };393 Indenter tabs{ 3 }; 394 394 std::ostringstream ss; 395 395 ss << tabs << "Unsatisfiable alternative:\n"; -
src/ResolvExpr/Resolver.cc
r67130fe rc6a1e8a 21 21 #include "Alternative.h" // for Alternative, AltList 22 22 #include "AlternativeFinder.h" // for AlternativeFinder, resolveIn... 23 #include "Candidate.hpp" 24 #include "CandidateFinder.hpp" 23 25 #include "CurrentObject.h" // for CurrentObject 24 26 #include "RenameVars.h" // for RenameVars, global_renamer … … 27 29 #include "typeops.h" // for extractResultType 28 30 #include "Unify.h" // for unify 31 #include "AST/Chain.hpp" 32 #include "AST/Decl.hpp" 33 #include "AST/Init.hpp" 29 34 #include "AST/Pass.hpp" 35 #include "AST/Print.hpp" 30 36 #include "AST/SymbolTable.hpp" 31 37 #include "Common/PassVisitor.h" // for PassVisitor … … 113 119 114 120 namespace { 115 struct DeleteFinder : public WithShortCircuiting {121 struct DeleteFinder_old : public WithShortCircuiting { 116 122 DeletedExpr * delExpr = nullptr; 117 123 void previsit( DeletedExpr * expr ) { … … 127 133 128 134 DeletedExpr * findDeletedExpr( Expression * expr ) { 129 PassVisitor<DeleteFinder > finder;135 PassVisitor<DeleteFinder_old> finder; 130 136 expr->accept( finder ); 131 137 return finder.pass.delExpr; … … 133 139 134 140 namespace { 135 struct StripCasts {141 struct StripCasts_old { 136 142 Expression * postmutate( CastExpr * castExpr ) { 137 143 if ( castExpr->isGenerated && ResolvExpr::typesCompatible( castExpr->arg->result, castExpr->result, SymTab::Indexer() ) ) { … … 146 152 147 153 static void strip( Expression *& expr ) { 148 PassVisitor<StripCasts > stripper;154 PassVisitor<StripCasts_old> stripper; 149 155 expr = expr->acceptMutator( stripper ); 150 156 } … … 154 160 expr->env = oldenv ? oldenv->clone() : new TypeSubstitution; 155 161 env.makeSubstitution( *expr->env ); 156 StripCasts ::strip( expr ); // remove unnecessary casts that may be buried in an expression162 StripCasts_old::strip( expr ); // remove unnecessary casts that may be buried in an expression 157 163 } 158 164 … … 405 411 406 412 void Resolver_old::previsit( ObjectDecl * objectDecl ) { 407 // To handle initialization of routine pointers, e.g., int (*fp)(int) = foo(), means that 408 // class-variable initContext is changed multiple time because the LHS is analysed twice. 409 // The second analysis changes initContext because of a function type can contain object 410 // declarations in the return and parameter types. So each value of initContext is 413 // To handle initialization of routine pointers, e.g., int (*fp)(int) = foo(), means that 414 // class-variable initContext is changed multiple time because the LHS is analysed twice. 415 // The second analysis changes initContext because of a function type can contain object 416 // declarations in the return and parameter types. So each value of initContext is 411 417 // retained, so the type on the first analysis is preserved and used for selecting the RHS. 412 418 GuardValue( currentObject ); … … 445 451 446 452 void Resolver_old::postvisit( FunctionDecl * functionDecl ) { 447 // default value expressions have an environment which shouldn't be there and trips up 453 // default value expressions have an environment which shouldn't be there and trips up 448 454 // later passes. 449 455 // xxx - it might be necessary to somehow keep the information from this environment, but I … … 937 943 /////////////////////////////////////////////////////////////////////////// 938 944 939 class Resolver_new final 940 : public ast::WithIndexer, public ast::WithGuards, public ast::WithVisitorRef<Resolver_new>, 941 public ast::WithShortCircuiting, public ast::WithStmtsToAdd<> { 942 943 public: 945 namespace { 946 /// Finds deleted expressions in an expression tree 947 struct DeleteFinder_new final : public ast::WithShortCircuiting { 948 const ast::DeletedExpr * delExpr = nullptr; 949 950 void previsit( const ast::DeletedExpr * expr ) { 951 if ( delExpr ) { visit_children = false; } 952 else { delExpr = expr; } 953 } 954 955 void previsit( const ast::Expr * ) { 956 if ( delExpr ) { visit_children = false; } 957 } 958 }; 959 960 /// Check if this expression is or includes a deleted expression 961 const ast::DeletedExpr * findDeletedExpr( const ast::Expr * expr ) { 962 ast::Pass<DeleteFinder_new> finder; 963 expr->accept( finder ); 964 return finder.pass.delExpr; 965 } 966 967 /// Calls the CandidateFinder and finds the single best candidate 968 CandidateRef findUnfinishedKindExpression( 969 const ast::Expr * untyped, const ast::SymbolTable & symtab, const std::string & kind, 970 std::function<bool(const Candidate &)> pred, ResolvMode mode = {} 971 ) { 972 if ( ! untyped ) return nullptr; 973 974 // xxx - this isn't thread-safe, but should work until we parallelize the resolver 975 static unsigned recursion_level = 0; 976 977 ++recursion_level; 978 ast::TypeEnvironment env; 979 CandidateFinder finder{ symtab, env }; 980 finder.find( untyped, recursion_level == 1 ? mode.atTopLevel() : mode ); 981 --recursion_level; 982 983 // produce a filtered list of candidates 984 CandidateList candidates; 985 for ( auto & cand : finder.candidates ) { 986 if ( pred( *cand ) ) { candidates.emplace_back( cand ); } 987 } 988 989 // produce invalid error if no candidates 990 if ( candidates.empty() ) { 991 SemanticError( untyped, 992 toString( "No reasonable alternatives for ", kind, (kind != "" ? " " : ""), 993 "expression: ") ); 994 } 995 996 // search for cheapest candidate 997 CandidateList winners; 998 bool seen_undeleted = false; 999 for ( CandidateRef & cand : candidates ) { 1000 int c = winners.empty() ? -1 : cand->cost.compare( winners.front()->cost ); 1001 1002 if ( c > 0 ) continue; // skip more expensive than winner 1003 1004 if ( c < 0 ) { 1005 // reset on new cheapest 1006 seen_undeleted = ! findDeletedExpr( cand->expr ); 1007 winners.clear(); 1008 } else /* if ( c == 0 ) */ { 1009 if ( findDeletedExpr( cand->expr ) ) { 1010 // skip deleted expression if already seen one equivalent-cost not 1011 if ( seen_undeleted ) continue; 1012 } else if ( ! seen_undeleted ) { 1013 // replace list of equivalent-cost deleted expressions with one non-deleted 1014 winners.clear(); 1015 seen_undeleted = true; 1016 } 1017 } 1018 1019 winners.emplace_back( std::move( cand ) ); 1020 } 1021 1022 // promote candidate.cvtCost to .cost 1023 for ( CandidateRef & cand : winners ) { 1024 cand->cost = cand->cvtCost; 1025 } 1026 1027 // produce ambiguous errors, if applicable 1028 if ( winners.size() != 1 ) { 1029 std::ostringstream stream; 1030 stream << "Cannot choose between " << winners.size() << " alternatives for " 1031 << kind << (kind != "" ? " " : "") << "expression\n"; 1032 ast::print( stream, untyped ); 1033 stream << " Alternatives are:\n"; 1034 print( stream, winners, 1 ); 1035 SemanticError( untyped->location, stream.str() ); 1036 } 1037 1038 // single selected choice 1039 CandidateRef & choice = winners.front(); 1040 1041 // fail on only expression deleted 1042 if ( ! seen_undeleted ) { 1043 SemanticError( untyped->location, choice->expr.get(), "Unique best alternative " 1044 "includes deleted identifier in " ); 1045 } 1046 1047 return std::move( choice ); 1048 } 1049 1050 /// Strips extraneous casts out of an expression 1051 struct StripCasts_new final { 1052 const ast::Expr * postmutate( const ast::CastExpr * castExpr ) { 1053 if ( 1054 castExpr->isGenerated 1055 && typesCompatible( castExpr->arg->result, castExpr->result ) 1056 ) { 1057 // generated cast is the same type as its argument, remove it after keeping env 1058 ast::ptr<ast::Expr> arg = castExpr->arg; 1059 arg.get_and_mutate()->env = castExpr->env; 1060 return arg; 1061 } 1062 return castExpr; 1063 } 1064 1065 static void strip( ast::ptr< ast::Expr > & expr ) { 1066 ast::Pass< StripCasts_new > stripper; 1067 expr = expr->accept( stripper ); 1068 } 1069 }; 1070 1071 /// Establish post-resolver invariants for expressions 1072 void finishExpr( 1073 ast::ptr< ast::Expr > & expr, const ast::TypeEnvironment & env, 1074 const ast::TypeSubstitution * oldenv = nullptr 1075 ) { 1076 // set up new type substitution for expression 1077 ast::ptr< ast::TypeSubstitution > newenv = 1078 oldenv ? oldenv : new ast::TypeSubstitution{}; 1079 env.writeToSubstitution( *newenv.get_and_mutate() ); 1080 expr.get_and_mutate()->env = std::move( newenv ); 1081 // remove unncecessary casts 1082 StripCasts_new::strip( expr ); 1083 } 1084 1085 /// resolve `untyped` to the expression whose candidate satisfies `pred` with the 1086 /// lowest cost, returning the resolved version 1087 ast::ptr< ast::Expr > findKindExpression( 1088 const ast::Expr * untyped, const ast::SymbolTable & symtab, const std::string & kind, 1089 std::function<bool(const Candidate &)> pred, ResolvMode mode = {} 1090 ) { 1091 if ( ! untyped ) return {}; 1092 CandidateRef choice = 1093 findUnfinishedKindExpression( untyped, symtab, kind, pred, mode ); 1094 finishExpr( choice->expr, choice->env, untyped->env ); 1095 return std::move( choice->expr ); 1096 } 1097 1098 /// Predicate for "Candidate has integral type" 1099 bool hasIntegralType( const Candidate & i ) { 1100 const ast::Type * type = i.expr->result; 1101 1102 if ( auto bt = dynamic_cast< const ast::BasicType * >( type ) ) { 1103 return bt->isInteger(); 1104 } else if ( 1105 dynamic_cast< const ast::EnumInstType * >( type ) 1106 || dynamic_cast< const ast::ZeroType * >( type ) 1107 || dynamic_cast< const ast::OneType * >( type ) 1108 ) { 1109 return true; 1110 } else return false; 1111 } 1112 1113 /// Resolve `untyped` as an integral expression, returning the resolved version 1114 ast::ptr< ast::Expr > findIntegralExpression( 1115 const ast::Expr * untyped, const ast::SymbolTable & symtab 1116 ) { 1117 return findKindExpression( untyped, symtab, "condition", hasIntegralType ); 1118 } 1119 } 1120 1121 class Resolver_new final 1122 : public ast::WithSymbolTable, public ast::WithGuards, 1123 public ast::WithVisitorRef<Resolver_new>, public ast::WithShortCircuiting, 1124 public ast::WithStmtsToAdd<> { 1125 1126 ast::ptr< ast::Type > functionReturn = nullptr; 1127 // ast::CurrentObject currentObject = nullptr; 1128 bool inEnumDecl = false; 1129 1130 public: 944 1131 Resolver_new() = default; 945 Resolver_new( const ast::SymbolTable & syms ) { /*symtab = syms;*/}946 947 void previsit( ast::FunctionDecl * functionDecl);948 ast::DeclWithType * postvisit( ast::FunctionDecl * functionDecl);949 void previsit( ast::ObjectDecl * objectDecl);950 void previsit( ast::EnumDecl * enumDecl);951 void previsit( ast::StaticAssertDecl * assertDecl);952 953 void previsit( ast::ArrayType * at);954 void previsit( ast::PointerType * pt);955 956 void previsit( ast::ExprStmt * exprStmt);957 void previsit( ast::AsmExpr * asmExpr);958 void previsit( ast::AsmStmt * asmStmt);959 void previsit( ast::IfStmt * ifStmt);960 void previsit( ast::WhileStmt * whileStmt);961 void previsit( ast::ForStmt * forStmt);962 void previsit( ast::SwitchStmt * switchStmt);963 void previsit( ast::CaseStmt * caseStmt);964 void previsit( ast::BranchStmt * branchStmt);965 void previsit( ast::ReturnStmt * returnStmt);966 void previsit( ast::ThrowStmt * throwStmt);967 void previsit( ast::CatchStmt * catchStmt);968 void previsit( ast::WaitForStmt * stmt);969 970 void previsit( ast::SingleInit * singleInit);971 void previsit( ast::ListInit * listInit);972 void previsit( ast::ConstructorInit * ctorInit);1132 Resolver_new( const ast::SymbolTable & syms ) { symtab = syms; } 1133 1134 void previsit( const ast::FunctionDecl * ); 1135 const ast::FunctionDecl * postvisit( const ast::FunctionDecl * ); 1136 void previsit( const ast::ObjectDecl * ); 1137 void previsit( const ast::EnumDecl * ); 1138 const ast::StaticAssertDecl * previsit( const ast::StaticAssertDecl * ); 1139 1140 void previsit( const ast::ArrayType * ); 1141 void previsit( const ast::PointerType * ); 1142 1143 void previsit( const ast::ExprStmt * ); 1144 void previsit( const ast::AsmExpr * ); 1145 void previsit( const ast::AsmStmt * ); 1146 void previsit( const ast::IfStmt * ); 1147 void previsit( const ast::WhileStmt * ); 1148 void previsit( const ast::ForStmt * ); 1149 void previsit( const ast::SwitchStmt * ); 1150 void previsit( const ast::CaseStmt * ); 1151 void previsit( const ast::BranchStmt * ); 1152 void previsit( const ast::ReturnStmt * ); 1153 void previsit( const ast::ThrowStmt * ); 1154 void previsit( const ast::CatchStmt * ); 1155 void previsit( const ast::WaitForStmt * ); 1156 1157 void previsit( const ast::SingleInit * ); 1158 void previsit( const ast::ListInit * ); 1159 void previsit( const ast::ConstructorInit * ); 973 1160 }; 974 1161 … … 978 1165 } 979 1166 980 void previsit( ast::FunctionDecl * functionDecl ) { 981 #warning unimplemented; Resolver port in progress 982 (void)functionDecl; 983 assert(false); 984 } 985 986 ast::DeclWithType * postvisit( ast::FunctionDecl * functionDecl ) { 987 #warning unimplemented; Resolver port in progress 988 (void)functionDecl; 989 assert(false); 990 return nullptr; 991 } 992 993 void previsit( ast::ObjectDecl * objectDecl ) { 1167 void Resolver_new::previsit( const ast::FunctionDecl * functionDecl ) { 1168 GuardValue( functionReturn ); 1169 functionReturn = extractResultType( functionDecl->type ); 1170 } 1171 1172 const ast::FunctionDecl * Resolver_new::postvisit( const ast::FunctionDecl * functionDecl ) { 1173 // default value expressions have an environment which shouldn't be there and trips up 1174 // later passes. 1175 ast::ptr< ast::FunctionDecl > ret = functionDecl; 1176 for ( unsigned i = 0; i < functionDecl->type->params.size(); ++i ) { 1177 const ast::ptr<ast::DeclWithType> & d = functionDecl->type->params[i]; 1178 1179 if ( const ast::ObjectDecl * obj = d.as< ast::ObjectDecl >() ) { 1180 if ( const ast::SingleInit * init = obj->init.as< ast::SingleInit >() ) { 1181 if ( init->value->env == nullptr ) continue; 1182 // clone initializer minus the initializer environment 1183 ast::chain_mutate( ret ) 1184 ( &ast::FunctionDecl::type ) 1185 ( &ast::FunctionType::params )[i] 1186 ( &ast::ObjectDecl::init ) 1187 ( &ast::SingleInit::value )->env = nullptr; 1188 1189 assert( functionDecl != ret.get() || functionDecl->unique() ); 1190 assert( ! ret->type->params[i].strict_as< ast::ObjectDecl >()->init.strict_as< ast::SingleInit >()->value->env ); 1191 } 1192 } 1193 } 1194 return ret.get(); 1195 } 1196 1197 void Resolver_new::previsit( const ast::ObjectDecl * objectDecl ) { 994 1198 #warning unimplemented; Resolver port in progress 995 1199 (void)objectDecl; … … 997 1201 } 998 1202 999 void previsit( ast::EnumDecl * enumDecl ) { 1000 #warning unimplemented; Resolver port in progress 1001 (void)enumDecl; 1002 assert(false); 1003 } 1004 1005 void previsit( ast::StaticAssertDecl * assertDecl ) { 1006 #warning unimplemented; Resolver port in progress 1007 (void)assertDecl; 1008 assert(false); 1009 } 1010 1011 void previsit( ast::ArrayType * at ) { 1203 void Resolver_new::previsit( const ast::EnumDecl * ) { 1204 // in case we decide to allow nested enums 1205 GuardValue( inEnumDecl ); 1206 inEnumDecl = false; 1207 } 1208 1209 const ast::StaticAssertDecl * Resolver_new::previsit( 1210 const ast::StaticAssertDecl * assertDecl 1211 ) { 1212 ast::ptr< ast::Expr > cond = findIntegralExpression( assertDecl->cond, symtab ); 1213 if ( cond == assertDecl->cond ) return assertDecl; 1214 1215 ast::StaticAssertDecl * ret = mutate( assertDecl ); 1216 ret->cond = cond; 1217 return ret; 1218 } 1219 1220 void Resolver_new::previsit( const ast::ArrayType * at ) { 1012 1221 #warning unimplemented; Resolver port in progress 1013 1222 (void)at; … … 1015 1224 } 1016 1225 1017 void previsit(ast::PointerType * pt ) {1226 void Resolver_new::previsit( const ast::PointerType * pt ) { 1018 1227 #warning unimplemented; Resolver port in progress 1019 1228 (void)pt; … … 1021 1230 } 1022 1231 1023 void previsit(ast::ExprStmt * exprStmt ) {1232 void Resolver_new::previsit( const ast::ExprStmt * exprStmt ) { 1024 1233 #warning unimplemented; Resolver port in progress 1025 1234 (void)exprStmt; … … 1027 1236 } 1028 1237 1029 void previsit(ast::AsmExpr * asmExpr ) {1238 void Resolver_new::previsit( const ast::AsmExpr * asmExpr ) { 1030 1239 #warning unimplemented; Resolver port in progress 1031 1240 (void)asmExpr; … … 1033 1242 } 1034 1243 1035 void previsit(ast::AsmStmt * asmStmt ) {1244 void Resolver_new::previsit( const ast::AsmStmt * asmStmt ) { 1036 1245 #warning unimplemented; Resolver port in progress 1037 1246 (void)asmStmt; … … 1039 1248 } 1040 1249 1041 void previsit(ast::IfStmt * ifStmt ) {1250 void Resolver_new::previsit( const ast::IfStmt * ifStmt ) { 1042 1251 #warning unimplemented; Resolver port in progress 1043 1252 (void)ifStmt; … … 1045 1254 } 1046 1255 1047 void previsit(ast::WhileStmt * whileStmt ) {1256 void Resolver_new::previsit( const ast::WhileStmt * whileStmt ) { 1048 1257 #warning unimplemented; Resolver port in progress 1049 1258 (void)whileStmt; … … 1051 1260 } 1052 1261 1053 void previsit(ast::ForStmt * forStmt ) {1262 void Resolver_new::previsit( const ast::ForStmt * forStmt ) { 1054 1263 #warning unimplemented; Resolver port in progress 1055 1264 (void)forStmt; … … 1057 1266 } 1058 1267 1059 void previsit(ast::SwitchStmt * switchStmt ) {1268 void Resolver_new::previsit( const ast::SwitchStmt * switchStmt ) { 1060 1269 #warning unimplemented; Resolver port in progress 1061 1270 (void)switchStmt; … … 1063 1272 } 1064 1273 1065 void previsit(ast::CaseStmt * caseStmt ) {1274 void Resolver_new::previsit( const ast::CaseStmt * caseStmt ) { 1066 1275 #warning unimplemented; Resolver port in progress 1067 1276 (void)caseStmt; … … 1069 1278 } 1070 1279 1071 void previsit(ast::BranchStmt * branchStmt ) {1280 void Resolver_new::previsit( const ast::BranchStmt * branchStmt ) { 1072 1281 #warning unimplemented; Resolver port in progress 1073 1282 (void)branchStmt; … … 1075 1284 } 1076 1285 1077 void previsit(ast::ReturnStmt * returnStmt ) {1286 void Resolver_new::previsit( const ast::ReturnStmt * returnStmt ) { 1078 1287 #warning unimplemented; Resolver port in progress 1079 1288 (void)returnStmt; … … 1081 1290 } 1082 1291 1083 void previsit(ast::ThrowStmt * throwStmt ) {1292 void Resolver_new::previsit( const ast::ThrowStmt * throwStmt ) { 1084 1293 #warning unimplemented; Resolver port in progress 1085 1294 (void)throwStmt; … … 1087 1296 } 1088 1297 1089 void previsit(ast::CatchStmt * catchStmt ) {1298 void Resolver_new::previsit( const ast::CatchStmt * catchStmt ) { 1090 1299 #warning unimplemented; Resolver port in progress 1091 1300 (void)catchStmt; … … 1093 1302 } 1094 1303 1095 void previsit(ast::WaitForStmt * stmt ) {1304 void Resolver_new::previsit( const ast::WaitForStmt * stmt ) { 1096 1305 #warning unimplemented; Resolver port in progress 1097 1306 (void)stmt; … … 1099 1308 } 1100 1309 1101 void previsit(ast::SingleInit * singleInit ) {1310 void Resolver_new::previsit( const ast::SingleInit * singleInit ) { 1102 1311 #warning unimplemented; Resolver port in progress 1103 1312 (void)singleInit; … … 1105 1314 } 1106 1315 1107 void previsit(ast::ListInit * listInit ) {1316 void Resolver_new::previsit( const ast::ListInit * listInit ) { 1108 1317 #warning unimplemented; Resolver port in progress 1109 1318 (void)listInit; … … 1111 1320 } 1112 1321 1113 void previsit(ast::ConstructorInit * ctorInit ) {1322 void Resolver_new::previsit( const ast::ConstructorInit * ctorInit ) { 1114 1323 #warning unimplemented; Resolver port in progress 1115 1324 (void)ctorInit; -
src/ResolvExpr/module.mk
r67130fe rc6a1e8a 19 19 ResolvExpr/Alternative.cc \ 20 20 ResolvExpr/AlternativeFinder.cc \ 21 ResolvExpr/Candidate.cpp \ 22 ResolvExpr/CandidateFinder.cpp \ 21 23 ResolvExpr/CastCost.cc \ 22 24 ResolvExpr/CommonType.cc \ -
src/ResolvExpr/typeops.h
r67130fe rc6a1e8a 103 103 104 104 bool typesCompatible( 105 const ast::Type *, const ast::Type *, const ast::SymbolTable & ,105 const ast::Type *, const ast::Type *, const ast::SymbolTable & symtab = {}, 106 106 const ast::TypeEnvironment & env = {} ); 107 107 -
src/SynTree/BaseSyntaxNode.h
r67130fe rc6a1e8a 41 41 /// * Expressions should not finish with a newline, since the expression's parent has better information. 42 42 virtual void print( std::ostream & os, Indenter indent = {} ) const = 0; 43 void print( std::ostream & os, unsigned int indent ) {44 print( os, Indenter{ Indenter::tabsize, indent });45 }46 43 }; 47 44 -
src/SynTree/DeclReplacer.cc
r67130fe rc6a1e8a 30 30 bool debug; 31 31 public: 32 size_t replaced; 33 34 public: 32 35 DeclReplacer( const DeclMap & declMap, const TypeMap & typeMap, bool debug = false ); 33 36 … … 45 48 bool debug; 46 49 public: 50 size_t replaced; 51 52 public: 47 53 ExprDeclReplacer( const ExprMap & exprMap, bool debug = false ); 48 54 … … 52 58 } 53 59 54 voidreplace( BaseSyntaxNode * node, const DeclMap & declMap, const TypeMap & typeMap, bool debug ) {60 size_t replace( BaseSyntaxNode * node, const DeclMap & declMap, const TypeMap & typeMap, bool debug ) { 55 61 PassVisitor<DeclReplacer> replacer( declMap, typeMap, debug ); 56 62 maybeAccept( node, replacer ); 63 return replacer.pass.replaced; 57 64 } 58 65 59 voidreplace( BaseSyntaxNode * node, const DeclMap & declMap, bool debug ) {66 size_t replace( BaseSyntaxNode * node, const DeclMap & declMap, bool debug ) { 60 67 TypeMap typeMap; 61 re place( node, declMap, typeMap, debug );68 return replace( node, declMap, typeMap, debug ); 62 69 } 63 70 64 voidreplace( BaseSyntaxNode * node, const TypeMap & typeMap, bool debug ) {71 size_t replace( BaseSyntaxNode * node, const TypeMap & typeMap, bool debug ) { 65 72 DeclMap declMap; 66 re place( node, declMap, typeMap, debug );73 return replace( node, declMap, typeMap, debug ); 67 74 } 68 75 69 voidreplace( BaseSyntaxNode *& node, const ExprMap & exprMap, bool debug ) {76 size_t replace( BaseSyntaxNode *& node, const ExprMap & exprMap, bool debug ) { 70 77 PassVisitor<ExprDeclReplacer> replacer( exprMap, debug ); 71 78 node = maybeMutate( node, replacer ); 79 return replacer.pass.replaced; 72 80 } 73 81 74 82 namespace { 75 DeclReplacer::DeclReplacer( const DeclMap & declMap, const TypeMap & typeMap, bool debug ) : declMap( declMap ), typeMap( typeMap ) , debug( debug ) {}83 DeclReplacer::DeclReplacer( const DeclMap & declMap, const TypeMap & typeMap, bool debug ) : declMap( declMap ), typeMap( typeMap ) , debug( debug ), replaced( 0 ) {} 76 84 77 85 // replace variable with new node from decl map … … 79 87 // xxx - assertions and parameters aren't accounted for in this... (i.e. they aren't inserted into the map when it's made, only DeclStmts are) 80 88 if ( declMap.count( varExpr->var ) ) { 89 replaced++; 81 90 auto replacement = declMap.at( varExpr->var ); 82 91 if ( debug ) { … … 89 98 void DeclReplacer::previsit( TypeInstType * inst ) { 90 99 if ( typeMap.count( inst->baseType ) ) { 100 replaced++; 91 101 auto replacement = typeMap.at( inst->baseType ); 92 102 if ( debug ) { … … 97 107 } 98 108 99 ExprDeclReplacer::ExprDeclReplacer( const ExprMap & exprMap, bool debug ) : exprMap( exprMap ), debug( debug ) {}109 ExprDeclReplacer::ExprDeclReplacer( const ExprMap & exprMap, bool debug ) : exprMap( exprMap ), debug( debug ), replaced( 0 ) {} 100 110 101 111 Expression * ExprDeclReplacer::postmutate( VariableExpr * varExpr ) { 102 112 if ( exprMap.count( varExpr->var ) ) { 113 replaced++; 103 114 Expression * replacement = exprMap.at( varExpr->var )->clone(); 104 115 if ( debug ) { -
src/SynTree/DeclReplacer.h
r67130fe rc6a1e8a 28 28 typedef std::map< DeclarationWithType *, Expression * > ExprMap; 29 29 30 voidreplace( BaseSyntaxNode * node, const DeclMap & declMap, bool debug = false );31 voidreplace( BaseSyntaxNode * node, const TypeMap & typeMap, bool debug = false );32 voidreplace( BaseSyntaxNode * node, const DeclMap & declMap, const TypeMap & typeMap, bool debug = false );30 size_t replace( BaseSyntaxNode * node, const DeclMap & declMap, bool debug = false ); 31 size_t replace( BaseSyntaxNode * node, const TypeMap & typeMap, bool debug = false ); 32 size_t replace( BaseSyntaxNode * node, const DeclMap & declMap, const TypeMap & typeMap, bool debug = false ); 33 33 34 voidreplace( BaseSyntaxNode *& node, const ExprMap & exprMap, bool debug = false);34 size_t replace( BaseSyntaxNode *& node, const ExprMap & exprMap, bool debug = false); 35 35 template<typename T> 36 36 void replace( T *& node, const ExprMap & exprMap, bool debug = false ) { -
src/main.cc
r67130fe rc6a1e8a 10 10 // Created On : Fri May 15 23:12:02 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon Jun 3 17:35:59201913 // Update Count : 60 012 // Last Modified On : Wed Jun 5 20:35:13 2019 13 // Update Count : 601 14 14 // 15 15 … … 406 406 407 407 408 static const char optstring[] = ":hlLmNn :pP:S:twW:D:F:";408 static const char optstring[] = ":hlLmNnpP:S:twW:D:F:"; 409 409 410 410 enum { PreludeDir = 128 };
Note: See TracChangeset
for help on using the changeset viewer.