private CollectionLiteralAnnotationTerm startCollection(Tree.Term t) {
   Unit unit = t.getUnit();
   // Continue the visit to collect the elements
   ProducedType iteratedType = unit.getIteratedType(parameter().getType());
   TypeDeclaration declaration = iteratedType.getDeclaration();
   LiteralAnnotationTerm factory;
   if (unit.getStringDeclaration().equals(declaration)) {
     factory = StringLiteralAnnotationTerm.FACTORY;
   } else if (unit.getIntegerDeclaration().equals(declaration)) {
     factory = IntegerLiteralAnnotationTerm.FACTORY;
   } else if (unit.getCharacterDeclaration().equals(declaration)) {
     factory = CharacterLiteralAnnotationTerm.FACTORY;
   } else if (unit.getBooleanDeclaration().equals(declaration)) {
     factory = BooleanLiteralAnnotationTerm.FACTORY;
   } else if (unit.getFloatDeclaration().equals(declaration)) {
     factory = FloatLiteralAnnotationTerm.FACTORY;
   } else if (Decl.isEnumeratedTypeWithAnonCases(iteratedType)) {
     factory = ObjectLiteralAnnotationTerm.FACTORY;
   } else if (Decl.isAnnotationClass(declaration)) {
     t.addError(
         "compiler bug: iterables of annotation classes or annotation constructors not supported as literal "
             + (checkingDefaults ? "defaulted parameters" : "arguments"));
     return null;
   } else if (iteratedType.isSubtypeOf(
       ((TypeDeclaration) unit.getLanguageModuleDeclarationDeclaration("Declaration"))
           .getType())) {
     factory = DeclarationLiteralAnnotationTerm.FACTORY;
   } else {
     throw new RuntimeException();
   }
   CollectionLiteralAnnotationTerm result = this.elements;
   this.elements = new CollectionLiteralAnnotationTerm(factory);
   return result;
 }
 private void checkVariable(Tree.Term term, Node node) {
   if (isEffectivelyBaseMemberExpression(term)) { // Note: other cases handled in ExpressionVisitor
     Tree.StaticMemberOrTypeExpression mte = (Tree.StaticMemberOrTypeExpression) term;
     Declaration member = mte.getDeclaration();
     if (member == declaration) {
       if ((declaration.isFormal() || declaration.isDefault()) && !isForwardReferenceable()) {
         term.addError(
             "member is formal or default and may not be assigned here: '"
                 + member.getName()
                 + "'");
       } else if (!isVariable() && !isLate()) {
         if (member instanceof Value) {
           if (node instanceof Tree.AssignOp) {
             term.addError(
                 "value is not a variable and may not be assigned here: '"
                     + member.getName()
                     + "'",
                 803);
           } else {
             term.addError("value is not a variable: '" + member.getName() + "'", 800);
           }
         } else {
           term.addError("not a variable value: '" + member.getName() + "'");
         }
       }
     }
   }
 }
 /** Constructs an {@code AbstractCallable} suitable for wrapping a method reference. */
 public static CallableBuilder methodReference(
     CeylonTransformer gen, Tree.Term expr, ParameterList parameterList) {
   CallableBuilder cb = new CallableBuilder(gen);
   cb.paramLists = parameterList;
   cb.typeModel = expr.getTypeModel();
   cb.forwardCallTo = expr;
   return cb;
 }
 @Override
 public void visit(Tree.AssignOp that) {
   Tree.Term lt = that.getLeftTerm();
   if (isEffectivelyBaseMemberExpression(lt)) {
     Tree.StaticMemberOrTypeExpression m = (Tree.StaticMemberOrTypeExpression) lt;
     //            Declaration member = getTypedDeclaration(m.getScope(),
     //                    name(m.getIdentifier()), null, false, m.getUnit());
     Declaration member = m.getDeclaration();
     if (member == declaration) {
       if (that.getRightTerm() != null) {
         that.getRightTerm().visit(this);
       }
       checkVariable(lt, that);
       specify();
       lt.visit(this);
     } else {
       super.visit(that);
     }
   }
 }
 @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 extractExpressionInFile(TextChange tfc) throws CoreException {
    tfc.setEdit(new MultiTextEdit());
    IDocument doc = tfc.getCurrentDocument(null);

    Tree.Term term = (Tree.Term) node;
    Integer start = term.getStartIndex();
    int length = term.getStopIndex() - start + 1;
    String exp = toString(term);
    FindContainerVisitor fsv = new FindContainerVisitor(term);
    rootNode.visit(fsv);
    Tree.Declaration decNode = fsv.getDeclaration();
    /*if (decNode instanceof Tree.Declaration) {
    	Tree.AnnotationList anns = ((Tree.Declaration) decNode).getAnnotationList();
    	if (anns!=null && !anns.getAnnotations().isEmpty()) {
    		decNode = anns.getAnnotations().get(0);
    	}
    }*/
    Declaration dec = decNode.getDeclarationModel();
    FindLocalReferencesVisitor flrv = new FindLocalReferencesVisitor(dec);
    term.visit(flrv);
    List<TypeDeclaration> localTypes = new ArrayList<TypeDeclaration>();
    for (Tree.BaseMemberExpression bme : flrv.getLocalReferences()) {
      addLocalType(dec, bme.getTypeModel(), localTypes, new ArrayList<ProducedType>());
    }

    String params = "";
    String args = "";
    if (!flrv.getLocalReferences().isEmpty()) {
      for (Tree.BaseMemberExpression bme : flrv.getLocalReferences()) {
        params +=
            bme.getTypeModel().getProducedTypeName() + " " + bme.getIdentifier().getText() + ", ";
        args += bme.getIdentifier().getText() + ", ";
      }
      params = params.substring(0, params.length() - 2);
      args = args.substring(0, args.length() - 2);
    }

    String indent = "\n" + getIndent(decNode, doc);
    String extraIndent = indent + getDefaultIndent();

    String typeParams = "";
    String constraints = "";
    if (!localTypes.isEmpty()) {
      for (TypeDeclaration t : localTypes) {
        typeParams += t.getName() + ", ";
        if (!t.getSatisfiedTypes().isEmpty()) {
          constraints += extraIndent + getDefaultIndent() + "given " + t.getName() + " satisfies ";
          for (ProducedType pt : t.getSatisfiedTypes()) {
            constraints += pt.getProducedTypeName() + "&";
          }
          constraints = constraints.substring(0, constraints.length() - 1);
        }
      }
      typeParams = "<" + typeParams.substring(0, typeParams.length() - 2) + ">";
    }

    String type;
    String ending;
    if ("Void".equals(term.getTypeModel().getProducedTypeName())) {
      type = "void";
      ending = "";
    } else {
      type =
          explicitType || dec.isToplevel() ? term.getTypeModel().getProducedTypeName() : "function";
      ending = "return ";
    }

    tfc.addEdit(
        new InsertEdit(
            decNode.getStartIndex(),
            type
                + " "
                + newName
                + typeParams
                + "("
                + params
                + ")"
                + constraints
                + " {"
                + extraIndent
                + ending
                + exp
                + ";"
                + indent
                + "}"
                + indent
                + indent));
    tfc.addEdit(new ReplaceEdit(start, length, newName + "(" + args + ")"));
  }
  public static String getInitialValueDescription(
      final Declaration dec, CeylonParseController cpc) {
    if (cpc != null) {
      Node refnode = getReferencedNode(dec);
      Tree.SpecifierOrInitializerExpression sie = null;
      String arrow = null;
      if (refnode instanceof Tree.AttributeDeclaration) {
        Tree.AttributeDeclaration ad = (Tree.AttributeDeclaration) refnode;
        sie = ad.getSpecifierOrInitializerExpression();
        arrow = " = ";
      } else if (refnode instanceof Tree.MethodDeclaration) {
        Tree.MethodDeclaration md = (Tree.MethodDeclaration) refnode;
        sie = md.getSpecifierExpression();
        arrow = " => ";
      }
      Tree.CompilationUnit lcu = cpc.getLastCompilationUnit();
      if (sie == null) {
        class FindInitializerVisitor extends Visitor {
          Tree.SpecifierOrInitializerExpression result;

          @Override
          public void visit(Tree.InitializerParameter that) {
            super.visit(that);
            Declaration d = that.getParameterModel().getModel();
            if (d != null && d.equals(dec)) {
              result = that.getSpecifierExpression();
            }
          }
        }
        FindInitializerVisitor fiv = new FindInitializerVisitor();
        fiv.visit(lcu);
        sie = fiv.result;
      }
      if (sie != null) {
        Tree.Expression e = sie.getExpression();
        if (e != null) {
          Tree.Term term = e.getTerm();
          if (term instanceof Tree.Literal) {
            String text = term.getToken().getText();
            if (text.length() < 20) {
              return arrow + text;
            }
          } else if (term instanceof Tree.BaseMemberOrTypeExpression) {
            Tree.BaseMemberOrTypeExpression bme = (Tree.BaseMemberOrTypeExpression) term;
            Tree.Identifier id = bme.getIdentifier();
            if (id != null && bme.getTypeArguments() == null) {
              return arrow + id.getText();
            }
          } else if (term != null) {
            Unit unit = lcu.getUnit();
            if (term.getUnit().equals(unit)) {
              String impl = Nodes.text(term, cpc.getTokens());
              if (impl.length() < 10) {
                return arrow + impl;
              }
            }
          }
          // don't have the token stream :-/
          // TODO: figure out where to get it from!
          return arrow + "...";
        }
      }
    }
    return "";
  }
 @Override
 public void visit(Tree.Term term) {
   if (annotationConstructor != null && !checkingDefaults) {
     term.addError("compiler bug: unsupported term " + term.getClass().getSimpleName());
   }
 }