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: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * The Original Software is NetBeans. The Initial Developer of the Original
038: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
039: * Microsystems, Inc. All Rights Reserved.
040: *
041: * If you wish your version of this file to be governed by only the CDDL
042: * or only the GPL Version 2, indicate your decision by adding
043: * "[Contributor] elects to include this software in this distribution
044: * under the [CDDL or GPL Version 2] license." If you do not indicate a
045: * single choice of license, a recipient has the option to distribute
046: * your version of this file under either the CDDL, the GPL Version 2 or
047: * to extend the choice of license to its licensees as provided above.
048: * However, if you add GPL Version 2 code and therefore, elected the GPL
049: * Version 2 license, then the option applies only if the new code is
050: * made subject to such option by the copyright holder.
051: */
052:
053: package org.netbeans.modules.cnd.xref.impl;
054:
055: import org.netbeans.modules.cnd.api.model.CsmClass;
056: import org.netbeans.modules.cnd.api.model.CsmFile;
057: import org.netbeans.modules.cnd.api.model.CsmFunctionDefinition;
058: import org.netbeans.modules.cnd.api.model.CsmIdentifiable;
059: import org.netbeans.modules.cnd.api.model.CsmObject;
060: import org.netbeans.modules.cnd.api.model.CsmOffsetable;
061: import org.netbeans.modules.cnd.api.model.CsmUID;
062: import org.netbeans.modules.cnd.api.model.util.CsmBaseUtilities;
063: import org.netbeans.modules.cnd.api.model.util.CsmKindUtilities;
064: import org.netbeans.modules.cnd.api.model.xref.CsmReference;
065: import org.netbeans.modules.cnd.api.model.xref.CsmReferenceKind;
066:
067: /**
068: *
069: * @author Vladimir Voskresensky
070: */
071: public class ReferenceSupportImpl {
072: public CsmReference createObjectReference(CsmOffsetable obj) {
073: return createObjectReference(obj, obj);
074: }
075:
076: public CsmReference createObjectReference(CsmObject target,
077: CsmOffsetable owner) {
078: int start = getStartRefenceOffset(owner);
079: int end = getEndReferenceOffset(owner);
080: CsmUID<CsmObject> targetUID = target != null ? getUID(target)
081: : null;
082: CsmUID<CsmObject> ownerUID = getUID((CsmObject) owner);
083: CsmUID<CsmFile> fileUID = getUID(owner.getContainingFile());
084: CsmReferenceKind kind = getObjectKind(target, owner);
085: return new ObjectReferenceImpl(targetUID, ownerUID, fileUID,
086: kind, start, end);
087: }
088:
089: private CsmReferenceKind getObjectKind(CsmObject target,
090: CsmObject owner) {
091: CsmReferenceKind kind = CsmReferenceKind.UNKNOWN;
092: if (target == null && CsmKindUtilities.isInclude(owner)) {
093: kind = CsmReferenceKind.DIRECT_USAGE;
094: } else if (CsmKindUtilities.isFile(target)) {
095: kind = CsmReferenceKind.DIRECT_USAGE;
096: } else if (target != null) {
097: if (owner != null && !owner.equals(target)) {
098: CsmObject[] decDef = CsmBaseUtilities
099: .getDefinitionDeclaration(target, true);
100: CsmObject targetDecl = decDef[0];
101: CsmObject targetDef = decDef[1];
102: kind = CsmReferenceKind.DIRECT_USAGE;
103: if (owner.equals(targetDecl)) {
104: kind = CsmReferenceKind.DECLARATION;
105: } else if (owner.equals(targetDef)) {
106: kind = CsmReferenceKind.DEFINITION;
107: }
108: } else {
109: kind = CsmReferenceKind.DECLARATION;
110: if (CsmKindUtilities.isFunctionDefinition(target)
111: || CsmKindUtilities
112: .isVariableDefinition(target)) {
113: kind = CsmReferenceKind.DEFINITION;
114: }
115: }
116: }
117: return kind;
118: }
119:
120: private int getStartRefenceOffset(CsmOffsetable obj) {
121: return obj.getStartOffset();
122: }
123:
124: private int getEndReferenceOffset(CsmOffsetable obj) {
125: int end = obj.getEndOffset();
126: if (CsmKindUtilities.isClass(obj)) {
127: end = ((CsmClass) obj).getLeftBracketOffset();
128: } else if (CsmKindUtilities.isFunctionDefinition(obj)) {
129: if (((CsmFunctionDefinition) obj).getBody() != null) {
130: end = ((CsmFunctionDefinition) obj).getBody()
131: .getStartOffset();
132: }
133: }
134: return end;
135: }
136:
137: @SuppressWarnings("unchecked")
138: public <T extends CsmObject> CsmUID<T> getUID(T element) {
139: CsmUID<T> uid = null;
140: if (CsmKindUtilities.isIdentifiable(element)) {
141: uid = ((CsmIdentifiable<T>) element).getUID();
142: boolean checkAssert = false;
143: assert checkAssert = true;
144: if (checkAssert && (uid.getObject() == null)) {
145: System.err.println("UID " + uid
146: + "can't return object " + element);
147: uid = null;
148: }
149: }
150: if (uid == null) {
151: uid = new SelfUID(element);
152: }
153: return uid;
154: }
155:
156: private static final class SelfUID<T> implements CsmUID<T> {
157: private final T element;
158:
159: SelfUID(T element) {
160: this .element = element;
161: }
162:
163: public T getObject() {
164: return this.element;
165: }
166: }
167: }
|