001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.cnd.modelimpl.csm.deep;
043:
044: import java.util.*;
045:
046: import org.netbeans.modules.cnd.api.model.*;
047: import org.netbeans.modules.cnd.api.model.deep.*;
048:
049: import org.netbeans.modules.cnd.modelimpl.csm.*;
050: import org.netbeans.modules.cnd.modelimpl.csm.core.*;
051:
052: import antlr.collections.AST;
053: import org.netbeans.modules.cnd.modelimpl.parser.generated.CPPTokenTypes;
054:
055: /**
056: * Implementation of CsmDeclarationStatement
057: * @author Vladimir Kvashin
058: */
059: public class DeclarationStatementImpl extends StatementBase implements
060: CsmDeclarationStatement {
061:
062: private List<CsmDeclaration> declarators;
063:
064: public DeclarationStatementImpl(AST ast, CsmFile file,
065: CsmScope scope) {
066: super (ast, file, scope);
067: }
068:
069: public CsmStatement.Kind getKind() {
070: return CsmStatement.Kind.DECLARATION;
071: }
072:
073: public List<CsmDeclaration> getDeclarators() {
074: if (declarators == null) {
075: declarators = new ArrayList<CsmDeclaration>();
076: render();
077: }
078: return declarators;
079: }
080:
081: public String toString() {
082: return "" + getKind() + ' ' + getOffsetString() + '['
083: + declarators + ']'; // NOI18N
084: }
085:
086: private void render() {
087: AstRenderer renderer = new DSRenderer();
088: renderer.render(getAst(), null, null);
089: }
090:
091: private class DSRenderer extends AstRenderer {
092:
093: public DSRenderer() {
094: super ((FileImpl) getContainingFile());
095: }
096:
097: protected VariableImpl createVariable(AST offsetAst,
098: CsmFile file, CsmType type, String name,
099: boolean _static,
100: MutableDeclarationsContainer container1,
101: MutableDeclarationsContainer container2, CsmScope scope) {
102: VariableImpl var = super .createVariable(offsetAst, file,
103: type, name, _static, container1, container2,
104: getScope());
105: declarators.add(var);
106: return var;
107: }
108:
109: public void render(AST tree, NamespaceImpl currentNamespace,
110: MutableDeclarationsContainer container) {
111: if (tree != null) {
112: AST token = tree;
113: switch (token.getType()) {
114: case CPPTokenTypes.CSM_FOR_INIT_STATEMENT:
115: case CPPTokenTypes.CSM_DECLARATION_STATEMENT:
116: if (!renderVariable(token, currentNamespace,
117: container)) {
118: render(token.getFirstChild(), currentNamespace,
119: container);
120: }
121: break;
122: case CPPTokenTypes.CSM_NAMESPACE_ALIAS:
123: declarators.add(new NamespaceAliasImpl(token,
124: getContainingFile()));
125: break;
126: case CPPTokenTypes.CSM_USING_DIRECTIVE:
127: declarators.add(new UsingDirectiveImpl(token,
128: getContainingFile()));
129: break;
130: case CPPTokenTypes.CSM_USING_DECLARATION:
131: declarators.add(new UsingDeclarationImpl(token,
132: getContainingFile()));
133: break;
134:
135: case CPPTokenTypes.CSM_CLASS_DECLARATION: {
136: ClassImpl cls = ClassImpl.create(token, null,
137: getContainingFile());
138: declarators.add(cls);
139: addTypedefs(renderTypedef(token, cls,
140: currentNamespace), currentNamespace,
141: container);
142: renderVariableInClassifier(token, cls,
143: currentNamespace, container);
144: break;
145: }
146: case CPPTokenTypes.CSM_ENUM_DECLARATION: {
147: CsmEnum csmEnum = EnumImpl.create(token,
148: currentNamespace, getContainingFile());
149: declarators.add(csmEnum);
150: renderVariableInClassifier(token, csmEnum,
151: currentNamespace, container);
152: break;
153: }
154: case CPPTokenTypes.CSM_TYPE_BUILTIN:
155: case CPPTokenTypes.CSM_TYPE_COMPOUND:
156: AST typeToken = token;
157: AST next = token.getNextSibling();
158: if (next != null
159: && next.getType() == CPPTokenTypes.CSM_QUALIFIED_ID) {
160: do {
161: TypeImpl type;
162: if (typeToken.getType() == CPPTokenTypes.CSM_TYPE_BUILTIN) {
163: type = TypeFactory.createBuiltinType(
164: typeToken.getText(), null, 0,
165: typeToken, getContainingFile());
166: } else {
167: type = TypeFactory.createType(
168: typeToken, getContainingFile(),
169: null, 0);
170: }
171: String name = next.getText();
172: VariableImpl var = createVariable(next,
173: getContainingFile(), type, name,
174: false, currentNamespace, container,
175: getScope());
176: // we ignore both currentNamespace and container; <= WHY?
177: // eat all tokens up to the comma that separates the next decl
178: next = next.getNextSibling();
179: if (next != null
180: && next.getType() == CPPTokenTypes.CSM_PARMLIST) {
181: next = next.getNextSibling();
182: }
183: if (next != null
184: && next.getType() == CPPTokenTypes.COMMA) {
185: next = next.getNextSibling();
186: }
187: } while (next != null
188: && next.getType() == CPPTokenTypes.CSM_QUALIFIED_ID);
189: }
190: break;
191: case CPPTokenTypes.CSM_GENERIC_DECLARATION:
192: CsmTypedef[] typedefs = renderTypedef(token,
193: (FileImpl) getContainingFile(), getScope());
194: if (typedefs != null && typedefs.length > 0) {
195: for (int i = 0; i < typedefs.length; i++) {
196: declarators.add(typedefs[i]);
197: }
198: }
199: }
200: }
201: }
202:
203: // Never used
204: // /**
205: // * Creates a variable for declaration like int x(y);
206: // * Returns a token that follows this declaration or null
207: // */
208: // private AST createVarWithCtor(AST token) {
209: // assert(token.getType() == CPPTokenTypes.CSM_TYPE_BUILTIN || token.getType() == CPPTokenTypes.CSM_TYPE_COMPOUND);
210: // AST typeToken = token;
211: // AST next = token.getNextSibling();
212: // if( next != null && next.getType() == CPPTokenTypes.CSM_QUALIFIED_ID ) {
213: // TypeImpl type;
214: // if( typeToken.getType() == CPPTokenTypes.CSM_TYPE_BUILTIN ) {
215: // type = TypeFactory.createBuiltinType(typeToken.getText(), null, 0, typeToken, getContainingFile());
216: // }
217: // else {
218: // type = TypeFactory.createType(typeToken, getContainingFile(), null, 0);
219: // }
220: // String name = next.getText();
221: // VariableImpl var = new VariableImpl(next, getContainingFile(), type, name, true);
222: // // we ignore both currentNamespace and container
223: // declarators.add(var);
224: // // eat all tokens up to the comma that separates the next decl
225: // next = next.getNextSibling();
226: // if( next != null && next.getType() == CPPTokenTypes.CSM_PARMLIST ) {
227: // next = next.getNextSibling();
228: // }
229: // if( next != null && next.getType() == CPPTokenTypes.COMMA ) {
230: // next = next.getNextSibling();
231: // }
232: // return next;
233: // }
234: // return null;
235: // }
236: }
237:
238: }
|