Changes in / [fba44f8:90e2334]
- Location:
- src
- Files:
-
- 13 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Common/utility.h
rfba44f8 r90e2334 246 246 } 247 247 248 // RAII object to regulate "save and restore" behaviour, e.g.249 // void Foo::bar() {250 // ValueGuard<int> guard(var); // var is a member of type Foo251 // var = ...;252 // } // var's original value is restored253 template< typename T >254 struct ValueGuard {255 T old;256 T& ref;257 258 ValueGuard(T& inRef) : old(inRef), ref(inRef) {}259 ~ValueGuard() { ref = old; }260 };261 262 248 #endif // _UTILITY_H 263 249 -
src/InitTweak/FixInit.cc
rfba44f8 r90e2334 31 31 #include "SynTree/Mutator.h" 32 32 #include "SymTab/Indexer.h" 33 #include "SymTab/Autogen.h"34 33 #include "GenPoly/PolyMutator.h" 35 34 #include "SynTree/AddStmtVisitor.h" … … 177 176 }; 178 177 179 class GenStructMemberCalls : public SymTab::Indexer {178 class WarnStructMembers : public Visitor { 180 179 public: 181 typedef Indexer Parent; 182 /// generate default/copy ctor and dtor calls for user-defined struct ctor/dtors 183 /// for any member that is missing a corresponding ctor/dtor call. 184 /// error if a member is used before constructed 185 static void generate( std::list< Declaration * > & translationUnit ); 180 typedef Visitor Parent; 181 /// warn if a user-defined constructor or destructor is missing calls for 182 /// a struct member or if a member is used before constructed 183 static void warnings( std::list< Declaration * > & translationUnit ); 186 184 187 185 virtual void visit( FunctionDecl * funcDecl ); … … 197 195 198 196 FunctionDecl * function = 0; 199 std::set< DeclarationWithType * > unhandled , usedUninit;197 std::set< DeclarationWithType * > unhandled; 200 198 ObjectDecl * thisParam = 0; 201 bool isCtor = false; // true if current function is a constructor202 };203 204 // very simple resolver-like mutator class - used to205 // resolve UntypedExprs that are found within newly206 // generated constructor/destructor calls207 class MutatingResolver : public Mutator {208 public:209 MutatingResolver( SymTab::Indexer & indexer ) : indexer( indexer ) {}210 211 virtual DeclarationWithType* mutate( ObjectDecl *objectDecl );212 213 virtual Expression* mutate( UntypedExpr *untypedExpr );214 private:215 SymTab::Indexer & indexer;216 199 }; 217 200 } // namespace … … 229 212 FixCopyCtors::fixCopyCtors( translationUnit ); 230 213 231 GenStructMemberCalls::generate( translationUnit );214 WarnStructMembers::warnings( translationUnit ); 232 215 } 233 216 … … 274 257 } 275 258 276 void GenStructMemberCalls::generate( std::list< Declaration * > & translationUnit ) { 277 GenStructMemberCalls warner; 278 acceptAll( translationUnit, warner ); 279 280 // visitor doesn't throw so that it can collect all errors 281 if ( ! warner.errors.isEmpty() ) { 282 throw warner.errors; 259 void WarnStructMembers::warnings( std::list< Declaration * > & translationUnit ) { 260 if ( true ) { // fix this condition to skip this pass if warnings aren't enabled 261 WarnStructMembers warner; 262 acceptAll( translationUnit, warner ); 263 264 // visitor doesn't throw so that it can collect all errors 265 if ( ! warner.errors.isEmpty() ) { 266 throw warner.errors; 267 } 283 268 } 284 269 } … … 551 536 FunctionDecl * dtorCaller = new FunctionDecl( objDecl->get_mangleName() + dtorCallerNamer.newName(), DeclarationNode::Static, LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ), false, false ); 552 537 dtorCaller->fixUniqueId(); 553 dtorCaller->get_statements()-> push_back( dtorStmt );538 dtorCaller->get_statements()->get_kids().push_back( dtorStmt ); 554 539 555 540 // atexit(dtor_atexit); … … 566 551 // at global scope and there could be multiple function-scoped 567 552 // static variables with the same name in different functions. 568 // Note: it isn't sufficient to modify only the mangleName, because569 // then subsequent Indexer passes can choke on seeing the object's name570 // if another object has the same name and type. An unfortunate side-effect571 // of renaming the object is that subsequent NameExprs may fail to resolve,572 // but there shouldn't be any remaining past this point.573 553 static UniqueName staticNamer( "_static_var" ); 574 objDecl->set_name( objDecl->get_name() + staticNamer.newName() ); 575 objDecl->set_mangleName( SymTab::Mangler::mangle( objDecl ) ); 554 objDecl->set_mangleName( objDecl->get_mangleName() + staticNamer.newName() ); 576 555 577 556 objDecl->set_init( NULL ); … … 738 717 } 739 718 740 void GenStructMemberCalls::visit( FunctionDecl * funcDecl ) { 741 ValueGuard< FunctionDecl * > oldFunction( funcDecl ); 742 ValueGuard< std::set< DeclarationWithType * > > oldUnhandled( unhandled ); 743 ValueGuard< std::set< DeclarationWithType * > > oldUsedUninit( usedUninit ); 744 ValueGuard< ObjectDecl * > oldThisParam( thisParam ); 745 ValueGuard< bool > oldIsCtor( isCtor ); 746 747 // need to start with fresh sets 748 unhandled.clear(); 749 usedUninit.clear(); 719 void WarnStructMembers::visit( FunctionDecl * funcDecl ) { 720 WarnStructMembers old = *this; 721 *this = WarnStructMembers(); 750 722 751 723 function = funcDecl; 752 isCtor = isConstructor( function->get_name() ); 753 if ( checkWarnings( function ) ) { 754 FunctionType * type = function->get_functionType(); 724 if ( checkWarnings( funcDecl ) ) { 725 FunctionType * type = funcDecl->get_functionType(); 755 726 assert( ! type->get_parameters().empty() ); 756 727 thisParam = safe_dynamic_cast< ObjectDecl * >( type->get_parameters().front() ); … … 768 739 } 769 740 } 770 Parent::visit( function ); 771 772 // remove the unhandled objects from usedUninit, because a call is inserted 773 // to handle them - only objects that are later constructed are used uninitialized. 774 std::set< DeclarationWithType * > diff; 775 std::set_difference( usedUninit.begin(), usedUninit.end(), unhandled.begin(), unhandled.end(), std::inserter( diff, diff.begin() ) ); 776 for ( DeclarationWithType * member : diff ) { 777 emit( "in ", CodeGen::genType( function->get_functionType(), function->get_name(), false ), ", member ", member->get_name(), " used before being constructed" ); 741 Parent::visit( funcDecl ); 742 743 for ( DeclarationWithType * member : unhandled ) { 744 // emit a warning for each unhandled member 745 emit( "in ", CodeGen::genType( function->get_functionType(), function->get_name(), false ), ", member ", member->get_name(), " may not have been ", isConstructor( funcDecl->get_name() ) ? "constructed" : "destructed" ); 778 746 } 779 747 780 if ( ! unhandled.empty() ) { 781 // need to explicitly re-add function parameters in order to resolve copy constructors 782 enterScope(); 783 // maybeAccept( function->get_functionType(), *this ); 784 for ( DeclarationWithType * member : unhandled ) { 785 // insert and resolve default/copy constructor call for each member that's unhandled 786 std::list< Statement * > stmt; 787 UntypedExpr * deref = new UntypedExpr( new NameExpr( "*?" ) ); 788 deref->get_args().push_back( new VariableExpr( thisParam ) ); 789 InitExpander srcParam( (Initializer *)NULL ); // xxx - if copy ctor, need to pass appropriate argument - second param of this function dot member 790 SymTab::genImplicitCall( srcParam, new MemberExpr( member, deref ), function->get_name(), back_inserter( stmt ), member, isCtor ); 791 792 assert( stmt.size() <= 1 ); 793 if ( stmt.size() == 1 ) { 794 Statement * callStmt = stmt.front(); 795 796 MutatingResolver resolver( *this ); 797 try { 798 // xxx - these should be in field declaration order 799 callStmt->acceptMutator( resolver ); 800 if ( isCtor ) { 801 function->get_statements()->push_front( callStmt ); 802 } else { 803 // destructor statements should be added at the end 804 function->get_statements()->push_back( callStmt ); 805 } 806 } catch ( SemanticError & error ) { 807 emit( "in ", CodeGen::genType( function->get_functionType(), function->get_name(), false ), ", member ", member->get_name(), " not explicitly ", isCtor ? "constructed" : "destructed", " and no ", isCtor ? "default constructor" : "destructor", " found" ); 808 } 809 } 810 } 811 leaveScope(); 812 } 813 } 814 815 void GenStructMemberCalls::visit( ApplicationExpr * appExpr ) { 748 // need to steal the errors before they're lost 749 old.errors.append( errors ); 750 *this = old; 751 } 752 753 void WarnStructMembers::visit( ApplicationExpr * appExpr ) { 816 754 if ( ! checkWarnings( function ) ) return; 817 755 … … 842 780 } 843 781 844 void GenStructMemberCalls::handleFirstParam( Expression * firstParam ) {782 void WarnStructMembers::handleFirstParam( Expression * firstParam ) { 845 783 using namespace std; 846 784 if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( firstParam ) ) { … … 859 797 } 860 798 861 void GenStructMemberCalls::visit( MemberExpr * memberExpr ) {799 void WarnStructMembers::visit( MemberExpr * memberExpr ) { 862 800 if ( ! checkWarnings( function ) ) return; 863 if ( ! isC tor) return;801 if ( ! isConstructor( function->get_name() ) ) return; 864 802 865 803 if ( ApplicationExpr * deref = dynamic_cast< ApplicationExpr * >( memberExpr->get_aggregate() ) ) { … … 869 807 if ( unhandled.count( memberExpr->get_member() ) ) { 870 808 // emit a warning because a member was used before it was constructed 871 usedUninit.insert( memberExpr->get_member());809 emit( "in ", CodeGen::genType( function->get_functionType(), function->get_name(), false ), ", member ", memberExpr->get_member()->get_name(), " used before being constructed" ); 872 810 } 873 811 } … … 884 822 885 823 template< typename... Params > 886 void GenStructMemberCalls::emit( const Params &... params ) {824 void WarnStructMembers::emit( const Params &... params ) { 887 825 // toggle warnings vs. errors here. 888 826 // warn( params... ); 889 827 error( *this, params... ); 890 }891 892 DeclarationWithType * MutatingResolver::mutate( ObjectDecl *objectDecl ) {893 // add object to the indexer assumes that there will be no name collisions894 // in generated code. If this changes, add mutate methods for entities with895 // scope and call {enter,leave}Scope explicitly.896 objectDecl->accept( indexer );897 return objectDecl;898 }899 900 Expression* MutatingResolver::mutate( UntypedExpr *untypedExpr ) {901 return safe_dynamic_cast< ApplicationExpr * >( ResolvExpr::findVoidExpression( untypedExpr, indexer ) );902 828 } 903 829 } // namespace -
src/Parser/ParseNode.cc
rfba44f8 r90e2334 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // ParseNode.cc -- 7 // ParseNode.cc -- 8 8 // 9 9 // Author : Rodolfo G. Esteves … … 12 12 // Last Modified On : Wed Aug 17 23:14:16 2016 13 13 // Update Count : 126 14 // 14 // 15 15 16 16 #include "ParseNode.h" … … 19 19 int ParseNode::indent_by = 4; 20 20 21 std::ostream & operator<<( std::ostream & out, const ParseNode * node ) {22 node->print( out );23 return out;24 }25 26 21 // Local Variables: // 27 22 // tab-width: 4 // -
src/Parser/ParseNode.h
rfba44f8 r90e2334 409 409 } 410 410 411 // in ParseNode.cc412 std::ostream & operator<<( std::ostream & out, const ParseNode * node );413 411 414 412 #endif // PARSENODE_H -
src/SymTab/Autogen.h
rfba44f8 r90e2334 102 102 } 103 103 104 ObjectDecl *index = new ObjectDecl( indexName.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), new SingleInit( begin, std::list<Expression*>() ) ); 104 ObjectDecl *index = new ObjectDecl( indexName.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), NULL ); 105 106 UntypedExpr *init = new UntypedExpr( new NameExpr( "?=?" ) ); 107 init->get_args().push_back( new AddressExpr( new VariableExpr( index ) ) ); 108 init->get_args().push_back( begin ); 109 index->set_init( new SingleInit( init, std::list<Expression*>() ) ); 105 110 106 111 UntypedExpr *cond = new UntypedExpr( cmp ); -
src/SynTree/Declaration.cc
rfba44f8 r90e2334 56 56 } 57 57 58 std::ostream & operator<<( std::ostream & out, constDeclaration * decl ) {58 std::ostream & operator<<( std::ostream & out, Declaration * decl ) { 59 59 decl->print( out ); 60 60 return out; -
src/SynTree/Declaration.h
rfba44f8 r90e2334 279 279 }; 280 280 281 std::ostream & operator<<( std::ostream & out, constDeclaration * decl );281 std::ostream & operator<<( std::ostream & out, Declaration * decl ); 282 282 283 283 #endif // DECLARATION_H -
src/SynTree/Expression.cc
rfba44f8 r90e2334 358 358 assert( member ); 359 359 os << std::string( indent + 2, ' ' ); 360 os << (void*)member << " "; 360 361 member->print( os, indent + 2 ); 361 362 os << std::endl; … … 540 541 } 541 542 542 std::ostream & operator<<( std::ostream & out, constExpression * expr ) {543 std::ostream & operator<<( std::ostream & out, Expression * expr ) { 543 544 expr->print( out ); 544 545 return out; -
src/SynTree/Expression.h
rfba44f8 r90e2334 653 653 }; 654 654 655 std::ostream & operator<<( std::ostream & out, constExpression * expr );655 std::ostream & operator<<( std::ostream & out, Expression * expr ); 656 656 657 657 #endif // EXPRESSION_H -
src/SynTree/Statement.cc
rfba44f8 r90e2334 387 387 } 388 388 389 std::ostream & operator<<( std::ostream & out, constStatement * statement ) {389 std::ostream & operator<<( std::ostream & out, Statement * statement ) { 390 390 statement->print( out ); 391 391 return out; -
src/SynTree/Statement.h
rfba44f8 r90e2334 47 47 48 48 std::list<Statement*>& get_kids() { return kids; } 49 void push_back( Statement * stmt ) { kids.push_back( stmt ); }50 void push_front( Statement * stmt ) { kids.push_front( stmt ); }51 49 52 50 virtual CompoundStmt *clone() const { return new CompoundStmt( *this ); } … … 397 395 398 396 399 std::ostream & operator<<( std::ostream & out, constStatement * statement );397 std::ostream & operator<<( std::ostream & out, Statement * statement ); 400 398 401 399 #endif // STATEMENT_H -
src/SynTree/Type.cc
rfba44f8 r90e2334 84 84 } 85 85 86 std::ostream & operator<<( std::ostream & out, constType * type ) {86 std::ostream & operator<<( std::ostream & out, Type * type ) { 87 87 type->print( out ); 88 88 return out; -
src/SynTree/Type.h
rfba44f8 r90e2334 481 481 } 482 482 483 std::ostream & operator<<( std::ostream & out, constType * type );483 std::ostream & operator<<( std::ostream & out, Type * type ); 484 484 485 485 #endif // TYPE_H
Note: See TracChangeset
for help on using the changeset viewer.