static void addCreateParameterProposal(
     Collection<ICompletionProposal> proposals,
     String def,
     String desc,
     Image image,
     Declaration dec,
     PhasedUnit unit,
     Tree.Declaration decNode,
     Tree.ParameterList paramList,
     ProducedType t) {
   IFile file = CeylonBuilder.getFile(unit);
   TextFileChange change = new TextFileChange("Add Parameter", file);
   change.setEdit(new MultiTextEdit());
   int offset = paramList.getStopIndex();
   int il = importType(change, t, unit.getCompilationUnit());
   change.addEdit(new InsertEdit(offset, def));
   proposals.add(
       new CreateProposal(
           def,
           "Add " + desc + " to '" + dec.getName() + "'",
           image,
           0,
           offset + il,
           file,
           change));
 }
Example #2
0
 /**
  * Generates the code for single or multiple parameter lists, with a callback function to generate
  * the function blocks.
  */
 static void generateParameterLists(
     final Node context,
     final List<Tree.ParameterList> plist,
     final Scope scope,
     final ParameterListCallback callback,
     final boolean emitFunctionKeyword,
     final GenerateJsVisitor gen) {
   if (plist.size() == 1) {
     if (emitFunctionKeyword) {
       gen.out("function");
     }
     Tree.ParameterList paramList = plist.get(0);
     paramList.visit(gen);
     callback.completeFunction();
   } else {
     List<MplData> metas = new ArrayList<>(plist.size());
     Function m = scope instanceof Function ? (Function) scope : null;
     for (Tree.ParameterList paramList : plist) {
       final MplData mpl = new MplData();
       metas.add(mpl);
       mpl.n = context;
       if (metas.size() == 1) {
         if (emitFunctionKeyword) {
           gen.out("function");
         }
       } else {
         mpl.name = gen.getNames().createTempVariable();
         mpl.params = paramList;
         gen.out("var ", mpl.name, "=function");
       }
       paramList.visit(gen);
       if (metas.size() == 1) {
         gen.beginBlock();
         gen.initSelf(context);
         Scope parent = scope == null ? null : scope.getContainer();
         gen.initParameters(
             paramList, parent instanceof TypeDeclaration ? (TypeDeclaration) parent : null, m);
       } else {
         gen.out("{");
       }
     }
     callback.completeFunction();
     closeMPL(metas, m.getType(), gen);
     gen.endBlock();
   }
 }
Example #3
0
 Type tupleFromParameterList() {
   if (params.getParameters().isEmpty()) {
     return n.getUnit().getEmptyType();
   }
   List<Type> types = new ArrayList<>(params.getParameters().size());
   int firstDefaulted = -1;
   int count = 0;
   for (Tree.Parameter p : params.getParameters()) {
     types.add(p.getParameterModel().getType());
     if (p.getParameterModel().isDefaulted()) firstDefaulted = count;
     count++;
   }
   return n.getUnit()
       .getTupleType(
           types,
           params
               .getParameters()
               .get(params.getParameters().size() - 1)
               .getParameterModel()
               .isSequenced(),
           params
               .getParameters()
               .get(params.getParameters().size() - 1)
               .getParameterModel()
               .isAtLeastOne(),
           firstDefaulted);
 }
  public JCNewClass build() {
    // Generate a subclass of Callable
    ListBuffer<JCTree> classBody = new ListBuffer<JCTree>();
    int numParams = paramLists.getParameters().size();
    int minimumParams = 0;
    for (Parameter p : paramLists.getParameters()) {
      if (p.isDefaulted() || p.isSequenced()) break;
      minimumParams++;
    }
    boolean isVariadic = minimumParams != numParams;
    if (parameterListTree != null) {
      // generate a method for each defaulted param
      for (Tree.Parameter p : parameterListTree.getParameters()) {
        if (p.getDefaultArgument() != null || p.getDeclarationModel().isSequenced()) {
          MethodDefinitionBuilder methodBuilder =
              gen.classGen().makeParamDefaultValueMethod(false, null, parameterListTree, p);
          classBody.append(methodBuilder.build());
        }
      }
    }

    // collect each parameter type from the callable type model rather than the declarations to get
    // them all bound
    java.util.List<ProducedType> parameterTypes = new ArrayList<ProducedType>(numParams);
    if (forwardCallTo != null) {
      for (int i = 0; i < numParams; i++)
        parameterTypes.add(gen.getParameterTypeOfCallable(typeModel, i));
    } else {
      // get them from our declaration
      for (Parameter p : paramLists.getParameters()) parameterTypes.add(p.getType());
    }

    // now generate a method for each supported minimum number of parameters below 4
    // which delegates to the $call$typed method if required
    for (int i = minimumParams, max = Math.min(numParams, 4); i < max; i++) {
      classBody.append(makeDefaultedCall(i, isVariadic, parameterTypes));
    }
    // generate the $call method for the max number of parameters,
    // which delegates to the $call$typed method if required
    classBody.append(makeDefaultedCall(numParams, isVariadic, parameterTypes));
    // generate the $call$typed method if required
    if (isVariadic && forwardCallTo == null)
      classBody.append(makeCallTypedMethod(body, parameterTypes));

    JCClassDecl classDef =
        gen.make().AnonymousClassDef(gen.make().Modifiers(0), classBody.toList());

    JCNewClass instance =
        gen.make()
            .NewClass(
                null,
                null,
                gen.makeJavaType(typeModel, JT_EXTENDS | JT_CLASS_NEW),
                List.<JCExpression>of(gen.make().Literal(typeModel.getProducedTypeName(true))),
                classDef);
    return instance;
  }
Example #5
0
 void outputMetamodelAndReturn(GenerateJsVisitor gen, Type t) {
   gen.out(name, ".$crtmm$=function(){return{", MetamodelGenerator.KEY_PARAMS, ":");
   TypeUtils.encodeParameterListForRuntime(true, n, params.getModel(), gen);
   if (t != null) {
     // Add the type to the innermost method
     gen.out(",", MetamodelGenerator.KEY_TYPE, ":");
     TypeUtils.typeNameOrList(n, t, gen, false);
   }
   gen.out("};};return ", gen.getClAlias(), "jsc$3(0,", name, ");");
 }
 @Override
 public void visit(Tree.SpecifierStatement that) {
   if (that.getRefinement()) {
     Tree.Term lhs = that.getBaseMemberExpression();
     if (lhs instanceof Tree.ParameterizedExpression) {
       Tree.ParameterizedExpression pe = (Tree.ParameterizedExpression) lhs;
       for (Tree.ParameterList pl : pe.getParameterLists()) {
         if (pl != null) {
           pl.visit(this);
         }
       }
       Tree.TypeParameterList tpl = pe.getTypeParameterList();
       if (tpl != null) {
         tpl.visit(this);
       }
     }
     // the LHS will be treated as a refinement by
     // FindRefinementsVisitor so ignore it here
     super.visit(that.getSpecifierExpression());
   } else {
     super.visit(that);
   }
 }
 private static void parameters(Tree.ParameterList pl, StyledString label) {
   if (pl == null || pl.getParameters().isEmpty()) {
     label.append("()");
   } else {
     label.append("(");
     int len = pl.getParameters().size(), i = 0;
     for (Tree.Parameter p : pl.getParameters()) {
       if (p != null) {
         label
             .append(type(p.getType()), TYPE_STYLER)
             .append(" ")
             .append(name(p.getIdentifier()), ID_STYLER);
         if (p instanceof Tree.FunctionalParameterDeclaration) {
           Tree.FunctionalParameterDeclaration fp = (Tree.FunctionalParameterDeclaration) p;
           for (Tree.ParameterList ipl : fp.getParameterLists()) {
             parameters(ipl, label);
           }
         }
       }
       if (++i < len) label.append(", ");
     }
     label.append(")");
   }
 }
 static void addCreateParameterAndAttributeProposal(
     Collection<ICompletionProposal> proposals,
     String pdef,
     String adef,
     String desc,
     Image image,
     Declaration dec,
     PhasedUnit unit,
     Tree.Declaration decNode,
     Tree.ParameterList paramList,
     Tree.Body body,
     ProducedType t) {
   IFile file = CeylonBuilder.getFile(unit);
   TextFileChange change = new TextFileChange("Add Attribute", file);
   change.setEdit(new MultiTextEdit());
   int offset = paramList.getStopIndex();
   IDocument doc = CreateProposal.getDocument(change);
   String indent;
   String indentAfter;
   int offset2;
   List<Tree.Statement> statements = body.getStatements();
   if (statements.isEmpty()) {
     indentAfter = "\n" + CeylonQuickFixAssistant.getIndent(decNode, doc);
     indent = indentAfter + getDefaultIndent();
     offset2 = body.getStartIndex() + 1;
   } else {
     Tree.Statement statement = statements.get(statements.size() - 1);
     indent = "\n" + CeylonQuickFixAssistant.getIndent(statement, doc);
     offset2 = statement.getStopIndex() + 1;
     indentAfter = "";
   }
   int il = importType(change, t, unit.getCompilationUnit());
   change.addEdit(new InsertEdit(offset, pdef));
   change.addEdit(new InsertEdit(offset2, indent + adef + indentAfter));
   proposals.add(
       new CreateProposal(
           pdef,
           "Add " + desc + " to '" + dec.getName() + "'",
           image,
           0,
           offset + il,
           file,
           change));
 }
Example #9
0
  static void methodDefinition(
      final Tree.MethodDefinition that,
      final GenerateJsVisitor gen,
      final boolean needsName,
      final boolean verboseStitcher) {
    final Function d = that.getDeclarationModel();
    if (TypeUtils.isNativeExternal(d)) {
      if (gen.stitchNative(d, that)) {
        if (verboseStitcher) {
          gen.spitOut(
              "Stitching in native method "
                  + d.getQualifiedNameString()
                  + ", ignoring Ceylon definition");
        }
        if (d.isShared()) {
          gen.share(d);
        }
        return;
      }
    }
    if (that.getParameterLists().size() == 1) {
      if (needsName) {
        gen.out(GenerateJsVisitor.function, gen.getNames().name(d));
      } else {
        gen.out("function");
      }
      Tree.ParameterList paramList = that.getParameterLists().get(0);
      paramList.visit(gen);
      gen.beginBlock();
      if (d.getContainer() instanceof TypeDeclaration) {
        gen.initSelf(that);
      }
      addParentMethodTypeParameters(d, gen);
      gen.initParameters(paramList, null, d);
      gen.visitStatements(that.getBlock().getStatements());
      gen.endBlock();
    } else {
      List<MplData> metas = new ArrayList<>(that.getParameterLists().size());
      for (Tree.ParameterList paramList : that.getParameterLists()) {
        final MplData mpl = new MplData();
        mpl.n = that;
        metas.add(mpl);
        if (metas.size() == 1) {
          if (needsName) {
            gen.out(GenerateJsVisitor.function, gen.getNames().name(d));
          } else {
            gen.out("function");
          }
        } else {
          mpl.name = gen.getNames().createTempVariable();
          mpl.params = paramList;
          gen.out("var ", mpl.name, "=function");
        }
        paramList.visit(gen);
        gen.beginBlock();
        if (metas.size() == 1 && d.getContainer() instanceof TypeDeclaration) {
          gen.initSelf(that);
        }
        gen.initParameters(paramList, null, d);
      }
      gen.visitStatements(that.getBlock().getStatements());
      closeMPL(metas, d.getType(), gen);
      gen.endBlock();
    }

    if (!gen.share(d)) {
      gen.out(";");
    }
  }