Search in sources :

Example 1 with Import

use of com.google.devtools.j2objc.types.Import in project j2objc by google.

the class ObjectiveCImplementationGenerator method printImports.

private void printImports() {
    Set<String> includeFiles = Sets.newTreeSet();
    includeFiles.add("J2ObjC_source.h");
    includeFiles.add(getGenerationUnit().getOutputPath() + ".h");
    for (GeneratedType generatedType : getOrderedTypes()) {
        for (Import imp : generatedType.getImplementationIncludes()) {
            if (!isLocalType(imp.getTypeName())) {
                includeFiles.add(imp.getImportFileName());
            }
        }
    }
    newline();
    for (String header : includeFiles) {
        printf("#include \"%s\"\n", header);
    }
    for (String code : getGenerationUnit().getNativeImplementationBlocks()) {
        print(code);
    }
    Set<String> seenTypes = Sets.newHashSet();
    Set<Import> forwardDecls = Sets.newHashSet();
    for (GeneratedType generatedType : getOrderedTypes()) {
        String name = generatedType.getTypeName();
        seenTypes.add(name);
        for (Import imp : generatedType.getImplementationForwardDeclarations()) {
            String typeName = imp.getTypeName();
            GeneratedType localType = getLocalType(typeName);
            // For non-local types, only forward declare types that haven't been imported.
            if (localType != null ? (localType.isPrivate() && !seenTypes.contains(typeName)) : !includeFiles.contains(imp.getImportFileName())) {
                forwardDecls.add(imp);
            }
        }
    }
    printForwardDeclarations(forwardDecls);
}
Also used : Import(com.google.devtools.j2objc.types.Import)

Example 2 with Import

use of com.google.devtools.j2objc.types.Import in project j2objc by google.

the class GeneratedType method fromTypeDeclaration.

public static GeneratedType fromTypeDeclaration(AbstractTypeDeclaration typeNode) {
    TypeElement typeElement = typeNode.getTypeElement();
    CompilationUnit unit = TreeUtil.getCompilationUnit(typeNode);
    NameTable nameTable = unit.getEnv().nameTable();
    boolean emitLineDirectives = unit.getEnv().options().emitLineDirectives();
    ImmutableList.Builder<String> superTypes = ImmutableList.builder();
    TypeElement superclass = ElementUtil.getSuperclass(typeElement);
    if (superclass != null) {
        superTypes.add(nameTable.getFullName(superclass));
    }
    for (TypeElement superInterface : ElementUtil.getInterfaces(typeElement)) {
        superTypes.add(nameTable.getFullName(superInterface));
    }
    HeaderImportCollector headerCollector = new HeaderImportCollector(unit, HeaderImportCollector.Filter.PUBLIC_ONLY);
    typeNode.accept(headerCollector);
    HeaderImportCollector privateDeclarationCollector = new HeaderImportCollector(unit, HeaderImportCollector.Filter.PRIVATE_ONLY);
    typeNode.accept(privateDeclarationCollector);
    ImplementationImportCollector importCollector = new ImplementationImportCollector(unit);
    typeNode.accept(importCollector);
    SourceBuilder builder = new SourceBuilder(emitLineDirectives);
    TypeDeclarationGenerator.generate(builder, typeNode);
    String publicDeclarationCode = builder.toString();
    builder = new SourceBuilder(emitLineDirectives);
    TypePrivateDeclarationGenerator.generate(builder, typeNode);
    String privateDeclarationCode;
    String implementationCode;
    Options options = unit.getEnv().options();
    if (unit.getEnv().translationUtil().generateImplementation(typeElement)) {
        builder = new SourceBuilder(options.emitLineDirectives());
        TypePrivateDeclarationGenerator.generate(builder, typeNode);
        privateDeclarationCode = builder.toString();
        builder = new SourceBuilder(options.emitLineDirectives());
        TypeImplementationGenerator.generate(builder, typeNode);
        implementationCode = builder.toString();
    } else {
        privateDeclarationCode = "";
        implementationCode = String.format("// Implementation not generated because %s is on the bootclasspath.\n", ElementUtil.getQualifiedName(typeElement));
    }
    ImmutableSet.Builder<Import> implementationIncludes = ImmutableSet.builder();
    implementationIncludes.addAll(privateDeclarationCollector.getSuperTypes());
    implementationIncludes.addAll(importCollector.getImports());
    return new GeneratedType(nameTable.getFullName(typeElement), typeNode.hasPrivateDeclaration(), superTypes.build(), ImmutableSet.copyOf(headerCollector.getForwardDeclarations()), ImmutableSet.copyOf(headerCollector.getSuperTypes()), ImmutableSet.copyOf(privateDeclarationCollector.getForwardDeclarations()), implementationIncludes.build(), publicDeclarationCode, privateDeclarationCode, implementationCode);
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) Options(com.google.devtools.j2objc.Options) Import(com.google.devtools.j2objc.types.Import) TypeElement(javax.lang.model.element.TypeElement) ImmutableList(com.google.common.collect.ImmutableList) HeaderImportCollector(com.google.devtools.j2objc.types.HeaderImportCollector) ImplementationImportCollector(com.google.devtools.j2objc.types.ImplementationImportCollector) ImmutableSet(com.google.common.collect.ImmutableSet) NameTable(com.google.devtools.j2objc.util.NameTable)

Example 3 with Import

use of com.google.devtools.j2objc.types.Import in project j2objc by google.

the class ObjectiveCHeaderGenerator method generateFileHeader.

protected void generateFileHeader() {
    printf("#ifndef %s_H\n", varPrefix);
    printf("#define %s_H\n", varPrefix);
    pushIgnoreDeprecatedDeclarationsPragma();
    pushIgnoreNullabilityCompletenessPragma();
    Set<String> seenTypes = Sets.newHashSet();
    Set<String> includeFiles = Sets.newTreeSet();
    Set<Import> forwardDeclarations = Sets.newHashSet();
    includeFiles.add("J2ObjC_header.h");
    for (GeneratedType type : getOrderedTypes()) {
        String name = type.getTypeName();
        if (!type.isPrivate()) {
            seenTypes.add(name);
        }
        for (Import imp : type.getHeaderIncludes()) {
            if (!isLocalType(imp.getTypeName())) {
                includeFiles.add(imp.getImportFileName());
            }
        }
        for (Import imp : type.getHeaderForwardDeclarations()) {
            // Filter out any declarations that are resolved by an include.
            if (!seenTypes.contains(imp.getTypeName()) && !includeFiles.contains(imp.getImportFileName())) {
                forwardDeclarations.add(imp);
            }
        }
    }
    // Print collected includes.
    newline();
    for (String header : includeFiles) {
        printf("#include \"%s\"\n", header);
    }
    printForwardDeclarations(forwardDeclarations);
    // Print OCNI blocks
    for (String code : getGenerationUnit().getNativeHeaderBlocks()) {
        print(code);
    }
}
Also used : Import(com.google.devtools.j2objc.types.Import)

Example 4 with Import

use of com.google.devtools.j2objc.types.Import in project j2objc by google.

the class ObjectiveCSegmentedHeaderGenerator method printLocalIncludes.

/**
   * Given a {@link com.google.devtools.j2objc.gen.GeneratedType}
   * and its collected {@link com.google.devtools.j2objc.types.Import}s,
   * print its 'local includes'; viz.,
   * {@code INCLUDE} directives for all supertypes that are defined in the
   * current segmented header.
   */
private void printLocalIncludes(GeneratedType type) {
    String typeName = type.getTypeName();
    Set<Import> includes = type.getHeaderIncludes();
    List<Import> localImports = Lists.newArrayList();
    for (Import imp : includes) {
        if (isLocalType(imp.getTypeName())) {
            localImports.add(imp);
        }
    }
    if (!localImports.isEmpty()) {
        printf("#ifdef INCLUDE_%s\n", typeName);
        for (Import imp : localImports) {
            printf("#define INCLUDE_%s 1\n", imp.getTypeName());
        }
        println("#endif");
    }
}
Also used : Import(com.google.devtools.j2objc.types.Import)

Example 5 with Import

use of com.google.devtools.j2objc.types.Import in project j2objc by google.

the class ObjectiveCSegmentedHeaderGenerator method printTypeDeclaration.

@Override
protected void printTypeDeclaration(GeneratedType type) {
    String typeName = type.getTypeName();
    String code = type.getPublicDeclarationCode();
    if (code.length() == 0) {
        return;
    }
    newline();
    printf("#if !defined (%s_) && (INCLUDE_ALL_%s || defined(INCLUDE_%s))\n", typeName, varPrefix, typeName);
    printf("#define %s_\n", typeName);
    Set<Import> forwardDeclarations = Sets.newHashSet(type.getHeaderForwardDeclarations());
    for (Import imp : type.getHeaderIncludes()) {
        // Verify this import isn't declared in this source file.
        if (isLocalType(imp.getTypeName())) {
            continue;
        }
        newline();
        printf("#define RESTRICT_%s 1\n", getVarPrefix(imp.getImportFileName()));
        printf("#define INCLUDE_%s 1\n", imp.getTypeName());
        printf("#include \"%s\"\n", imp.getImportFileName());
        forwardDeclarations.remove(imp);
    }
    printForwardDeclarations(forwardDeclarations);
    print(code);
    newline();
    println("#endif");
}
Also used : Import(com.google.devtools.j2objc.types.Import)

Aggregations

Import (com.google.devtools.j2objc.types.Import)6 HeaderImportCollector (com.google.devtools.j2objc.types.HeaderImportCollector)2 ImplementationImportCollector (com.google.devtools.j2objc.types.ImplementationImportCollector)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Options (com.google.devtools.j2objc.Options)1 CompilationUnit (com.google.devtools.j2objc.ast.CompilationUnit)1 NameTable (com.google.devtools.j2objc.util.NameTable)1 TypeElement (javax.lang.model.element.TypeElement)1