static List<TypedDeclaration> collectUninitializedMembers(Tree.Body body) {
   List<TypedDeclaration> uninitialized = new ArrayList<TypedDeclaration>();
   if (body != null) {
     List<Tree.Statement> statements = body.getStatements();
     for (Tree.Statement st : statements) {
       if (st instanceof Tree.AttributeDeclaration) {
         Tree.AttributeDeclaration ad = (Tree.AttributeDeclaration) st;
         if (ad.getSpecifierOrInitializerExpression() == null) {
           Value v = ad.getDeclarationModel();
           if (!v.isFormal()) {
             uninitialized.add(v);
           }
         }
       } else if (st instanceof Tree.MethodDeclaration) {
         Tree.MethodDeclaration md = (Tree.MethodDeclaration) st;
         if (md.getSpecifierExpression() == null) {
           Function m = md.getDeclarationModel();
           if (!m.isFormal()) {
             uninitialized.add(m);
           }
         }
       } else if (st instanceof Tree.SpecifierStatement) {
         Tree.SpecifierStatement ss = (Tree.SpecifierStatement) st;
         Tree.Term term = ss.getBaseMemberExpression();
         if (term instanceof Tree.BaseMemberExpression) {
           Tree.BaseMemberExpression bme = (Tree.BaseMemberExpression) term;
           uninitialized.remove(bme.getDeclaration());
         }
       }
     }
   }
   return uninitialized;
 }
 @Override
 public void visit(Tree.MethodDeclaration d) {
   if (isAnnotationConstructor(d) && d.getSpecifierExpression() != null) {
     annotationConstructor = d;
     instantiation = new AnnotationInvocation();
     instantiation.setConstructorDeclaration(d.getDeclarationModel());
     d.getDeclarationModel().setAnnotationConstructor(instantiation);
   }
   super.visit(d);
   if (isAnnotationConstructor(d) && d.getSpecifierExpression() != null) {
     instantiation = null;
     annotationConstructor = null;
   }
 }
Пример #3
0
 @Override
 public void visit(Tree.MethodDeclaration that) {
   if (that.getDeclarationModel() == declaration) {
     if (that.getSpecifierExpression() != null) {
       specify();
       super.visit(that);
     } else {
       super.visit(that);
       if (declaration.isToplevel() && !isNativeHeader(declaration)) {
         that.addError(
             "toplevel function must be specified: '"
                 + declaration.getName()
                 + "' may not be forward declared");
       } else if (declaration.isClassMember()
           && !isNativeHeader(declaration)
           && isInNativeContainer(declaration)) {
         that.addError(
             "member in native container must be native: '" + declaration.getName() + "'", 1450);
       } else if (declaration.isClassMember()
           && !isNativeHeader(declaration)
           && !declaration.isFormal()
           && that.getDeclarationModel().getInitializerParameter() == null
           && declarationSection) {
         that.addError(
             "forward declaration may not occur in declaration section: '"
                 + declaration.getName()
                 + "'",
             1450);
       } else if (declaration.isInterfaceMember()
           && !isNativeHeader(declaration)
           && !declaration.isFormal()) {
         that.addError(
             "interface method must be formal or specified: '" + declaration.getName() + "'",
             1400);
       }
     }
   } else {
     super.visit(that);
   }
 }
Пример #4
0
  static void methodDeclaration(
      TypeDeclaration outer,
      Tree.MethodDeclaration that,
      GenerateJsVisitor gen,
      boolean verboseStitcher) {
    final Function m = that.getDeclarationModel();
    if (that.getSpecifierExpression() != null) {
      // method(params) => expr
      if (outer == null) {
        // Not in a prototype definition. Null to do here if it's a
        // member in prototype style.
        if (gen.opts.isOptimize() && m.isMember()) {
          return;
        }
        gen.comment(that);
        gen.initDefaultedParameters(that.getParameterLists().get(0), that);
        if (!(gen.opts.isOptimize() && m.isClassOrInterfaceMember())
            && TypeUtils.isNativeExternal(m)) {
          if (gen.stitchNative(m, that)) {
            if (verboseStitcher) {
              gen.spitOut(
                  "Stitching in native method "
                      + m.getQualifiedNameString()
                      + ", ignoring Ceylon declaration");
            }
            if (m.isShared()) {
              gen.share(m);
            }
            return;
          }
        }
        gen.out(m.isToplevel() ? GenerateJsVisitor.function : "var ");
      } else {
        // prototype definition
        gen.comment(that);
        gen.initDefaultedParameters(that.getParameterLists().get(0), that);
        if (m.isStatic()) {
          gen.out(gen.getNames().name(outer), ".$st$.");
        } else {
          gen.out(gen.getNames().self(outer), ".");
        }
      }
      gen.out(gen.getNames().name(m));
      if (!m.isToplevel()) gen.out("=");
      if (TypeUtils.isNativeExternal(m)) {
        if (gen.stitchNative(m, that)) {
          if (verboseStitcher) {
            gen.spitOut(
                "Stitching in native method "
                    + m.getQualifiedNameString()
                    + ", ignoring Ceylon declaration");
          }
          if (m.isShared()) {
            gen.share(m);
          }
          return;
        }
      }
      singleExprFunction(
          that.getParameterLists(),
          that.getSpecifierExpression().getExpression(),
          m,
          true,
          !m.isToplevel(),
          gen);
      gen.endLine(true);
      if (outer != null) {
        if (m.isStatic()) {
          gen.out(gen.getNames().name(outer), ".$st$.");
        } else {
          gen.out(gen.getNames().self(outer), ".");
        }
      }
      gen.out(gen.getNames().name(m), ".$crtmm$=");
      TypeUtils.encodeMethodForRuntime(that, gen);
      gen.endLine(true);
      gen.share(m);
    } else if (outer == null // don't do the following in a prototype definition
        && m == that.getScope()) { // Check for refinement of simple param declaration

      if (m.getContainer() instanceof Class && m.isClassOrInterfaceMember()) {
        // Declare the method just by pointing to the param function
        final String name =
            gen.getNames().name(((Class) m.getContainer()).getParameter(m.getName()));
        if (name != null) {
          gen.out(
              gen.getNames().self((Class) m.getContainer()),
              ".",
              gen.getNames().name(m),
              "=",
              name);
          gen.endLine(true);
        }
      } else if (m.getContainer() instanceof Function) {
        // Declare the function just by forcing the name we used in the param list
        final String name =
            gen.getNames().name(((Function) m.getContainer()).getParameter(m.getName()));
        gen.getNames().forceName(m, name);
      }
      // Only the first paramlist can have defaults
      gen.initDefaultedParameters(that.getParameterLists().get(0), that);
      if (!(gen.opts.isOptimize() && m.isClassOrInterfaceMember())
          && TypeUtils.isNativeExternal(m)) {
        if (gen.stitchNative(m, that)) {
          if (verboseStitcher) {
            gen.spitOut(
                "Stitching in native method "
                    + m.getQualifiedNameString()
                    + ", ignoring Ceylon declaration");
          }
          if (m.isShared()) {
            gen.share(m);
          }
        }
      }
    } else if (m == that.getScope()
        && m.getContainer() instanceof TypeDeclaration
        && m.isMember()
        && (m.isFormal() || TypeUtils.isNativeExternal(m))) {
      gen.out(
          gen.getNames().self((TypeDeclaration) m.getContainer()), ".",
          gen.getNames().name(m), "=");
      if (m.isFormal()) {
        gen.out("{$fml:1,$crtmm$:");
        TypeUtils.encodeForRuntime(that, m, gen);
        gen.out("};");
      } else if (TypeUtils.isNativeExternal(m)) {
        if (gen.stitchNative(m, that)) {
          if (verboseStitcher) {
            gen.spitOut(
                "Stitching in native method "
                    + m.getQualifiedNameString()
                    + ", ignoring Ceylon declaration");
          }
          if (m.isShared()) {
            gen.share(m);
          }
        }
      }
    }
  }
  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 "";
  }