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;
043:
044: import org.netbeans.modules.cnd.api.model.*;
045: import java.util.*;
046: import antlr.collections.AST;
047: import java.io.DataInput;
048: import java.io.DataOutput;
049: import java.io.IOException;
050: import org.netbeans.modules.cnd.modelimpl.parser.generated.CPPTokenTypes;
051: import org.netbeans.modules.cnd.modelimpl.csm.core.*;
052: import org.netbeans.modules.cnd.modelimpl.repository.PersistentUtils;
053: import org.netbeans.modules.cnd.modelimpl.textcache.NameCache;
054: import org.netbeans.modules.cnd.modelimpl.textcache.QualifiedNameCache;
055:
056: /**
057: * A class that
058: * 1) represents function template specilaization declaration (without body)
059: * 2) acts as a base class for FunctionDefinitionImpl.
060: * In other words, it corresponds to function that has a double colon "::" in its name
061: * @param T
062: * @author Vladimir Kvashin
063: */
064: public class FunctionImplEx<T> extends FunctionImpl<T> {
065:
066: private CharSequence qualifiedName;
067: private boolean qualifiedNameIsFake = false;
068: private final CharSequence[] classOrNspNames;
069:
070: public FunctionImplEx(AST ast, CsmFile file, CsmScope scope) {
071: this (ast, file, scope, true);
072: }
073:
074: protected FunctionImplEx(AST ast, CsmFile file, CsmScope scope,
075: boolean register) {
076: super (ast, file, scope, false);
077: classOrNspNames = CastUtils.isCast(ast) ? CastUtils
078: .getClassOrNspNames(ast) : initClassOrNspNames(ast);
079: if (register) {
080: registerInProject();
081: }
082: }
083:
084: /** @return either class or namespace */
085: protected CsmObject findOwner(Resolver parent) {
086: CharSequence[] cnn = classOrNspNames;
087: if (cnn != null) {
088: CsmObject obj = ResolverFactory
089: .createResolver(this , parent).resolve(cnn,
090: Resolver.CLASSIFIER | Resolver.NAMESPACE);
091: if (obj instanceof CsmClass) {
092: if (!(obj instanceof Unresolved.UnresolvedClass)) {
093: return (CsmClass) obj;
094: }
095: } else if (obj instanceof CsmNamespace) {
096: return (CsmNamespace) obj;
097: }
098: }
099: return null;
100: }
101:
102: public static String[] initClassOrNspNames(AST node) {
103: //qualified id
104: AST qid = AstUtil.findMethodName(node);
105: if (qid == null) {
106: return null;
107: }
108: int cnt = qid.getNumberOfChildren();
109: if (cnt >= 1) {
110: List/*<String>*/l = new ArrayList/*<String>*/();
111: for (AST token = qid.getFirstChild(); token != null; token = token
112: .getNextSibling()) {
113: if (token.getType() == CPPTokenTypes.ID) {
114: if (token.getNextSibling() != null) {
115: l.add(token.getText());
116: }
117: }
118: }
119: return (String[]) l.toArray(new String[l.size()]);
120: }
121: return null;
122: }
123:
124: @Override
125: public CharSequence getQualifiedName() {
126: if (qualifiedName == null) {
127: qualifiedName = QualifiedNameCache.getManager().getString(
128: findQualifiedName());
129: }
130: return qualifiedName;
131: }
132:
133: protected String findQualifiedName() {
134: CsmObject owner = findOwner(null);
135: if (owner instanceof CsmQualifiedNamedElement) {
136: qualifiedNameIsFake = false;
137: return ((CsmQualifiedNamedElement) owner)
138: .getQualifiedName().toString()
139: + getScopeSuffix()
140: + "::"
141: + getQualifiedNamePostfix(); // NOI18N
142: } else {
143: qualifiedNameIsFake = true;
144: CharSequence[] cnn = classOrNspNames;
145: CsmNamespaceDefinition nsd = findNamespaceDefinition();
146: StringBuilder sb = new StringBuilder();
147: if (nsd != null) {
148: sb.append(nsd.getQualifiedName());
149: }
150: if (cnn != null) {
151: for (int i = 0; i < cnn.length; i++) {
152: if (sb.length() > 0) {
153: sb.append("::"); // NOI18N
154: }
155: sb.append(cnn[i]);
156: }
157: }
158: if (sb.length() == 0) {
159: sb.append("unknown>"); // NOI18N
160: }
161: sb.append(getScopeSuffix());
162: sb.append("::"); // NOI18N
163: sb.append(getQualifiedNamePostfix());
164: return sb.toString();
165: }
166: }
167:
168: @Override
169: protected void registerInProject() {
170: super .registerInProject();
171: if (qualifiedNameIsFake) {
172: ((FileImpl) getContainingFile()).onFakeRegisration(this );
173: }
174: }
175:
176: public void fixFakeRegistration() {
177: CharSequence newQname = QualifiedNameCache.getManager()
178: .getString(findQualifiedName());
179: if (!newQname.equals(qualifiedName)) {
180: ProjectBase aProject = ((FileImpl) getContainingFile())
181: .getProjectImpl();
182: aProject.unregisterDeclaration(this );
183: this .cleanUID();
184: qualifiedName = newQname;
185: aProject.registerDeclaration(this );
186: }
187: }
188:
189: private CsmNamespaceDefinition findNamespaceDefinition() {
190: return findNamespaceDefinition(getContainingFile()
191: .getDeclarations());
192: }
193:
194: private CsmNamespaceDefinition findNamespaceDefinition(
195: Collection/*<CsmOffsetableDeclaration>*/declarations) {
196: for (Iterator it = declarations.iterator(); it.hasNext();) {
197: CsmOffsetableDeclaration decl = (CsmOffsetableDeclaration) it
198: .next();
199: if (decl.getStartOffset() > this .getStartOffset()) {
200: break;
201: }
202: if (decl.getKind() == CsmDeclaration.Kind.NAMESPACE_DEFINITION) {
203: if (this .getEndOffset() < decl.getEndOffset()) {
204: CsmNamespaceDefinition nsdef = (CsmNamespaceDefinition) decl;
205: CsmNamespaceDefinition inner = findNamespaceDefinition(nsdef
206: .getDeclarations());
207: return (inner == null) ? nsdef : inner;
208: }
209: }
210: }
211: return null;
212: }
213:
214: ////////////////////////////////////////////////////////////////////////////
215: // impl of SelfPersistent
216:
217: @Override
218: public void write(DataOutput output) throws IOException {
219: super .write(output);
220: // can be null
221: PersistentUtils.writeUTF(this .qualifiedName, output);
222: output.writeBoolean(this .qualifiedNameIsFake);
223: PersistentUtils.writeStrings(this .classOrNspNames, output);
224: }
225:
226: public FunctionImplEx(DataInput input) throws IOException {
227: super (input);
228: // can be null
229: String read = PersistentUtils.readUTF(input);
230: this.qualifiedName = read == null ? null : QualifiedNameCache
231: .getManager().getString(read);
232: this.qualifiedNameIsFake = input.readBoolean();
233: this.classOrNspNames = PersistentUtils.readStrings(input,
234: NameCache.getManager());
235: }
236: }
|