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.completion.csm;
043:
044: import java.util.Collection;
045: import org.netbeans.modules.cnd.api.model.CsmClass;
046: import org.netbeans.modules.cnd.api.model.CsmFile;
047: import org.netbeans.modules.cnd.api.model.CsmFunction;
048: import org.netbeans.modules.cnd.api.model.CsmFunctionDefinition;
049: import org.netbeans.modules.cnd.api.model.CsmInheritance;
050: import org.netbeans.modules.cnd.api.model.CsmInitializerListContainer;
051: import org.netbeans.modules.cnd.api.model.CsmObject;
052: import org.netbeans.modules.cnd.api.model.CsmType;
053: import org.netbeans.modules.cnd.api.model.util.CsmKindUtilities;
054: import org.netbeans.modules.cnd.api.model.CsmParameter;
055: import org.netbeans.modules.cnd.api.model.CsmVariable;
056: import org.netbeans.modules.cnd.api.model.deep.CsmExpression;
057:
058: /**
059: * resolve file objects under offset
060: * @author vv159170
061: */
062: public class CsmOffsetResolver {
063: private CsmFile file;
064:
065: /** Creates a new instance of CsmOffsetResolver */
066: public CsmOffsetResolver() {
067: }
068:
069: public CsmOffsetResolver(CsmFile file) {
070: this .file = file;
071: }
072:
073: public CsmFile getFile() {
074: return file;
075: }
076:
077: public void setFile(CsmFile file) {
078: this .file = file;
079: }
080:
081: // =================== resolve object under offset ============================
082:
083: public CsmObject findObject(int offset) {
084: return findObject(file, offset);
085: }
086:
087: // ==================== help methods =======================================
088:
089: public static CsmObject findObject(CsmFile file, int offset) {
090: assert (file != null) : "can't be null file in findObject";
091: // not interested in context, only object under offset
092: CsmObject last = findObjectWithContext(file, offset, null);
093: return last;
094: }
095:
096: public static CsmObject findObjectWithContext(CsmFile file,
097: int offset, CsmContext context) {
098: assert (file != null) : "can't be null file in findObject";
099: CsmObject last = null;
100: if (context == null) {
101: // create dummy context
102: context = new CsmContext(offset);
103: }
104: CsmObject lastObj = CsmDeclarationResolver.findInnerFileObject(
105: file, offset, context);
106: last = lastObj;
107: // for functions search deeper
108: if (CsmKindUtilities.isFunction(lastObj)) {
109: CsmFunction fun = (CsmFunction) lastObj;
110: // check if offset in return value
111: CsmType retType = fun.getReturnType();
112: if (CsmOffsetUtilities.isInObject(retType, offset)) {
113: context.setLastObject(retType);
114: return retType;
115: }
116: // check if offset in parameters
117: Collection<CsmParameter> params = fun.getParameters();
118: CsmParameter param = CsmOffsetUtilities.findObject(params,
119: context, offset);
120: if (param != null) {
121: CsmType type = param.getType();
122: if (CsmOffsetUtilities.isInObject(type, offset)) {
123: context.setLastObject(type);
124: return type;
125: }
126: return param;
127: }
128: // check for constructor initializers
129: if (CsmKindUtilities.isConstructor(lastObj)) {
130: CsmInitializerListContainer ctor = (CsmInitializerListContainer) lastObj;
131: for (CsmExpression izer : ctor.getInitializerList()) {
132: if (CsmOffsetUtilities.isInObject(izer, offset)) {
133: context.setLastObject(izer);
134: return izer;
135: }
136: }
137: }
138: // for function definition search deeper in body's statements
139: if (CsmKindUtilities.isFunctionDefinition(lastObj)) {
140: CsmFunctionDefinition funDef = (CsmFunctionDefinition) lastObj;
141: if (CsmOffsetUtilities.isInObject(funDef.getBody(),
142: offset)) {
143: last = null;
144: // offset is in body, try to find inners statement
145: if (CsmStatementResolver.findInnerObject(funDef
146: .getBody(), offset, context)) {
147: // if found exact object => return it, otherwise return last found scope
148: last = context.getLastObject();
149: }
150: }
151: }
152: } else if (CsmKindUtilities.isClass(lastObj)) {
153: // check if in inheritance part
154: CsmClass clazz = (CsmClass) lastObj;
155: Collection<CsmInheritance> inherits = clazz
156: .getBaseClasses();
157: CsmInheritance inh = CsmOffsetUtilities.findObject(
158: inherits, context, offset);
159: if (inh != null) {
160: last = inh;
161: }
162: } else if (CsmKindUtilities.isVariable(lastObj)) {
163: CsmType type = ((CsmVariable) lastObj).getType();
164: if (CsmOffsetUtilities.isInObject(type, offset)) {
165: context.setLastObject(type);
166: last = type;
167: }
168: }
169: return last;
170: }
171:
172: public static CsmContext findContext(CsmFile file, int offset) {
173: CsmContext context = new CsmContext(offset);
174: findObjectWithContext(file, offset, context);
175: return context;
176: }
177: }
|