List of usage examples for org.eclipse.jdt.internal.compiler.lookup Scope getTypeOrPackage
public final Binding getTypeOrPackage(char[][] compoundName)
From source file:org.eclipse.objectteams.otdt.internal.core.compiler.ast.TypeAnchorReference.java
License:Open Source License
ITeamAnchor resolveAnchor(Scope scope, Reference reference) { ITeamAnchor prefix = null;/*from w w w. jav a 2 s. c o m*/ ITeamAnchor currentAnchor = null; char[] currentToken = null; // for lookup and creation of problem binding // be careful not to trigger fields() which may be where we are called from! if (reference instanceof SingleNameReference) { SingleNameReference singleAnchor = (SingleNameReference) reference; currentToken = singleAnchor.token; currentAnchor = findVariable(scope, currentToken, scope.isStatic(), singleAnchor.sourceStart, singleAnchor.sourceEnd); this.anchor.bits |= (this.bits & DepthMASK); // could be ProblemAnchorBinding } else if (reference instanceof FieldReference) { FieldReference fieldRef = (FieldReference) reference; Expression prefixExpr = fieldRef.receiver; if (!(prefixExpr instanceof Reference)) throw new InternalCompilerError("Unexpected anchor prefix " + prefixExpr); //$NON-NLS-1$ prefix = resolveAnchor(scope, (Reference) prefixExpr); currentToken = fieldRef.token; // fieldRef holds on to problem binding: fieldRef.binding = TypeAnalyzer.findField(((ReferenceBinding) prefix.getResolvedType()).getRealClass(), currentToken, false/*static*/, true/*outer*/); currentAnchor = checkAnchor(scope, reference, currentToken, reference.sourceStart, reference.sourceEnd, fieldRef.binding); } else if (reference instanceof QualifiedBaseReference) { QualifiedBaseReference baseRef = (QualifiedBaseReference) reference; if (scope instanceof BlockScope) baseRef.resolveType((BlockScope) scope); else baseRef.resolveType((ClassScope) scope); currentAnchor = baseRef.baseField; } else if (reference instanceof QualifiedThisReference) { QualifiedThisReference thisRef = (QualifiedThisReference) reference; if (scope instanceof BlockScope) thisRef.resolveType((BlockScope) scope); else thisRef.resolveType((ClassScope) scope); if (thisRef.resolvedType.isTeam()) currentAnchor = ((ReferenceBinding) thisRef.resolvedType).getTeamModel().getTThis(); } else { boolean haveReportedProblem = false; long currentPos = 0; QualifiedNameReference qualifiedAnchor = (QualifiedNameReference) reference; char[][] tokens = qualifiedAnchor.tokens; currentToken = tokens[tokens.length - 1]; // default, so we never use null name for problem binding // check maximal static prefix: Binding staticPrefix = null; int j; for (j = 1; j <= tokens.length; j++) { Binding current = scope.getTypeOrPackage(CharOperation.subarray(tokens, 0, j)); if (current == null || !current.isValidBinding()) break; else staticPrefix = current; } if (j > tokens.length) { scope.problemReporter().typeAnchorReferenceNotAValue(reference); haveReportedProblem = true; } else { // find first field: if (staticPrefix != null) { currentPos = qualifiedAnchor.sourcePositions[j - 1]; currentToken = tokens[j - 1]; if (staticPrefix instanceof ReferenceBinding) { currentAnchor = TypeAnalyzer.findField(((ReferenceBinding) staticPrefix).getRealClass(), currentToken, /*static*/true, /*outer*/true); } else { scope.problemReporter().typeAnchorReferenceNotAnObjectRef((int) (currentPos >>> 32), (int) currentPos); haveReportedProblem = true; } } else { currentPos = qualifiedAnchor.sourcePositions[0]; currentToken = tokens[0]; currentAnchor = findVariable(scope, currentToken, scope.isStatic(), (int) (currentPos >>> 32), (int) currentPos); haveReportedProblem = currentAnchor == null; } if (currentAnchor != null) { if (!currentAnchor.hasValidReferenceType()) { if (j < tokens.length) // would need to process more tokens? currentAnchor = null; // replace with problem binding below } else { // find more fields: for (int i = j; i < tokens.length; i++) { TypeBinding fieldType = currentAnchor.getResolvedType().leafComponentType(); if (fieldType instanceof SourceTypeBinding) { SourceTypeBinding stb = (SourceTypeBinding) fieldType; if ((stb.scope != null) && (stb.scope.compilationUnitScope() != scope.compilationUnitScope()) && (stb.tagBits & (TagBits.BeginHierarchyCheck | TagBits.EndHierarchyCheck)) == (TagBits.BeginHierarchyCheck | TagBits.EndHierarchyCheck) && StateHelper.isReadyToProcess(stb, ITranslationStates.STATE_LENV_DONE_FIELDS_AND_METHODS)) Dependencies.ensureBindingState(stb, ITranslationStates.STATE_LENV_DONE_FIELDS_AND_METHODS); } currentPos = qualifiedAnchor.sourcePositions[i]; currentToken = tokens[i]; FieldBinding nextField = currentAnchor.getFieldOfType(currentToken, /*static*/false, true); if (nextField == null || !nextField.hasValidReferenceType()) { currentAnchor = null; // replace with problem binding below break; } currentAnchor = nextField.setPathPrefix(currentAnchor); } } } } if (!haveReportedProblem) { if (currentAnchor == null) { scope.problemReporter().typeAnchorNotFound(currentToken, (int) (currentPos >>> 32), (int) currentPos); } else if (!currentAnchor.hasValidReferenceType()) { scope.problemReporter().typeAnchorReferenceNotAnObjectRef((int) (currentPos >>> 32), (int) currentPos); } } } if (currentAnchor == null) { currentAnchor = new ProblemFieldBinding(scope.enclosingReceiverType(), currentToken, ProblemReasons.NotFound); ((FieldBinding) currentAnchor).type = reference.resolvedType = new ProblemReferenceBinding( "UnresolvedType".toCharArray(), null, ProblemReasons.NotFound); //$NON-NLS-1$ } else if (currentAnchor.isValidBinding()) { if (prefix != null && !(prefix instanceof TThisBinding)) currentAnchor = currentAnchor.setPathPrefix(prefix); // fill anchor with resolved data: reference.resolvedType = currentAnchor.getResolvedType(); reference.bits &= ~RestrictiveFlagMASK; // clear bits if (currentAnchor instanceof FieldBinding) { reference.bits |= Binding.FIELD; // TODO(SH): must we remember a previous anchor to set this correctly?: if (reference instanceof NameReference) ((NameReference) reference).actualReceiverType = ((FieldBinding) currentAnchor).declaringClass; if (reference instanceof FieldReference) ((FieldReference) reference).actualReceiverType = ((FieldBinding) currentAnchor).declaringClass; } else { reference.bits |= Binding.LOCAL; } reference.constant = Constant.NotAConstant; } if (reference instanceof NameReference) { ((NameReference) reference).binding = (Binding) currentAnchor; ((NameReference) reference).resolveFinished(); } else if (reference instanceof FieldReference) { ((FieldReference) reference).binding = (FieldBinding) currentAnchor; //TODO(SH): this method doesn't exist, is the call needed? //((FieldReference)this.anchor).resolveFinished(); } return currentAnchor; }
From source file:org.eclipse.objectteams.otdt.internal.core.compiler.ast.TypeAnchorReference.java
License:Open Source License
/** * If this reference has a static prefix return its resolved type. * (obviously only for qualified anchors). *//*from ww w.java 2 s .c o m*/ public ReferenceBinding resolveStaticPart(Scope scope) { // extract from above method: // (but cannot use this above, because more than one local variable // would need to be returned to the caller). if (!(this.anchor instanceof QualifiedNameReference)) return null; QualifiedNameReference qualifiedAnchor = (QualifiedNameReference) this.anchor; char[][] tokens = qualifiedAnchor.tokens; // check maximal static prefix: Binding staticPrefix = null; int j; for (j = 1; j <= tokens.length; j++) { Binding current = scope.getTypeOrPackage(CharOperation.subarray(tokens, 0, j)); if (current == null || !current.isValidBinding()) break; else staticPrefix = current; } if (j > tokens.length) { scope.problemReporter().typeAnchorReferenceNotAValue(this.anchor); return null; } if (staticPrefix instanceof ReferenceBinding) return (ReferenceBinding) staticPrefix; return null; }
From source file:org.eclipse.objectteams.otdt.internal.core.compiler.util.RoleTypeCreator.java
License:Open Source License
/** * Resolve an anchored type given the tokens of its path including the type. * * TODO(SH): validate: this hands out the ifc-part of an anchor's type! * * @param scope/*from w ww .j a va2 s . com*/ * @param typeExpression used for baseclass decapsulation * @param tokens * @param dimensions >0 signals an array type * @return a RoleTypeBinding or null (path does not denote an anchored type) or * a ProblemReferenceBinding with reason AnchorNotFinal or AnchorNotATeam. * For ProblemReferenceBindings the problem should not be reported yet. */ public static TypeBinding resolveAnchoredType(Scope scope, Expression typeExpression, char[][] tokens, int dimensions) { // first try a static prefix "some.package.Some.Type": int variableStart = 0; ReferenceBinding staticType = null; boolean havePackagePrefix = false; // check at most length-2 elements, leaving space for anchor.Type: for (int i = 0; i < tokens.length - 2; i++) { Binding staticPart = scope.getTypeOrPackage(CharOperation.subarray(tokens, 0, i + 1)); if (staticPart == null || !staticPart.isValidBinding()) { break; } if (staticPart instanceof ReferenceBinding) { staticType = (ReferenceBinding) staticPart; variableStart = i + 1; } else { havePackagePrefix = true; } } // get the first variable: ITeamAnchor anchor; if (staticType != null) { boolean isStatic = true; // Skip "this" in "Outer.this", because it is not the start of a variable part: if (CharOperation.equals(tokens[variableStart], "this".toCharArray())) { //$NON-NLS-1$ // static type remains as it is variableStart++; if (variableStart >= tokens.length) return null; isStatic = false; } anchor = TypeAnalyzer.findField(staticType, tokens[variableStart], isStatic, /*outer*/ true); } else { try { anchor = findResolvedVariable(scope, tokens[0]); } catch (InternalCompilerError ice) { // workaround to avoid "Prematurely searching field ..." regarding FQ class names if (havePackagePrefix) return null; else throw ice; } } if (anchor == null) return null; // from this point, as we found token[0] is a valid variable, don't return // null any more but only problems. ProblemReferenceBinding foundProblem = null; if (anchor.isValidBinding() && !anchor.isFinal()) { // t.T with t not a Team => plain Java error if (!anchor.isTeam() && variableStart == tokens.length - 2) return new ProblemReferenceBinding(tokens, null, ProblemReasons.NotFound); // OT-error on first segment might be irrelevant if the rest doesn't // resolve to type any way: foundProblem = new ProblemReferenceBinding(anchor, tokens[tokens.length - 1], null, ProblemReasons.AnchorNotFinal); } // build the anchor with appropriate bestNamePath from subsequent fields: TypeBinding resolved = resolveOtherPathElements(scope, typeExpression, anchor, tokens, variableStart + 1, dimensions); if (resolved != null && resolved.isValidBinding() && foundProblem != null) return foundProblem; // other elements are OK => report the first problem. return resolved; }