Skip to content

Commit

Permalink
Reorganize the implementation generation of the various types, extrac…
Browse files Browse the repository at this point in the history
…ting some duplication.

	Change on 2015/03/12 by kstanger <kstanger@google.com>
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=88445866
  • Loading branch information
kstanger committed Mar 18, 2015
1 parent 604177c commit e844a01
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 120 deletions.
Expand Up @@ -86,8 +86,6 @@ public void generate() {


for (AbstractTypeDeclaration type : unit.getTypes()) { for (AbstractTypeDeclaration type : unit.getTypes()) {
generateType(type); generateType(type);
newline();
printf("J2OBJC_TYPE_LITERAL_HEADER(%s)\n", NameTable.getFullName(type.getTypeBinding()));
} }


generateFileFooter(); generateFileFooter();
Expand All @@ -104,6 +102,7 @@ protected void generateType(AbstractTypeDeclaration node) {
printFieldSetters(node); printFieldSetters(node);
printStaticFieldDeclarations(node); printStaticFieldDeclarations(node);
printOuterDeclarations(node); printOuterDeclarations(node);
printTypeLiteralDeclaration(node);
printIncrementAndDecrementFunctions(binding); printIncrementAndDecrementFunctions(binding);


printUnprefixedAlias(binding); printUnprefixedAlias(binding);
Expand Down Expand Up @@ -287,6 +286,11 @@ private void generateAnnotationType(AnnotationTypeDeclaration node) {
printStaticInitFunction(node); printStaticInitFunction(node);
} }


private void printTypeLiteralDeclaration(AbstractTypeDeclaration node) {
newline();
printf("J2OBJC_TYPE_LITERAL_HEADER(%s)\n", NameTable.getFullName(node.getTypeBinding()));
}

private static final Set<String> NEEDS_INC_AND_DEC = ImmutableSet.of( private static final Set<String> NEEDS_INC_AND_DEC = ImmutableSet.of(
"int", "long", "double", "float", "short", "byte", "char"); "int", "long", "double", "float", "short", "byte", "char");


Expand Down
Expand Up @@ -95,11 +95,7 @@ public void generate() {
printPrivateDeclarations(types); printPrivateDeclarations(types);
printClassExtensions(types); printClassExtensions(types);
for (AbstractTypeDeclaration type : types) { for (AbstractTypeDeclaration type : types) {
generate(type); generateTypeImplementation(type);
newline();
ITypeBinding binding = type.getTypeBinding();
printf("J2OBJC_%s_TYPE_LITERAL_SOURCE(%s)\n",
binding.isInterface() ? "INTERFACE" : "CLASS", NameTable.getFullName(binding));
} }
popIgnoreDeprecatedDeclarationsPragma(); popIgnoreDeprecatedDeclarationsPragma();
} else if (unit.getMainTypeName().endsWith(NameTable.PACKAGE_INFO_MAIN_TYPE) } else if (unit.getMainTypeName().endsWith(NameTable.PACKAGE_INFO_MAIN_TYPE)
Expand All @@ -114,65 +110,87 @@ public void generate() {
save(getOutputPath()); save(getOutputPath());
} }


private void printIgnoreIncompletePragmas(CompilationUnit unit) { private void generateTypeImplementation(AbstractTypeDeclaration node) {
if (unit.hasIncompleteProtocol() || unit.hasIncompleteImplementation()) { printInitFlagDefinition(node);
newline(); printStaticVars(node);
} generateSpecificTypeImplementation(node);
if (unit.hasIncompleteProtocol()) { printOuterDefinitions(node);
println("#pragma clang diagnostic ignored \"-Wprotocol\""); printTypeLiteralImplementation(node);
} }
if (unit.hasIncompleteImplementation()) {
println("#pragma clang diagnostic ignored \"-Wincomplete-implementation\""); private void generateSpecificTypeImplementation(AbstractTypeDeclaration node) {
switch (node.getKind()) {
case ANNOTATION_TYPE_DECLARATION:
generateAnnotationTypeImplementation((AnnotationTypeDeclaration) node);
break;
case ENUM_DECLARATION:
generateEnumTypeImplementation((EnumDeclaration) node);
break;
case TYPE_DECLARATION:
if (((TypeDeclaration) node).isInterface()) {
generateInterfaceTypeImplementation((TypeDeclaration) node);
} else {
generateClassTypeImplementation((TypeDeclaration) node);
}
} }
} }


private String parameterKey(IMethodBinding method) { private void generateClassTypeImplementation(TypeDeclaration node) {
StringBuilder sb = new StringBuilder(); String typeName = NameTable.getFullName(node.getTypeBinding());
ITypeBinding[] parameterTypes = method.getParameterTypes(); newline();
for (int i = 0; i < parameterTypes.length; i++) { syncLineNumbers(node.getName()); // avoid doc-comment
if (i == 0) { printf("@implementation %s\n", typeName);
sb.append(NameTable.capitalize(NameTable.parameterKeyword(parameterTypes[i]))); printInnerDefinitions(node);
} else { printInitializeMethod(node);
sb.append(NameTable.parameterKeyword(parameterTypes[i])); if (TranslationUtil.needsReflection(node)) {
} printTypeAnnotationsMethod(node);
sb.append('_'); printMethodAnnotationMethods(TreeUtil.getMethodDeclarations(node));
printFieldAnnotationMethods(node);
printMetadata(node);
} }
return sb.toString(); println("\n@end");
} }


private String methodKey(IMethodBinding method) { private void generateInterfaceTypeImplementation(TypeDeclaration node) {
StringBuilder sb = new StringBuilder(NameTable.getMethodName(method)); String typeName = NameTable.getFullName(node.getTypeBinding());
sb.append(parameterKey(method)); boolean needsReflection = TranslationUtil.needsReflection(node);
return sb.toString(); boolean needsImplementation = hasInitializeMethod(node) || needsReflection;
if (needsImplementation && !hasInitializeMethod(node)) {
printf("\n@interface %s : NSObject\n@end\n", typeName);
}
if (!needsImplementation) {
return;
}
printf("\n@implementation %s\n", typeName);
printInitializeMethod(node);
if (needsReflection) {
printMetadata(node);
}
println("\n@end");
} }


@Override private void generateEnumTypeImplementation(EnumDeclaration node) {
public void generate(TypeDeclaration node) { List<EnumConstantDeclaration> constants = node.getEnumConstants();

String typeName = NameTable.getFullName(node.getTypeBinding()); String typeName = NameTable.getFullName(node.getTypeBinding());
if (node.isInterface()) { newline();
printStaticInterface(node, typeName); printf("%s *%s_values_[%s];\n", typeName, typeName, constants.size());
} else {
printInitFlagDefinition(node);
newline();
syncLineNumbers(node.getName()); // avoid doc-comment
printf("@implementation %s\n", typeName);
printStaticVars(node);
printInnerDefinitions(node);
printInitializeMethod(node);
if (TranslationUtil.needsReflection(node)) {
printTypeAnnotationsMethod(node);
printMethodAnnotationMethods(TreeUtil.getMethodDeclarations(node));
printFieldAnnotationMethods(node);
printMetadata(node);
}


println("\n@end"); newline();
printOuterDefinitions(node); syncLineNumbers(node.getName()); // avoid doc-comment
printf("@implementation %s\n", typeName);

printInnerDefinitions(node);
printInitializeMethod(node);

if (TranslationUtil.needsReflection(node)) {
printTypeAnnotationsMethod(node);
printMetadata(node);
} }
println("\n@end");
} }


@Override private void generateAnnotationTypeImplementation(AnnotationTypeDeclaration node) {
protected void generate(AnnotationTypeDeclaration node) {
boolean isRuntime = BindingUtil.isRuntimeAnnotation(node.getTypeBinding()); boolean isRuntime = BindingUtil.isRuntimeAnnotation(node.getTypeBinding());
boolean hasInitMethod = hasInitializeMethod(node); boolean hasInitMethod = hasInitializeMethod(node);
boolean needsReflection = TranslationUtil.needsReflection(node); boolean needsReflection = TranslationUtil.needsReflection(node);
Expand All @@ -182,8 +200,6 @@ protected void generate(AnnotationTypeDeclaration node) {
printf("\n@interface %s : NSObject\n@end\n", typeName); printf("\n@interface %s : NSObject\n@end\n", typeName);
} }


printInitFlagDefinition(node);
printStaticVars(node);


if (isRuntime || hasInitMethod || needsReflection) { if (isRuntime || hasInitMethod || needsReflection) {
syncLineNumbers(node.getName()); // avoid doc-comment syncLineNumbers(node.getName()); // avoid doc-comment
Expand Down Expand Up @@ -212,6 +228,38 @@ protected void generate(AnnotationTypeDeclaration node) {
} }
} }


private void printIgnoreIncompletePragmas(CompilationUnit unit) {
if (unit.hasIncompleteProtocol() || unit.hasIncompleteImplementation()) {
newline();
}
if (unit.hasIncompleteProtocol()) {
println("#pragma clang diagnostic ignored \"-Wprotocol\"");
}
if (unit.hasIncompleteImplementation()) {
println("#pragma clang diagnostic ignored \"-Wincomplete-implementation\"");
}
}

private String parameterKey(IMethodBinding method) {
StringBuilder sb = new StringBuilder();
ITypeBinding[] parameterTypes = method.getParameterTypes();
for (int i = 0; i < parameterTypes.length; i++) {
if (i == 0) {
sb.append(NameTable.capitalize(NameTable.parameterKeyword(parameterTypes[i])));
} else {
sb.append(NameTable.parameterKeyword(parameterTypes[i]));
}
sb.append('_');
}
return sb.toString();
}

private String methodKey(IMethodBinding method) {
StringBuilder sb = new StringBuilder(NameTable.getMethodName(method));
sb.append(parameterKey(method));
return sb.toString();
}

private void printAnnotationConstructor(ITypeBinding annotation) { private void printAnnotationConstructor(ITypeBinding annotation) {
newline(); newline();
print(annotationConstructorDeclaration(annotation)); print(annotationConstructorDeclaration(annotation));
Expand Down Expand Up @@ -274,51 +322,6 @@ private void printNativeDefinition(NativeDeclaration declaration) {
} }
} }


private void printStaticInterface(AbstractTypeDeclaration node, String typeName) {
boolean needsReflection = TranslationUtil.needsReflection(node);
boolean needsImplementation = hasInitializeMethod(node) || needsReflection;
if (needsImplementation && !hasInitializeMethod(node)) {
printf("\n@interface %s : NSObject\n@end\n", typeName);
}
printInitFlagDefinition(node);
printStaticVars(node);
if (!needsImplementation) {
return;
}
printf("\n@implementation %s\n", typeName);
printInitializeMethod(node);
if (needsReflection) {
printMetadata(node);
}
println("\n@end");
printOuterDefinitions(node);
}

@Override
protected void generate(EnumDeclaration node) {
List<EnumConstantDeclaration> constants = node.getEnumConstants();
syncLineNumbers(node.getName()); // avoid doc-comment

String typeName = NameTable.getFullName(node.getTypeBinding());
printInitFlagDefinition(node);
newline();
printf("%s *%s_values_[%s];\n", typeName, typeName, constants.size());

newline();
printf("@implementation %s\n", typeName);
printStaticVars(node);

printInnerDefinitions(node);
printInitializeMethod(node);

if (TranslationUtil.needsReflection(node)) {
printTypeAnnotationsMethod(node);
printMetadata(node);
}
println("\n@end");
printOuterDefinitions(node);
}

private void printInitFlagDefinition(AbstractTypeDeclaration node) { private void printInitFlagDefinition(AbstractTypeDeclaration node) {
ITypeBinding binding = node.getTypeBinding(); ITypeBinding binding = node.getTypeBinding();
String typeName = NameTable.getFullName(binding); String typeName = NameTable.getFullName(binding);
Expand Down Expand Up @@ -449,6 +452,13 @@ private void printStaticVars(AbstractTypeDeclaration node) {
} }
} }


private void printTypeLiteralImplementation(AbstractTypeDeclaration node) {
ITypeBinding binding = node.getTypeBinding();
newline();
printf("J2OBJC_%s_TYPE_LITERAL_SOURCE(%s)\n",
binding.isInterface() ? "INTERFACE" : "CLASS", NameTable.getFullName(binding));
}

private void printPrivateDeclarations(List<AbstractTypeDeclaration> types) { private void printPrivateDeclarations(List<AbstractTypeDeclaration> types) {
for (AbstractTypeDeclaration type : types) { for (AbstractTypeDeclaration type : types) {
printConstantDefines(type); printConstantDefines(type);
Expand Down
Expand Up @@ -70,26 +70,6 @@ protected ObjectiveCSourceFileGenerator(GenerationUnit unit, boolean emitLineDir
super(unit, emitLineDirectives); super(unit, emitLineDirectives);
} }


/**
* Generate an output source file from the specified type declaration.
*/
public void generate(AbstractTypeDeclaration node) {
if (node instanceof TypeDeclaration) {
generate((TypeDeclaration) node);
} else if (node instanceof EnumDeclaration) {
generate((EnumDeclaration) node);
} else if (node instanceof AnnotationTypeDeclaration) {
generate((AnnotationTypeDeclaration) node);
}
}

// TODO(kstanger): Remove these 3 methods.
protected void generate(TypeDeclaration node) {}

protected void generate(EnumDeclaration node) {}

protected void generate(AnnotationTypeDeclaration node) {}

private static final Predicate<BodyDeclaration> IS_STATIC = new Predicate<BodyDeclaration>() { private static final Predicate<BodyDeclaration> IS_STATIC = new Predicate<BodyDeclaration>() {
public boolean apply(BodyDeclaration decl) { public boolean apply(BodyDeclaration decl) {
return Modifier.isStatic(decl.getModifiers()); return Modifier.isStatic(decl.getModifiers());
Expand Down

0 comments on commit e844a01

Please sign in to comment.