- Timestamp:
- Nov 4, 2020, 2:56:30 PM (5 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- eeb5023
- Parents:
- 4b30e8cc (diff), a3f5208a (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:
-
- 2 added
- 43 edited
Legend:
- Unmodified
- Added
- Removed
-
src/AST/Convert.cpp
r4b30e8cc rc28ea4e 25 25 #include "AST/Init.hpp" 26 26 #include "AST/Stmt.hpp" 27 #include "AST/TranslationUnit.hpp" 27 28 #include "AST/TypeSubstitution.hpp" 28 29 … … 47 48 48 49 //================================================================================================ 49 namespace {50 namespace ast { 50 51 51 52 // This is to preserve the FindSpecialDecls hack. It does not (and perhaps should not) 52 53 // allow us to use the same stratagy in the new ast. 54 // xxx - since convert back pass works, this concern seems to be unnecessary. 55 56 // these need to be accessed in new FixInit now 53 57 ast::Type * sizeType = nullptr; 54 58 ast::FunctionDecl * dereferenceOperator = nullptr; … … 63 67 using Cache = std::unordered_map< const ast::Node *, BaseSyntaxNode * >; 64 68 Cache cache; 69 70 // Statements can no longer be shared. 71 // however, since StmtExprResult is now implemented, need to still maintain 72 // readonly references. 73 Cache readonlyCache; 65 74 66 75 template<typename T> … … 154 163 } 155 164 156 const ast::DeclWithType * visit( const ast::ObjectDecl * node ) override final { 157 auto&& bfwd = get<Expression>().accept1( node->bitfieldWidth ); 158 auto&& type = get<Type>().accept1( node->type ); 159 auto&& init = get<Initializer>().accept1( node->init ); 160 auto&& attr = get<Attribute>().acceptL( node->attributes ); 165 const ast::DeclWithType * visit( const ast::ObjectDecl * node ) override final { 161 166 if ( inCache( node ) ) { 162 167 return nullptr; 163 168 } 169 auto bfwd = get<Expression>().accept1( node->bitfieldWidth ); 170 auto type = get<Type>().accept1( node->type ); 171 auto attr = get<Attribute>().acceptL( node->attributes ); 172 164 173 auto decl = new ObjectDecl( 165 174 node->name, … … 168 177 bfwd, 169 178 type->clone(), 170 init,179 nullptr, // prevent infinite loop 171 180 attr, 172 181 Type::FuncSpecifiers( node->funcSpec.val ) 173 182 ); 174 return declWithTypePostamble( decl, node ); 183 184 // handles the case where node->init references itself 185 // xxx - does it really happen? 186 declWithTypePostamble(decl, node); 187 auto init = get<Initializer>().accept1( node->init ); 188 decl->init = init; 189 190 this->node = decl; 191 return nullptr; 175 192 } 176 193 … … 205 222 decl->statements = get<CompoundStmt>().accept1( node->stmts ); 206 223 decl->withExprs = get<Expression>().acceptL( node->withExprs ); 207 if ( dereferenceOperator == node ) {224 if ( ast::dereferenceOperator == node ) { 208 225 Validate::dereferenceOperator = decl; 209 226 } 210 if ( dtorStructDestroy == node ) {227 if ( ast::dtorStructDestroy == node ) { 211 228 Validate::dtorStructDestroy = decl; 212 229 } … … 267 284 ); 268 285 269 if ( dtorStruct == node ) {286 if ( ast::dtorStruct == node ) { 270 287 Validate::dtorStruct = decl; 271 288 } … … 320 337 321 338 const ast::Stmt * stmtPostamble( Statement * stmt, const ast::Stmt * node ) { 322 cache.emplace( node, stmt ); 339 // force statements in old tree to be unique. 340 // cache.emplace( node, stmt ); 341 readonlyCache.emplace( node, stmt ); 323 342 stmt->location = node->location; 324 343 stmt->labels = makeLabelL( stmt, node->labels ); … … 337 356 if ( inCache( node ) ) return nullptr; 338 357 auto stmt = new ExprStmt( nullptr ); 339 cache.emplace( node, stmt );340 358 stmt->expr = get<Expression>().accept1( node->expr ); 341 359 return stmtPostamble( stmt, node ); … … 1011 1029 auto stmts = node->stmts; 1012 1030 // disable sharing between multiple StmtExprs explicitly. 1013 if (inCache(stmts)) { 1014 stmts = ast::deepCopy(stmts.get()); 1015 } 1031 // this should no longer be true. 1032 1016 1033 auto rslt = new StmtExpr( 1017 1034 get<CompoundStmt>().accept1(stmts) … … 1020 1037 rslt->returnDecls = get<ObjectDecl>().acceptL(node->returnDecls); 1021 1038 rslt->dtors = get<Expression>().acceptL(node->dtors); 1039 if (node->resultExpr) { 1040 // this MUST be found by children visit 1041 rslt->resultExpr = strict_dynamic_cast<ExprStmt *>(readonlyCache.at(node->resultExpr)); 1042 } 1022 1043 1023 1044 auto expr = visitBaseExpr( node, rslt ); … … 1036 1057 1037 1058 auto expr = visitBaseExpr( node, rslt ); 1038 this->node = expr ;1059 this->node = expr->clone(); 1039 1060 return nullptr; 1040 1061 } … … 1126 1147 auto type = new BasicType{ cv( node ), (BasicType::Kind)(unsigned)node->kind }; 1127 1148 // I believe this should always be a BasicType. 1128 if ( sizeType == node ) {1149 if ( ast::sizeType == node ) { 1129 1150 Validate::SizeType = type; 1130 1151 } … … 1384 1405 }; 1385 1406 1386 std::list< Declaration * > convert( const std::list< ast::ptr< ast::Decl > >&& translationUnit ) {1407 std::list< Declaration * > convert( const ast::TranslationUnit && translationUnit ) { 1387 1408 ConverterNewToOld c; 1388 1409 std::list< Declaration * > decls; 1389 for(auto d : translationUnit ) {1410 for(auto d : translationUnit.decls) { 1390 1411 decls.emplace_back( c.decl( d ) ); 1391 1412 } … … 1529 1550 1530 1551 // function type is now derived from parameter decls instead of storing them 1552 1553 /* 1531 1554 auto ftype = new ast::FunctionType((ast::ArgumentFlag)old->type->isVarArgs, cv(old->type)); 1532 1555 ftype->params.reserve(paramVars.size()); … … 1540 1563 } 1541 1564 ftype->forall = std::move(forall); 1542 visitType(old->type, ftype); 1565 */ 1566 1567 // can function type have attributes? seems not to be the case. 1568 // visitType(old->type, ftype); 1543 1569 1544 1570 auto decl = new ast::FunctionDecl{ … … 1546 1572 old->name, 1547 1573 // GET_ACCEPT_1(type, FunctionType), 1574 std::move(forall), 1548 1575 std::move(paramVars), 1549 1576 std::move(returnVars), … … 1552 1579 { old->linkage.val }, 1553 1580 GET_ACCEPT_V(attributes, Attribute), 1554 { old->get_funcSpec().val } 1581 { old->get_funcSpec().val }, 1582 old->type->isVarArgs 1555 1583 }; 1556 1584 1557 decl->type = ftype;1585 // decl->type = ftype; 1558 1586 cache.emplace( old, decl ); 1559 1587 … … 1570 1598 1571 1599 if ( Validate::dereferenceOperator == old ) { 1572 dereferenceOperator = decl;1600 ast::dereferenceOperator = decl; 1573 1601 } 1574 1602 1575 1603 if ( Validate::dtorStructDestroy == old ) { 1576 dtorStructDestroy = decl;1604 ast::dtorStructDestroy = decl; 1577 1605 } 1578 1606 } … … 1599 1627 1600 1628 if ( Validate::dtorStruct == old ) { 1601 dtorStruct = decl;1629 ast::dtorStruct = decl; 1602 1630 } 1603 1631 } … … 2531 2559 // I believe this should always be a BasicType. 2532 2560 if ( Validate::SizeType == old ) { 2533 sizeType = type;2561 ast::sizeType = type; 2534 2562 } 2535 2563 visitType( old, type ); … … 2776 2804 #undef GET_ACCEPT_1 2777 2805 2778 std::list< ast::ptr< ast::Decl > >convert( const std::list< Declaration * > && translationUnit ) {2806 ast::TranslationUnit convert( const std::list< Declaration * > && translationUnit ) { 2779 2807 ConverterOldToNew c; 2780 std::list< ast::ptr< ast::Decl > > decls;2808 ast::TranslationUnit unit; 2781 2809 for(auto d : translationUnit) { 2782 2810 d->accept( c ); 2783 decls.emplace_back( c.decl() );2811 unit.decls.emplace_back( c.decl() ); 2784 2812 } 2785 2813 deleteAll(translationUnit); 2786 return decls;2814 return unit; 2787 2815 } -
src/AST/Convert.hpp
r4b30e8cc rc28ea4e 18 18 #include <list> 19 19 20 #include "AST/Node.hpp"21 22 20 class Declaration; 23 21 namespace ast { 24 class Decl;22 class TranslationUnit; 25 23 }; 26 24 27 std::list< Declaration * > convert( const std::list< ast::ptr< ast::Decl > >&& translationUnit );28 std::list< ast::ptr< ast::Decl > >convert( const std::list< Declaration * > && translationUnit );25 std::list< Declaration * > convert( const ast::TranslationUnit && translationUnit ); 26 ast::TranslationUnit convert( const std::list< Declaration * > && translationUnit ); -
src/AST/Decl.cpp
r4b30e8cc rc28ea4e 48 48 49 49 // --- FunctionDecl 50 51 FunctionDecl::FunctionDecl( const CodeLocation & loc, const std::string & name, 52 std::vector<ptr<TypeDecl>>&& forall, 53 std::vector<ptr<DeclWithType>>&& params, std::vector<ptr<DeclWithType>>&& returns, 54 CompoundStmt * stmts, Storage::Classes storage, Linkage::Spec linkage, 55 std::vector<ptr<Attribute>>&& attrs, Function::Specs fs, bool isVarArgs) 56 : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), params(std::move(params)), returns(std::move(returns)), 57 stmts( stmts ) { 58 FunctionType * ftype = new FunctionType(static_cast<ArgumentFlag>(isVarArgs)); 59 for (auto & param : this->params) { 60 ftype->params.emplace_back(param->get_type()); 61 } 62 for (auto & ret : this->returns) { 63 ftype->returns.emplace_back(ret->get_type()); 64 } 65 ftype->forall = std::move(forall); 66 this->type = ftype; 67 } 68 50 69 51 70 const Type * FunctionDecl::get_type() const { return type.get(); } -
src/AST/Decl.hpp
r4b30e8cc rc28ea4e 131 131 std::vector< ptr<Expr> > withExprs; 132 132 133 FunctionDecl( const CodeLocation & loc, const std::string & name, 133 FunctionDecl( const CodeLocation & loc, const std::string & name, std::vector<ptr<TypeDecl>>&& forall, 134 134 std::vector<ptr<DeclWithType>>&& params, std::vector<ptr<DeclWithType>>&& returns, 135 135 CompoundStmt * stmts, Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::C, 136 std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {} )137 : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), params(std::move(params)), returns(std::move(returns)),138 stmts( stmts ) {}136 std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {}, bool isVarArgs = false); 137 // : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), params(std::move(params)), returns(std::move(returns)), 138 // stmts( stmts ) {} 139 139 140 140 const Type * get_type() const override; -
src/AST/DeclReplacer.cpp
r4b30e8cc rc28ea4e 38 38 const ast::TypeInstType * previsit( const ast::TypeInstType * ); 39 39 }; 40 41 struct VarExprReplacer { 42 private: 43 const ExprMap & exprMap; 44 45 public: 46 VarExprReplacer(const ExprMap & exprMap): exprMap (exprMap) {} 47 48 const Expr * postvisit (const VariableExpr *); 49 }; 40 50 } 41 51 … … 54 64 DeclMap declMap; 55 65 return replace( node, declMap, typeMap, debug ); 66 } 67 68 const ast::Node * replace( const ast::Node * node, const ExprMap & exprMap) { 69 Pass<VarExprReplacer> replacer = {exprMap}; 70 return node->accept( replacer ); 56 71 } 57 72 … … 88 103 return ninst; 89 104 } 105 106 const Expr * VarExprReplacer::postvisit( const VariableExpr * expr ) { 107 if (!exprMap.count(expr->var)) return expr; 108 109 return exprMap.at(expr->var); 110 } 111 90 112 } 91 113 } -
src/AST/DeclReplacer.hpp
r4b30e8cc rc28ea4e 23 23 class DeclWithType; 24 24 class TypeDecl; 25 class Expr; 25 26 26 27 namespace DeclReplacer { 27 28 using DeclMap = std::unordered_map< const DeclWithType *, const DeclWithType * >; 28 29 using TypeMap = std::unordered_map< const TypeDecl *, const TypeDecl * >; 30 using ExprMap = std::unordered_map< const DeclWithType *, const Expr * >; 29 31 30 32 const Node * replace( const Node * node, const DeclMap & declMap, bool debug = false ); 31 33 const Node * replace( const Node * node, const TypeMap & typeMap, bool debug = false ); 32 34 const Node * replace( const Node * node, const DeclMap & declMap, const TypeMap & typeMap, bool debug = false ); 35 const Node * replace( const Node * node, const ExprMap & exprMap); 33 36 } 34 37 } -
src/AST/Expr.cpp
r4b30e8cc rc28ea4e 67 67 // --- UntypedExpr 68 68 69 UntypedExpr * UntypedExpr::createDeref( const CodeLocation & loc, Expr * arg ) {69 UntypedExpr * UntypedExpr::createDeref( const CodeLocation & loc, const Expr * arg ) { 70 70 assert( arg ); 71 71 … … 92 92 } 93 93 94 UntypedExpr * UntypedExpr::createAssign( const CodeLocation & loc, Expr * lhs,Expr * rhs ) {94 UntypedExpr * UntypedExpr::createAssign( const CodeLocation & loc, const Expr * lhs, const Expr * rhs ) { 95 95 assert( lhs && rhs ); 96 96 … … 102 102 } 103 103 return ret; 104 } 105 106 // --- VariableExpr 107 108 VariableExpr::VariableExpr( const CodeLocation & loc ) 109 : Expr( loc ), var( nullptr ) {} 110 111 VariableExpr::VariableExpr( const CodeLocation & loc, const DeclWithType * v ) 112 : Expr( loc ), var( v ) { 113 assert( var ); 114 assert( var->get_type() ); 115 result = shallowCopy( var->get_type() ); 116 } 117 118 bool VariableExpr::get_lvalue() const { 119 // It isn't always an lvalue, but it is never an rvalue. 120 return true; 121 } 122 123 VariableExpr * VariableExpr::functionPointer( 124 const CodeLocation & loc, const FunctionDecl * decl ) { 125 // wrap usually-determined result type in a pointer 126 VariableExpr * funcExpr = new VariableExpr{ loc, decl }; 127 funcExpr->result = new PointerType{ funcExpr->result }; 128 return funcExpr; 104 129 } 105 130 … … 238 263 } 239 264 240 // --- VariableExpr241 242 VariableExpr::VariableExpr( const CodeLocation & loc )243 : Expr( loc ), var( nullptr ) {}244 245 VariableExpr::VariableExpr( const CodeLocation & loc, const DeclWithType * v )246 : Expr( loc ), var( v ) {247 assert( var );248 assert( var->get_type() );249 result = shallowCopy( var->get_type() );250 }251 252 bool VariableExpr::get_lvalue() const {253 // It isn't always an lvalue, but it is never an rvalue.254 return true;255 }256 257 VariableExpr * VariableExpr::functionPointer(258 const CodeLocation & loc, const FunctionDecl * decl ) {259 // wrap usually-determined result type in a pointer260 VariableExpr * funcExpr = new VariableExpr{ loc, decl };261 funcExpr->result = new PointerType{ funcExpr->result };262 return funcExpr;263 }264 265 265 // --- ConstantExpr 266 266 -
src/AST/Expr.hpp
r4b30e8cc rc28ea4e 226 226 227 227 /// Creates a new dereference expression 228 static UntypedExpr * createDeref( const CodeLocation & loc, Expr * arg );228 static UntypedExpr * createDeref( const CodeLocation & loc, const Expr * arg ); 229 229 /// Creates a new assignment expression 230 static UntypedExpr * createAssign( const CodeLocation & loc, Expr * lhs,Expr * rhs );230 static UntypedExpr * createAssign( const CodeLocation & loc, const Expr * lhs, const Expr * rhs ); 231 231 232 232 const Expr * accept( Visitor & v ) const override { return v.visit( this ); } … … 247 247 private: 248 248 NameExpr * clone() const override { return new NameExpr{ *this }; } 249 MUTATE_FRIEND 250 }; 251 252 /// A reference to a named variable. 253 class VariableExpr final : public Expr { 254 public: 255 readonly<DeclWithType> var; 256 257 VariableExpr( const CodeLocation & loc ); 258 VariableExpr( const CodeLocation & loc, const DeclWithType * v ); 259 260 bool get_lvalue() const final; 261 262 /// generates a function pointer for a given function 263 static VariableExpr * functionPointer( const CodeLocation & loc, const FunctionDecl * decl ); 264 265 const Expr * accept( Visitor & v ) const override { return v.visit( this ); } 266 private: 267 VariableExpr * clone() const override { return new VariableExpr{ *this }; } 249 268 MUTATE_FRIEND 250 269 }; … … 392 411 }; 393 412 394 /// A reference to a named variable.395 class VariableExpr final : public Expr {396 public:397 readonly<DeclWithType> var;398 399 VariableExpr( const CodeLocation & loc );400 VariableExpr( const CodeLocation & loc, const DeclWithType * v );401 402 bool get_lvalue() const final;403 404 /// generates a function pointer for a given function405 static VariableExpr * functionPointer( const CodeLocation & loc, const FunctionDecl * decl );406 407 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }408 private:409 VariableExpr * clone() const override { return new VariableExpr{ *this }; }410 MUTATE_FRIEND411 };412 413 413 /// A compile-time constant. 414 414 /// Mostly carries C-source text from parse to code-gen, without interpretation. E.g. strings keep their outer quotes and never have backslashes interpreted. … … 422 422 const CodeLocation & loc, const Type * ty, const std::string & r, 423 423 std::optional<unsigned long long> i ) 424 : Expr( loc, ty ), rep( r ), ival( i ) {}424 : Expr( loc, ty ), rep( r ), ival( i ), underlyer(ty) {} 425 425 426 426 /// Gets the integer value of this constant, if one is appropriate to its type. … … 617 617 618 618 ImplicitCopyCtorExpr( const CodeLocation& loc, const ApplicationExpr * call ) 619 : Expr( loc, call->result ) { assert( call); }619 : Expr( loc, call->result ), callExpr(call) { assert( call ); assert(call->result); } 620 620 621 621 const Expr * accept( Visitor & v ) const override { return v.visit( this ); } … … 742 742 std::vector<ptr<Expr>> dtors; ///< destructor(s) for return variable(s) 743 743 744 readonly<ExprStmt> resultExpr; 745 744 746 StmtExpr( const CodeLocation & loc, const CompoundStmt * ss ); 745 747 -
src/AST/Fwd.hpp
r4b30e8cc rc28ea4e 137 137 typedef unsigned int UniqueId; 138 138 139 class TranslationUnit; 140 // TODO: Get from the TranslationUnit: 141 extern Type * sizeType; 142 extern FunctionDecl * dereferenceOperator; 143 extern StructDecl * dtorStruct; 144 extern FunctionDecl * dtorStructDestroy; 145 139 146 } -
src/AST/Node.hpp
r4b30e8cc rc28ea4e 49 49 50 50 bool unique() const { return strong_count == 1; } 51 bool isManaged() const {return strong_count > 0; } 51 52 52 53 private: -
src/AST/Pass.hpp
r4b30e8cc rc28ea4e 103 103 /// Construct and run a pass on a translation unit. 104 104 template< typename... Args > 105 static void run( std::list< ptr<Decl> >& decls, Args &&... args ) {105 static void run( TranslationUnit & decls, Args &&... args ) { 106 106 Pass<core_t> visitor( std::forward<Args>( args )... ); 107 107 accept_all( decls, visitor ); … … 119 119 // Versions of the above for older compilers. 120 120 template< typename... Args > 121 static void run( std::list< ptr<Decl> >& decls ) {121 static void run( TranslationUnit & decls ) { 122 122 Pass<core_t> visitor; 123 123 accept_all( decls, visitor ); … … 228 228 template<typename core_type> 229 229 friend void accept_all( std::list< ptr<Decl> > & decls, Pass<core_type>& visitor ); 230 231 bool isInFunction() const { 232 return inFunction; 233 } 234 230 235 private: 231 236 … … 235 240 const ast::Stmt * call_accept( const ast::Stmt * ); 236 241 const ast::Expr * call_accept( const ast::Expr * ); 242 243 // requests WithStmtsToAdd directly add to this statement, as if it is a compound. 244 245 const ast::Stmt * call_accept_as_compound(const ast::Stmt *); 237 246 238 247 template< typename node_t > … … 257 266 template<typename node_t, typename parent_t, typename child_t> 258 267 void maybe_accept(const node_t * &, child_t parent_t::* child); 268 269 template<typename node_t, typename parent_t, typename child_t> 270 void maybe_accept_as_compound(const node_t * &, child_t parent_t::* child); 259 271 260 272 private: … … 284 296 private: 285 297 bool inFunction = false; 298 bool atFunctionTop = false; 286 299 }; 287 300 … … 289 302 template<typename core_t> 290 303 void accept_all( std::list< ast::ptr<ast::Decl> > &, ast::Pass<core_t> & visitor ); 304 305 template<typename core_t> 306 void accept_all( ast::TranslationUnit &, ast::Pass<core_t> & visitor ); 291 307 292 308 //------------------------------------------------------------------------------------------------- … … 371 387 struct WithVisitorRef { 372 388 Pass<core_t> * const visitor = nullptr; 389 390 bool isInFunction() const { 391 return visitor->isInFunction(); 392 } 373 393 }; 374 394 -
src/AST/Pass.impl.hpp
r4b30e8cc rc28ea4e 20 20 #include <unordered_map> 21 21 22 #include "AST/TranslationUnit.hpp" 22 23 #include "AST/TypeSubstitution.hpp" 23 24 … … 167 168 __pedantic_pass_assert( stmt ); 168 169 170 return stmt->accept( *this ); 171 } 172 173 template< typename core_t > 174 const ast::Stmt * ast::Pass< core_t >::call_accept_as_compound( const ast::Stmt * stmt ) { 175 __pedantic_pass_assert( __visit_children() ); 176 __pedantic_pass_assert( stmt ); 177 169 178 // add a few useful symbols to the scope 170 179 using __pass::empty; … … 334 343 } 335 344 345 template< typename core_t > 346 template<typename node_t, typename parent_t, typename child_t> 347 void ast::Pass< core_t >::maybe_accept_as_compound( 348 const node_t * & parent, 349 child_t parent_t::*child 350 ) { 351 static_assert( std::is_base_of<parent_t, node_t>::value, "Error deducing member object" ); 352 353 if(__pass::skip(parent->*child)) return; 354 const auto & old_val = __pass::get(parent->*child, 0); 355 356 static_assert( !std::is_same<const ast::Node * &, decltype(old_val)>::value, "ERROR"); 357 358 auto new_val = call_accept_as_compound( old_val ); 359 360 static_assert( !std::is_same<const ast::Node *, decltype(new_val)>::value || std::is_same<int, decltype(old_val)>::value, "ERROR"); 361 362 if( __pass::differs(old_val, new_val) ) { 363 auto new_parent = __pass::mutate<core_t>(parent); 364 new_parent->*child = new_val; 365 parent = new_parent; 366 } 367 } 368 336 369 337 370 template< typename core_t > … … 398 431 pass_visitor_stats.depth--; 399 432 if ( !errors.isEmpty() ) { throw errors; } 433 } 434 435 template< typename core_t > 436 inline void ast::accept_all( ast::TranslationUnit & unit, ast::Pass< core_t > & visitor ) { 437 return ast::accept_all( unit.decls, visitor ); 400 438 } 401 439 … … 470 508 // foralls are still in function type 471 509 maybe_accept( node, &FunctionDecl::type ); 472 // function body needs to have the same scope as parameters - CompoundStmt will not enter 473 // a new scope if inFunction is true 510 // First remember that we are now within a function. 474 511 ValueGuard< bool > oldInFunction( inFunction ); 475 512 inFunction = true; 513 // The function body needs to have the same scope as parameters. 514 // A CompoundStmt will not enter a new scope if atFunctionTop is true. 515 ValueGuard< bool > oldAtFunctionTop( atFunctionTop ); 516 atFunctionTop = true; 476 517 maybe_accept( node, &FunctionDecl::stmts ); 477 518 maybe_accept( node, &FunctionDecl::attributes ); … … 639 680 const ast::CompoundStmt * ast::Pass< core_t >::visit( const ast::CompoundStmt * node ) { 640 681 VISIT_START( node ); 641 VISIT( {642 // do not enter a new scope if inFunction is true - needs to check old state before the assignment643 auto guard1 = makeFuncGuard( [this, inFunctionCpy = this->inFunction]() {644 if ( ! inFunctionCpy) __pass::symtab::enter(core, 0);645 }, [this, inFunctionCpy = this->inFunction]() {646 if ( ! inFunctionCpy) __pass::symtab::leave(core, 0);682 VISIT( 683 // Do not enter (or leave) a new scope if atFunctionTop. Remember to save the result. 684 auto guard1 = makeFuncGuard( [this, enterScope = !this->atFunctionTop]() { 685 if ( enterScope ) __pass::symtab::enter(core, 0); 686 }, [this, leaveScope = !this->atFunctionTop]() { 687 if ( leaveScope ) __pass::symtab::leave(core, 0); 647 688 }); 648 ValueGuard< bool > guard2( inFunction ); 689 ValueGuard< bool > guard2( atFunctionTop ); 690 atFunctionTop = false; 649 691 guard_scope guard3 { *this }; 650 inFunction = false;651 692 maybe_accept( node, &CompoundStmt::kids ); 652 })693 ) 653 694 VISIT_END( CompoundStmt, node ); 654 695 } … … 703 744 maybe_accept( node, &IfStmt::inits ); 704 745 maybe_accept( node, &IfStmt::cond ); 705 maybe_accept ( node, &IfStmt::thenPart );706 maybe_accept ( node, &IfStmt::elsePart );746 maybe_accept_as_compound( node, &IfStmt::thenPart ); 747 maybe_accept_as_compound( node, &IfStmt::elsePart ); 707 748 }) 708 749 … … 721 762 maybe_accept( node, &WhileStmt::inits ); 722 763 maybe_accept( node, &WhileStmt::cond ); 723 maybe_accept ( node, &WhileStmt::body );764 maybe_accept_as_compound( node, &WhileStmt::body ); 724 765 }) 725 766 … … 736 777 // for statements introduce a level of scope (for the initialization) 737 778 guard_symtab guard { *this }; 779 // xxx - old ast does not create WithStmtsToAdd scope for loop inits. should revisit this later. 738 780 maybe_accept( node, &ForStmt::inits ); 739 781 maybe_accept( node, &ForStmt::cond ); 740 782 maybe_accept( node, &ForStmt::inc ); 741 maybe_accept ( node, &ForStmt::body );783 maybe_accept_as_compound( node, &ForStmt::body ); 742 784 }) 743 785 … … 834 876 maybe_accept( node, &CatchStmt::decl ); 835 877 maybe_accept( node, &CatchStmt::cond ); 836 maybe_accept ( node, &CatchStmt::body );878 maybe_accept_as_compound( node, &CatchStmt::body ); 837 879 }) 838 880 -
src/AST/Pass.proto.hpp
r4b30e8cc rc28ea4e 22 22 template<typename core_t> 23 23 class Pass; 24 25 class TranslationUnit; 24 26 25 27 struct PureVisitor; -
src/AST/SymbolTable.cpp
r4b30e8cc rc28ea4e 335 335 } 336 336 337 /* 338 void SymbolTable::addFunction Type( const FunctionType * ftype) {339 addTypes( f type->forall );340 addIds( f type->returns );341 addIds( f type->params );342 } 343 */ 337 338 void SymbolTable::addFunction( const FunctionDecl * func ) { 339 addTypes( func->type->forall ); 340 addIds( func->returns ); 341 addIds( func->params ); 342 } 343 344 344 345 345 void SymbolTable::lazyInitScope() { -
src/AST/SymbolTable.hpp
r4b30e8cc rc28ea4e 145 145 146 146 /// convenience function for adding all of the declarations in a function type to the indexer 147 // void addFunctionType( const FunctionType * ftype);147 void addFunction( const FunctionDecl * ); 148 148 149 149 private: -
src/AST/Type.cpp
r4b30e8cc rc28ea4e 157 157 158 158 template<typename decl_t> 159 SueInstType<decl_t>::SueInstType( 160 const base_type * b, std::vector<ptr<Expr>> && params, 161 CV::Qualifiers q, std::vector<ptr<Attribute>> && as ) 162 : BaseInstType( b->name, std::move(params), q, std::move(as) ), base( b ) {} 163 164 template<typename decl_t> 159 165 bool SueInstType<decl_t>::isComplete() const { 160 166 return base ? base->body : false; -
src/AST/Type.hpp
r4b30e8cc rc28ea4e 302 302 class FunctionType final : public ParameterizedType { 303 303 public: 304 // std::vector<ptr<DeclWithType>> returns;305 // std::vector<ptr<DeclWithType>> params;306 307 304 std::vector<ptr<Type>> returns; 308 305 std::vector<ptr<Type>> params; … … 345 342 : ParameterizedType(q, std::move(as)), params(), name(n) {} 346 343 344 BaseInstType( 345 const std::string& n, std::vector<ptr<Expr>> && params, 346 CV::Qualifiers q = {}, std::vector<ptr<Attribute>> && as = {} ) 347 : ParameterizedType(q, std::move(as)), params(std::move(params)), name(n) {} 348 347 349 BaseInstType( const BaseInstType & o ); 348 350 … … 369 371 370 372 SueInstType( 371 const decl_t * b, CV::Qualifiers q = {}, std::vector<ptr<Attribute>> && as = {} ); 373 const base_type * b, CV::Qualifiers q = {}, std::vector<ptr<Attribute>> && as = {} ); 374 375 SueInstType( 376 const base_type * b, std::vector<ptr<Expr>> && params, 377 CV::Qualifiers q = {}, std::vector<ptr<Attribute>> && as = {} ); 372 378 373 379 bool isComplete() const override; -
src/AST/porting.md
r4b30e8cc rc28ea4e 30 30 * Base nodes now override `const Node * accept( Visitor & v ) const = 0` with, e.g. `const Stmt * accept( Visitor & v ) const override = 0` 31 31 * `PassVisitor` is replaced with `ast::Pass` 32 * Most one shot uses can use `ast::Pass::run` and `ast::Pass::read`. 33 34 `WithConstTypeSubstitution` 35 * `env` => `typeSubs` 32 36 33 37 ## Structural Changes ## … … 146 150 * allows `newObject` as just default settings 147 151 152 `FunctionDecl` 153 * `params` and `returns` added. 154 * Contain the declarations of the parameters and return variables. 155 * Types should match (even be shared with) the fields of `type`. 156 148 157 `NamedTypeDecl` 149 158 * `parameters` => `params` … … 154 163 `AggregateDecl` 155 164 * `parameters` => `params` 165 166 `StructDecl` 167 * `makeInst` replaced by better constructor on `StructInstType`. 156 168 157 169 `Expr` … … 245 257 * **TODO** move `kind`, `typeNames` into code generator 246 258 247 `ReferenceToType` 259 `ReferenceToType` => `BaseInstType` 248 260 * deleted `get_baseParameters()` from children 249 261 * replace with `aggr() ? aggr()->params : nullptr` … … 261 273 * `returnVals` => `returns` 262 274 * `parameters` => `params` 275 * Both now just point at types. 263 276 * `bool isVarArgs;` => `enum ArgumentFlag { FixedArgs, VariableArgs }; ArgumentFlag isVarArgs;` 277 278 `SueInstType` 279 * Template class, with specializations and using to implement some other types: 280 * `StructInstType`, `UnionInstType` & `EnumInstType` 264 281 265 282 `TypeInstType` -
src/Common/PassVisitor.h
r4b30e8cc rc28ea4e 354 354 virtual TypeSubstitution * mutate( TypeSubstitution * sub ) final; 355 355 356 bool isInFunction() const { 357 return inFunction; 358 } 359 356 360 private: 357 361 bool inFunction = false; 362 bool atFunctionTop = false; 358 363 359 364 template<typename pass_t> friend void acceptAll( std::list< Declaration* > &decls, PassVisitor< pass_t >& visitor ); … … 526 531 public: 527 532 PassVisitor<pass_type> * const visitor = nullptr; 533 534 bool isInFunction() const { 535 return visitor->isInFunction(); 536 } 528 537 }; 529 538 -
src/Common/PassVisitor.impl.h
r4b30e8cc rc28ea4e 532 532 indexerAddId( &func ); 533 533 maybeAccept_impl( node->type, *this ); 534 // function body needs to have the same scope as parameters - CompoundStmt will not enter 535 // a new scope if inFunction is true 534 // First remember that we are now within a function. 536 535 ValueGuard< bool > oldInFunction( inFunction ); 537 536 inFunction = true; 537 // The function body needs to have the same scope as parameters. 538 // A CompoundStmt will not enter a new scope if atFunctionTop is true. 539 ValueGuard< bool > oldAtFunctionTop( atFunctionTop ); 540 atFunctionTop = true; 538 541 maybeAccept_impl( node->statements, *this ); 539 542 maybeAccept_impl( node->attributes, *this ); … … 567 570 indexerAddId( &func ); 568 571 maybeAccept_impl( node->type, *this ); 569 // function body needs to have the same scope as parameters - CompoundStmt will not enter 570 // a new scope if inFunction is true 572 // First remember that we are now within a function. 571 573 ValueGuard< bool > oldInFunction( inFunction ); 572 574 inFunction = true; 575 // The function body needs to have the same scope as parameters. 576 // A CompoundStmt will not enter a new scope if atFunctionTop is true. 577 ValueGuard< bool > oldAtFunctionTop( atFunctionTop ); 578 atFunctionTop = true; 573 579 maybeAccept_impl( node->statements, *this ); 574 580 maybeAccept_impl( node->attributes, *this ); … … 601 607 indexerAddId( &func ); 602 608 maybeMutate_impl( node->type, *this ); 603 // function body needs to have the same scope as parameters - CompoundStmt will not enter 604 // a new scope if inFunction is true 609 // First remember that we are now within a function. 605 610 ValueGuard< bool > oldInFunction( inFunction ); 606 611 inFunction = true; 612 // The function body needs to have the same scope as parameters. 613 // A CompoundStmt will not enter a new scope if atFunctionTop is true. 614 ValueGuard< bool > oldAtFunctionTop( atFunctionTop ); 615 atFunctionTop = true; 607 616 maybeMutate_impl( node->statements, *this ); 608 617 maybeMutate_impl( node->attributes, *this ); … … 1007 1016 VISIT_START( node ); 1008 1017 { 1009 // do not enter a new scope if inFunction is true - needs to check old state before the assignment1010 ValueGuard< bool > old InFunction( inFunction);1011 auto guard1 = makeFuncGuard( [this, &oldInFunction]() { if ( ! oldInFunction.old ) indexerScopeEnter(); }, [this, &oldInFunction]() { if ( ! oldInFunction.old) indexerScopeLeave(); } );1018 // Do not enter a new scope if atFunctionTop is true, don't leave one either. 1019 ValueGuard< bool > oldAtFunctionTop( atFunctionTop ); 1020 auto guard1 = makeFuncGuard( [this, go = !atFunctionTop]() { if ( go ) indexerScopeEnter(); }, [this, go = !atFunctionTop]() { if ( go ) indexerScopeLeave(); } ); 1012 1021 auto guard2 = makeFuncGuard( [this]() { call_beginScope(); }, [this]() { call_endScope(); } ); 1013 inFunction= false;1022 atFunctionTop = false; 1014 1023 visitStatementList( node->kids ); 1015 1024 } … … 1021 1030 VISIT_START( node ); 1022 1031 { 1023 // do not enter a new scope if inFunction is true - needs to check old state before the assignment1024 ValueGuard< bool > old InFunction( inFunction);1025 auto guard1 = makeFuncGuard( [this, &oldInFunction]() { if ( ! oldInFunction.old ) indexerScopeEnter(); }, [this, &oldInFunction]() { if ( ! oldInFunction.old) indexerScopeLeave(); } );1032 // Do not enter a new scope if atFunctionTop is true, don't leave one either. 1033 ValueGuard< bool > oldAtFunctionTop( atFunctionTop ); 1034 auto guard1 = makeFuncGuard( [this, go = !atFunctionTop]() { if ( go ) indexerScopeEnter(); }, [this, go = !atFunctionTop]() { if ( go ) indexerScopeLeave(); } ); 1026 1035 auto guard2 = makeFuncGuard( [this]() { call_beginScope(); }, [this]() { call_endScope(); } ); 1027 inFunction= false;1036 atFunctionTop = false; 1028 1037 visitStatementList( node->kids ); 1029 1038 } … … 1035 1044 MUTATE_START( node ); 1036 1045 { 1037 // do not enter a new scope if inFunction is true - needs to check old state before the assignment1038 ValueGuard< bool > old InFunction( inFunction);1039 auto guard1 = makeFuncGuard( [this, &oldInFunction]() { if ( ! oldInFunction.old ) indexerScopeEnter(); }, [this, &oldInFunction]() { if ( ! oldInFunction.old) indexerScopeLeave(); } );1046 // Do not enter a new scope if atFunctionTop is true, don't leave one either. 1047 ValueGuard< bool > oldAtFunctionTop( atFunctionTop ); 1048 auto guard1 = makeFuncGuard( [this, go = !atFunctionTop]() { if ( go ) indexerScopeEnter(); }, [this, go = !atFunctionTop]() { if ( go ) indexerScopeLeave(); } ); 1040 1049 auto guard2 = makeFuncGuard( [this]() { call_beginScope(); }, [this]() { call_endScope(); } ); 1041 inFunction= false;1050 atFunctionTop = false; 1042 1051 mutateStatementList( node->kids ); 1043 1052 } -
src/Common/utility.h
r4b30e8cc rc28ea4e 360 360 reverse_iterate_t( T & ref ) : ref(ref) {} 361 361 362 typedef typename T::reverse_iterator iterator; 363 iterator begin() { return ref.rbegin(); } 364 iterator end() { return ref.rend(); } 362 // this does NOT work on const T!!! 363 // typedef typename T::reverse_iterator iterator; 364 auto begin() { return ref.rbegin(); } 365 auto end() { return ref.rend(); } 365 366 }; 366 367 -
src/Concurrency/Keywords.cc
r4b30e8cc rc28ea4e 46 46 } 47 47 48 // Only detects threads constructed with the keyword thread. 49 inline static bool isThread( DeclarationWithType * decl ) { 50 Type * baseType = decl->get_type()->stripDeclarator(); 51 StructInstType * instType = dynamic_cast<StructInstType *>( baseType ); 52 if ( nullptr == instType ) { return false; } 53 return instType->baseStruct->is_thread(); 54 } 55 48 56 //============================================================================================= 49 57 // Pass declarations … … 119 127 "get_thread", 120 128 "thread keyword requires threads to be in scope, add #include <thread.hfa>\n", 121 " ",129 "ThreadCancelled", 122 130 true, 123 131 AggregateDecl::Thread … … 290 298 std::list<DeclarationWithType*> findMutexArgs( FunctionDecl*, bool & first ); 291 299 void validate( DeclarationWithType * ); 292 void addDtorStatments( FunctionDecl* func, CompoundStmt *, const std::list<DeclarationWithType * > &); 293 void addStatments( FunctionDecl* func, CompoundStmt *, const std::list<DeclarationWithType * > &); 300 void addDtorStatements( FunctionDecl* func, CompoundStmt *, const std::list<DeclarationWithType * > &); 301 void addStatements( FunctionDecl* func, CompoundStmt *, const std::list<DeclarationWithType * > &); 302 void addThreadDtorStatements( FunctionDecl* func, CompoundStmt * body, const std::list<DeclarationWithType * > & args ); 294 303 295 304 static void implement( std::list< Declaration * > & translationUnit ) { … … 302 311 StructDecl* guard_decl = nullptr; 303 312 StructDecl* dtor_guard_decl = nullptr; 313 StructDecl* thread_guard_decl = nullptr; 304 314 305 315 static std::unique_ptr< Type > generic_func; … … 801 811 bool first = false; 802 812 std::list<DeclarationWithType*> mutexArgs = findMutexArgs( decl, first ); 803 bool isDtor = CodeGen::isDestructor( decl->name );813 bool const isDtor = CodeGen::isDestructor( decl->name ); 804 814 805 815 // Is this function relevant to monitors … … 849 859 850 860 // Instrument the body 851 if( isDtor ) { 852 addDtorStatments( decl, body, mutexArgs ); 861 if ( isDtor && isThread( mutexArgs.front() ) ) { 862 if( !thread_guard_decl ) { 863 SemanticError( decl, "thread destructor requires threads to be in scope, add #include <thread.hfa>\n" ); 864 } 865 addThreadDtorStatements( decl, body, mutexArgs ); 866 } 867 else if ( isDtor ) { 868 addDtorStatements( decl, body, mutexArgs ); 853 869 } 854 870 else { 855 addStat ments( decl, body, mutexArgs );871 addStatements( decl, body, mutexArgs ); 856 872 } 857 873 } … … 870 886 assert( !dtor_guard_decl ); 871 887 dtor_guard_decl = decl; 888 } 889 else if( decl->name == "thread_dtor_guard_t" && decl->body ) { 890 assert( !thread_guard_decl ); 891 thread_guard_decl = decl; 872 892 } 873 893 } … … 908 928 } 909 929 910 void MutexKeyword::addDtorStat ments( FunctionDecl* func, CompoundStmt * body, const std::list<DeclarationWithType * > & args ) {930 void MutexKeyword::addDtorStatements( FunctionDecl* func, CompoundStmt * body, const std::list<DeclarationWithType * > & args ) { 911 931 Type * arg_type = args.front()->get_type()->clone(); 912 932 arg_type->set_mutex( false ); … … 957 977 958 978 //$monitor * __monitors[] = { get_monitor(a), get_monitor(b) }; 959 body->push_front( new DeclStmt( monitors) ); 960 } 961 962 void MutexKeyword::addStatments( FunctionDecl* func, CompoundStmt * body, const std::list<DeclarationWithType * > & args ) { 979 body->push_front( new DeclStmt( monitors ) ); 980 } 981 982 void MutexKeyword::addThreadDtorStatements( 983 FunctionDecl*, CompoundStmt * body, 984 const std::list<DeclarationWithType * > & args ) { 985 assert( args.size() == 1 ); 986 DeclarationWithType * arg = args.front(); 987 Type * arg_type = arg->get_type()->clone(); 988 assert( arg_type->get_mutex() ); 989 arg_type->set_mutex( false ); 990 991 // thread_dtor_guard_t __guard = { this, intptr( 0 ) }; 992 body->push_front( 993 new DeclStmt( new ObjectDecl( 994 "__guard", 995 noStorageClasses, 996 LinkageSpec::Cforall, 997 nullptr, 998 new StructInstType( 999 noQualifiers, 1000 thread_guard_decl 1001 ), 1002 new ListInit( 1003 { 1004 new SingleInit( new CastExpr( new VariableExpr( arg ), arg_type ) ), 1005 new SingleInit( new UntypedExpr( 1006 new NameExpr( "intptr" ), { 1007 new ConstantExpr( Constant::from_int( 0 ) ), 1008 } 1009 ) ), 1010 }, 1011 noDesignators, 1012 true 1013 ) 1014 )) 1015 ); 1016 } 1017 1018 void MutexKeyword::addStatements( FunctionDecl* func, CompoundStmt * body, const std::list<DeclarationWithType * > & args ) { 963 1019 ObjectDecl * monitors = new ObjectDecl( 964 1020 "__monitors", -
src/GenPoly/GenPoly.cc
r4b30e8cc rc28ea4e 46 46 } 47 47 48 bool hasPolyParams( const std::vector<ast::ptr<ast::Expr>> & params, const ast::TypeSubstitution * env) { 49 for (auto ¶m : params) { 50 auto paramType = param.strict_as<ast::TypeExpr>(); 51 if (isPolyType(paramType->type, env)) return true; 52 } 53 return false; 54 } 55 48 56 /// Checks a parameter list for polymorphic parameters from tyVars; will substitute according to env if present 49 57 bool hasPolyParams( std::list< Expression* >& params, const TyVarMap &tyVars, const TypeSubstitution *env ) { … … 56 64 } 57 65 66 bool hasPolyParams( const std::vector<ast::ptr<ast::Expr>> & params, const TyVarMap & tyVars, const ast::TypeSubstitution * env) { 67 for (auto ¶m : params) { 68 auto paramType = param.strict_as<ast::TypeExpr>(); 69 if (isPolyType(paramType->type, tyVars, env)) return true; 70 } 71 return false; 72 } 73 58 74 /// Checks a parameter list for dynamic-layout parameters from tyVars; will substitute according to env if present 59 75 bool hasDynParams( std::list< Expression* >& params, const TyVarMap &tyVars, const TypeSubstitution *env ) { … … 92 108 Type *newType = env->lookup( typeInst->get_name() ); 93 109 if ( newType ) return newType; 110 } 111 return type; 112 } 113 114 const ast::Type * replaceTypeInst(const ast::Type * type, const ast::TypeSubstitution * env) { 115 if (!env) return type; 116 if (auto typeInst = dynamic_cast<const ast::TypeInstType*> (type)) { 117 auto newType = env->lookup(typeInst->name); 118 if (newType) return newType; 94 119 } 95 120 return type; … … 111 136 } 112 137 138 const ast::Type * isPolyType(const ast::Type * type, const ast::TypeSubstitution * env) { 139 type = replaceTypeInst( type, env ); 140 141 if ( dynamic_cast< const ast::TypeInstType * >( type ) ) { 142 return type; 143 } else if ( auto arrayType = dynamic_cast< const ast::ArrayType * >( type ) ) { 144 return isPolyType( arrayType->base, env ); 145 } else if ( auto structType = dynamic_cast< const ast::StructInstType* >( type ) ) { 146 if ( hasPolyParams( structType->params, env ) ) return type; 147 } else if ( auto unionType = dynamic_cast< const ast::UnionInstType* >( type ) ) { 148 if ( hasPolyParams( unionType->params, env ) ) return type; 149 } 150 return 0; 151 } 152 113 153 Type *isPolyType( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) { 114 154 type = replaceTypeInst( type, env ); … … 126 166 } 127 167 return 0; 168 } 169 170 const ast::Type * isPolyType(const ast::Type * type, const TyVarMap & tyVars, const ast::TypeSubstitution * env) { 171 type = replaceTypeInst( type, env ); 172 173 if ( auto typeInst = dynamic_cast< const ast::TypeInstType * >( type ) ) { 174 return tyVars.find(typeInst->name) != tyVars.end() ? type : nullptr; 175 } else if ( auto arrayType = dynamic_cast< const ast::ArrayType * >( type ) ) { 176 return isPolyType( arrayType->base, env ); 177 } else if ( auto structType = dynamic_cast< const ast::StructInstType* >( type ) ) { 178 if ( hasPolyParams( structType->params, env ) ) return type; 179 } else if ( auto unionType = dynamic_cast< const ast::UnionInstType* >( type ) ) { 180 if ( hasPolyParams( unionType->params, env ) ) return type; 181 } 182 return nullptr; 128 183 } 129 184 … … 449 504 } 450 505 506 namespace { 507 // temporary hack to avoid re-implementing anything related to TyVarMap 508 // does this work? these two structs have identical definitions. 509 inline TypeDecl::Data convData(const ast::TypeDecl::Data & data) { 510 return *reinterpret_cast<const TypeDecl::Data *>(&data); 511 } 512 } 513 451 514 bool needsBoxing( Type * param, Type * arg, const TyVarMap &exprTyVars, const TypeSubstitution * env ) { 452 515 // is parameter is not polymorphic, don't need to box … … 459 522 } 460 523 524 bool needsBoxing( const ast::Type * param, const ast::Type * arg, const TyVarMap &exprTyVars, const ast::TypeSubstitution * env) { 525 // is parameter is not polymorphic, don't need to box 526 if ( ! isPolyType( param, exprTyVars ) ) return false; 527 ast::ptr<ast::Type> newType = arg; 528 if ( env ) env->apply( newType ); 529 // if the argument's type is polymorphic, we don't need to box again! 530 return ! isPolyType( newType ); 531 } 532 461 533 bool needsBoxing( Type * param, Type * arg, ApplicationExpr * appExpr, const TypeSubstitution * env ) { 462 534 FunctionType * function = getFunctionType( appExpr->function->result ); … … 467 539 } 468 540 541 bool needsBoxing( const ast::Type * param, const ast::Type * arg, const ast::ApplicationExpr * appExpr, const ast::TypeSubstitution * env) { 542 const ast::FunctionType * function = getFunctionType(appExpr->func->result); 543 assertf( function, "ApplicationExpr has non-function type: %s", toString( appExpr->func->result ).c_str() ); 544 TyVarMap exprTyVars(TypeDecl::Data{}); 545 makeTyVarMap(function, exprTyVars); 546 return needsBoxing(param, arg, exprTyVars, env); 547 548 } 549 469 550 void addToTyVarMap( TypeDecl * tyVar, TyVarMap &tyVarMap ) { 470 551 tyVarMap.insert( tyVar->name, TypeDecl::Data{ tyVar } ); 552 } 553 554 void addToTyVarMap( const ast::TypeDecl * tyVar, TyVarMap & tyVarMap) { 555 tyVarMap.insert(tyVar->name, convData(ast::TypeDecl::Data{tyVar})); 471 556 } 472 557 … … 478 563 if ( PointerType *pointer = dynamic_cast< PointerType* >( type ) ) { 479 564 makeTyVarMap( pointer->get_base(), tyVarMap ); 565 } 566 } 567 568 void makeTyVarMap(const ast::Type * type, TyVarMap & tyVarMap) { 569 if (auto ptype = dynamic_cast<const ast::ParameterizedType *>(type)) { 570 for (auto & tyVar : ptype->forall) { 571 assert (tyVar); 572 addToTyVarMap(tyVar, tyVarMap); 573 } 574 } 575 if (auto pointer = dynamic_cast<const ast::PointerType *>(type)) { 576 makeTyVarMap(pointer->base, tyVarMap); 480 577 } 481 578 } -
src/GenPoly/GenPoly.h
r4b30e8cc rc28ea4e 26 26 27 27 namespace GenPoly { 28 28 29 typedef ErasableScopedMap< std::string, TypeDecl::Data > TyVarMap; 29 30 30 /// Replaces a TypeInstType by its referrent in the environment, if applicable 31 31 Type* replaceTypeInst( Type* type, const TypeSubstitution* env ); … … 33 33 /// returns polymorphic type if is polymorphic type, NULL otherwise; will look up substitution in env if provided 34 34 Type *isPolyType( Type *type, const TypeSubstitution *env = 0 ); 35 const ast::Type * isPolyType(const ast::Type * type, const ast::TypeSubstitution * env = nullptr); 35 36 36 37 /// returns polymorphic type if is polymorphic type in tyVars, NULL otherwise; will look up substitution in env if provided 37 38 Type *isPolyType( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env = 0 ); 39 const ast::Type * isPolyType(const ast::Type * type, const TyVarMap & tyVars, const ast::TypeSubstitution * env = nullptr); 38 40 39 41 /// returns dynamic-layout type if is dynamic-layout type in tyVars, NULL otherwise; will look up substitution in env if provided … … 84 86 /// true if arg requires boxing given exprTyVars 85 87 bool needsBoxing( Type * param, Type * arg, const TyVarMap &exprTyVars, const TypeSubstitution * env ); 88 bool needsBoxing( const ast::Type * param, const ast::Type * arg, const TyVarMap &exprTyVars, const ast::TypeSubstitution * env); 86 89 87 90 /// true if arg requires boxing in the call to appExpr 88 91 bool needsBoxing( Type * param, Type * arg, ApplicationExpr * appExpr, const TypeSubstitution * env ); 92 bool needsBoxing( const ast::Type * param, const ast::Type * arg, const ast::ApplicationExpr * appExpr, const ast::TypeSubstitution * env); 89 93 90 94 /// Adds the type variable `tyVar` to `tyVarMap` … … 93 97 /// Adds the declarations in the forall list of type (and its pointed-to type if it's a pointer type) to `tyVarMap` 94 98 void makeTyVarMap( Type *type, TyVarMap &tyVarMap ); 99 void makeTyVarMap(const ast::Type * type, TyVarMap & tyVarMap); 95 100 96 101 /// Prints type variable map -
src/GenPoly/Specialize.cc
r4b30e8cc rc28ea4e 218 218 thunkFunc->get_attributes().push_back( new Attribute( "unused" ) ); 219 219 220 // Thunks at the global level must be static to avoid collisions between files. 221 // (Conversly thunks inside a function must be unique and not static.) 222 thunkFunc->storageClasses.is_static = !isInFunction(); 223 220 224 // thread thunk parameters into call to actual function, naming thunk parameters as we go 221 225 UniqueName paramNamer( paramPrefix ); -
src/InitTweak/FixGlobalInit.cc
r4b30e8cc rc28ea4e 34 34 #include "SynTree/Visitor.h" // for acceptAll, Visitor 35 35 36 #include "AST/Expr.hpp" 37 #include "AST/Node.hpp" 38 #include "AST/Pass.hpp" 39 36 40 namespace InitTweak { 37 41 class GlobalFixer : public WithShortCircuiting { … … 50 54 FunctionDecl * initFunction; 51 55 FunctionDecl * destroyFunction; 56 }; 57 58 class GlobalFixer_new : public ast::WithShortCircuiting { 59 public: 60 void previsit (const ast::ObjectDecl *); 61 void previsit (const ast::FunctionDecl *) { visit_children = false; } 62 void previsit (const ast::StructDecl *) { visit_children = false; } 63 void previsit (const ast::UnionDecl *) { visit_children = false; } 64 void previsit (const ast::EnumDecl *) { visit_children = false; } 65 void previsit (const ast::TraitDecl *) { visit_children = false; } 66 void previsit (const ast::TypeDecl *) { visit_children = false; } 67 68 std::list< ast::ptr<ast::Stmt> > initStmts; 69 std::list< ast::ptr<ast::Stmt> > destroyStmts; 52 70 }; 53 71 … … 91 109 } 92 110 111 void fixGlobalInit(ast::TranslationUnit & translationUnit, bool inLibrary) { 112 ast::Pass<GlobalFixer_new> fixer; 113 accept_all(translationUnit, fixer); 114 115 if ( !fixer.core.initStmts.empty() ) { 116 std::vector<ast::ptr<ast::Expr>> ctorParams; 117 if (inLibrary) ctorParams.emplace_back(ast::ConstantExpr::from_int({}, 200)); 118 auto initFunction = new ast::FunctionDecl({}, "__global_init__", {}, {}, {}, new ast::CompoundStmt({}, std::move(fixer.core.initStmts)), 119 ast::Storage::Static, ast::Linkage::C, {new ast::Attribute("constructor", std::move(ctorParams))}); 120 121 translationUnit.decls.emplace_back( initFunction ); 122 } // if 123 124 if ( !fixer.core.destroyStmts.empty() ) { 125 std::vector<ast::ptr<ast::Expr>> dtorParams; 126 if (inLibrary) dtorParams.emplace_back(ast::ConstantExpr::from_int({}, 200)); 127 auto destroyFunction = new ast::FunctionDecl({}, "__global_destroy__", {}, {}, {}, new ast::CompoundStmt({}, std::move(fixer.core.destroyStmts)), 128 ast::Storage::Static, ast::Linkage::C, {new ast::Attribute("destructor", std::move(dtorParams))}); 129 130 translationUnit.decls.emplace_back(destroyFunction); 131 } // if 132 } 133 93 134 void GlobalFixer::previsit( ObjectDecl *objDecl ) { 94 135 std::list< Statement * > & initStatements = initFunction->get_statements()->get_kids(); … … 112 153 } // if 113 154 if ( Statement * ctor = ctorInit->ctor ) { 114 // Translation 1: Add this attribute on the global declaration: 115 // __attribute__((section (".data#"))) 116 // which makes gcc put the global in the data section, 117 // so that the global is writeable (via a const cast) in the init function. 118 // The trailing # is an injected assembly comment, to suppress the "a" in 119 // .section .data,"a" 120 // .section .data#,"a" 121 // to avoid assembler warning "ignoring changed section attributes for .data" 122 Type *strLitT = new PointerType( Type::Qualifiers( ), 123 new BasicType( Type::Qualifiers( ), BasicType::Char ) ); 124 std::list< Expression * > attr_params; 125 attr_params.push_back( 126 new ConstantExpr( Constant( strLitT, "\".data#\"", std::nullopt ) ) ); 127 objDecl->attributes.push_back(new Attribute("section", attr_params)); 128 // Translation 2: Move the initizliation off the global declaration, 129 // into the startup function. 155 addDataSectonAttribute( objDecl ); 130 156 initStatements.push_back( ctor ); 131 157 objDecl->init = nullptr; … … 142 168 } 143 169 170 void GlobalFixer_new::previsit(const ast::ObjectDecl * objDecl) { 171 auto mutDecl = mutate(objDecl); 172 assertf(mutDecl == objDecl, "Global object decl must be unique"); 173 if ( auto ctorInit = objDecl->init.as<ast::ConstructorInit>() ) { 174 // a decision should have been made by the resolver, so ctor and init are not both non-NULL 175 assert( ! ctorInit->ctor || ! ctorInit->init ); 176 177 const ast::Stmt * dtor = ctorInit->dtor; 178 if ( dtor && ! isIntrinsicSingleArgCallStmt( dtor ) ) { 179 // don't need to call intrinsic dtor, because it does nothing, but 180 // non-intrinsic dtors must be called 181 destroyStmts.push_front( dtor ); 182 // ctorInit->dtor = nullptr; 183 } // if 184 if ( const ast::Stmt * ctor = ctorInit->ctor ) { 185 initStmts.push_back( ctor ); 186 mutDecl->init = nullptr; 187 // ctorInit->ctor = nullptr; 188 } else if ( const ast::Init * init = ctorInit->init ) { 189 mutDecl->init = init; 190 // ctorInit->init = nullptr; 191 } else { 192 // no constructor and no initializer, which is okay 193 mutDecl->init = nullptr; 194 } // if 195 // delete ctorInit; 196 } // if 197 } 198 144 199 // only modify global variables 145 200 void GlobalFixer::previsit( FunctionDecl * ) { visit_children = false; } -
src/InitTweak/FixGlobalInit.h
r4b30e8cc rc28ea4e 19 19 #include <string> // for string 20 20 21 #include <AST/Fwd.hpp> 22 23 21 24 class Declaration; 22 25 … … 26 29 /// function is for library code. 27 30 void fixGlobalInit( std::list< Declaration * > & translationUnit, bool inLibrary ); 31 void fixGlobalInit( ast::TranslationUnit & translationUnit, bool inLibrary ); 28 32 } // namespace 29 33 -
src/InitTweak/FixInit.cc
r4b30e8cc rc28ea4e 219 219 }; 220 220 221 struct SplitExpressions : public WithShortCircuiting, public WithTypeSubstitution,public WithStmtsToAdd {221 struct SplitExpressions : public WithShortCircuiting, /*public WithTypeSubstitution, */public WithStmtsToAdd { 222 222 /// add CompoundStmts around top-level expressions so that temporaries are destroyed in the correct places. 223 223 static void split( std::list< Declaration * > &translationUnit ); … … 802 802 if ( Statement * ctor = ctorInit->get_ctor() ) { 803 803 if ( objDecl->get_storageClasses().is_static ) { 804 805 // The ojbect needs to go in the data section, regardless of dtor complexity below. 806 // The attribute works, and is meant to apply, both for leaving the static local alone, 807 // and for hoisting it out as a static global. 808 addDataSectonAttribute( objDecl ); 809 804 810 // originally wanted to take advantage of gcc nested functions, but 805 811 // we get memory errors with this approach. To remedy this, the static -
src/InitTweak/FixInit.h
r4b30e8cc rc28ea4e 20 20 21 21 class Declaration; 22 namespace ast { 23 class TranslationUnit; 24 } 22 25 23 26 namespace InitTweak { 24 27 /// replace constructor initializers with expression statements and unwrap basic C-style initializers 25 28 void fix( std::list< Declaration * > & translationUnit, bool inLibrary ); 29 30 void fix( ast::TranslationUnit & translationUnit, bool inLibrary); 26 31 } // namespace 27 32 -
src/InitTweak/GenInit.cc
r4b30e8cc rc28ea4e 283 283 assert( stmts.size() <= 1 ); 284 284 return stmts.size() == 1 ? strict_dynamic_cast< ImplicitCtorDtorStmt * >( stmts.front() ) : nullptr; 285 286 } 287 288 ast::ptr<ast::Stmt> genCtorDtor (const CodeLocation & loc, const std::string & fname, const ast::ObjectDecl * objDecl, const ast::Expr * arg) { 289 assertf(objDecl, "genCtorDtor passed null objDecl"); 290 InitExpander_new srcParam(arg); 291 return SymTab::genImplicitCall(srcParam, new ast::VariableExpr(loc, objDecl), loc, fname, objDecl); 285 292 } 286 293 -
src/InitTweak/GenInit.h
r4b30e8cc rc28ea4e 33 33 /// generates a single ctor/dtor statement using objDecl as the 'this' parameter and arg as the optional argument 34 34 ImplicitCtorDtorStmt * genCtorDtor( const std::string & fname, ObjectDecl * objDecl, Expression * arg = nullptr ); 35 ast::ptr<ast::Stmt> genCtorDtor (const CodeLocation & loc, const std::string & fname, const ast::ObjectDecl * objDecl, const ast::Expr * arg = nullptr); 35 36 36 37 /// creates an appropriate ConstructorInit node which contains a constructor, destructor, and C-initializer -
src/InitTweak/InitTweak.cc
r4b30e8cc rc28ea4e 498 498 } 499 499 500 const ast::ObjectDecl * getParamThis(const ast::FunctionDecl * func) { 501 assertf( func, "getParamThis: nullptr ftype" ); 502 auto & params = func->params; 503 assertf( ! params.empty(), "getParamThis: ftype with 0 parameters: %s", toString( func ).c_str()); 504 return params.front().strict_as<ast::ObjectDecl>(); 505 } 506 500 507 bool tryConstruct( DeclarationWithType * dwt ) { 501 508 ObjectDecl * objDecl = dynamic_cast< ObjectDecl * >( dwt ); … … 511 518 } 512 519 520 bool tryConstruct( const ast::DeclWithType * dwt ) { 521 auto objDecl = dynamic_cast< const ast::ObjectDecl * >( dwt ); 522 if ( ! objDecl ) return false; 523 return (objDecl->init == nullptr || 524 ( objDecl->init != nullptr && objDecl->init->maybeConstructed )) 525 && ! objDecl->storage.is_extern 526 && isConstructable( objDecl->type ); 527 } 528 529 bool isConstructable( const ast::Type * type ) { 530 return ! dynamic_cast< const ast::VarArgsType * >( type ) && ! dynamic_cast< const ast::ReferenceType * >( type ) 531 && ! dynamic_cast< const ast::FunctionType * >( type ) && ! Tuples::isTtype( type ); 532 } 533 513 534 struct CallFinder_old { 514 535 CallFinder_old( const std::list< std::string > & names ) : names( names ) {} … … 536 557 537 558 struct CallFinder_new final { 538 std::vector< ast::ptr< ast::Expr >> matches;559 std::vector< const ast::Expr * > matches; 539 560 const std::vector< std::string > names; 540 561 … … 558 579 } 559 580 560 std::vector< ast::ptr< ast::Expr >> collectCtorDtorCalls( const ast::Stmt * stmt ) {581 std::vector< const ast::Expr * > collectCtorDtorCalls( const ast::Stmt * stmt ) { 561 582 ast::Pass< CallFinder_new > finder{ std::vector< std::string >{ "?{}", "^?{}" } }; 562 583 maybe_accept( stmt, finder ); … … 696 717 template <typename Predicate> 697 718 bool allofCtorDtor( const ast::Stmt * stmt, const Predicate & pred ) { 698 std::vector< ast::ptr< ast::Expr >> callExprs = collectCtorDtorCalls( stmt );719 std::vector< const ast::Expr * > callExprs = collectCtorDtorCalls( stmt ); 699 720 return std::all_of( callExprs.begin(), callExprs.end(), pred ); 700 721 } … … 939 960 } 940 961 962 // looks like some other such codegen uses UntypedExpr and does not create fake function. should revisit afterwards 963 // following passes may accidentally resolve this expression if returned as untyped... 964 ast::Expr * createBitwiseAssignment (const ast::Expr * dst, const ast::Expr * src) { 965 static ast::ptr<ast::FunctionDecl> assign = nullptr; 966 if (!assign) { 967 auto td = new ast::TypeDecl({}, "T", {}, nullptr, ast::TypeDecl::Dtype, true); 968 assign = new ast::FunctionDecl({}, "?=?", {}, 969 { new ast::ObjectDecl({}, "_dst", new ast::ReferenceType(new ast::TypeInstType("T", td))), 970 new ast::ObjectDecl({}, "_src", new ast::TypeInstType("T", td))}, 971 { new ast::ObjectDecl({}, "_ret", new ast::TypeInstType("T", td))}, nullptr, {}, ast::Linkage::Intrinsic); 972 } 973 if (dst->result.as<ast::ReferenceType>()) { 974 for (int depth = dst->result->referenceDepth(); depth > 0; depth--) { 975 dst = new ast::AddressExpr(dst); 976 } 977 } 978 else { 979 dst = new ast::CastExpr(dst, new ast::ReferenceType(dst->result, {})); 980 } 981 if (src->result.as<ast::ReferenceType>()) { 982 for (int depth = src->result->referenceDepth(); depth > 0; depth--) { 983 src = new ast::AddressExpr(src); 984 } 985 } 986 return new ast::ApplicationExpr(dst->location, ast::VariableExpr::functionPointer(dst->location, assign), {dst, src}); 987 } 988 941 989 struct ConstExprChecker : public WithShortCircuiting { 942 990 // most expressions are not const expr … … 1055 1103 return isCopyFunction( decl, "?{}" ); 1056 1104 } 1105 1106 void addDataSectonAttribute( ObjectDecl * objDecl ) { 1107 Type *strLitT = new PointerType( Type::Qualifiers( ), 1108 new BasicType( Type::Qualifiers( ), BasicType::Char ) ); 1109 std::list< Expression * > attr_params; 1110 attr_params.push_back( 1111 new ConstantExpr( Constant( strLitT, "\".data#\"", std::nullopt ) ) ); 1112 objDecl->attributes.push_back(new Attribute("section", attr_params)); 1113 } 1114 1057 1115 } -
src/InitTweak/InitTweak.h
r4b30e8cc rc28ea4e 38 38 /// returns the first parameter of a constructor/destructor/assignment function 39 39 ObjectDecl * getParamThis( FunctionType * ftype ); 40 const ast::ObjectDecl * getParamThis(const ast::FunctionDecl * func); 40 41 41 42 /// generate a bitwise assignment operation. 42 43 ApplicationExpr * createBitwiseAssignment( Expression * dst, Expression * src ); 44 45 ast::Expr * createBitwiseAssignment( const ast::Expr * dst, const ast::Expr * src); 43 46 44 47 /// transform Initializer into an argument list that can be passed to a call expression … … 48 51 /// True if the resolver should try to construct dwt 49 52 bool tryConstruct( DeclarationWithType * dwt ); 53 bool tryConstruct( const ast::DeclWithType * dwt ); 50 54 51 55 /// True if the type can have a user-defined constructor 52 56 bool isConstructable( Type * t ); 57 bool isConstructable( const ast::Type * t ); 53 58 54 59 /// True if the Initializer contains designations … … 79 84 /// get all Ctor/Dtor call expressions from a Statement 80 85 void collectCtorDtorCalls( Statement * stmt, std::list< Expression * > & matches ); 81 std::vector< ast::ptr< ast::Expr >> collectCtorDtorCalls( const ast::Stmt * stmt );86 std::vector< const ast::Expr * > collectCtorDtorCalls( const ast::Stmt * stmt ); 82 87 83 88 /// get the Ctor/Dtor call expression from a Statement that looks like a generated ctor/dtor call … … 102 107 bool isConstExpr( Expression * expr ); 103 108 bool isConstExpr( Initializer * init ); 109 110 /// Modifies objDecl to have: 111 /// __attribute__((section (".data#"))) 112 /// which makes gcc put the declared variable in the data section, 113 /// which is helpful for global constants on newer gcc versions, 114 /// so that CFA's generated initialization won't segfault when writing it via a const cast. 115 /// The trailing # is an injected assembly comment, to suppress the "a" in 116 /// .section .data,"a" 117 /// .section .data#,"a" 118 /// to avoid assembler warning "ignoring changed section attributes for .data" 119 void addDataSectonAttribute( ObjectDecl * objDecl ); 104 120 105 121 class InitExpander_old { -
src/InitTweak/module.mk
r4b30e8cc rc28ea4e 23 23 InitTweak/GenInit.h \ 24 24 InitTweak/InitTweak.cc \ 25 InitTweak/InitTweak.h 25 InitTweak/InitTweak.h \ 26 InitTweak/FixInitNew.cpp 26 27 27 28 SRCDEMANGLE += \ -
src/Parser/ParseNode.h
r4b30e8cc rc28ea4e 10 10 // Created On : Sat May 16 13:28:16 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon Jul 6 09:33:32202013 // Update Count : 89 212 // Last Modified On : Sat Oct 24 03:53:54 2020 13 // Update Count : 895 14 14 // 15 15 … … 205 205 struct TypeData; 206 206 207 class DeclarationNode : public ParseNode { 208 public: 207 struct DeclarationNode : public ParseNode { 209 208 // These enumerations must harmonize with their names in DeclarationNode.cc. 210 209 enum BasicType { Void, Bool, Char, Int, Int128, … … 304 303 bool get_inLine() const { return inLine; } 305 304 DeclarationNode * set_inLine( bool inL ) { inLine = inL; return this; } 306 public: 305 307 306 DeclarationNode * get_last() { return (DeclarationNode *)ParseNode::get_last(); } 308 307 … … 360 359 //############################################################################## 361 360 362 class StatementNode final : public ParseNode { 363 public: 361 struct StatementNode final : public ParseNode { 364 362 StatementNode() { stmt = nullptr; } 365 363 StatementNode( Statement * stmt ) : stmt( stmt ) {} … … 382 380 os << stmt.get() << std::endl; 383 381 } 384 private: 382 385 383 std::unique_ptr<Statement> stmt; 386 384 }; // StatementNode … … 426 424 Statement * build_finally( StatementNode * stmt ); 427 425 Statement * build_compound( StatementNode * first ); 426 StatementNode * maybe_build_compound( StatementNode * first ); 428 427 Statement * build_asm( bool voltile, Expression * instruction, ExpressionNode * output = nullptr, ExpressionNode * input = nullptr, ExpressionNode * clobber = nullptr, LabelNode * gotolabels = nullptr ); 429 428 Statement * build_directive( std::string * directive ); -
src/Parser/StatementNode.cc
r4b30e8cc rc28ea4e 10 10 // Created On : Sat May 16 14:59:41 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sat Aug 4 09:39:25 201813 // Update Count : 3 6312 // Last Modified On : Sat Oct 24 04:20:55 2020 13 // Update Count : 383 14 14 // 15 15 … … 345 345 } // build_compound 346 346 347 // A single statement in a control structure is always converted to a compound statement so subsequent generated code 348 // can be placed within this compound statement. Otherwise, code generation has to constantly check for a single 349 // statement and wrap it into a compound statement to insert additional code. Hence, all control structures have a 350 // conical form for code generation. 351 StatementNode * maybe_build_compound( StatementNode * first ) { 352 // Optimization: if the control-structure statement is a compound statement, do not wrap it. 353 // e.g., if (...) {...} do not wrap the existing compound statement. 354 if ( ! dynamic_cast<CompoundStmt *>( first->stmt.get() ) ) { // unique_ptr 355 CompoundStmt * cs = new CompoundStmt(); 356 buildMoveList( first, cs->get_kids() ); 357 return new StatementNode( cs ); 358 } // if 359 return first; 360 } // maybe_build_compound 361 347 362 Statement * build_asm( bool voltile, Expression * instruction, ExpressionNode * output, ExpressionNode * input, ExpressionNode * clobber, LabelNode * gotolabels ) { 348 363 std::list< Expression * > out, in; -
src/Parser/parser.yy
r4b30e8cc rc28ea4e 10 10 // Created On : Sat Sep 1 20:22:55 2001 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Oct 9 18:09:09202013 // Update Count : 46 1412 // Last Modified On : Sat Oct 24 08:21:14 2020 13 // Update Count : 4624 14 14 // 15 15 … … 1080 1080 IF '(' if_control_expression ')' statement %prec THEN 1081 1081 // explicitly deal with the shift/reduce conflict on if/else 1082 { $$ = new StatementNode( build_if( $3, $5, nullptr ) ); }1082 { $$ = new StatementNode( build_if( $3, maybe_build_compound( $5 ), nullptr ) ); } 1083 1083 | IF '(' if_control_expression ')' statement ELSE statement 1084 { $$ = new StatementNode( build_if( $3, $5, $7) ); }1084 { $$ = new StatementNode( build_if( $3, maybe_build_compound( $5 ), maybe_build_compound( $7 ) ) ); } 1085 1085 ; 1086 1086 … … 1130 1130 1131 1131 case_clause: // CFA 1132 case_label_list statement { $$ = $1->append_last_case( new StatementNode( build_compound( $2 )) ); }1132 case_label_list statement { $$ = $1->append_last_case( maybe_build_compound( $2 ) ); } 1133 1133 ; 1134 1134 … … 1148 1148 iteration_statement: 1149 1149 WHILE '(' push if_control_expression ')' statement pop 1150 { $$ = new StatementNode( build_while( $4, $6) ); }1150 { $$ = new StatementNode( build_while( $4, maybe_build_compound( $6 ) ) ); } 1151 1151 | WHILE '(' ')' statement // CFA => while ( 1 ) 1152 { $$ = new StatementNode( build_while( new IfCtrl( nullptr, new ExpressionNode( build_constantInteger( *new string( "1" ) ) ) ), $4) ); }1152 { $$ = new StatementNode( build_while( new IfCtrl( nullptr, new ExpressionNode( build_constantInteger( *new string( "1" ) ) ) ), maybe_build_compound( $4 ) ) ); } 1153 1153 | DO statement WHILE '(' comma_expression ')' ';' 1154 { $$ = new StatementNode( build_do_while( $5, $2) ); }1154 { $$ = new StatementNode( build_do_while( $5, maybe_build_compound( $2 ) ) ); } 1155 1155 | DO statement WHILE '(' ')' ';' // CFA => do while( 1 ) 1156 { $$ = new StatementNode( build_do_while( new ExpressionNode( build_constantInteger( *new string( "1" ) ) ), $2) ); }1156 { $$ = new StatementNode( build_do_while( new ExpressionNode( build_constantInteger( *new string( "1" ) ) ), maybe_build_compound( $2 ) ) ); } 1157 1157 | FOR '(' push for_control_expression_list ')' statement pop 1158 { $$ = new StatementNode( build_for( $4, $6) ); }1158 { $$ = new StatementNode( build_for( $4, maybe_build_compound( $6 ) ) ); } 1159 1159 | FOR '(' ')' statement // CFA => for ( ;; ) 1160 { $$ = new StatementNode( build_for( new ForCtrl( (ExpressionNode * )nullptr, (ExpressionNode * )nullptr, (ExpressionNode * )nullptr ), $4) ); }1160 { $$ = new StatementNode( build_for( new ForCtrl( (ExpressionNode * )nullptr, (ExpressionNode * )nullptr, (ExpressionNode * )nullptr ), maybe_build_compound( $4 ) ) ); } 1161 1161 ; 1162 1162 … … 1341 1341 waitfor_clause: 1342 1342 when_clause_opt waitfor statement %prec THEN 1343 { $$ = build_waitfor( $2, $3, $1 ); }1343 { $$ = build_waitfor( $2, maybe_build_compound( $3 ), $1 ); } 1344 1344 | when_clause_opt waitfor statement WOR waitfor_clause 1345 { $$ = build_waitfor( $2, $3, $1, $5 ); }1345 { $$ = build_waitfor( $2, maybe_build_compound( $3 ), $1, $5 ); } 1346 1346 | when_clause_opt timeout statement %prec THEN 1347 { $$ = build_waitfor_timeout( $2, $3, $1 ); }1347 { $$ = build_waitfor_timeout( $2, maybe_build_compound( $3 ), $1 ); } 1348 1348 | when_clause_opt ELSE statement 1349 { $$ = build_waitfor_timeout( nullptr, $3, $1 ); }1349 { $$ = build_waitfor_timeout( nullptr, maybe_build_compound( $3 ), $1 ); } 1350 1350 // "else" must be conditional after timeout or timeout is never triggered (i.e., it is meaningless) 1351 1351 | when_clause_opt timeout statement WOR ELSE statement 1352 1352 { SemanticError( yylloc, "else clause must be conditional after timeout or timeout never triggered." ); $$ = nullptr; } 1353 1353 | when_clause_opt timeout statement WOR when_clause ELSE statement 1354 { $$ = build_waitfor_timeout( $2, $3, $1, $7, $5 ); }1354 { $$ = build_waitfor_timeout( $2, maybe_build_compound( $3 ), $1, maybe_build_compound( $7 ), $5 ); } 1355 1355 ; 1356 1356 -
src/ResolvExpr/Resolver.cc
r4b30e8cc rc28ea4e 1105 1105 } 1106 1106 1107 /// Establish post-resolver invariants for expressions 1107 1108 } // anonymous namespace 1109 /// Establish post-resolver invariants for expressions 1108 1110 void finishExpr( 1109 1111 ast::ptr< ast::Expr > & expr, const ast::TypeEnvironment & env, … … 1118 1120 StripCasts_new::strip( expr ); 1119 1121 } 1120 } // anonymous namespace1121 1122 1122 1123 1123 ast::ptr< ast::Expr > resolveInVoidContext( … … 1139 1139 } 1140 1140 1141 namespace { 1142 /// Resolve `untyped` to the expression whose candidate is the best match for a `void` 1141 /// Resolve `untyped` to the expression whose candidate is the best match for a `void` 1143 1142 /// context. 1144 1143 ast::ptr< ast::Expr > findVoidExpression( … … 1151 1150 return newExpr; 1152 1151 } 1152 1153 namespace { 1154 1153 1155 1154 1156 /// resolve `untyped` to the expression whose candidate satisfies `pred` with the … … 1162 1164 CandidateRef choice = 1163 1165 findUnfinishedKindExpression( untyped, symtab, kind, pred, mode ); 1164 finishExpr( choice->expr, choice->env, untyped->env );1166 ResolvExpr::finishExpr( choice->expr, choice->env, untyped->env ); 1165 1167 return std::move( choice->expr ); 1166 1168 } … … 1272 1274 // size_t Resolver_new::traceId = Stats::Heap::new_stacktrace_id("Resolver"); 1273 1275 1274 void resolve( std::list< ast::ptr< ast::Decl > >& translationUnit ) {1276 void resolve( ast::TranslationUnit& translationUnit ) { 1275 1277 ast::Pass< Resolver_new >::run( translationUnit ); 1276 1278 } -
src/ResolvExpr/Resolver.h
r4b30e8cc rc28ea4e 35 35 class StmtExpr; 36 36 class SymbolTable; 37 class TranslationUnit; 37 38 class Type; 38 39 class TypeEnvironment; … … 55 56 56 57 /// Checks types and binds syntactic constructs to typed representations 57 void resolve( std::list< ast::ptr<ast::Decl> >& translationUnit );58 void resolve( ast::TranslationUnit& translationUnit ); 58 59 /// Searches expr and returns the first DeletedExpr found, otherwise nullptr 59 60 const ast::DeletedExpr * findDeletedExpr( const ast::Expr * expr ); … … 66 67 ast::ptr< ast::Expr > findSingleExpression( 67 68 const ast::Expr * untyped, const ast::Type * type, const ast::SymbolTable & symtab ); 69 ast::ptr< ast::Expr > findVoidExpression( 70 const ast::Expr * untyped, const ast::SymbolTable & symtab); 68 71 /// Resolves a constructor init expression 69 ast::ptr< ast::Init > resolveCtorInit( 72 ast::ptr< ast::Init > resolveCtorInit( 70 73 const ast::ConstructorInit * ctorInit, const ast::SymbolTable & symtab ); 71 74 /// Resolves a statement expression 72 ast::ptr< ast::Expr > resolveStmtExpr( 75 ast::ptr< ast::Expr > resolveStmtExpr( 73 76 const ast::StmtExpr * stmtExpr, const ast::SymbolTable & symtab ); 74 77 } // namespace ResolvExpr -
src/SymTab/Autogen.cc
r4b30e8cc rc28ea4e 233 233 } 234 234 235 // shallow copy the pointer list for return 236 std::vector<ast::ptr<ast::TypeDecl>> getGenericParams (const ast::Type * t) { 237 if (auto structInst = dynamic_cast<const ast::StructInstType*>(t)) { 238 return structInst->base->params; 239 } 240 if (auto unionInst = dynamic_cast<const ast::UnionInstType*>(t)) { 241 return unionInst->base->params; 242 } 243 return {}; 244 } 245 235 246 /// given type T, generate type of default ctor/dtor, i.e. function type void (*) (T *) 236 247 FunctionType * genDefaultType( Type * paramType, bool maybePolymorphic ) { … … 244 255 ftype->parameters.push_back( dstParam ); 245 256 return ftype; 257 } 258 259 /// 260 ast::FunctionDecl * genDefaultFunc(const CodeLocation loc, const std::string fname, const ast::Type * paramType, bool maybePolymorphic) { 261 std::vector<ast::ptr<ast::TypeDecl>> typeParams; 262 if (maybePolymorphic) typeParams = getGenericParams(paramType); 263 auto dstParam = new ast::ObjectDecl(loc, "_dst", new ast::ReferenceType(paramType), nullptr, {}, ast::Linkage::Cforall); 264 return new ast::FunctionDecl(loc, fname, std::move(typeParams), {dstParam}, {}, new ast::CompoundStmt(loc)); 246 265 } 247 266 -
src/SymTab/Autogen.h
r4b30e8cc rc28ea4e 55 55 /// maybePolymorphic is true if the resulting FunctionType is allowed to be polymorphic 56 56 FunctionType * genDefaultType( Type * paramType, bool maybePolymorphic = true ); 57 58 ast::FunctionDecl * genDefaultFunc(const CodeLocation loc, const std::string fname, const ast::Type * paramType, bool maybePolymorphic = true); 57 59 58 60 /// generate the type of a copy constructor for paramType. -
src/SynTree/Expression.h
r4b30e8cc rc28ea4e 163 163 }; 164 164 165 /// VariableExpr represents an expression that simply refers to the value of a named variable. 166 /// Does not take ownership of var. 167 class VariableExpr : public Expression { 168 public: 169 DeclarationWithType * var; 170 171 VariableExpr(); 172 VariableExpr( DeclarationWithType * var ); 173 VariableExpr( const VariableExpr & other ); 174 virtual ~VariableExpr(); 175 176 bool get_lvalue() const final; 177 178 DeclarationWithType * get_var() const { return var; } 179 void set_var( DeclarationWithType * newValue ) { var = newValue; } 180 181 static VariableExpr * functionPointer( FunctionDecl * decl ); 182 183 virtual VariableExpr * clone() const override { return new VariableExpr( * this ); } 184 virtual void accept( Visitor & v ) override { v.visit( this ); } 185 virtual void accept( Visitor & v ) const override { v.visit( this ); } 186 virtual Expression * acceptMutator( Mutator & m ) override { return m.mutate( this ); } 187 virtual void print( std::ostream & os, Indenter indent = {} ) const override; 188 }; 189 165 190 // The following classes are used to represent expression types that cannot be converted into 166 191 // function-call format. … … 329 354 }; 330 355 331 /// VariableExpr represents an expression that simply refers to the value of a named variable.332 /// Does not take ownership of var.333 class VariableExpr : public Expression {334 public:335 DeclarationWithType * var;336 337 VariableExpr();338 VariableExpr( DeclarationWithType * var );339 VariableExpr( const VariableExpr & other );340 virtual ~VariableExpr();341 342 bool get_lvalue() const final;343 344 DeclarationWithType * get_var() const { return var; }345 void set_var( DeclarationWithType * newValue ) { var = newValue; }346 347 static VariableExpr * functionPointer( FunctionDecl * decl );348 349 virtual VariableExpr * clone() const override { return new VariableExpr( * this ); }350 virtual void accept( Visitor & v ) override { v.visit( this ); }351 virtual void accept( Visitor & v ) const override { v.visit( this ); }352 virtual Expression * acceptMutator( Mutator & m ) override { return m.mutate( this ); }353 virtual void print( std::ostream & os, Indenter indent = {} ) const override;354 };355 356 356 /// ConstantExpr represents an expression that simply refers to the value of a constant 357 357 class ConstantExpr : public Expression { -
src/main.cc
r4b30e8cc rc28ea4e 343 343 auto transUnit = convert( move( translationUnit ) ); 344 344 PASS( "Resolve", ResolvExpr::resolve( transUnit ) ); 345 if ( exprp ) { 346 translationUnit = convert( move( transUnit ) ); 347 dump( translationUnit ); 348 return EXIT_SUCCESS; 349 } // if 350 351 PASS( "Fix Init", InitTweak::fix(transUnit, buildingLibrary())); 345 352 translationUnit = convert( move( transUnit ) ); 346 353 } else { 347 354 PASS( "Resolve", ResolvExpr::resolve( translationUnit ) ); 355 if ( exprp ) { 356 dump( translationUnit ); 357 return EXIT_SUCCESS; 358 } 359 360 PASS( "Fix Init", InitTweak::fix( translationUnit, buildingLibrary() ) ); 348 361 } 349 362 350 if ( exprp ) {351 dump( translationUnit );352 return EXIT_SUCCESS;353 } // if354 355 363 // fix ObjectDecl - replaces ConstructorInit nodes 356 PASS( "Fix Init", InitTweak::fix( translationUnit, buildingLibrary() ) );357 364 if ( ctorinitp ) { 358 365 dump ( translationUnit );
Note:
See TracChangeset
for help on using the changeset viewer.