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());
}
}
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);
}
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"));
}
}
Aggregations