List of usage examples for org.eclipse.jdt.internal.compiler.lookup BlockScope referenceType
public TypeDeclaration referenceType()
From source file:org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding.java
License:Open Source License
public FieldBinding addSyntheticFieldForClassLiteral(TypeBinding targetType, BlockScope blockScope) { if (this.synthetics == null) this.synthetics = new HashMap[MAX_SYNTHETICS]; if (this.synthetics[SourceTypeBinding.CLASS_LITERAL_EMUL] == null) this.synthetics[SourceTypeBinding.CLASS_LITERAL_EMUL] = new HashMap(5); // use a different table than FIELDS, given there might be a collision between emulation of X.this$0 and X.class. FieldBinding synthField = (FieldBinding) this.synthetics[SourceTypeBinding.CLASS_LITERAL_EMUL] .get(targetType);/*from ww w . j a v a 2s. c o m*/ if (synthField == null) { synthField = new SyntheticFieldBinding( CharOperation.concat(TypeConstants.SYNTHETIC_CLASS, String.valueOf(this.synthetics[SourceTypeBinding.CLASS_LITERAL_EMUL].size()) .toCharArray()), blockScope.getJavaLangClass(), ClassFileConstants.AccDefault | ClassFileConstants.AccStatic | ClassFileConstants.AccSynthetic, this, Constant.NotAConstant, this.synthetics[SourceTypeBinding.CLASS_LITERAL_EMUL].size()); this.synthetics[SourceTypeBinding.CLASS_LITERAL_EMUL].put(targetType, synthField); } // ensure there is not already such a field defined by the user FieldBinding existingField; if ((existingField = getField(synthField.name, true /*resolve*/)) != null) { TypeDeclaration typeDecl = blockScope.referenceType(); FieldDeclaration[] typeDeclarationFields = typeDecl.fields; int max = typeDeclarationFields == null ? 0 : typeDeclarationFields.length; for (int i = 0; i < max; i++) { FieldDeclaration fieldDecl = typeDeclarationFields[i]; if (fieldDecl.binding == existingField) { blockScope.problemReporter().duplicateFieldInType(this, fieldDecl); break; } } } return synthField; }
From source file:org.eclipse.objectteams.otdt.internal.core.compiler.ast.BaseAllocationExpression.java
License:Open Source License
/** * Initialize the type to create from the "playedBy" clause. * @param scope non-null/* w w w . j ava 2 s .c o m*/ */ private void createAst(BlockScope scope) { if (this.isAstCreated) return; // already done. this.isAstCreated = true; // even if creation fails, don't try again. ReferenceBinding enclType; AbstractMethodDeclaration enclMethodDecl; ReferenceBinding baseclass = null; enclType = scope.enclosingSourceType(); enclMethodDecl = (AbstractMethodDeclaration) scope.methodScope().referenceContext; if (enclType.isDirectRole()) baseclass = ((MemberTypeBinding) enclType).baseclass(); if (baseclass == null || !enclMethodDecl.isConstructor()) { scope.problemReporter().baseConstructorCallInWrongMethod(this, scope.methodScope().referenceContext); return; } ConstructorDeclaration enclCtor = (ConstructorDeclaration) enclMethodDecl; if (this.isExpression) { if (!isArgOfOtherCtor(enclCtor, scope)) scope.problemReporter().baseConstructorExpressionOutsideCtorCall(this); } else { if (enclCtor.statements[0] != this) scope.problemReporter().baseConstructorCallIsNotFirst(this); } AstGenerator gen = new AstGenerator(this.sourceStart, this.sourceEnd); Expression allocation; if (this.enclosingInstance != null) { this.enclosingInstance = new PotentialLowerExpression(this.enclosingInstance, baseclass.enclosingType()); // FIXME(SH): check baseclass.enclosingType(); } if (baseclass.isDirectRole()) { // instead of new B() create: // receiver._OT$createB(): Expression receiver; if (RoleTypeBinding.isRoleWithExplicitAnchor(baseclass)) { RoleTypeBinding baseRole = (RoleTypeBinding) baseclass; ITeamAnchor anchor = baseRole._teamAnchor; ReferenceBinding startClass = anchor.getFirstDeclaringClass(); char[][] tokens = anchor.tokens(); if (startClass != null) { // relevant start class, create as receiver: // EnclType.this.field1. TypeReference startReference = gen.typeReference(startClass); startReference.setBaseclassDecapsulation(DecapsulationState.ALLOWED); receiver = gen.qualifiedThisReference(startReference); for (int i = 0; i < tokens.length; i++) { receiver = gen.fieldReference(receiver, tokens[i]); } } else { // the best name path defines the receiver: receiver = gen.qualifiedNameReference(tokens); } } else { if (this.enclosingInstance != null) { receiver = this.enclosingInstance; } else { if (TypeBinding.equalsEquals(baseclass.enclosingType(), enclType.enclosingType())) receiver = gen.thisReference(); // creating a role of the same team as base instance?? else receiver = gen.qualifiedThisReference(gen.typeReference(baseclass.enclosingType())); } } char[] selector = CharOperation.concat(CREATOR_PREFIX_NAME, baseclass.sourceName()); MessageSend allocSend = new MessageSend() { @Override public boolean isDecapsulationAllowed(Scope scope2) { // this message send can decapsulate independent of scope return true; } @Override public DecapsulationState getBaseclassDecapsulation() { return DecapsulationState.ALLOWED; } }; gen.setPositions(allocSend); allocSend.receiver = receiver; allocSend.selector = selector; allocSend.arguments = this.arguments; allocSend.accessId = -1; // request that MessageSend.resolveType() assigns a fresh accessId if decapsulation is detected allocation = allocSend; } else { AllocationExpression alloc = newAllocation(baseclass, gen); alloc.type.setBaseclassDecapsulation(DecapsulationState.ALLOWED); // report individually alloc.arguments = this.arguments; alloc.sourceStart = this.sourceStart; alloc.sourceEnd = this.sourceEnd; alloc.statementEnd = this.statementEnd; allocation = alloc; } this.arguments = null; // don't use any more. ExplicitConstructorCall selfcall = enclCtor.constructorCall; if (selfcall.isImplicitSuper() && enclType.superclass().isDirectRole() && enclType.superclass().baseclass() != null) { // implement 2.4.2(c): // transform "super(); base(args);" => "super(new MyBase(args)); nop;" enclCtor.constructorCall = genLiftCtorCall(allocation); enclCtor.statements[0] = new AstGenerator(this.sourceStart, this.sourceEnd).emptyStatement(); // pretend we are not calling base() because we already call the lifting-ctor. } else if (this.isExpression) { // similar to above: // translate "super(base(args), ...);" as "super(new MyBase(args), ...);" this.expression = allocation; // and ignore the assignment flavor of this node. } else { // needed by ASTConverter: this.expression = allocation; if (!enclType.roleModel.hasBaseclassProblem() && !scope.referenceType().ignoreFurtherInvestigation) { MethodModel.setCallsBaseCtor(enclCtor); // really creating base here, need to register this base object RoleModel boundRootRoleModel = enclType.roleModel.getBoundRootRole(); if (boundRootRoleModel == null) throw new InternalCompilerError( "Unexpected: role has neither baseclassProblem nor boundRootRole"); //$NON-NLS-1$ Statement[] regStats = Lifting.genRoleRegistrationStatements(scope, boundRootRoleModel, baseclass, enclCtor, gen); int len = enclCtor.statements.length; Statement[] newStats = new Statement[len + regStats.length]; newStats[0] = this; System.arraycopy(regStats, 0, newStats, 1, regStats.length); System.arraycopy(enclCtor.statements, 1, newStats, regStats.length + 1, len - 1); enclCtor.setStatements(newStats); } } }
From source file:org.eclipse.objectteams.otdt.internal.core.compiler.ast.BaseAllocationExpression.java
License:Open Source License
public void traverse(ASTVisitor visitor, BlockScope scope) { // we might be the first to analyse this expression: // (Actually triggered by TransformStatementsVisitor // - but don't do it before STATE_LENV_DONE_FIELDS_AND_METHODS, // which is needed to lookup baseclass()!) TypeDeclaration enclType = (scope != null) ? scope.referenceType() : null; if (enclType != null && enclType.isDirectRole() && StateHelper.hasState(enclType.binding, ITranslationStates.STATE_LENV_DONE_FIELDS_AND_METHODS)) { if (checkGenerate(scope)) { // only if successful: // when called from createAst->isArgOfOtherCtor we don't yet have the expression generated if (this.isExpression && this.expression != null) this.expression.traverse(visitor, scope); else/* w w w. j a v a2s . c om*/ super.traverse(visitor, scope); } } else { if (this.expression != null) super.traverse(visitor, scope); } }
From source file:org.eclipse.objectteams.otdt.internal.core.compiler.ast.BaseAllocationExpression.java
License:Open Source License
public TypeBinding resolveType(BlockScope scope) { TypeDeclaration roleDecl = scope.referenceType(); if (roleDecl != null && roleDecl.isRole() && roleDecl.getRoleModel()._playedByEnclosing) { scope.problemReporter().baseAllocationDespiteBaseclassCycle(this, roleDecl); return null; }/*from www . j a v a 2s .c o m*/ if (!checkGenerate(scope)) { // createAst failed. return null; } if (this.isExpression) // don't treat as assignment return this.resolvedType = this.expression.resolveType(scope); if (!scope.methodScope().referenceContext.hasErrors()) return super.resolveType(scope); return null; }
From source file:org.eclipse.objectteams.otdt.internal.core.compiler.ast.LiftingTypeReference.java
License:Open Source License
@Override public TypeBinding resolveType(BlockScope scope, boolean checkBounds) { TypeBinding baseType = this.resolvedType = this.baseReference.resolveType(scope); if (this.roleReference.getTypeName().length > 1) { scope.problemReporter().qualifiedLiftingType(this.roleReference, scope.enclosingSourceType()); return invalidate(baseType); }/* w ww. jav a 2s.c o m*/ TypeBinding roleType = this.roleReference.resolveType(scope); if (scope.kind != Scope.BLOCK_SCOPE) { // not a catch block? if (!TeamModel.isAnyTeam(scope.enclosingSourceType())) { scope.problemReporter().liftingTypeNotAllowedHere(scope.methodScope().referenceContext, this); return invalidate(roleType); } } if (baseType == null || baseType instanceof MissingTypeBinding) return invalidate(roleType); if (roleType == null || roleType instanceof MissingTypeBinding) return invalidate(baseType); if (roleType.isArrayType()) { baseType = baseType.leafComponentType(); roleType = roleType.leafComponentType(); } if (roleType.isBaseType()) { scope.problemReporter().primitiveTypeNotAllowedForLifting(scope.referenceType(), this.roleReference, roleType); return invalidate(roleType); } if (baseType.isBaseType()) { scope.problemReporter().primitiveTypeNotAllowedForLifting(scope.referenceType(), this.baseReference, baseType); return invalidate(roleType); } ReferenceBinding roleRefType = (ReferenceBinding) roleType; if (!roleRefType.isValidBinding()) // already reported. return invalidate(roleType); if (!roleRefType.isDirectRole()) { scope.problemReporter().needRoleInLiftingType(scope.referenceType(), this.roleReference, roleType); return invalidate(roleType); } if (roleRefType.isSynthInterface()) roleRefType = roleRefType.getRealClass(); if (roleRefType.roleModel.hasBaseclassProblem()) {// already reported for the role class. scope.referenceContext().tagAsHavingErrors(); // -> mark method as erroneous, too. return invalidate(roleType); } // TODO (SH): maybe look for bound sub-type? // Note (SH): calling baseclass() requires STATE_LENV_DONE_FIELDS_AND_METHODS: Dependencies.ensureBindingState(roleRefType, ITranslationStates.STATE_LENV_DONE_FIELDS_AND_METHODS); if (baseType.isTypeVariable() && ((TypeVariableBinding) baseType).roletype != null) { // resolving "<B base R> as R": roleRefType = ((TypeVariableBinding) baseType).roletype; // ambiguity is handled by _OT$lift_dynamic which may or may not declare LiftingFailedException } else if ((baseType.tagBits & TagBits.HierarchyHasProblems) != 0) { // already reported (?) } else { // static adjustment (OTJLD 2.3.2(a)): roleRefType = (ReferenceBinding) TeamModel.getRoleToLiftTo(scope, baseType, roleRefType, true, this); if (roleRefType == null) roleRefType = (ReferenceBinding) roleType; // revert unsuccessful adjustment if (roleRefType.baseclass() == null || RoleModel.hasTagBit(roleRefType, RoleModel.BaseclassHasProblems)) { scope.problemReporter().roleNotBoundCantLift(scope.referenceType(), this.roleReference, roleType); return invalidate(roleType); } if (baseType.isRole()) // see http://trac.objectteams.org/ot/ticket/73 baseType = RoleTypeCreator.maybeWrapUnqualifiedRoleType(baseType, scope.enclosingReceiverType()); if (baseType == null) return invalidate(roleType); Config oldConfig = Config.createOrResetConfig(this); try { // fetch role's base class and perform substitutions: ReferenceBinding roleBase = roleRefType.baseclass(); if (roleType.isParameterizedType()) { ParameterizedTypeBinding parameterizedRole = (ParameterizedTypeBinding) roleType; TypeBinding[] typeArgs = parameterizedRole.arguments; ITeamAnchor anchor = null; if (roleRefType.baseclass() instanceof RoleTypeBinding) anchor = ((RoleTypeBinding) roleRefType.baseclass())._teamAnchor; roleBase = parameterizedRole.environment.createParameterizedType( (ReferenceBinding) roleBase.original(), typeArgs, anchor, -1, roleBase.enclosingType(), roleBase.getTypeAnnotations()); } // THE compatibility check: if (!baseType.isCompatibleWith(roleBase) || Config.getLoweringRequired()) { scope.problemReporter().incompatibleBaseForRole(scope.referenceType(), this, roleType, baseType); return invalidate(roleType); } } finally { Config.removeOrRestore(oldConfig, this); } } return this.resolvedType; }
From source file:org.eclipse.objectteams.otdt.internal.core.compiler.ast.OTQualifiedAllocationExpression.java
License:Open Source License
/** * During resolve we make the decision which variant to use. *///w w w . ja v a 2 s . c o m public TypeBinding resolveType(BlockScope scope) { if (this.anonymousType == null && this.creatorCall == null && this.enclosingInstance == null) // special case during code assist return super.resolveType(scope); CompilationResult compilationResult = scope.referenceContext().compilationResult(); CheckPoint cp = compilationResult.getCheckPoint(scope.referenceContext()); this.hasEnclosingInstanceProblem = false; if (this.anonymousType == null && this.creatorCall == null) { // no double processing if (this.enclosingInstance instanceof CastExpression) this.enclosingInstance.bits |= DisableUnnecessaryCastCheck; // will check later on (within super.resolveType()) TypeBinding enclosingInstanceType = this.enclosingInstance.resolveType(scope); this.hasEnclosingInstanceProblem = enclosingInstanceType == null; if (!scope.isGeneratedScope() && enclosingInstanceType != null && enclosingInstanceType.isTeam()) // non reference types will trigger error reporting via super.resolveType() { if (this.enclosingInstance instanceof NameReference) { final NameReference anchorRef = (NameReference) this.enclosingInstance; if (!((VariableBinding) anchorRef.binding).isFinal()) { // replace non-final anchor with fake-binding, // so that this type is not compatibly to anything else: char[] variableName = ((VariableBinding) anchorRef.binding).name; switch (anchorRef.bits & ASTNode.RestrictiveFlagMASK) { case Binding.LOCAL: final LocalVariableBinding localOrig = (LocalVariableBinding) anchorRef.binding; // mark the original as used before we procede with a fake copy: localOrig.useFlag = LocalVariableBinding.USED; anchorRef.binding = new LocalVariableBinding(variableName, enclosingInstanceType, ClassFileConstants.AccFinal, false) { @Override public int problemId() { return IProblem.AnchorNotFinal; } }; this.preGenerateTask = new Runnable() { public void run() { // need to transfer this info from the real local to the fake one (don't have that info yet): ((LocalVariableBinding) anchorRef.binding).resolvedPosition = localOrig.resolvedPosition; } }; break; case Binding.FIELD: anchorRef.binding = new FieldBinding(variableName, enclosingInstanceType, ClassFileConstants.AccFinal, scope.referenceType().binding, Constant.NotAConstant) { @Override public int problemId() { return IProblem.AnchorNotFinal; } }; break; default: throw new InternalCompilerError("Unexpected bits, neither local nor field " //$NON-NLS-1$ + anchorRef.bits + ": " + anchorRef); //$NON-NLS-1$ } } } if (this.type.getTypeName().length > 1) { scope.problemReporter().roleCreationNotRelativeToEnclosingTeam(this); return null; } // now it's finally time to create the alternate version: this.creatorCall = CopyInheritance.createConstructorMethodInvocationExpression(scope, this); if (this.creatorCall == null) return null; } } if (this.creatorCall == null) { TypeBinding typ = super.resolveType(scope); if (typ == null || typ instanceof PolyTypeBinding) return typ; if (!this.hasEnclosingInstanceProblem) { // more checks only if no error already // if enclosing is a role request a cast to the class part as required by the inner constructor if (this.enclosingInstance != null) { TypeBinding enclosingType = this.enclosingInstance.resolvedType; if (enclosingType instanceof ReferenceBinding && ((ReferenceBinding) enclosingType).isDirectRole()) this.enclosingInstanceCast = ((ReferenceBinding) enclosingType).getRealClass(); } ReferenceBinding superType = null; if (this.resolvedType instanceof ReferenceBinding) superType = ((ReferenceBinding) this.resolvedType).superclass(); if (superType != null && (superType instanceof RoleTypeBinding)) { RoleTypeBinding superRole = (RoleTypeBinding) superType; if (superRole.hasExplicitAnchor()) scope.problemReporter().extendingExternalizedRole(superRole, this.type); } } } else { // === with creatorCall === this.constant = Constant.NotAConstant; this.resolvedType = this.creatorCall.resolveType(scope); // when creating role nested instance, no cast of enclosing role needed in this branch, // because creator call is routed via the interface of the enclosing role. if (this.resolvedType != null) { if (((ReferenceBinding) this.resolvedType).isAbstract()) { if (!((ReferenceBinding) enclosingInstance().resolvedType).isAbstract()) scope.problemReporter().abstractRoleIsRelevant(this, (ReferenceBinding) this.creatorCall.resolvedType); } if (this.resolvedType.isValidBinding()) { // FIXME(SH): remove cast unwrapping Expression createExpr = this.creatorCall; while (createExpr instanceof CastExpression) // may have been wrapped using CollectedReplacementsTransformer createExpr = ((CastExpression) createExpr).expression; this.binding = ((MessageSend) createExpr).binding; // store the method binding // using lift-ctor in a qualified way? (OTJDL 2.4.1(a)) ReferenceBinding role = (ReferenceBinding) this.resolvedType; MethodBinding creator = this.binding; if (creator != null) { MethodBinding ctor = role.getExactConstructor(creator.parameters); if (Lifting.isLiftToConstructor(ctor, role)) scope.problemReporter().qualifiedUseOfLiftingConstructor(ctor, this.creatorCall); } } } } return this.resolvedType; }
From source file:org.eclipse.objectteams.otdt.internal.core.compiler.statemachine.transformer.RecordLocalTypesVisitor.java
License:Open Source License
/** * This is the pay-load of this visitor: add local types to the model of the enclosing role *//*from w ww . j av a 2 s . co m*/ public boolean visit(TypeDeclaration td, BlockScope scope) { if ((td.bits & ASTNode.IsLocalType) != 0) { SourceTypeBinding enclosingType = null; TypeDeclaration enclosingTypeDecl = null; // find best guess for enclosing: if (scope != null) { enclosingType = scope.enclosingSourceType(); enclosingTypeDecl = scope.referenceType(); } else if (!this._classScopeStack.isEmpty()) { ClassScope classScope = this._classScopeStack.peek(); if (classScope != null) { // someone pushed null? enclosingTypeDecl = classScope.referenceContext; enclosingType = enclosingTypeDecl.binding; } } if (enclosingType != null) { if (enclosingType.isRole()) { RoleModel model = enclosingType.roleModel; // pre-set enclosing type, which would otherwise only be set during buildLocalType() td.enclosingType = enclosingTypeDecl; // create and link model, didn't know before that it is role-ish. model.addLocalType(null, td.getRoleModel(model.getTeamModel())); } else { TypeModel model = enclosingType.model; model.addLocalType(td); } } if (td.scope == null) return false; // don't descend further, local type is not in a useful state yet. } return true; }