Ejemplo n.º 1
0
 public static Declaration getRefinedDeclaration(Declaration declaration) {
   // Reproduces the algorithm used to build the type hierarchy
   // first walk up the superclass hierarchy
   if (declaration.isClassOrInterfaceMember() && declaration.isShared()) {
     TypeDeclaration dec = (TypeDeclaration) declaration.getContainer();
     List<Type> signature = getSignature(declaration);
     Declaration refined = declaration.getRefinedDeclaration();
     while (dec != null) {
       Type extended = dec.getExtendedType();
       if (extended != null) {
         TypeDeclaration superDec = extended.getDeclaration();
         Declaration superMemberDec =
             superDec.getDirectMember(declaration.getName(), signature, false);
         if (superMemberDec != null) {
           Declaration superRefined = superMemberDec.getRefinedDeclaration();
           if (superRefined != null
               && refined != null
               && !isAbstraction(superMemberDec)
               && superRefined.equals(refined)) {
             return superMemberDec;
           }
         }
         dec = superDec;
       } else {
         dec = null;
       }
     }
     // now look at the very top of the hierarchy, even if it is an interface
     Declaration refinedDeclaration = refined;
     if (refinedDeclaration != null && !declaration.equals(refinedDeclaration)) {
       List<Declaration> directlyInheritedMembers =
           getInterveningRefinements(
               declaration.getName(),
               signature,
               refinedDeclaration,
               (TypeDeclaration) declaration.getContainer(),
               (TypeDeclaration) refinedDeclaration.getContainer());
       directlyInheritedMembers.remove(refinedDeclaration);
       // TODO: do something for the case of
       //      multiple intervening interfaces?
       if (directlyInheritedMembers.size() == 1) {
         // exactly one intervening interface
         return directlyInheritedMembers.get(0);
       } else {
         // no intervening interfaces
         return refinedDeclaration;
       }
     }
   }
   return null;
 }
Ejemplo n.º 2
0
 @Override
 public void visit(Tree.BaseMemberOrTypeExpression that) {
   Declaration d = that.getDeclaration();
   if (d != null && d.equals(dec)) {
     detected = true;
   }
 }
 public RefactoringStatus checkFinalConditions(IProgressMonitor pm)
     throws CoreException, OperationCanceledException {
   if (!newName.matches("^[a-zA-Z_]\\w*$")) {
     return createErrorStatus("Not a legal Ceylon identifier");
   } else if (escaping_.get_().isKeyword(newName)) {
     return createErrorStatus("'" + newName + "' is a Ceylon keyword");
   } else {
     int ch = newName.codePointAt(0);
     if (declaration instanceof TypedDeclaration) {
       if (!Character.isLowerCase(ch) && ch != '_') {
         return createErrorStatus("Not an initial lowercase identifier");
       }
     } else if (declaration instanceof TypeDeclaration) {
       if (!Character.isUpperCase(ch)) {
         return createErrorStatus("Not an initial uppercase identifier");
       }
     }
   }
   Declaration existing =
       declaration
           .getContainer()
           .getMemberOrParameter(declaration.getUnit(), newName, null, false);
   if (null != existing && !existing.equals(declaration)) {
     return createWarningStatus(
         "An existing declaration named '" + newName + "' already exists in the same scope");
   }
   return new RefactoringStatus();
 }
 private void addRemoval(Tree.Identifier id, Declaration d) {
   if (id != null
       && d != null
       && dec.equals(getAbstraction(d))
       && id.getText().equals(aid.getText())) {
     change.addEdit(new ReplaceEdit(id.getStartIndex(), id.getDistance(), dec.getName()));
   }
 }
 @Override
 public void visit(Tree.InitializerParameter that) {
   super.visit(that);
   Declaration d = that.getParameterModel().getModel();
   if (d != null && d.equals(dec)) {
     result = that.getSpecifierExpression();
   }
 }
 @Override
 public void visit(Tree.DocLink that) {
   super.visit(that);
   // TODO: copy/paste from EnterAliasRefactoring
   Declaration base = that.getBase();
   if (base != null && dec.equals(base)) {
     Region region = DocLinks.nameRegion(that, 0);
     change.addEdit(new ReplaceEdit(region.getOffset(), region.getLength(), dec.getName()));
   }
 }
Ejemplo n.º 7
0
 public static boolean removeImport(
     String originalPackage,
     final Declaration dec,
     Tree.CompilationUnit cu,
     TextChange tc,
     Set<String> packages) {
   boolean foundOriginal = false;
   Tree.ImportList il = cu.getImportList();
   for (Tree.Import imp : il.getImports()) {
     Referenceable model = imp.getImportPath().getModel();
     if (model != null) {
       if (model.getNameAsString().equals(originalPackage)) {
         Tree.ImportMemberOrTypeList imtl = imp.getImportMemberOrTypeList();
         if (imtl != null) {
           List<ImportMemberOrType> imts = imtl.getImportMemberOrTypes();
           for (int j = 0; j < imts.size(); j++) {
             Tree.ImportMemberOrType imt = imts.get(j);
             Declaration d = imt.getDeclarationModel();
             if (d != null && d.equals(dec)) {
               int offset;
               int length;
               if (j > 0) {
                 offset = getNodeEndOffset(imts.get(j - 1));
                 length = getNodeEndOffset(imt) - offset;
               } else if (j < imts.size() - 1) {
                 offset = getNodeStartOffset(imt);
                 length = getNodeStartOffset(imts.get(j + 1)) - offset;
               } else {
                 if (packages.contains(originalPackage)) {
                   // we're adding to this import statement,
                   // so don't delete the whole import
                   offset = getNodeStartOffset(imt);
                   length = getNodeLength(imt);
                 } else {
                   offset = getNodeStartOffset(imp);
                   length = getNodeLength(imp);
                 }
               }
               tc.addEdit(new DeleteEdit(offset, length));
               foundOriginal = true;
               break;
               // TODO: return the alias!
             }
           }
         }
         break;
       }
     }
   }
   return foundOriginal;
 }
  private Referenceable resolveNative(
      Referenceable referenceable, Declaration dec, Backends backends) {
    Unit unit = dec.getUnit();
    Scope containerToSearchHeaderIn = null;
    if (unit instanceof CeylonBinaryUnit) {
      CeylonBinaryUnit binaryUnit = (CeylonBinaryUnit) unit;
      ExternalPhasedUnit phasedUnit = binaryUnit.getPhasedUnit();
      if (phasedUnit != null) {
        ExternalSourceFile sourceFile = phasedUnit.getUnit();
        if (sourceFile != null) {
          String sourceRelativePath =
              toJavaString(
                  binaryUnit
                      .getCeylonModule()
                      .toSourceUnitRelativePath(toCeylonString(unit.getRelativePath())));
          if (sourceRelativePath != null && sourceRelativePath.endsWith(".ceylon")) {
            for (Declaration sourceDecl : sourceFile.getDeclarations()) {
              if (sourceDecl.equals(dec)) {
                containerToSearchHeaderIn = sourceDecl.getContainer();
                break;
              }
            }
          } else {
            for (Declaration sourceDecl : sourceFile.getDeclarations()) {
              if (sourceDecl.getQualifiedNameString().equals(dec.getQualifiedNameString())) {
                containerToSearchHeaderIn = sourceDecl.getContainer();
                break;
              }
            }
          }
        }
      }
    } else {
      containerToSearchHeaderIn = dec.getContainer();
    }

    if (containerToSearchHeaderIn != null) {
      Declaration headerDeclaration = getNativeHeader(containerToSearchHeaderIn, dec.getName());
      if (headerDeclaration == null || !headerDeclaration.isNative()) return null;
      if (backends.header()) {
        referenceable = headerDeclaration;
      } else {
        if (headerDeclaration != null) {
          referenceable = getNativeDeclaration(headerDeclaration, supportedBackends());
        }
      }
    }
    return referenceable;
  }
 @Override
 public void visit(Tree.DocLink that) {
   Declaration base = that.getBase();
   if (base != null) {
     if (base.equals(declaration)) {
       count++;
     } else {
       List<Declaration> qualified = that.getQualified();
       if (qualified != null) {
         if (qualified.contains(declaration)) {
           count++;
         }
       }
     }
   }
 }
Ejemplo n.º 10
0
 private boolean checkForHiddenToplevel(Tree.Identifier id, Import i, Tree.Alias alias) {
   for (Declaration d : unit.getDeclarations()) {
     String n = d.getName();
     Declaration idec = i.getDeclaration();
     if (d.isToplevel()
         && n != null
         && i.getAlias().equals(n)
         && !idec.equals(d)
         &&
         // it is legal to import an object declaration
         // in the current package without providing an
         // alias:
         !isLegalAliasFreeImport(d, idec)) {
       if (alias == null) {
         id.addError("toplevel declaration with this name declared in this unit: '" + n + "'");
       } else {
         alias.addError("toplevel declaration with this name declared in this unit: '" + n + "'");
       }
       return true;
     }
   }
   return false;
 }
Ejemplo n.º 11
0
 @Override
 public void visit(Tree.SpecifierStatement that) {
   Tree.Term term = that.getBaseMemberExpression();
   boolean parameterized = false;
   while (term instanceof Tree.ParameterizedExpression) {
     Tree.ParameterizedExpression pe = (Tree.ParameterizedExpression) term;
     term = pe.getPrimary();
     parameterized = true;
   }
   if (term instanceof Tree.StaticMemberOrTypeExpression) {
     Tree.StaticMemberOrTypeExpression bme = (Tree.StaticMemberOrTypeExpression) term;
     //            Declaration member = getTypedDeclaration(bme.getScope(),
     //                    name(bme.getIdentifier()), null, false, bme.getUnit());
     Declaration member = bme.getDeclaration();
     if (member == declaration) {
       if ((declaration.isFormal() || declaration.isDefault()) && !isForwardReferenceable()) {
         // TODO: is this error correct?! look at the condition above
         bme.addError(
             "member is formal and may not be specified: '"
                 + member.getName()
                 + "' is declared formal");
       }
       if (that.getRefinement()) {
         declare();
       }
       Tree.SpecifierExpression se = that.getSpecifierExpression();
       boolean lazy = se instanceof Tree.LazySpecifierExpression;
       if (declaration instanceof Value) {
         Value value = (Value) declaration;
         if (!value.isVariable() && lazy != value.isTransient()) {
           // check that all assignments to a non-variable, in
           // different paths of execution, all use the same
           // kind of specifier, all =>, or all =
           // TODO: sometimes this error appears only because
           //       of a later line which illegally reassigns
           se.addError(
               "value must be specified using => lazy specifier: '" + member.getName() + "'");
         }
         if (lazy) {
           if (value.isVariable()) {
             se.addError(
                 "variable value may not be specified using => lazy specifier: '"
                     + member.getName()
                     + "'");
           } else if (value.isLate()) {
             se.addError(
                 "late reference may not be specified using => lazy specifier: '"
                     + member.getName()
                     + "'");
           }
         }
       }
       if (!lazy || !parameterized) {
         se.visit(this);
       }
       boolean constant = !isVariable() && !isLate();
       Scope scope = that.getScope();
       if (constant && !declaration.isDefinedInScope(scope)) {
         // this error is added by ExpressionVisitor
         //                    that.addError("inherited member is not variable and may not be
         // specified here: '" +
         //                            member.getName() + "'");
       } else if (!declared && constant) {
         bme.addError(shortdesc() + " is not yet declared: '" + member.getName() + "'");
       } else if (inLoop && constant && !(endsInBreakReturnThrow && lastContinue == null)) {
         if (specified.definitely) {
           bme.addError(
               longdesc() + " is aready definitely specified: '" + member.getName() + "'", 803);
         } else {
           bme.addError(
               longdesc() + " is not definitely unspecified in loop: '" + member.getName() + "'",
               803);
         }
       } else if (withinDeclaration && constant && !that.getRefinement()) {
         Declaration dec = getContainingDeclarationOfScope(scope);
         if (dec != null && dec.equals(member)) {
           bme.addError(
               "cannot specify "
                   + shortdesc()
                   + " from within its own body: '"
                   + member.getName()
                   + "'");
         } else {
           bme.addError(
               "cannot specify "
                   + shortdesc()
                   + " declared in outer scope: '"
                   + member.getName()
                   + "'",
               803);
         }
       } else if (specified.possibly && constant) {
         if (specified.definitely) {
           bme.addError(
               longdesc() + " is aready definitely specified: '" + member.getName() + "'", 803);
         } else {
           bme.addError(
               longdesc() + " is not definitely unspecified: '" + member.getName() + "'", 803);
         }
       } else {
         specify();
         term.visit(this);
       }
       if (lazy && parameterized) {
         se.visit(this);
       }
     } else {
       super.visit(that);
     }
   } else {
     super.visit(that);
   }
 }
 private void visitIt(Region region, Declaration dec) {
   if (dec != null && dec.equals(declaration)) {
     links.add(region);
   }
 }