Search in sources :

Example 1 with PhasedUnit

use of com.redhat.ceylon.compiler.typechecker.context.PhasedUnit in project ceylon-compiler by ceylon.

the class ClassOrPackageDoc method writeConstantValue.

private void writeConstantValue(Value v) throws IOException {
    Node node = tool.getNode(v);
    PhasedUnit pu = tool.getUnit(v);
    if (pu == null || !(node instanceof Tree.AttributeDeclaration)) {
        return;
    }
    Tree.AttributeDeclaration attribute = (Tree.AttributeDeclaration) node;
    Tree.SpecifierOrInitializerExpression specifierExpression = attribute.getSpecifierOrInitializerExpression();
    if (specifierExpression == null) {
        return;
    }
    String value = getSourceCode(pu, specifierExpression);
    int newLineIndex = value.indexOf("\n");
    String valueFirstLine = newLineIndex != -1 ? value.substring(0, newLineIndex) : value;
    around("span class='specifier'", valueFirstLine);
    if (newLineIndex != -1) {
        around("a class='specifier-ellipsis' href='#' title='Click for expand the rest of value.'", "...");
        open("div class='specifier-rest'");
        write(value.substring(newLineIndex + 1));
        close("div");
    }
}
Also used : Node(com.redhat.ceylon.compiler.typechecker.tree.Node) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) PhasedUnit(com.redhat.ceylon.compiler.typechecker.context.PhasedUnit)

Example 2 with PhasedUnit

use of com.redhat.ceylon.compiler.typechecker.context.PhasedUnit in project ceylon-compiler by ceylon.

the class ClassOrPackageDoc method getParameterDefaultValue.

private String getParameterDefaultValue(Parameter param) throws IOException {
    String defaultValue = null;
    if (param.isDefaulted()) {
        PhasedUnit pu = tool.getParameterUnit(param);
        Node paramNode = tool.getParameterNode(param);
        if (pu != null && paramNode instanceof Tree.Parameter) {
            Tree.SpecifierOrInitializerExpression defArg = getDefaultArgument((Tree.Parameter) paramNode);
            if (defArg != null) {
                defaultValue = getSourceCode(pu, defArg.getExpression());
                if (defaultValue != null) {
                    defaultValue = defaultValue.trim();
                }
            }
        }
    }
    return defaultValue;
}
Also used : Node(com.redhat.ceylon.compiler.typechecker.tree.Node) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) PhasedUnit(com.redhat.ceylon.compiler.typechecker.context.PhasedUnit)

Example 3 with PhasedUnit

use of com.redhat.ceylon.compiler.typechecker.context.PhasedUnit in project ceylon-compiler by ceylon.

the class ClassOrPackageDoc method getParametersAssertions.

private Map<Parameter, Map<Tree.Assertion, List<Tree.Condition>>> getParametersAssertions(final Declaration decl) {
    final Map<Parameter, Map<Tree.Assertion, List<Tree.Condition>>> parametersAssertions = new LinkedHashMap<Parameter, Map<Tree.Assertion, List<Tree.Condition>>>();
    if (((Functional) decl).getParameterLists().isEmpty()) {
        return parametersAssertions;
    }
    Node node = tool.getNode(decl);
    PhasedUnit pu = tool.getUnit(decl);
    if (node == null || pu == null) {
        return parametersAssertions;
    }
    Tree.Body body = null;
    if (node instanceof Tree.MethodDefinition) {
        body = ((Tree.MethodDefinition) node).getBlock();
    } else if (node instanceof Tree.ClassDefinition) {
        body = ((Tree.ClassDefinition) node).getClassBody();
    }
    if (body == null) {
        return parametersAssertions;
    }
    final Map<String, Parameter> parametersNames = new HashMap<String, Parameter>();
    for (ParameterList parameterList : ((Functional) decl).getParameterLists()) {
        for (Parameter parameter : parameterList.getParameters()) {
            parametersNames.put(parameter.getName(), parameter);
        }
    }
    body.visitChildren(new Visitor() {

        private boolean stop = false;

        private Tree.Assertion assertion = null;

        private Set<Parameter> referencedParameters = new HashSet<Parameter>();

        @Override
        public void visit(Tree.Assertion that) {
            assertion = that;
            super.visit(that);
            assertion = null;
        }

        @Override
        public void visit(Tree.Condition that) {
            referencedParameters.clear();
            super.visit(that);
            if (assertion != null && !referencedParameters.isEmpty()) {
                for (Parameter referencedParameter : referencedParameters) {
                    Map<Tree.Assertion, List<Tree.Condition>> parameterAssertions = parametersAssertions.get(referencedParameter);
                    if (parameterAssertions == null) {
                        parameterAssertions = new LinkedHashMap<Tree.Assertion, List<Tree.Condition>>();
                        parametersAssertions.put(referencedParameter, parameterAssertions);
                    }
                    List<Tree.Condition> parameterConditions = parameterAssertions.get(assertion);
                    if (parameterConditions == null) {
                        parameterConditions = new ArrayList<Tree.Condition>();
                        parameterAssertions.put(assertion, parameterConditions);
                    }
                    parameterConditions.add(that);
                }
            }
        }

        @Override
        public void visit(Tree.BaseMemberExpression that) {
            if (assertion != null) {
                Declaration d = that.getDeclaration();
                Scope realScope = com.redhat.ceylon.model.typechecker.model.ModelUtil.getRealScope(d.getScope());
                if (parametersNames.containsKey(d.getName()) && realScope == decl) {
                    referencedParameters.add(parametersNames.get(d.getName()));
                }
            }
            super.visit(that);
        }

        @Override
        public void visit(Tree.Statement that) {
            if (assertion == null) {
                stop = true;
            }
            super.visit(that);
        }

        @Override
        public void visitAny(Node that) {
            if (!stop) {
                super.visitAny(that);
            }
        }
    });
    return parametersAssertions;
}
Also used : Visitor(com.redhat.ceylon.compiler.typechecker.tree.Visitor) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Node(com.redhat.ceylon.compiler.typechecker.tree.Node) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) PhasedUnit(com.redhat.ceylon.compiler.typechecker.context.PhasedUnit) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) ArrayList(java.util.ArrayList) List(java.util.List) ParameterList(com.redhat.ceylon.model.typechecker.model.ParameterList) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Declaration(com.redhat.ceylon.model.typechecker.model.Declaration) Util.findBottomMostRefinedDeclaration(com.redhat.ceylon.ceylondoc.Util.findBottomMostRefinedDeclaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) HashSet(java.util.HashSet) Functional(com.redhat.ceylon.model.typechecker.model.Functional) Scope(com.redhat.ceylon.model.typechecker.model.Scope) TypeParameter(com.redhat.ceylon.model.typechecker.model.TypeParameter) Parameter(com.redhat.ceylon.model.typechecker.model.Parameter) ParameterList(com.redhat.ceylon.model.typechecker.model.ParameterList) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 4 with PhasedUnit

use of com.redhat.ceylon.compiler.typechecker.context.PhasedUnit in project ceylon-compiler by ceylon.

the class LanguageCompiler method loadModuleFromSource.

private Module loadModuleFromSource(String pkgName, LinkedList<JCCompilationUnit> moduleTrees, List<JCCompilationUnit> parsedTrees) {
    if (pkgName.isEmpty())
        return null;
    String moduleClassName = pkgName + ".module";
    JavaFileObject fileObject;
    try {
        if (options.get(OptionName.VERBOSE) != null) {
            Log.printLines(log.noticeWriter, "[Trying to load module " + moduleClassName + "]");
        }
        fileObject = fileManager.getJavaFileForInput(StandardLocation.SOURCE_PATH, moduleClassName, Kind.SOURCE);
        if (options.get(OptionName.VERBOSE) != null) {
            Log.printLines(log.noticeWriter, "[Got file object: " + fileObject + "]");
        }
    } catch (IOException e) {
        e.printStackTrace();
        return loadModuleFromSource(getParentPackage(pkgName), moduleTrees, parsedTrees);
    }
    if (fileObject != null) {
        // we really want to compile.
        for (JCCompilationUnit parsedTree : parsedTrees) {
            if (parsedTree.sourcefile.equals(fileObject) && parsedTree instanceof CeylonCompilationUnit) {
                // same file! we already parsed it, let's return this one's module
                PhasedUnit phasedUnit = ((CeylonCompilationUnit) parsedTree).phasedUnit;
                // the module visitor does load the module but does not set the unit's package module
                if (phasedUnit.getPackage().getModule() == null) {
                    // so find the module it created
                    for (Module mod : ceylonContext.getModules().getListOfModules()) {
                        // we recognise it with the unit
                        if (mod.getUnit() == phasedUnit.getUnit()) {
                            // set the package's module
                            Package pkg = phasedUnit.getPackage();
                            pkg.setModule(mod);
                            mod.getPackages().add(pkg);
                            modulesLoadedFromSource.add(mod);
                            break;
                        }
                    }
                }
                // now return it
                return phasedUnit.getPackage().getModule();
            }
        }
        JCCompilationUnit javaCompilationUnit = parse(fileObject);
        Module module;
        if (javaCompilationUnit instanceof CeylonCompilationUnit) {
            CeylonCompilationUnit ceylonCompilationUnit = (CeylonCompilationUnit) javaCompilationUnit;
            moduleTrees.add(ceylonCompilationUnit);
            // parse the module info from there
            module = ceylonCompilationUnit.phasedUnit.visitSrcModulePhase();
            ceylonCompilationUnit.phasedUnit.visitRemainingModulePhase();
            // now set the module
            if (module != null) {
                ceylonCompilationUnit.phasedUnit.getPackage().setModule(module);
            }
        } else {
            // there was a syntax error in the module descriptor, make a pretend module so that we can
            // correctly mark all declarations as part of that module, but we won't generate any code
            // for it
            ModuleManager moduleManager = phasedUnits.getModuleManager();
            module = moduleManager.getOrCreateModule(Arrays.asList(pkgName.split("\\.")), "bogus");
        }
        // now remember it
        if (module != null) {
            modulesLoadedFromSource.add(module);
            return module;
        }
    }
    return loadModuleFromSource(getParentPackage(pkgName), moduleTrees, parsedTrees);
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) JavaFileObject(javax.tools.JavaFileObject) CeylonCompilationUnit(com.redhat.ceylon.compiler.java.codegen.CeylonCompilationUnit) IOException(java.io.IOException) Package(com.redhat.ceylon.model.typechecker.model.Package) Module(com.redhat.ceylon.model.typechecker.model.Module) ImportModule(com.redhat.ceylon.compiler.typechecker.tree.Tree.ImportModule) ModuleManager(com.redhat.ceylon.model.typechecker.util.ModuleManager) PhasedUnit(com.redhat.ceylon.compiler.typechecker.context.PhasedUnit)

Example 5 with PhasedUnit

use of com.redhat.ceylon.compiler.typechecker.context.PhasedUnit in project ceylon-compiler by ceylon.

the class LanguageCompiler method ceylonParse.

private JCCompilationUnit ceylonParse(JavaFileObject filename, CharSequence readSource) {
    if (ceylonEnter.hasRun())
        throw new RunTwiceException("Trying to load new source file after CeylonEnter has been called: " + filename);
    try {
        ModuleManager moduleManager = phasedUnits.getModuleManager();
        ModuleSourceMapper moduleSourceMapper = phasedUnits.getModuleSourceMapper();
        File sourceFile = new File(filename.getName());
        // FIXME: temporary solution
        VirtualFile file = vfs.getFromFile(sourceFile);
        VirtualFile srcDir = vfs.getFromFile(getSrcDir(sourceFile));
        String source = readSource.toString();
        char[] chars = source.toCharArray();
        LineMap map = Position.makeLineMap(chars, chars.length, false);
        PhasedUnit phasedUnit = null;
        PhasedUnit externalPhasedUnit = compilerDelegate.getExternalSourcePhasedUnit(srcDir, file);
        String suppressWarnings = options.get(OptionName.CEYLONSUPPRESSWARNINGS);
        final EnumSet<Warning> suppressedWarnings;
        if (suppressWarnings != null) {
            if (suppressWarnings.trim().isEmpty()) {
                suppressedWarnings = EnumSet.allOf(Warning.class);
            } else {
                suppressedWarnings = EnumSet.noneOf(Warning.class);
                for (String name : suppressWarnings.trim().split(" *, *")) {
                    suppressedWarnings.add(Warning.valueOf(name));
                }
            }
        } else {
            suppressedWarnings = EnumSet.noneOf(Warning.class);
        }
        if (externalPhasedUnit != null) {
            phasedUnit = new CeylonPhasedUnit(externalPhasedUnit, filename, map);
            phasedUnit.setSuppressedWarnings(suppressedWarnings);
            phasedUnits.addPhasedUnit(externalPhasedUnit.getUnitFile(), phasedUnit);
            gen.setMap(map);
            String pkgName = phasedUnit.getPackage().getQualifiedNameString();
            if ("".equals(pkgName)) {
                pkgName = null;
            }
            return gen.makeJCCompilationUnitPlaceholder(phasedUnit.getCompilationUnit(), filename, pkgName, phasedUnit);
        }
        if (phasedUnit == null) {
            ANTLRStringStream input = new NewlineFixingStringStream(source);
            CeylonLexer lexer = new CeylonLexer(input);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            CeylonParser parser = new CeylonParser(tokens);
            CompilationUnit cu = parser.compilationUnit();
            java.util.List<LexError> lexerErrors = lexer.getErrors();
            for (LexError le : lexerErrors) {
                printError(le, le.getMessage(), "ceylon.lexer", map);
            }
            java.util.List<ParseError> parserErrors = parser.getErrors();
            for (ParseError pe : parserErrors) {
                printError(pe, pe.getMessage(), "ceylon.parser", map);
            }
            // if we continue and it's not a descriptor, we don't care about errors
            if ((options.get(OptionName.CEYLONCONTINUE) != null && !ModuleManager.MODULE_FILE.equals(sourceFile.getName()) && !ModuleManager.PACKAGE_FILE.equals(sourceFile.getName())) || // otherwise we care about errors
            (lexerErrors.size() == 0 && parserErrors.size() == 0)) {
                // FIXME: this is bad in many ways
                String pkgName = getPackage(filename);
                // make a Package with no module yet, we will resolve them later
                /*
                     * Stef: see javadoc for findOrCreateModulelessPackage() for why this is here.
                     */
                com.redhat.ceylon.model.typechecker.model.Package p = modelLoader.findOrCreateModulelessPackage(pkgName == null ? "" : pkgName);
                phasedUnit = new CeylonPhasedUnit(file, srcDir, cu, p, moduleManager, moduleSourceMapper, ceylonContext, filename, map);
                phasedUnit.setSuppressedWarnings(suppressedWarnings);
                phasedUnits.addPhasedUnit(file, phasedUnit);
                gen.setMap(map);
                return gen.makeJCCompilationUnitPlaceholder(cu, filename, pkgName, phasedUnit);
            }
        }
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    JCCompilationUnit result = make.TopLevel(List.<JCAnnotation>nil(), null, List.<JCTree>of(make.Erroneous()));
    result.sourcefile = filename;
    return result;
}
Also used : VirtualFile(com.redhat.ceylon.compiler.typechecker.io.VirtualFile) JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) Warning(com.redhat.ceylon.compiler.typechecker.analyzer.Warning) ModuleManager(com.redhat.ceylon.model.typechecker.util.ModuleManager) NewlineFixingStringStream(com.redhat.ceylon.compiler.typechecker.util.NewlineFixingStringStream) PhasedUnit(com.redhat.ceylon.compiler.typechecker.context.PhasedUnit) ParseError(com.redhat.ceylon.compiler.typechecker.parser.ParseError) ModuleSourceMapper(com.redhat.ceylon.compiler.typechecker.analyzer.ModuleSourceMapper) CeylonParser(com.redhat.ceylon.compiler.typechecker.parser.CeylonParser) ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CeylonCompilationUnit(com.redhat.ceylon.compiler.java.codegen.CeylonCompilationUnit) CompilationUnit(com.redhat.ceylon.compiler.typechecker.tree.Tree.CompilationUnit) JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) CommonTokenStream(org.antlr.runtime.CommonTokenStream) LineMap(com.sun.tools.javac.util.Position.LineMap) CeylonLexer(com.redhat.ceylon.compiler.typechecker.parser.CeylonLexer) IOException(java.io.IOException) Package(com.redhat.ceylon.model.typechecker.model.Package) VirtualFile(com.redhat.ceylon.compiler.typechecker.io.VirtualFile) File(java.io.File) LexError(com.redhat.ceylon.compiler.typechecker.parser.LexError)

Aggregations

PhasedUnit (com.redhat.ceylon.compiler.typechecker.context.PhasedUnit)16 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)8 Declaration (com.redhat.ceylon.model.typechecker.model.Declaration)5 TypeDeclaration (com.redhat.ceylon.model.typechecker.model.TypeDeclaration)5 CeylonCompilationUnit (com.redhat.ceylon.compiler.java.codegen.CeylonCompilationUnit)4 Package (com.redhat.ceylon.model.typechecker.model.Package)4 JCCompilationUnit (com.sun.tools.javac.tree.JCTree.JCCompilationUnit)4 IOException (java.io.IOException)4 SourceDeclarationVisitor (com.redhat.ceylon.compiler.java.loader.SourceDeclarationVisitor)3 Node (com.redhat.ceylon.compiler.typechecker.tree.Node)3 CompilationUnit (com.redhat.ceylon.compiler.typechecker.tree.Tree.CompilationUnit)3 ModuleDescriptor (com.redhat.ceylon.compiler.typechecker.tree.Tree.ModuleDescriptor)3 Visitor (com.redhat.ceylon.compiler.typechecker.tree.Visitor)3 File (java.io.File)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 RuntimeModelLoader (com.redhat.ceylon.compiler.java.runtime.model.RuntimeModelLoader)2 PhasedUnits (com.redhat.ceylon.compiler.typechecker.context.PhasedUnits)2 PackageDescriptor (com.redhat.ceylon.compiler.typechecker.tree.Tree.PackageDescriptor)2 AbstractModelLoader (com.redhat.ceylon.model.loader.AbstractModelLoader)2