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