From a63af80942eeefd197afab3999cd50ec7c7762f8 Mon Sep 17 00:00:00 2001 From: Markus Schorn Date: Fri, 10 Jul 2009 11:13:53 +0000 Subject: [PATCH] API to access node factories, bug 268972. --- .../core/dom/ast/ASTNodeFactoryFactory.java | 36 +++++++++++++++++++ .../cdt/core/dom/ast/IASTTranslationUnit.java | 4 +-- .../cdt/core/dom/ast/INodeFactory.java | 14 ++++++-- .../cdt/core/dom/ast/cpp/ICPPNodeFactory.java | 13 +++++++ .../core/dom/parser/c/CNodeFactory.java | 10 +++++- .../core/dom/parser/c/GNUCSourceParser.java | 5 +-- .../core/dom/parser/cpp/CPPNodeFactory.java | 10 +++++- .../dom/parser/cpp/GNUCPPSourceParser.java | 5 +-- 8 files changed, 81 insertions(+), 16 deletions(-) create mode 100644 core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ASTNodeFactoryFactory.java diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ASTNodeFactoryFactory.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ASTNodeFactoryFactory.java new file mode 100644 index 00000000000..adeab8c3f09 --- /dev/null +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ASTNodeFactoryFactory.java @@ -0,0 +1,36 @@ +/******************************************************************************* + * Copyright (c) 2009 Wind River Systems, Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Markus Schorn - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.dom.ast; + +import org.eclipse.cdt.core.dom.ast.c.ICNodeFactory; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPNodeFactory; +import org.eclipse.cdt.internal.core.dom.parser.c.CNodeFactory; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPNodeFactory; + +/** + * Provides access to the node factories. + * + * @noextend This class is not intended to be subclassed by clients. + * @noinstantiate This class is not intended to be instantiated by clients. + * @since 5.2 + */ +public class ASTNodeFactoryFactory { + + ASTNodeFactoryFactory() {} + + public static ICNodeFactory getDefaultCNodeFactory() { + return CNodeFactory.getDefault(); + } + + public static ICPPNodeFactory getDefaultCPPNodeFactory() { + return CPPNodeFactory.getDefault(); + } +} diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTTranslationUnit.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTTranslationUnit.java index 6e54be7b5f9..3f9cf0f8e69 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTTranslationUnit.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTTranslationUnit.java @@ -271,9 +271,7 @@ public interface IASTTranslationUnit extends IASTNode, IASTDeclarationListOwner, /** * Returns the node factory that was used to build the AST. - * - * @noreference This method is not intended to be referenced by clients. - * @since 5.1 + * @since 5.2 */ public INodeFactory getASTNodeFactory(); diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/INodeFactory.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/INodeFactory.java index ae749e57acf..956b22e106e 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/INodeFactory.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/INodeFactory.java @@ -12,6 +12,7 @@ package org.eclipse.cdt.core.dom.ast; import org.eclipse.cdt.core.dom.ast.IASTEnumerationSpecifier.IASTEnumerator; import org.eclipse.cdt.core.dom.ast.gnu.IGNUASTCompoundStatementExpression; +import org.eclipse.cdt.core.parser.IScanner; /** @@ -46,11 +47,18 @@ public interface INodeFactory { public IASTName newName(char[] name); /** - * Calling the method getASTNodeFactory() on the translation unit returned by this - * method will return the node factory that was used to create the IASTTranslationUnit. + * @deprecated use {@link #newTranslationUnit(IScanner)}, instead. */ + @Deprecated public IASTTranslationUnit newTranslationUnit(); - + + /** + * Creates a new translation unit that cooperates with the given scanner in order + * to track macro-expansions and location information. + * @scanner the preprocessor the translation unit interacts with. + * @since 5.2 + */ + public IASTTranslationUnit newTranslationUnit(IScanner scanner); public IASTLiteralExpression newLiteralExpression(int kind, String rep); diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/cpp/ICPPNodeFactory.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/cpp/ICPPNodeFactory.java index d48d19ca926..9e9b1213aa7 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/cpp/ICPPNodeFactory.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/cpp/ICPPNodeFactory.java @@ -26,6 +26,7 @@ import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTExplicitTemplateInstantiation import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTPointer; import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTPointerToMember; import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTSimpleDeclSpecifier; +import org.eclipse.cdt.core.parser.IScanner; /** * Factory for AST nodes for the C++ programming language. @@ -37,7 +38,19 @@ import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTSimpleDeclSpecifier; */ public interface ICPPNodeFactory extends INodeFactory { + /** + * @deprecated use {@link #newTranslationUnit(IScanner)}, instead. + */ + @Deprecated public ICPPASTTranslationUnit newTranslationUnit(); + + /** + * Creates a new translation unit that cooperates with the given scanner in order + * to track macro-expansions and location information. + * @scanner the preprocessor the translation unit interacts with. + * @since 5.2 + */ + public ICPPASTTranslationUnit newTranslationUnit(IScanner scanner); public ICPPASTLiteralExpression newLiteralExpression(int kind, String rep); diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CNodeFactory.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CNodeFactory.java index 68d424d1872..367fe2ded1e 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CNodeFactory.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CNodeFactory.java @@ -77,6 +77,7 @@ import org.eclipse.cdt.core.dom.ast.gnu.IGNUASTCompoundStatementExpression; import org.eclipse.cdt.core.dom.ast.gnu.c.ICASTKnRFunctionDeclarator; import org.eclipse.cdt.core.dom.ast.gnu.c.IGCCASTArrayRangeDesignator; import org.eclipse.cdt.core.dom.ast.gnu.c.IGCCASTSimpleDeclSpecifier; +import org.eclipse.cdt.core.parser.IScanner; /** * Abstract factory implementation that creates AST nodes for C99. @@ -93,9 +94,16 @@ public class CNodeFactory implements ICNodeFactory { return DEFAULT_INSTANCE; } - public IASTTranslationUnit newTranslationUnit() { + return newTranslationUnit(null); + } + + public IASTTranslationUnit newTranslationUnit(IScanner scanner) { CASTTranslationUnit tu = new CASTTranslationUnit(); + + if (scanner != null) { + tu.setLocationResolver(scanner.getLocationResolver()); + } tu.setASTNodeFactory(this); return tu; } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/GNUCSourceParser.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/GNUCSourceParser.java index e1c916342dc..4c45087853c 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/GNUCSourceParser.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/GNUCSourceParser.java @@ -90,7 +90,6 @@ import org.eclipse.cdt.core.parser.util.CharArrayUtils; import org.eclipse.cdt.internal.core.dom.parser.ASTInternal; import org.eclipse.cdt.internal.core.dom.parser.ASTNode; import org.eclipse.cdt.internal.core.dom.parser.ASTQueries; -import org.eclipse.cdt.internal.core.dom.parser.ASTTranslationUnit; import org.eclipse.cdt.internal.core.dom.parser.AbstractGNUSourceCodeParser; import org.eclipse.cdt.internal.core.dom.parser.BacktrackException; import org.eclipse.cdt.internal.core.dom.parser.DeclarationOptions; @@ -466,7 +465,7 @@ public class GNUCSourceParser extends AbstractGNUSourceCodeParser { @Override protected void setupTranslationUnit() throws DOMException { - translationUnit = nodeFactory.newTranslationUnit(); + translationUnit = nodeFactory.newTranslationUnit(scanner); translationUnit.setIndex(index); // add built-in names to the scope @@ -478,8 +477,6 @@ public class GNUCSourceParser extends AbstractGNUSourceCodeParser { ASTInternal.addBinding(tuScope, binding); } } - if(translationUnit instanceof ASTTranslationUnit) - ((ASTTranslationUnit)translationUnit).setLocationResolver(scanner.getLocationResolver()); } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPNodeFactory.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPNodeFactory.java index 7a1774f657b..4d6bbc80a2b 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPNodeFactory.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPNodeFactory.java @@ -103,6 +103,7 @@ import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTExplicitTemplateInstantiation import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTPointer; import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTPointerToMember; import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTSimpleDeclSpecifier; +import org.eclipse.cdt.core.parser.IScanner; /** @@ -116,9 +117,16 @@ public class CPPNodeFactory implements ICPPNodeFactory { return DEFAULT_INSTANCE; } - public ICPPASTTranslationUnit newTranslationUnit() { + return newTranslationUnit(null); + } + + public ICPPASTTranslationUnit newTranslationUnit(IScanner scanner) { CPPASTTranslationUnit tu = new CPPASTTranslationUnit(); + + if (scanner != null) { + tu.setLocationResolver(scanner.getLocationResolver()); + } tu.setASTNodeFactory(this); return tu; } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/GNUCPPSourceParser.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/GNUCPPSourceParser.java index da4b86ce869..60d1da36c40 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/GNUCPPSourceParser.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/GNUCPPSourceParser.java @@ -127,7 +127,6 @@ import org.eclipse.cdt.core.parser.util.CharArrayUtils; import org.eclipse.cdt.internal.core.dom.parser.ASTInternal; import org.eclipse.cdt.internal.core.dom.parser.ASTNode; import org.eclipse.cdt.internal.core.dom.parser.ASTQueries; -import org.eclipse.cdt.internal.core.dom.parser.ASTTranslationUnit; import org.eclipse.cdt.internal.core.dom.parser.AbstractGNUSourceCodeParser; import org.eclipse.cdt.internal.core.dom.parser.BacktrackException; import org.eclipse.cdt.internal.core.dom.parser.DeclarationOptions; @@ -3390,7 +3389,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser { @Override protected void setupTranslationUnit() throws DOMException { - translationUnit = nodeFactory.newTranslationUnit(); + translationUnit = nodeFactory.newTranslationUnit(scanner); translationUnit.setIndex(index); // add built-in names to the scope @@ -3403,8 +3402,6 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser { ASTInternal.addBinding(tuScope, binding); } } - if(translationUnit instanceof ASTTranslationUnit) - ((ASTTranslationUnit)translationUnit).setLocationResolver(scanner.getLocationResolver()); }