From: Baptiste L. <bl...@us...> - 2004-06-20 11:01:02
|
Update of /cvsroot/cpptool/CppParser/examples/parser In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv17967/examples/parser Modified Files: location.h project.h scope.cpp scope.h symboldeclarator.cpp symboldeclarator.h symboltable.cpp symboltable.h symboltabletest.cpp testproject.cpp testproject.h Log Message: * added support for anonymous namespace Index: testproject.h =================================================================== RCS file: /cvsroot/cpptool/CppParser/examples/parser/testproject.h,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -C2 -d -r1.1.1.1 -r1.2 *** testproject.h 8 Jun 2004 20:23:18 -0000 1.1.1.1 --- testproject.h 20 Jun 2004 11:00:38 -0000 1.2 *************** *** 21,25 **** std::string readSourceFile( const std::string &path ); ! std::string sourcePath( Parser::FileId file ); Parser::SourceEditor &editSource( Parser::FileId file ); --- 21,27 ---- std::string readSourceFile( const std::string &path ); ! Parser::FileId fileIdFromPath( const std::string &path ); ! ! std::string pathFromFileId( Parser::FileId file ); Parser::SourceEditor &editSource( Parser::FileId file ); Index: scope.h =================================================================== RCS file: /cvsroot/cpptool/CppParser/examples/parser/scope.h,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** scope.h 14 Jun 2004 23:08:11 -0000 1.2 --- scope.h 20 Jun 2004 11:00:38 -0000 1.3 *************** *** 12,15 **** --- 12,16 ---- /// @todo: there is a cycle between SymbolDeclaration & Scope which cause memory not to be freed. + class AnonymousNamespaceScope; class BlockScope; class ClassScope; *************** *** 20,27 **** class TypedefScope; typedef boost::shared_ptr<ClassScope> ClassScopePtr; typedef boost::shared_ptr<NamespaceScope> NamespaceScopePtr; typedef boost::shared_ptr<OverallScope> OverallScopePtr; typedef boost::shared_ptr<TypedefScope> TypedefScopePtr; ! typedef boost::shared_ptr<MemberFunctionScope> MemberFunctionScopePtr; class Type; --- 21,29 ---- class TypedefScope; typedef boost::shared_ptr<ClassScope> ClassScopePtr; + typedef boost::shared_ptr<MemberFunctionScope> MemberFunctionScopePtr; typedef boost::shared_ptr<NamespaceScope> NamespaceScopePtr; typedef boost::shared_ptr<OverallScope> OverallScopePtr; typedef boost::shared_ptr<TypedefScope> TypedefScopePtr; ! typedef boost::shared_ptr<TranslationUnitScope> TranslationUnitScopePtr; class Type; *************** *** 31,34 **** --- 33,37 ---- { public: + virtual void visit( AnonymousNamespaceScope &scope ) = 0; virtual void visit( BlockScope &scope ) = 0; virtual void visit( ClassScope &scope ) = 0; *************** *** 81,85 **** { public: ! ScopePtr translationUnitScope( FileId file ) const; public: // overridden from Scope --- 84,88 ---- { public: ! TranslationUnitScopePtr translationUnitScope( FileId file ) const; public: // overridden from Scope *************** *** 89,93 **** std::string str() const; private: ! typedef std::map<FileId,ScopePtr> TranslationUnitScopes; mutable TranslationUnitScopes scopes_; }; --- 92,96 ---- std::string str() const; private: ! typedef std::map<FileId,TranslationUnitScopePtr> TranslationUnitScopes; mutable TranslationUnitScopes scopes_; }; *************** *** 105,108 **** --- 108,128 ---- class TranslationUnitScope : public Scope { + public: + TranslationUnitScope(); + + ScopePtr anonymousNamespaceScope() const; + + public: // overridden from Scope + void accept( ScopeVisitor &visitor ); + SymbolDeclarationPtr resolve( const std::string &name ) const; + SymbolDeclarationPtr memberResolve( const std::string &name ) const; + std::string str() const; + + private: + ScopePtr anonymousNamespaceScope_; + }; + + class AnonymousNamespaceScope : public Scope + { public: // overridden from Scope void accept( ScopeVisitor &visitor ); Index: location.h =================================================================== RCS file: /cvsroot/cpptool/CppParser/examples/parser/location.h,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -C2 -d -r1.1.1.1 -r1.2 *** location.h 8 Jun 2004 20:23:14 -0000 1.1.1.1 --- location.h 20 Jun 2004 11:00:38 -0000 1.2 *************** *** 7,10 **** --- 7,12 ---- typedef unsigned int FileId; + enum { invalidFileId = -1 }; + class Location { *************** *** 32,36 **** inline Location::Location() ! : file_( -1 ) , pos_( -1 ) { --- 34,38 ---- inline Location::Location() ! : file_( invalidFileId ) , pos_( -1 ) { Index: symboltabletest.cpp =================================================================== RCS file: /cvsroot/cpptool/CppParser/examples/parser/symboltabletest.cpp,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** symboltabletest.cpp 20 Jun 2004 10:07:56 -0000 1.4 --- symboltabletest.cpp 20 Jun 2004 11:00:38 -0000 1.5 *************** *** 38,41 **** --- 38,42 ---- globalDeclarations.addChild( "type_alias" ); globalDeclarations.addChild( "namespace" ); + globalDeclarations.addChild( "anonymous_namespace" ); globalDeclarations.addChild( "variable" ); globalDeclarations.addChild( "enumeration" ); *************** *** 336,339 **** --- 337,341 ---- const Parser::SymbolDeclarationPtr &declaration ); void checkNamespace( const NodePtr &namespaceNode ); + void checkAnonymousNamespace( const NodePtr &namespaceNode ); CppUT::Message makeMessage( const NodePtr &node, *************** *** 364,368 **** { std::ostringstream os; ! std::string path = project_.sourcePath( location.file() ); std::string text = project_.readSourceFile( path ); int line = std::count( text.begin(), text.begin() + location.pos(), '\n' ); --- 366,370 ---- { std::ostringstream os; ! std::string path = project_.pathFromFileId( location.file() ); std::string text = project_.readSourceFile( path ); int line = std::count( text.begin(), text.begin() + location.pos(), '\n' ); *************** *** 473,476 **** --- 475,480 ---- else if ( child->specificationName() == "namespace" ) checkNamespace( child ); + else if ( child->specificationName() == "anonymous_namespace" ) + checkAnonymousNamespace( child ); else CppUT::fail( makeMessage( child, "don't know how to check node type." ) ); *************** *** 549,552 **** --- 553,579 ---- } + + void + DeclarationChecker::checkAnonymousNamespace( const NodePtr &namespaceNode ) + { + std::string translationUnitName = namespaceNode->braceValue(); + try + { + Parser::FileId translationUnitId = project_.fileIdFromPath( translationUnitName ); + Parser::TranslationUnitScopePtr translationUnitScope = table_.translationUnitScope( translationUnitId ); + Parser::ScopePtr anonymousScope = translationUnitScope->anonymousNamespaceScope(); + if ( namespaceNode->hasChild("members") ) + { + enterScope( anonymousScope ); + checkDeclarations( namespaceNode->child("members") ); + exitScope(); + } + } + catch ( Parser::ProjectError & ) + { + CppUT::fail( makeMessage( namespaceNode, "Unknown translation unit." ) ); + } + } + } // anonymous namespace Index: symboldeclarator.cpp =================================================================== RCS file: /cvsroot/cpptool/CppParser/examples/parser/symboldeclarator.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** symboldeclarator.cpp 19 Jun 2004 15:25:43 -0000 1.2 --- symboldeclarator.cpp 20 Jun 2004 11:00:38 -0000 1.3 *************** *** 60,63 **** --- 60,65 ---- else if ( declaration->name() == ConstString("named_namespace_def") ) declareNamespaceDef( declaration, parentScope ); + else if ( declaration->name() == ConstString("unnamed_namespace_def") ) + declareAnonymousNamespaceDef( declaration, parentScope ); } } *************** *** 293,295 **** --- 295,308 ---- } + + void + SymbolDeclarator::declareAnonymousNamespaceDef( const NodePtr &namespaceDeclaration, + Scope &parentScope ) + { + TranslationUnitScopePtr translationScope = symbolTable_.translationUnitScope( currentFile_ ); + ScopePtr anonymousScope = translationScope->anonymousNamespaceScope(); + NodePtr body = safeGetChild( namespaceDeclaration, "declarations" ); + declareDeclarationSequence( body, *anonymousScope ); + } + } // namespace Parser Index: symboltable.cpp =================================================================== RCS file: /cvsroot/cpptool/CppParser/examples/parser/symboltable.cpp,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -C2 -d -r1.1.1.1 -r1.2 *** symboltable.cpp 8 Jun 2004 20:23:18 -0000 1.1.1.1 --- symboltable.cpp 20 Jun 2004 11:00:38 -0000 1.2 *************** *** 58,62 **** ! ScopePtr SymbolTable::translationUnitScope( FileId file ) { --- 58,62 ---- ! TranslationUnitScopePtr SymbolTable::translationUnitScope( FileId file ) { Index: symboltable.h =================================================================== RCS file: /cvsroot/cpptool/CppParser/examples/parser/symboltable.h,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -C2 -d -r1.1.1.1 -r1.2 *** symboltable.h 8 Jun 2004 20:23:18 -0000 1.1.1.1 --- symboltable.h 20 Jun 2004 11:00:38 -0000 1.2 *************** *** 10,14 **** --- 10,16 ---- namespace Parser { class OverallScope; + class TranslationUnitScope; typedef boost::shared_ptr<OverallScope> OverallScopePtr; + typedef boost::shared_ptr<TranslationUnitScope> TranslationUnitScopePtr; class SymbolTable *************** *** 29,33 **** OverallScopePtr overallScope() const; ! ScopePtr translationUnitScope( FileId file ); SymbolDeclarationPtr resolve( const std::string &name ) const; --- 31,35 ---- OverallScopePtr overallScope() const; ! TranslationUnitScopePtr translationUnitScope( FileId file ); SymbolDeclarationPtr resolve( const std::string &name ) const; Index: testproject.cpp =================================================================== RCS file: /cvsroot/cpptool/CppParser/examples/parser/testproject.cpp,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -C2 -d -r1.1.1.1 -r1.2 *** testproject.cpp 8 Jun 2004 20:23:18 -0000 1.1.1.1 --- testproject.cpp 20 Jun 2004 11:00:38 -0000 1.2 *************** *** 63,68 **** std::string ! TestProject::sourcePath( Parser::FileId file ) { IdsFile::const_iterator it = idToPaths_.find( file ); --- 63,77 ---- + Parser::FileId + TestProject::fileIdFromPath( const std::string &path ) + { + FileIds::const_iterator it = pathToIds_.find( path ); + if ( it == pathToIds_.end() ) + throw Parser::ProjectError( "Unregistered path." ); + return it->second; + } + std::string ! TestProject::pathFromFileId( Parser::FileId file ) { IdsFile::const_iterator it = idToPaths_.find( file ); *************** *** 80,84 **** return *(it->second); ! std::string path = sourcePath( file ); Parser::SourceEditorPtr editor( new Parser::SourceEditor( readSourceFile(path), *this ) ); editors_[ file ] = editor; --- 89,93 ---- return *(it->second); ! std::string path = pathFromFileId( file ); Parser::SourceEditorPtr editor( new Parser::SourceEditor( readSourceFile(path), *this ) ); editors_[ file ] = editor; *************** *** 101,105 **** for ( ; it != editors_.end(); ++it ) { ! std::string path = sourcePath( it->first ); Parser::SourceEditor &editor = *(it->second); files_[path] = editor.source(); --- 110,114 ---- for ( ; it != editors_.end(); ++it ) { ! std::string path = pathFromFileId( it->first ); Parser::SourceEditor &editor = *(it->second); files_[path] = editor.source(); Index: project.h =================================================================== RCS file: /cvsroot/cpptool/CppParser/examples/parser/project.h,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -C2 -d -r1.1.1.1 -r1.2 *** project.h 8 Jun 2004 20:23:17 -0000 1.1.1.1 --- project.h 20 Jun 2004 11:00:38 -0000 1.2 *************** *** 54,58 **** virtual const ProjectConfiguration &configuration() const = 0; ! // virtual FileId fileId( const std::string &path ) = 0; // virtual bool sourceFileExist( const std::string &path ) = 0; --- 54,62 ---- virtual const ProjectConfiguration &configuration() const = 0; ! /// @exception ProjectError if source file does not exist ! virtual FileId fileIdFromPath( const std::string &path ) = 0; ! ! /// @exception ProjectError if source file does not exist ! virtual std::string pathFromFileId( FileId file ) = 0; // virtual bool sourceFileExist( const std::string &path ) = 0; *************** *** 62,68 **** /// @exception ProjectError if source file does not exist - virtual std::string sourcePath( FileId file ) = 0; - - /// @exception ProjectError if source file does not exist virtual SourceEditor &editSource( FileId file ) = 0; --- 66,69 ---- Index: scope.cpp =================================================================== RCS file: /cvsroot/cpptool/CppParser/examples/parser/scope.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** scope.cpp 14 Jun 2004 23:08:11 -0000 1.2 --- scope.cpp 20 Jun 2004 11:00:38 -0000 1.3 *************** *** 66,70 **** // class OverallScope // ////////////////////////////////////////////////////////////////// ! ScopePtr OverallScope::translationUnitScope( FileId file ) const { --- 66,70 ---- // class OverallScope // ////////////////////////////////////////////////////////////////// ! TranslationUnitScopePtr OverallScope::translationUnitScope( FileId file ) const { *************** *** 140,143 **** --- 140,156 ---- // ////////////////////////////////////////////////////////////////// + TranslationUnitScope::TranslationUnitScope() + : anonymousNamespaceScope_( new AnonymousNamespaceScope() ) + { + } + + + ScopePtr + TranslationUnitScope::anonymousNamespaceScope() const + { + return anonymousNamespaceScope_; + } + + void TranslationUnitScope::accept( ScopeVisitor &visitor ) *************** *** 149,152 **** --- 162,169 ---- TranslationUnitScope::resolve( const std::string &name ) const { + SymbolDeclarationPtr declaration = anonymousNamespaceScope_->resolve( name ); + if ( declaration ) + return declaration; + for ( int index = 0; index < subScopeCount(); ++index ) { *************** *** 161,164 **** --- 178,185 ---- TranslationUnitScope::memberResolve( const std::string &name ) const { + SymbolDeclarationPtr declaration = anonymousNamespaceScope_->getMemberDeclaration( name ); + if ( declaration ) + return declaration; + for ( int index = 0; index < subScopeCount(); ++index ) { *************** *** 177,180 **** --- 198,241 ---- + // class AnonymousNamespaceScope + // ////////////////////////////////////////////////////////////////// + + void + AnonymousNamespaceScope::accept( ScopeVisitor &visitor ) + { + visitor.visit( *this ); + } + + SymbolDeclarationPtr + AnonymousNamespaceScope::resolve( const std::string &name ) const + { + for ( int index = 0; index < subScopeCount(); ++index ) + { + SymbolDeclarationPtr declaration = subScopeAt(index)->resolve( name ); + if ( declaration ) + return declaration; + } + return SymbolDeclarationPtr(); + } + + SymbolDeclarationPtr + AnonymousNamespaceScope::memberResolve( const std::string &name ) const + { + for ( int index = 0; index < subScopeCount(); ++index ) + { + SymbolDeclarationPtr declaration = subScopeAt(index)->getMemberDeclaration( name ); + if ( declaration ) + return declaration; + } + return SymbolDeclarationPtr(); + } + + std::string + AnonymousNamespaceScope::str() const + { + return "AnonymousNamespaceScope<>"; + } + + // class NamespaceScope // ////////////////////////////////////////////////////////////////// Index: symboldeclarator.h =================================================================== RCS file: /cvsroot/cpptool/CppParser/examples/parser/symboldeclarator.h,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -C2 -d -r1.1.1.1 -r1.2 *** symboldeclarator.h 8 Jun 2004 20:23:18 -0000 1.1.1.1 --- symboldeclarator.h 20 Jun 2004 11:00:38 -0000 1.2 *************** *** 53,56 **** --- 53,59 ---- Scope &parentScope ); + void declareAnonymousNamespaceDef( const NodePtr &namespaceDeclaration, + Scope &parentScope ); + CppUT::Enumerator<std::string> getAllChildSymbols( const NodePtr &node ) const; |