/** Returns a newly created TypeCheck. */
  private static TypeCheck createTypeCheck(Compiler compiler, CheckLevel level) {
    ReverseAbstractInterpreter rai =
        new SemanticReverseAbstractInterpreter(
            compiler.getCodingConvention(), compiler.getTypeRegistry());

    return new TypeCheck(compiler, rai, compiler.getTypeRegistry(), level);
  }
 public void testCharSetExpansion() {
   testSame("");
   assertEquals("US-ASCII", lastCompiler.getOptions().outputCharset);
   args.add("--charset=UTF-8");
   testSame("");
   assertEquals("UTF-8", lastCompiler.getOptions().outputCharset);
 }
示例#3
0
  public CompilationManager(String alias, Compiler compiler) {

    // this is our alias
    this.alias = alias;

    // grab compiler state
    this.compiler = compiler;
    this.tagDecorator = compiler.createTagDecorator();
    this.tagLibrary = compiler.createTagLibrary(this.getCompilationMessageHolder());

    // namespace management
    this.namespaceManager = new NamespaceManager();

    // tag uids
    this.tagId = 0;

    // for composition use
    this.finished = false;

    // our compilationunit stack
    this.units = new Stack<CompilationUnit>();
    this.units.push(new CompilationUnit());

    config = WebConfiguration.getInstance();
  }
示例#4
0
  Type getOptimisticType(final Optimistic node) {
    assert compiler.useOptimisticTypes();

    final int programPoint = node.getProgramPoint();
    final Type validType = compiler.getInvalidatedProgramPointType(programPoint);

    if (validType != null) {
      return validType;
    }

    final Type mostOptimisticType = node.getMostOptimisticType();
    final Type evaluatedType = getEvaluatedType(node);

    if (evaluatedType != null) {
      if (evaluatedType.widerThan(mostOptimisticType)) {
        final Type newValidType =
            evaluatedType.isObject() || evaluatedType.isBoolean() ? Type.OBJECT : evaluatedType;
        // Update invalidatedProgramPoints so we don't re-evaluate the expression next time. This is
        // a heuristic
        // as we're doing a tradeoff. Re-evaluating expressions on each recompile takes time, but it
        // might
        // notice a widening in the type of the expression and thus prevent an unnecessary
        // deoptimization later.
        // We'll presume though that the types of expressions are mostly stable, so if we evaluated
        // it in one
        // compilation, we'll keep to that and risk a low-probability deoptimization if its type
        // gets widened
        // in the future.
        compiler.addInvalidatedProgramPoint(node.getProgramPoint(), newValidType);
      }
      return evaluatedType;
    }
    return mostOptimisticType;
  }
示例#5
0
  @Test
  public void testSimpleC() throws IOException {
    final Compiler compiler = new Compiler();
    String code =
        "#include airport.h;"
            + "\n"
            + "#define MAX;"
            + "\n"
            + "typedef struct\n"
            + "{\n"
            + "\tint id ;\n"
            + "\tint tm ;\n"
            + "} plane;\n"
            + "\n";
    final ParseTree ast = compiler.parse(code, false);

    Assert.assertNotNull(ast);

    final TreePrinterListener treePrinterListener = new TreePrinterListener(compiler.getParser());
    ParseTreeWalker.DEFAULT.walk(treePrinterListener, ast);
    final String formatted = treePrinterListener.toString();
    System.out.println(formatted);

    final String reconstructedFileWithoutSpaces = treePrinterListener.getLeafsAsText();
    code = getStringWithoutSpaces(code);
    Assert.assertArrayEquals(reconstructedFileWithoutSpaces.getBytes(), code.getBytes());
  }
  public static LinkedHashMap<String, byte[]> generate(Config conf, List<Table> tables) {
    LinkedHashMap<String, byte[]> generatedClasses = new LinkedHashMap<String, byte[]>();
    for (Table t : tables) {
      if (t.numColumns() <= 3) continue;

      MethodDecls decls = new MethodDecls();
      for (int i = 0; i < t.getColumnGroups().size(); i++) {
        ColumnGroup cg = t.getColumnGroups().get(i);
        if (i == 0 && cg.size() <= 3) continue;
        if (i == 1 && cg.size() == 1) continue;

        boolean last = (i == t.getColumnGroups().size() - 1);
        if (last && cg.size() <= 3) continue;

        decls.add(new MethodDecl(cg.columns(), last));
      }
      if (decls.isEmpty()) continue;

      Compiler c = new Compiler(conf.isVerbose());
      VisitorBaseGen baseGen = new VisitorBaseGen(false);
      String fullname = VisitorCodeGen.visitorPackage + "." + baseGen.name();
      t.setVisitorInterface(fullname);
      for (MethodDecl d : decls) baseGen.add(d);
      boolean success = c.compile(fullname, baseGen.generate());
      if (!success) {
        String msg = "Cannot compile " + fullname + ".class";
        throw new SociaLiteException(msg);
      }
      generatedClasses.putAll(c.getCompiledClasses());
    }
    return generatedClasses;
  }
  public static void gen() {
    Compiler c = new Compiler();
    // VisitorBaseGen baseGen = new VisitorBaseGen(true);
    VisitorBaseGen baseGen = new VisitorBaseGen(false);

    for (int nest = 0; nest <= 1; nest++) {
      for (int len = 1; len <= 3; len++) {
        List<MethodDecl> decls;
        if (nest == 1) {
          decls = getMethodDecls(nest, len, true);
          baseGen.addAll(decls);
        } else {
          decls = getMethodDecls(nest, len, false);
          baseGen.addAll(decls);
        }
      }
    }
    String visitorImpl = VisitorCodeGen.visitorPackage + ".VisitorImpl";
    boolean success = c.compile(visitorImpl, baseGen.generate());
    if (!success) {
      String msg = "Cannot compile VisitorImpl class";
      System.out.println(c.getErrorMsg());
      throw new SociaLiteException(msg);
    }
  }
  public void testReport() {
    final List<JSError> errors = new ArrayList<JSError>();

    Compiler compiler =
        new Compiler(
            new BasicErrorManager() {

              @Override
              public void report(CheckLevel level, JSError error) {
                errors.add(error);
              }

              @Override
              public void println(CheckLevel level, JSError error) {}

              @Override
              protected void printSummary() {}
            });
    compiler.initCompilerOptionsIfTesting();

    NodeTraversal t = new NodeTraversal(compiler, null);
    DiagnosticType dt = DiagnosticType.warning("FOO", "{0}, {1} - {2}");

    t.report(null, dt, "Foo", "Bar", "Hello");
    assertEquals(1, errors.size());
    assertEquals("Foo, Bar - Hello", errors.get(0).description);
  }
示例#9
0
  @Test
  public void testFST2() throws IOException {
    String inputValues[] = {
      "brats", "cat", "dog", "dogs", "rat",
    };

    int outputValues[] = {1, 3, 5, 7, 11};

    Builder builder = new Builder();
    builder.build(inputValues, outputValues);

    for (int i = 0; i < inputValues.length; i++) {
      assertEquals(outputValues[i], builder.transduce(inputValues[i]));
    }

    Compiler compiledFST = builder.getCompiler();
    FST fst = new FST(compiledFST.getByteArray());

    assertEquals(0, fst.lookup("brat")); // Prefix match
    assertEquals(1, fst.lookup("brats"));
    assertEquals(3, fst.lookup("cat"));
    assertEquals(5, fst.lookup("dog"));
    assertEquals(7, fst.lookup("dogs"));
    assertEquals(11, fst.lookup("rat"));
    assertEquals(-1, fst.lookup("rats")); // No match
  }
示例#10
0
 protected void compileTypes(File dir) throws ToolsException {
   Compiler compiler = new Compiler();
   List<String> fileNames = new ArrayList<String>();
   for (File f : javaFiles) {
     fileNames.add(f.getPath());
   }
   compiler.compile(fileNames.toArray(new String[fileNames.size()]), dir);
 }
 private void test(String js, FunctionInformationMap expected) {
   Compiler compiler = new Compiler();
   compiler.init(
       new JSSourceFile[] {JSSourceFile.fromCode("externs", "")},
       new JSSourceFile[] {JSSourceFile.fromCode("testcode", js)},
       new CompilerOptions());
   test(compiler, expected);
 }
  public void testFormattingSingleQuote() {
    testSame("var x = '';");
    assertEquals("var x=\"\";", lastCompiler.toSource());

    args.add("--formatting=SINGLE_QUOTES");
    testSame("var x = '';");
    assertEquals("var x='';", lastCompiler.toSource());
  }
  /**
   * Verifies that the compiler pass's JS output matches the expected output and (optionally) that
   * an expected warning is issued. Or, if an error is expected, this method just verifies that the
   * error is encountered.
   *
   * @param modules Module inputs
   * @param expected Expected JS outputs (one per module)
   * @param error Expected error, or null if no error is expected
   * @param warning Expected warning, or null if no warning is expected
   */
  public void test(
      JSModule[] modules, String[] expected, DiagnosticType error, DiagnosticType warning) {
    Compiler compiler = createCompiler();
    lastCompiler = compiler;

    compiler.initModules(externsInputs, Lists.newArrayList(modules), getOptions());
    test(compiler, expected, error, warning);
  }
 public void testES5StrictUseStrictMultipleInputs() {
   args.add("--language_in=ECMASCRIPT5_STRICT");
   Compiler compiler =
       compile(new String[] {"var x = f.function", "var y = f.function", "var z = f.function"});
   String outputSource = compiler.toSource();
   assertEquals("'use strict'", outputSource.substring(0, 12));
   assertEquals(outputSource.substring(13).indexOf("'use strict'"), -1);
 }
示例#15
0
  /**
   * Convenience method for above, which assumes stdMethodName, a return type of 'int', and no
   * arguments.
   */
  public void assertInvokeInterfaceEquals(int value, Class target, Interface iface) {

    Compiler compiler = compilerLocal.get();
    compiler.setFlags(compilerFlags());

    assertInvokeInterfaceEquals(value, target, new Extends(iface), stdAM);

    compiler.cleanup();
  }
 public void testMergeRequirements_findsDuplicates() {
   Compiler compiler = createCompiler();
   ErrorManager errorManager = new BlackHoleErrorManager();
   compiler.setErrorManager(errorManager);
   ConformanceConfig.Builder builder = ConformanceConfig.newBuilder();
   builder.addRequirementBuilder().addWhitelist("x").addWhitelist("x");
   CheckConformance.mergeRequirements(compiler, ImmutableList.of(builder.build()));
   assertEquals(1, errorManager.getErrorCount());
 }
 public void testSourceMapExpansion3() {
   useModules = ModulePattern.CHAIN;
   args.add("--create_source_map=%outname%.map");
   args.add("--module_output_path_prefix=foo_");
   testSame(new String[] {"var x = 3;", "var y = 5;"});
   assertEquals(
       "foo_m0.js.map",
       lastCommandLineRunner.expandSourceMapPath(
           lastCompiler.getOptions(), lastCompiler.getModuleGraph().getRootModule()));
 }
  protected void testExternChanges(String extern, String input, String expectedExtern) {
    Compiler compiler = createCompiler();
    CompilerOptions options = getOptions();
    compiler.init(
        ImmutableList.of(SourceFile.fromCode("extern", extern)),
        ImmutableList.of(SourceFile.fromCode("input", input)),
        options);
    compiler.parseInputs();
    assertFalse(compiler.hasErrors());

    Node externsAndJs = compiler.getRoot();
    Node root = externsAndJs.getLastChild();

    Node externs = externsAndJs.getFirstChild();

    Node expected = compiler.parseTestCode(expectedExtern);
    assertFalse(compiler.hasErrors());

    (getProcessor(compiler)).process(externs, root);

    String externsCode = compiler.toSource(externs);
    String expectedCode = compiler.toSource(expected);

    assertEquals(expectedCode, externsCode);
  }
示例#19
0
  /** Tests that the current file does what it is supposed to when compiled. */
  @Test
  public void testFrontEnd() throws IOException {
    try {
      System.out.println("Testing " + filename + "... ");
      Reader reader = new FileReader(TEST_DIRECTORY + "/" + filename);

      Compiler compiler = new Compiler();

      if (filename.startsWith("synerror")) {
        // Expect at least one error during syntax checking
        compiler.checkSyntax(reader);
        assertTrue("Supposed to have syntax errors", compiler.getErrorCount() != 0);

      } else if (filename.startsWith("semerror")) {
        // Expect no syntax errors, but one or more semantic errors
        Script script = compiler.checkSyntax(reader);
        assertTrue("Supposed to have NO syntax errors", compiler.getErrorCount() == 0);
        compiler.checkSemantics(script);
        assertTrue("Supposed to have semantic errors", compiler.getErrorCount() != 0);

      } else {
        // Expect no errors even after all semantic checks
        compiler.checkSemantics(reader);
        assertTrue("Supposed to be error free", compiler.getErrorCount() == 0);
      }
      System.out.println("PASS");
    } catch (AssertionError e) {
      System.out.println("FAIL: " + e.getMessage());
      throw e;
    }
  }
示例#20
0
  public static void main(String[] args) {

    String source = "let a = \"hello\" in let b = 7 in";

    Compiler compiler = new Compiler();
    compiler.setParserStrategy(new TRIVParserStrategy());
    compiler.setLexerStrategy(new TRIVLexerStrategy());
    compiler.setPatternStrategy(new TRIVPatternStrategy());

    compiler.compile(source);
  }
示例#21
0
  public static Pair expand(VM vm, ArcObject body, Map[] lexicalBindings) {
    List result = new LinkedList();

    while (!(body instanceof Nil) && body instanceof Pair) {
      ArcObject next = body.car();
      body = body.cdr();
      result.add(Compiler.compile(vm, next, lexicalBindings).reduce());
    }

    return (Pair) Pair.buildFrom(result, Compiler.compile(vm, body, lexicalBindings).reduce());
  }
示例#22
0
  /**
   * Creates a class which calls target::method(args) via invokeinterface through 'iface', compiles
   * and loads both it and 'target', and then invokes the method. If the returned value does not
   * match 'value' then a test failure is indicated.
   */
  public void assertInvokeInterfaceEquals(
      Object value, Class target, Extends iface, AbstractMethod method, String... args) {

    Compiler compiler = compilerLocal.get();
    compiler.setFlags(compilerFlags());

    Class ii = invokeInterfaceHarness(target, iface, method, args);
    ClassLoader loader = compiler.compile(ii, target);

    assertStaticCallEquals(loader, ii, method.getName(), value);
    compiler.cleanup();
  }
  /**
   * Verifies that the compiler pass's JS output matches the expected output and (optionall) that an
   * expected warning is issued. Or, if an error is expected, this method just verifies that the
   * error is encountered.
   *
   * @param inputs Inputs
   * @param expected Expected JS output
   * @param error Expected error, or null if no error is expected
   * @param description The description of the expected warning, or null if no warning is expected
   *     or if the warning's description should no be examined
   */
  public void test(
      List<SourceFile> inputs,
      String[] expected,
      DiagnosticType error,
      DiagnosticType warning,
      String description) {
    Compiler compiler = createCompiler();
    lastCompiler = compiler;

    compiler.init(externsInputs, inputs, getOptions());
    test(compiler, expected, error, warning, description);
  }
示例#24
0
  /**
   * Creates a class which calls target::method(args) via invokevirtual, compiles and loads both the
   * new class and 'target', and then invokes the method. If the returned value does not match
   * 'value' then a test failure is indicated.
   */
  public void assertInvokeVirtualEquals(
      Object value, Class target, ConcreteMethod method, String returns, String... args) {

    Compiler compiler = compilerLocal.get();
    compiler.setFlags(compilerFlags());

    Class iv = invokeVirtualHarness(target, method, returns, args);
    ClassLoader loader = compiler.compile(iv, target);

    assertStaticCallEquals(loader, iv, method.getName(), value);
    compiler.cleanup();
  }
  /**
   * Given an input in JavaScript, get a control flow graph for it.
   *
   * @param input Input JavaScript.
   */
  private ControlFlowGraph<Node> createCfg(String input,
      boolean runSynBlockPass) {
    Compiler compiler = new Compiler();
    ControlFlowAnalysis cfa = new ControlFlowAnalysis(compiler, true, true);

    Node root = compiler.parseSyntheticCode("cfgtest", input);
    if (runSynBlockPass) {
      CreateSyntheticBlocks pass = new CreateSyntheticBlocks(
          compiler, "START", "END");
      pass.process(null, root);
    }
    cfa.process(null, root);
    return cfa.getCfg();
  }
示例#26
0
  /**
   * Declares a symbol name as belonging to a non-scoped local variable during an on-demand
   * compilation of a single function. This method will add an explicit Undefined binding for the
   * local into the runtime scope if it's otherwise implicitly undefined so that when an expression
   * is evaluated for the name, it won't accidentally find an unrelated value higher up the scope
   * chain. It is only required to call this method when doing an optimistic on-demand compilation.
   *
   * @param symbolName the name of the symbol that is to be declared as being a non-scoped local
   *     variable.
   */
  void declareLocalSymbol(final String symbolName) {
    assert compiler.useOptimisticTypes() && compiler.isOnDemandCompilation() && runtimeScope != null
        : "useOptimistic="
            + compiler.useOptimisticTypes()
            + " isOnDemand="
            + compiler.isOnDemandCompilation()
            + " scope="
            + runtimeScope;

    if (runtimeScope.findProperty(symbolName, false) == null) {
      runtimeScope.addOwnProperty(
          symbolName, NOT_WRITABLE | NOT_ENUMERABLE | NOT_CONFIGURABLE, ScriptRuntime.UNDEFINED);
    }
  }
  /**
   * Given an input in JavaScript, test if the control flow analysis
   * creates the proper control flow graph by comparing the expected
   * Dot file output.
   *
   * @param input Input JavaScript.
   * @param expected Expected Graphviz Dot file.
   * @param shouldTraverseFunctions Whether to traverse functions when
   *    constructing the CFG (true by default). Passed in to the
   *    constructor of {@link ControlFlowAnalysis}.
   */
  private void testCfg(String input, String expected,
      boolean shouldTraverseFunctions) {
    Compiler compiler = new Compiler();
    ControlFlowAnalysis cfa =
        new ControlFlowAnalysis(compiler, shouldTraverseFunctions, true);

    Node root = compiler.parseSyntheticCode("cfgtest", input);
    cfa.process(null, root);
    ControlFlowGraph<Node> cfg = cfa.getCfg();
    try {
      assertEquals(expected, DotFormatter.toDot(root, cfg));
    } catch (java.io.IOException e) {
      fail("Tests failed with IOExceptions");
    }
  }
 public void testSourceMapFormat2() {
   args.add("--js_output_file");
   args.add("/path/to/out.js");
   args.add("--source_map_format=V3");
   testSame("var x = 3;");
   assertEquals(SourceMap.Format.V3, lastCompiler.getOptions().sourceMapFormat);
 }
  /** Parses expected JS inputs and returns the root of the parse tree. */
  protected Node parseExpectedJs(List<SourceFile> inputs) {
    Compiler compiler = createCompiler();

    compiler.init(externsInputs, inputs, getOptions());
    Node root = compiler.parseInputs();
    assertTrue(
        "Unexpected parse error(s): " + Joiner.on("\n").join(compiler.getErrors()), root != null);
    Node externsRoot = root.getFirstChild();
    Node mainRoot = externsRoot.getNext();
    // Only run the normalize pass, if asked.
    if (normalizeEnabled && normalizeExpected && !compiler.hasErrors()) {
      Normalize normalize = new Normalize(compiler, false);
      normalize.process(externsRoot, mainRoot);
    }
    return mainRoot;
  }
 private Expression leaveOptimistic(final Optimistic opt) {
   final int pp = opt.getProgramPoint();
   if (isValid(pp) && !neverOptimistic.peek().get(pp)) {
     return (Expression) opt.setType(compiler.getOptimisticType(opt));
   }
   return (Expression) opt;
 }