Search in sources :

Example 1 with PackageDescriptor

use of com.redhat.ceylon.compiler.typechecker.tree.Tree.PackageDescriptor in project ceylon-compiler by ceylon.

the class CeylonTransformer method makeDefs.

private List<JCTree> makeDefs(CompilationUnit t) {
    final ListBuffer<JCTree> defs = new ListBuffer<JCTree>();
    t.visit(new SourceDeclarationVisitor() {

        @Override
        public void loadFromSource(Declaration decl) {
            if (!checkNative(decl))
                return;
            long flags = decl instanceof Tree.AnyInterface ? Flags.INTERFACE : 0;
            String name = Naming.toplevelClassName("", decl);
            defs.add(makeClassDef(decl, flags, name, WantedDeclaration.Normal));
            if (decl instanceof Tree.AnyInterface) {
                String implName = Naming.getImplClassName(name);
                defs.add(makeClassDef(decl, 0, implName, WantedDeclaration.Normal));
            }
            // only do it for Bootstrap where we control the annotations, because it's so dodgy ATM
            if (options.get(OptionName.BOOTSTRAPCEYLON) != null && decl instanceof Tree.AnyClass && TreeUtil.hasAnnotation(decl.getAnnotationList(), "annotation", decl.getUnit())) {
                String annotationName = Naming.suffixName(Suffix.$annotation$, name);
                defs.add(makeClassDef(decl, Flags.ANNOTATION, annotationName, WantedDeclaration.Annotation));
                for (Tree.StaticType sat : ((Tree.AnyClass) decl).getSatisfiedTypes().getTypes()) {
                    if (sat instanceof Tree.BaseType && ((Tree.BaseType) sat).getIdentifier().getText().equals("SequencedAnnotation")) {
                        String annotationsName = Naming.suffixName(Suffix.$annotations$, name);
                        defs.add(makeClassDef(decl, Flags.ANNOTATION, annotationsName, WantedDeclaration.AnnotationSequence));
                    }
                }
            }
        }

        private JCTree makeClassDef(Declaration decl, long flags, String name, WantedDeclaration wantedDeclaration) {
            ListBuffer<JCTree.JCTypeParameter> typarams = new ListBuffer<JCTree.JCTypeParameter>();
            if (decl instanceof Tree.ClassOrInterface) {
                Tree.ClassOrInterface classDecl = (ClassOrInterface) decl;
                if (classDecl.getTypeParameterList() != null) {
                    for (Tree.TypeParameterDeclaration typeParamDecl : classDecl.getTypeParameterList().getTypeParameterDeclarations()) {
                        // we don't need a valid name, just a name, and making it BOGUS helps us find it later if it turns out
                        // we failed to reset everything properly
                        typarams.add(make().TypeParameter(names().fromString("BOGUS-" + typeParamDecl.getIdentifier().getText()), List.<JCExpression>nil()));
                    }
                }
            }
            return make().ClassDef(make().Modifiers(flags | Flags.PUBLIC), names().fromString(name), typarams.toList(), null, List.<JCExpression>nil(), makeClassBody(decl, wantedDeclaration));
        }

        private List<JCTree> makeClassBody(Declaration decl, WantedDeclaration wantedDeclaration) {
            // only do it for Bootstrap where we control the annotations, because it's so dodgy ATM
            if (wantedDeclaration == WantedDeclaration.Annotation) {
                ListBuffer<JCTree> body = new ListBuffer<JCTree>();
                for (Tree.Parameter param : ((Tree.ClassDefinition) decl).getParameterList().getParameters()) {
                    String name;
                    JCExpression type = make().TypeArray(make().Type(syms().stringType));
                    if (param instanceof Tree.InitializerParameter)
                        name = ((Tree.InitializerParameter) param).getIdentifier().getText();
                    else if (param instanceof Tree.ParameterDeclaration) {
                        Tree.TypedDeclaration typedDeclaration = ((Tree.ParameterDeclaration) param).getTypedDeclaration();
                        name = typedDeclaration.getIdentifier().getText();
                        type = getAnnotationTypeFor(typedDeclaration.getType());
                    } else
                        name = "ERROR";
                    JCMethodDecl method = make().MethodDef(make().Modifiers(Flags.PUBLIC), names().fromString(name), type, List.<JCTypeParameter>nil(), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), null, null);
                    body.append(method);
                }
                return body.toList();
            }
            if (wantedDeclaration == WantedDeclaration.AnnotationSequence) {
                String name = Naming.toplevelClassName("", decl);
                String annotationName = Naming.suffixName(Suffix.$annotation$, name);
                JCExpression type = make().TypeArray(make().Ident(names().fromString(annotationName)));
                JCMethodDecl method = make().MethodDef(make().Modifiers(Flags.PUBLIC), names().fromString("value"), type, List.<JCTypeParameter>nil(), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), null, null);
                return List.<JCTree>of(method);
            }
            return List.<JCTree>nil();
        }

        private JCExpression getAnnotationTypeFor(Tree.Type type) {
            if (type instanceof Tree.BaseType) {
                String name = ((Tree.BaseType) type).getIdentifier().getText();
                if (name.equals("String") || name.equals("Declaration"))
                    return make().Type(syms().stringType);
                if (name.equals("Boolean"))
                    return make().Type(syms().booleanType);
                if (name.equals("Integer"))
                    return make().Type(syms().longType);
                if (name.equals("Float"))
                    return make().Type(syms().doubleType);
                if (name.equals("Byte"))
                    return make().Type(syms().byteType);
                if (name.equals("Character"))
                    return make().Type(syms().charType);
                if (name.equals("Declaration") || name.equals("ClassDeclaration") || name.equals("InterfaceDeclaration") || name.equals("ClassOrInterfaceDeclaration"))
                    return make().Type(syms().stringType);
            }
            if (type instanceof Tree.SequencedType) {
                return make().TypeArray(getAnnotationTypeFor(((Tree.SequencedType) type).getType()));
            }
            if (type instanceof Tree.SequenceType) {
                return make().TypeArray(getAnnotationTypeFor(((Tree.SequenceType) type).getElementType()));
            }
            if (type instanceof Tree.IterableType) {
                return make().TypeArray(getAnnotationTypeFor(((Tree.IterableType) type).getElementType()));
            }
            if (type instanceof Tree.TupleType) {
                // can only be one, must be a SequencedType
                Tree.Type sequencedType = ((Tree.TupleType) type).getElementTypes().get(0);
                return getAnnotationTypeFor(sequencedType);
            }
            System.err.println("Unknown Annotation type: " + type);
            return make().TypeArray(make().Type(syms().stringType));
        }

        @Override
        public void loadFromSource(ModuleDescriptor that) {
        // don't think we care about these
        }

        @Override
        public void loadFromSource(PackageDescriptor that) {
        // don't think we care about these
        }
    });
    return defs.toList();
}
Also used : ClassOrInterface(com.redhat.ceylon.compiler.typechecker.tree.Tree.ClassOrInterface) ListBuffer(com.sun.tools.javac.util.ListBuffer) PackageDescriptor(com.redhat.ceylon.compiler.typechecker.tree.Tree.PackageDescriptor) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) SourceDeclarationVisitor(com.redhat.ceylon.compiler.java.loader.SourceDeclarationVisitor) List(com.sun.tools.javac.util.List) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Declaration(com.redhat.ceylon.compiler.typechecker.tree.Tree.Declaration) ClassOrInterface(com.redhat.ceylon.compiler.typechecker.tree.Tree.ClassOrInterface) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) JCTree(com.sun.tools.javac.tree.JCTree) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) ModuleDescriptor(com.redhat.ceylon.compiler.typechecker.tree.Tree.ModuleDescriptor) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) Parameter(com.redhat.ceylon.model.typechecker.model.Parameter) JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter)

Example 2 with PackageDescriptor

use of com.redhat.ceylon.compiler.typechecker.tree.Tree.PackageDescriptor in project ceylon-compiler by ceylon.

the class CeylonModelLoader method setupSourceFileObjects.

public static void setupSourceFileObjects(java.util.List<?> treeHolders, final ClassReader reader, final Names names) {
    for (Object treeHolder : treeHolders) {
        if (!(treeHolder instanceof CeylonCompilationUnit)) {
            continue;
        }
        final CeylonCompilationUnit tree = (CeylonCompilationUnit) treeHolder;
        CompilationUnit ceylonTree = tree.ceylonTree;
        final String pkgName = tree.getPackageName() != null ? Util.quoteJavaKeywords(tree.getPackageName().toString()) : "";
        ceylonTree.visit(new SourceDeclarationVisitor() {

            @Override
            public void loadFromSource(Declaration decl) {
                if (!checkNative(decl))
                    return;
                String fqn = Naming.toplevelClassName(pkgName, decl);
                try {
                    reader.enterClass(names.fromString(fqn), tree.getSourceFile());
                } catch (AssertionError error) {
                // this happens when we have already registered a source file for this decl, hopefully the typechecker
                // will catch this and log an error
                }
            }

            @Override
            public void loadFromSource(ModuleDescriptor that) {
                try {
                    reader.enterClass(names.fromString(pkgName + "." + Naming.MODULE_DESCRIPTOR_CLASS_NAME), tree.getSourceFile());
                } catch (AssertionError error) {
                // this happens when we have already registered a source file for this decl, hopefully the typechecker
                // will catch this and log an error
                }
            }

            @Override
            public void loadFromSource(PackageDescriptor that) {
                try {
                    reader.enterClass(names.fromString(pkgName + "." + Naming.PACKAGE_DESCRIPTOR_CLASS_NAME), tree.getSourceFile());
                } catch (AssertionError error) {
                // this happens when we have already registered a source file for this decl, hopefully the typechecker
                // will catch this and log an error
                }
            }
        });
    }
}
Also used : CeylonCompilationUnit(com.redhat.ceylon.compiler.java.codegen.CeylonCompilationUnit) CompilationUnit(com.redhat.ceylon.compiler.typechecker.tree.Tree.CompilationUnit) ModuleDescriptor(com.redhat.ceylon.compiler.typechecker.tree.Tree.ModuleDescriptor) CeylonCompilationUnit(com.redhat.ceylon.compiler.java.codegen.CeylonCompilationUnit) JavaFileObject(javax.tools.JavaFileObject) Declaration(com.redhat.ceylon.compiler.typechecker.tree.Tree.Declaration) PackageDescriptor(com.redhat.ceylon.compiler.typechecker.tree.Tree.PackageDescriptor)

Example 3 with PackageDescriptor

use of com.redhat.ceylon.compiler.typechecker.tree.Tree.PackageDescriptor in project ceylon-compiler by ceylon.

the class CeylonDocTool method initTypeCheckedUnits.

private void initTypeCheckedUnits(TypeChecker typeChecker) {
    for (PhasedUnit unit : typeChecker.getPhasedUnits().getPhasedUnits()) {
        // obtain the unit container path
        final String pkgName = Util.getUnitPackageName(unit);
        unit.getCompilationUnit().visit(new SourceDeclarationVisitor() {

            @Override
            public void loadFromSource(com.redhat.ceylon.compiler.typechecker.tree.Tree.Declaration decl) {
                compiledClasses.add(Util.getQuotedFQN(pkgName, decl));
            }

            @Override
            public void loadFromSource(ModuleDescriptor that) {
            // don't think we care about these
            }

            @Override
            public void loadFromSource(PackageDescriptor that) {
            // don't think we care about these
            }
        });
    }
}
Also used : ModuleDescriptor(com.redhat.ceylon.compiler.typechecker.tree.Tree.ModuleDescriptor) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) SourceDeclarationVisitor(com.redhat.ceylon.compiler.java.loader.SourceDeclarationVisitor) PackageDescriptor(com.redhat.ceylon.compiler.typechecker.tree.Tree.PackageDescriptor) PhasedUnit(com.redhat.ceylon.compiler.typechecker.context.PhasedUnit)

Aggregations

ModuleDescriptor (com.redhat.ceylon.compiler.typechecker.tree.Tree.ModuleDescriptor)3 PackageDescriptor (com.redhat.ceylon.compiler.typechecker.tree.Tree.PackageDescriptor)3 SourceDeclarationVisitor (com.redhat.ceylon.compiler.java.loader.SourceDeclarationVisitor)2 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)2 Declaration (com.redhat.ceylon.compiler.typechecker.tree.Tree.Declaration)2 CeylonCompilationUnit (com.redhat.ceylon.compiler.java.codegen.CeylonCompilationUnit)1 PhasedUnit (com.redhat.ceylon.compiler.typechecker.context.PhasedUnit)1 ClassOrInterface (com.redhat.ceylon.compiler.typechecker.tree.Tree.ClassOrInterface)1 CompilationUnit (com.redhat.ceylon.compiler.typechecker.tree.Tree.CompilationUnit)1 Parameter (com.redhat.ceylon.model.typechecker.model.Parameter)1 TypedDeclaration (com.redhat.ceylon.model.typechecker.model.TypedDeclaration)1 JCTree (com.sun.tools.javac.tree.JCTree)1 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)1 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)1 JCTypeParameter (com.sun.tools.javac.tree.JCTree.JCTypeParameter)1 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)1 List (com.sun.tools.javac.util.List)1 ListBuffer (com.sun.tools.javac.util.ListBuffer)1 JavaFileObject (javax.tools.JavaFileObject)1