Search in sources :

Example 1 with NativeDeclaration

use of com.google.devtools.j2objc.ast.NativeDeclaration in project j2objc by google.

the class GenerationUnit method addNativeBlocks.

private void addNativeBlocks(CompilationUnit unit, String qualifiedMainType) {
    SourceBuilder headerBuilder = new SourceBuilder(false);
    SourceBuilder implBuilder = new SourceBuilder(false);
    for (NativeDeclaration decl : unit.getNativeBlocks()) {
        String headerCode = decl.getHeaderCode();
        if (headerCode != null) {
            headerBuilder.newline();
            headerBuilder.println(headerBuilder.reindent(headerCode));
        }
        String implCode = decl.getImplementationCode();
        if (implCode != null) {
            implBuilder.newline();
            implBuilder.println(implBuilder.reindent(implCode));
        }
    }
    if (headerBuilder.length() > 0) {
        nativeHeaderBlocks.put(qualifiedMainType, headerBuilder.toString());
    }
    if (implBuilder.length() > 0) {
        nativeImplementationBlocks.put(qualifiedMainType, implBuilder.toString());
    }
}
Also used : NativeDeclaration(com.google.devtools.j2objc.ast.NativeDeclaration)

Example 2 with NativeDeclaration

use of com.google.devtools.j2objc.ast.NativeDeclaration in project j2objc by google.

the class EnumRewriter method addExtraNativeDecls.

private void addExtraNativeDecls(EnumDeclaration node) {
    String typeName = nameTable.getFullName(node.getTypeElement());
    int numConstants = node.getEnumConstants().size();
    // The native type is not declared for an empty enum.
    if (numConstants > 0) {
        String nativeName = NameTable.getNativeEnumName(typeName);
        node.addBodyDeclaration(NativeDeclaration.newInnerDeclaration(UnicodeUtils.format("- (%s)toNSEnum;\n", nativeName), UnicodeUtils.format("- (%s)toNSEnum {\n" + "  return (%s)[self ordinal];\n" + "}\n\n", nativeName, nativeName)));
    }
    StringBuilder outerHeader = new StringBuilder();
    StringBuilder outerImpl = new StringBuilder();
    outerHeader.append(UnicodeUtils.format("FOUNDATION_EXPORT %s *%s_fromOrdinal(NSUInteger ordinal);\n", typeName, typeName));
    outerImpl.append(UnicodeUtils.format("%s *%s_fromOrdinal(NSUInteger ordinal) {\n", typeName, typeName));
    // Avoid "comparison of unsigned expression >= 0 is always true" error.
    if (numConstants == 0) {
        outerImpl.append("  return nil;\n}\n");
    } else {
        outerImpl.append(UnicodeUtils.format("  %s_initialize();\n" + // Param is unsigned, so don't need to check lower bound.
        "  if (ordinal >= %s) {\n" + "    return nil;\n" + "  }\n" + "  return %s_values_[ordinal];\n" + "}\n", typeName, numConstants, typeName));
    }
    NativeDeclaration outerDecl = NativeDeclaration.newOuterDeclaration(outerHeader.toString(), outerImpl.toString());
    outerDecl.addImplementationImportType(GeneratedTypeElement.newEmulatedClass("java.lang.IllegalArgumentException", typeUtil.resolveJavaType("java.lang.RuntimeException").asType()).asType());
    if (options.stripEnumConstants()) {
        outerDecl.addImplementationImportType(GeneratedTypeElement.newEmulatedClass("java.lang.Error", typeUtil.getJavaThrowable().asType()).asType());
    }
    node.addBodyDeclaration(outerDecl);
}
Also used : NativeDeclaration(com.google.devtools.j2objc.ast.NativeDeclaration)

Example 3 with NativeDeclaration

use of com.google.devtools.j2objc.ast.NativeDeclaration in project j2objc by google.

the class OcniExtractor method visitType.

private void visitType(AbstractTypeDeclaration node) {
    TypeElement type = node.getTypeElement();
    Set<String> methodsPrinted = Sets.newHashSet();
    List<BodyDeclaration> bodyDeclarations = node.getBodyDeclarations();
    int minPos = 0;
    int declIdx = 0;
    for (Comment comment : blockComments.get(node)) {
        int commentPos = comment.getStartPosition();
        while (declIdx < bodyDeclarations.size()) {
            BodyDeclaration decl = bodyDeclarations.get(declIdx);
            if (decl.getStartPosition() > commentPos) {
                break;
            }
            minPos = Math.max(minPos, decl.getStartPosition() + decl.getLength());
            declIdx++;
        }
        if (commentPos > minPos) {
            NativeDeclaration nativeDecl = extractNativeDeclaration(comment);
            if (nativeDecl != null) {
                findMethodSignatures(nativeDecl.getImplementationCode(), methodsPrinted);
                bodyDeclarations.add(declIdx++, nativeDecl);
            }
        }
    }
    // methods are always live.
    if (typeUtil.findSupertype(type.asType(), "java.lang.Iterable") != null && !methodsPrinted.contains("countByEnumeratingWithState:objects:count:") && (deadCodeMap == null || !deadCodeMap.containsClass(type, elementUtil))) {
        bodyDeclarations.add(NativeDeclaration.newInnerDeclaration(null, "- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state " + "objects:(__unsafe_unretained id *)stackbuf count:(NSUInteger)len {\n" + "  return JreDefaultFastEnumeration(self, state, stackbuf);\n}\n"));
    }
}
Also used : Comment(com.google.devtools.j2objc.ast.Comment) NativeDeclaration(com.google.devtools.j2objc.ast.NativeDeclaration) TypeElement(javax.lang.model.element.TypeElement) BodyDeclaration(com.google.devtools.j2objc.ast.BodyDeclaration)

Aggregations

NativeDeclaration (com.google.devtools.j2objc.ast.NativeDeclaration)3 BodyDeclaration (com.google.devtools.j2objc.ast.BodyDeclaration)1 Comment (com.google.devtools.j2objc.ast.Comment)1 TypeElement (javax.lang.model.element.TypeElement)1