Update of /cvsroot/cpptool/rfta/src/rfta In directory sc8-pr-cvs1:/tmp/cvs-serv11715/rfta/src/rfta Modified Files: IdentifierScopeTest.h IdentifierScopeTest.cpp IdentifierScope.h IdentifierScope.cpp IdentifierResolverContext.cpp Log Message: -- extended identifier scope by "attributed" identifiers, see tests for usage Index: IdentifierScopeTest.h =================================================================== RCS file: /cvsroot/cpptool/rfta/src/rfta/IdentifierScopeTest.h,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** IdentifierScopeTest.h 6 Sep 2003 21:48:55 -0000 1.2 --- IdentifierScopeTest.h 8 Sep 2003 19:12:07 -0000 1.3 *************** *** 19,22 **** --- 19,23 ---- CPPUNIT_TEST( testIdentifierResolutionInOneScope ); CPPUNIT_TEST( testQualifiedScopeLookup ); + CPPUNIT_TEST( testAttributedIdentifier ); CPPUNIT_TEST_SUITE_END(); *************** *** 34,37 **** --- 35,39 ---- void testIdentifierResolutionInOneScope(); void testQualifiedScopeLookup(); + void testAttributedIdentifier(); }; Index: IdentifierScopeTest.cpp =================================================================== RCS file: /cvsroot/cpptool/rfta/src/rfta/IdentifierScopeTest.cpp,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** IdentifierScopeTest.cpp 7 Sep 2003 13:28:02 -0000 1.3 --- IdentifierScopeTest.cpp 8 Sep 2003 19:12:07 -0000 1.4 *************** *** 50,74 **** ASTNodePtr nodeC = ASTNode::create(ASTNodeTypes::variableIdentifier,ASTNodeWeakPtr(),0,0,SourceASTNodeWeakPtr()); ! rootScope->addIdentifierDeclaration("C",nodeC); ! scopeA ->addIdentifierDeclaration("B",nodeB); try { ! ASTNodePtr decl = rootScope->getIdentifierDeclaration("C"); CPPUNIT_ASSERT_MESSAGE( "Expected a AST node.", bool(decl) ); CPPUNIT_ASSERT_EQUAL_MESSAGE ( "Expect a different AST node. (memory adress)", nodeC , decl ); ! decl = scopeA->getIdentifierDeclaration("::C"); CPPUNIT_ASSERT_MESSAGE( "Expected a AST node.", bool(decl) ); CPPUNIT_ASSERT_EQUAL_MESSAGE ( "Expect a different AST node. (memory adress)", nodeC , decl ); ! decl = scopeA->getIdentifierDeclaration("B"); CPPUNIT_ASSERT_MESSAGE( "Expected a AST node.", bool(decl) ); CPPUNIT_ASSERT_EQUAL_MESSAGE ( "Expect a different AST node. (memory adress)", nodeB , decl ); ! decl = rootScope->getIdentifierDeclaration("A::B"); CPPUNIT_ASSERT_MESSAGE( "Expected a AST node.", bool(decl) ); CPPUNIT_ASSERT_EQUAL_MESSAGE ( "Expect a different AST node. (memory adress)", nodeB , decl ); ! decl = scopeA->getIdentifierDeclaration("::A::B"); CPPUNIT_ASSERT_MESSAGE( "Expected a AST node.", bool(decl) ); CPPUNIT_ASSERT_EQUAL_MESSAGE ( "Expect a different AST node. (memory adress)", nodeB , decl ); --- 50,74 ---- ASTNodePtr nodeC = ASTNode::create(ASTNodeTypes::variableIdentifier,ASTNodeWeakPtr(),0,0,SourceASTNodeWeakPtr()); ! rootScope->registerIdentifier("C",nodeC); ! scopeA ->registerIdentifier("B",nodeB); try { ! ASTNodePtr decl = rootScope->findIdentifier("C"); CPPUNIT_ASSERT_MESSAGE( "Expected a AST node.", bool(decl) ); CPPUNIT_ASSERT_EQUAL_MESSAGE ( "Expect a different AST node. (memory adress)", nodeC , decl ); ! decl = scopeA->findIdentifier("::C"); CPPUNIT_ASSERT_MESSAGE( "Expected a AST node.", bool(decl) ); CPPUNIT_ASSERT_EQUAL_MESSAGE ( "Expect a different AST node. (memory adress)", nodeC , decl ); ! decl = scopeA->findIdentifier("B"); CPPUNIT_ASSERT_MESSAGE( "Expected a AST node.", bool(decl) ); CPPUNIT_ASSERT_EQUAL_MESSAGE ( "Expect a different AST node. (memory adress)", nodeB , decl ); ! decl = rootScope->findIdentifier("A::B"); CPPUNIT_ASSERT_MESSAGE( "Expected a AST node.", bool(decl) ); CPPUNIT_ASSERT_EQUAL_MESSAGE ( "Expect a different AST node. (memory adress)", nodeB , decl ); ! decl = scopeA->findIdentifier("::A::B"); CPPUNIT_ASSERT_MESSAGE( "Expected a AST node.", bool(decl) ); CPPUNIT_ASSERT_EQUAL_MESSAGE ( "Expect a different AST node. (memory adress)", nodeB , decl ); *************** *** 92,95 **** --- 92,120 ---- CPPUNIT_ASSERT_EQUAL_MESSAGE( "Expected a different scope pointer.", scopeD, scopeC->getQualifiedScope("A::B::D") ); CPPUNIT_ASSERT_EQUAL_MESSAGE( "Expected a different scope pointer.", scopeC, scopeD->getQualifiedScope("::C") ); + } + + void + IdentifierScopeTest::testAttributedIdentifier() + { + IdentifierScopePtr rootScope = IdentifierScope::createScope(IdentifierScopeWeakPtr(),"ROOT"); + IdentifierScopePtr scopeA = IdentifierScope::createSubScope(rootScope,"A"); + + ASTNodePtr nodeB = ASTNode::create(ASTNodeTypes::variableIdentifier,ASTNodeWeakPtr(),0,0,SourceASTNodeWeakPtr()); + ASTNodePtr nodeC = ASTNode::create(ASTNodeTypes::classSpecifier,ASTNodeWeakPtr(),0,0,SourceASTNodeWeakPtr()); + ASTNodePtr nodeD = ASTNode::create(ASTNodeTypes::classSpecifier,ASTNodeWeakPtr(),0,0,SourceASTNodeWeakPtr()); + + scopeA ->registerIdentifier("B",nodeB); + scopeA ->registerAttributedIdentifier("B",IdentifierAttributes::ClassIdentifier, nodeC); + scopeA ->registerAttributedIdentifier("C",IdentifierAttributes::ClassIdentifier, nodeD); + + ASTNodePtr decl; + decl = scopeA->findIdentifier("B"); + CPPUNIT_ASSERT_EQUAL_MESSAGE ( "Expect a different AST node.", nodeB , decl ); + + decl = scopeA->findAttributedIdentifier("B",IdentifierAttributes::ClassIdentifier); + CPPUNIT_ASSERT_EQUAL_MESSAGE ( "Expect a different AST node.", nodeC , decl ); + + decl = scopeA->findAttributedIdentifier("C",IdentifierAttributes::ClassIdentifier); + CPPUNIT_ASSERT_EQUAL_MESSAGE ( "Expect a different AST node.", nodeD , decl ); } Index: IdentifierScope.h =================================================================== RCS file: /cvsroot/cpptool/rfta/src/rfta/IdentifierScope.h,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** IdentifierScope.h 7 Sep 2003 13:28:58 -0000 1.3 --- IdentifierScope.h 8 Sep 2003 19:12:07 -0000 1.4 *************** *** 32,35 **** --- 32,46 ---- }; + class IdentifierAttributes { + public: + enum type { + NoAttribute = 0, + ClassIdentifier = 1, + EnumIdentifier = 2, + UnionIdentifier = 3, + StructIdentifier = 4 + }; + }; + /// objects of this class store identifier scopes and sub scopes /// (identifier visibility, top level is file scope). *************** *** 37,40 **** --- 48,52 ---- { public: + static IdentifierScopePtr createScope(IdentifierScopeWeakPtr parent, std::string scopeName); static IdentifierScopePtr createSubScope(IdentifierScopeWeakPtr parent, std::string scopeName); *************** *** 55,64 **** */ bool removeSubScope( IdentifierScopePtr child ); ! // TODO-1: define sub ranges for variables/types/classes/unions ! // TODO-2: add function to register "identifier uses" ! void addIdentifierDeclaration( std::string qualifiedName, ASTNodePtr declaration); ! void addIdentifierUse( std::string qualifiedName, ASTNodePtr declaration); ! ASTNodePtr getIdentifierDeclaration( std::string qualifiedName ) const; protected: --- 67,88 ---- */ bool removeSubScope( IdentifierScopePtr child ); + + /** + * register identifier with no attributes: + */ + void registerIdentifier( std::string qualifiedName, ASTNodePtr declaration); + /** + * find identifier 'not-attributed' have higher priority + */ + ASTNodePtr findIdentifier( std::string qualifiedName ) const; ! /** ! * register identifier with attribute ! */ ! void registerAttributedIdentifier( std::string qualifiedName, IdentifierAttributes::type attr, ASTNodePtr declaration); ! /** ! * find identifier with attribute ! */ ! ASTNodePtr findAttributedIdentifier( std::string qualifiedName, IdentifierAttributes::type attr ) const; protected: *************** *** 67,80 **** IdentifierScope(IdentifierScopeWeakPtr parent, std::string name); private: ! void registerSubScope(std::string scopeName, IdentifierScopePtr& subScope ); ! void addLocalIdentifier( std::string localName, ASTNodePtr declaration); ! ASTNodePtr getLocalIdentifier( std::string localName ); ! bool isLocalIdentifier( std::string localName ); ! typedef std::map<std::string, IdentifierScopePtr> SubScopeMap; ! typedef std::map<std::string, ASTNodePtr> IdentifierDeclarationMap; ! SubScopeMap subScopes_; //< sub scopes can be defined by types and namespaces ! IdentifierDeclarationMap identifierDeclaration_; //< stores declaration of this scope ! IdentifierScopeWeakPtr parent_; --- 91,102 ---- IdentifierScope(IdentifierScopeWeakPtr parent, std::string name); private: ! void registerSubScope(std::string scopeName, IdentifierScopePtr& subScope ); ! typedef std::map<std::string, IdentifierScopePtr> SubScopeMap; ! typedef std::map<IdentifierAttributes::type, ASTNodePtr> AttributedIdentifier; ! typedef std::map<std::string, AttributedIdentifier> IdentifierMap; ! SubScopeMap subScopes_; //< sub scopes can be defined by types and namespaces ! IdentifierMap identifierDeclaration_; //< stores declaration of this scope ! IdentifierScopeWeakPtr parent_; Index: IdentifierScope.cpp =================================================================== RCS file: /cvsroot/cpptool/rfta/src/rfta/IdentifierScope.cpp,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** IdentifierScope.cpp 7 Sep 2003 13:28:58 -0000 1.3 --- IdentifierScope.cpp 8 Sep 2003 19:12:07 -0000 1.4 *************** *** 75,166 **** void ! IdentifierScope::addIdentifierDeclaration( std::string qualifiedName, ASTNodePtr declaration) ! { ! int pos = qualifiedName.find(':'); ! if ( pos != std::string::npos ) ! { ! // check validity of the name: ! if ( pos == qualifiedName.length()-1 || ! qualifiedName[pos+1] != ':' ) ! throw IdentifierScopeException(IdentifierScopeException::InvalidIdentifierName); ! ! // check if qualified name references 'root-scope' ! if ( pos == 0 ) ! boost::make_shared(root_)->addIdentifierDeclaration(qualifiedName.substr(2),declaration); ! ! // find out sub scope: ! std::string subScopeName = qualifiedName.substr(0,pos-1); ! if (!hasSubScope( subScopeName )) ! throw IdentifierScopeException(IdentifierScopeException::WrongQualifiedIdentifier); ! ! // call registration routine of sub scope ! getSubScope( subScopeName ) ->addIdentifierDeclaration(qualifiedName.substr(pos+2),declaration); ! ! } else ! { ! // identifier of this scope. ! IdentifierDeclarationMap::iterator it = identifierDeclaration_.find( qualifiedName ); ! if ( it != identifierDeclaration_.end() ) ! throw IdentifierScopeException(IdentifierScopeException::IdentifierDeclaredTwice); ! identifierDeclaration_[qualifiedName] = declaration; ! } ! } ! ! ASTNodePtr ! IdentifierScope::getIdentifierDeclaration( std::string qualifiedName ) const ! { ! int pos = qualifiedName.find(':'); ! if ( pos != std::string::npos ) ! { ! // check validity of the name: ! if ( pos == qualifiedName.length()-1 || ! qualifiedName[pos+1] != ':' ) ! throw IdentifierScopeException(IdentifierScopeException::InvalidIdentifierName); ! ! // check if qualified name references 'root-scope' ! if ( pos == 0 ) ! return boost::make_shared(root_)->getIdentifierDeclaration(qualifiedName.substr(2)); ! ! // find out sub scope: ! std::string subScopeName = qualifiedName.substr(0,pos); ! if (!hasSubScope( subScopeName )) ! throw IdentifierScopeException(IdentifierScopeException::WrongQualifiedIdentifier); ! ! // call registration routine of sub scope ! return getSubScope( subScopeName ) ->getIdentifierDeclaration(qualifiedName.substr(pos+2)); ! ! } else ! { ! // identifier of this scope. ! IdentifierDeclarationMap::const_iterator it = identifierDeclaration_.find( qualifiedName ); ! if ( it == identifierDeclaration_.end() ) ! { ! if (name_.empty() && !getParentScope().expired()) ! return boost::make_shared(getParentScope())->getIdentifierDeclaration(qualifiedName); ! else ! return ASTNodePtr (); ! } ! else ! return it->second; ! } ! } ! ! void ! IdentifierScope::addLocalIdentifier( std::string localName, ASTNodePtr declaration) { } ASTNodePtr ! IdentifierScope::getLocalIdentifier( std::string localName ) ! { ! return ASTNodePtr (); ! } ! ! bool IdentifierScope::isLocalIdentifier( std::string localName ) { ! return false; } - const char * IdentifierScopeException::what() const --- 75,89 ---- void ! IdentifierScope::registerIdentifier( std::string qualifiedName, ASTNodePtr declaration) { + registerAttributedIdentifier( qualifiedName, IdentifierAttributes::NoAttribute, declaration); } ASTNodePtr ! IdentifierScope::findIdentifier( std::string qualifiedName ) const { ! return findAttributedIdentifier( qualifiedName, IdentifierAttributes::NoAttribute ); } const char * IdentifierScopeException::what() const *************** *** 221,224 **** --- 144,247 ---- } while (pos != std::string::npos); return scopePtr; + } + + void + IdentifierScope::registerAttributedIdentifier( + std::string qualifiedName, + IdentifierAttributes::type attr, + ASTNodePtr declaration + ) + { + int pos = qualifiedName.find(':'); + if ( pos != std::string::npos ) + { + // check validity of the name: + if ( pos == qualifiedName.length()-1 || + qualifiedName[pos+1] != ':' ) + throw IdentifierScopeException(IdentifierScopeException::InvalidIdentifierName); + + // check if qualified name references 'root-scope' + if ( pos == 0 ) + boost::make_shared(root_)->registerAttributedIdentifier(qualifiedName.substr(2),attr,declaration); + + // find out sub scope: + std::string subScopeName = qualifiedName.substr(0,pos-1); + if (!hasSubScope( subScopeName )) + throw IdentifierScopeException(IdentifierScopeException::WrongQualifiedIdentifier); + + // call registration routine of sub scope + getSubScope( subScopeName ) ->registerAttributedIdentifier(qualifiedName.substr(pos+2),attr,declaration); + + } else + { + // identifier of this scope. + IdentifierMap::iterator it = identifierDeclaration_.find( qualifiedName ); + + if ( it == identifierDeclaration_.end() ) + { + // first identifier with this name + AttributedIdentifier attributeMap; + attributeMap[attr] = declaration; + identifierDeclaration_[qualifiedName] = attributeMap; + } else + { + // this identifier was declared allready, check if only attributed + AttributedIdentifier& attributeMap = it->second; + if (attributeMap.find(attr) != attributeMap.end()) + throw IdentifierScopeException(IdentifierScopeException::IdentifierDeclaredTwice); + attributeMap[attr] = declaration; + } + } + } + + ASTNodePtr + IdentifierScope::findAttributedIdentifier( std::string qualifiedName, IdentifierAttributes::type attr ) const + { + int pos = qualifiedName.find(':'); + if ( pos != std::string::npos ) + { + // check validity of the name: + if ( pos == qualifiedName.length()-1 || + qualifiedName[pos+1] != ':' ) + throw IdentifierScopeException(IdentifierScopeException::InvalidIdentifierName); + + // check if qualified name references 'root-scope' + if ( pos == 0 ) + return boost::make_shared(root_)->findIdentifier(qualifiedName.substr(2)); + + // find out sub scope: + std::string subScopeName = qualifiedName.substr(0,pos); + if (!hasSubScope( subScopeName )) + throw IdentifierScopeException(IdentifierScopeException::WrongQualifiedIdentifier); + + // call registration routine of sub scope + return getSubScope( subScopeName ) ->findIdentifier(qualifiedName.substr(pos+2)); + + } else + { + // identifier of this scope. + IdentifierMap::const_iterator it = identifierDeclaration_.find( qualifiedName ); + if ( it == identifierDeclaration_.end() ) + { + if (name_.empty() && !getParentScope().expired()) + return boost::make_shared(getParentScope())->findIdentifier(qualifiedName); + else + return ASTNodePtr (); + } + else + { + const AttributedIdentifier& mapAttributes = it->second; + AttributedIdentifier::const_iterator jt = mapAttributes.find(attr); + if (jt != mapAttributes.end()) + return jt->second; + else + { + if (attr != IdentifierAttributes::NoAttribute) + return ASTNodePtr (); + else + return mapAttributes.begin()->second; + } + } + } } Index: IdentifierResolverContext.cpp =================================================================== RCS file: /cvsroot/cpptool/rfta/src/rfta/IdentifierResolverContext.cpp,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** IdentifierResolverContext.cpp 7 Sep 2003 13:29:39 -0000 1.10 --- IdentifierResolverContext.cpp 8 Sep 2003 19:12:07 -0000 1.11 *************** *** 53,57 **** { const std::string name = getLocalVariableName( localVariableDeclNode ); ! identifierCurrentScope_->addIdentifierDeclaration(name, localVariableDeclNode); ASTNodePtr variableNameNode = getLocalVariableNameNode( localVariableDeclNode ); --- 53,57 ---- { const std::string name = getLocalVariableName( localVariableDeclNode ); ! identifierCurrentScope_->registerIdentifier(name, localVariableDeclNode); ASTNodePtr variableNameNode = getLocalVariableNameNode( localVariableDeclNode ); *************** *** 66,70 **** const std::string name = getIdentifierName( identifierNode ); ! ASTNodePtr declNode = identifierCurrentScope_->getIdentifierDeclaration(name); if (!declNode) // no declaration found ... { --- 66,70 ---- const std::string name = getIdentifierName( identifierNode ); ! ASTNodePtr declNode = identifierCurrentScope_->findIdentifier(name); if (!declNode) // no declaration found ... { |