Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DirectedCodegen to make codegen simpler #1167

Merged
merged 4 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
* {@link SmithyIntegration}.
*
* @param <S> The settings object used to configure the generator.
* @param <W> The type of {@link SymbolWriter} used by the generator.
*/
public interface CodegenContext<S> {
public interface CodegenContext<S, W extends SymbolWriter<W, ?>> {
/**
* @return Gets the model being code generated.
*/
Expand All @@ -44,4 +45,16 @@ public interface CodegenContext<S> {
* @return Gets the FileManifest being written to for code generation.
*/
FileManifest fileManifest();

/**
* Get the WriterDelegator used for generating code.
*
* <p>Generates might need other delegators for specific purposes, and it's fine to
* add more methods for those specific purposes. If an implementation uses a specific
* subclass of a WriterDelegator, implementations can override this method to return
* a more specific WriterDelegator type.
*
* @return Returns the writer delegator used by the generator.
*/
WriterDelegator<W> writerDelegator();
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import java.util.Set;
import java.util.logging.Logger;

final class IntegrationTopologicalSort<S, I extends SmithyIntegration<S, ?, ?>> {
final class IntegrationTopologicalSort<I extends SmithyIntegration<?, ?, ?>> {

private static final Logger LOGGER = Logger.getLogger(IntegrationTopologicalSort.class.getName());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
* @param <C> The CodegenContext value used by the generator.
*/
@SmithyUnstableApi
public interface SmithyIntegration<S, W extends AbstractCodeWriter<W>, C extends CodegenContext<S>> {
public interface SmithyIntegration<S, W extends SymbolWriter<W, ?>, C extends CodegenContext<S, W>> {
/**
* Gets the name of the integration.
*
Expand Down Expand Up @@ -162,14 +162,12 @@ default void customize(C codegenContext) {
* runBefore, and runAfter, and integration names.
*
* @param integrations Integrations to sort.
* @param <S> The type of settings being sorted.
* @param <I> The type of integration to sort.
* @return Returns the sorted integrations.
* @throws IllegalArgumentException If a cycle is found between integrations.
* @throws IllegalArgumentException If multiple integrations share the same name.
*/
static <S, W extends AbstractCodeWriter<W>, C extends CodegenContext<S>, I extends SmithyIntegration<S, W, C>>
List<I> sort(Iterable<I> integrations) {
static <I extends SmithyIntegration<?, ?, ?>> List<I> sort(Iterable<I> integrations) {
return new IntegrationTopologicalSort<>(integrations).sort();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.Collections;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.logging.Logger;
import software.amazon.smithy.utils.AbstractCodeWriter;
import software.amazon.smithy.utils.SmithyUnstableApi;
Expand Down Expand Up @@ -319,37 +318,4 @@ final void addImportReferences(Symbol symbol, SymbolReference.ContextOption... o
}
}
}

/**
* Writes documentation comments.
*
* <p>This method is responsible for setting up the writer to begin
* writing documentation comments. This includes writing any necessary
* opening tokens (e.g., "/*"), adding tokens to the beginning of lines
* (e.g., "*"), sanitizing documentation strings, and writing any
* tokens necessary to close documentation comments (e.g., "*\/").
*
* <p>This method <em>does not</em> automatically escape the expression
* start character ("$" by default). Write calls made by the Runnable
* should either use {@link AbstractCodeWriter#writeWithNoFormatting} or escape
* the expression start character manually.
*
* <p>This method may be overridden as needed.
*
* @param consumer Consumer that accepts the writer and writes documentation.
* @return Returns the writer.
*/
public abstract W writeDocs(Consumer<W> consumer);

/**
* Writes documentation comments from a string.
*
* @param docs Documentation to write.
* @return Returns the writer.
*/
@SuppressWarnings("unchecked")
public final W writeDocs(String docs) {
writeDocs(w -> w.writeWithNoFormatting(docs));
return (W) this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.smithy.codegen.core.directed;

import software.amazon.smithy.build.FileManifest;
import software.amazon.smithy.codegen.core.CodegenContext;
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.model.shapes.ServiceShape;

/**
* Directive that contains a {@link CodegenContext}.
*
* @param <C> CodegenContext type.
* @param <S> Codegen settings type.
*/
public abstract class ContextualDirective<C extends CodegenContext<S, ?>, S> extends Directive<S> {

private final C context;

ContextualDirective(C context, ServiceShape service) {
super(context.model(), context.settings(), service);
this.context = context;
}

/**
* @return Returns the SymbolProvider used during codegen.
* @see CodegenContext#symbolProvider()
*/
public final SymbolProvider symbolProvider() {
return context().symbolProvider();
}

/**
* @return Returns the codegen context object.
*/
public final C context() {
return context;
}

/**
* @return Gets the FileManifest being written to for code generation.
*/
public final FileManifest fileManifest() {
return context.fileManifest();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.smithy.codegen.core.directed;

import java.util.Map;
import software.amazon.smithy.build.FileManifest;
import software.amazon.smithy.codegen.core.CodegenContext;
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.knowledge.ServiceIndex;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.traits.Trait;

/**
* Directive used to create a {@link CodegenContext}.
*
* @param <S> Codegen settings type.
* @see DirectedCodegen#createContext
*/
public final class CreateContext<S> extends Directive<S> {

private final SymbolProvider symbolProvider;
private final FileManifest fileManifest;

CreateContext(
Model model,
S settings,
ServiceShape service,
SymbolProvider symbolProvider,
FileManifest fileManifest
) {
super(model, settings, service);
this.symbolProvider = symbolProvider;
this.fileManifest = fileManifest;
}

/**
* @return Returns the SymbolProvider used during codegen.
*/
public SymbolProvider symbolProvider() {
return symbolProvider;
}

/**
* @return Gets the FileManifest being written to for code generation.
*/
public FileManifest fileManifest() {
return fileManifest;
}

/**
* Get a map of supported protocols on the service shape in the form of shape ID to
* the definition of the trait.
*
* @return Returns the protocol shape IDs of the service.
* @see ServiceIndex
*/
public Map<ShapeId, Trait> supportedProtocols() {
return ServiceIndex.of(model()).getProtocols(service());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.smithy.codegen.core.directed;

import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.shapes.ServiceShape;

/**
* Directive used to create a {@link SymbolProvider}.
*
* @param <S> Codegen settings type.
* @see DirectedCodegen#createContext
*/
public final class CreateSymbolProvider<S> extends Directive<S> {
CreateSymbolProvider(Model model, S settings, ServiceShape service) {
super(model, settings, service);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.smithy.codegen.core.directed;

import software.amazon.smithy.codegen.core.CodegenContext;
import software.amazon.smithy.model.shapes.ServiceShape;

/**
* Directive used to perform post-processing code generation.
*
* @param <C> CodegenContext type.
* @param <S> Codegen settings type.
* @see DirectedCodegen#customizeBeforeIntegrations
* @see DirectedCodegen#customizeAfterIntegrations
*/
public final class Customize<C extends CodegenContext<S, ?>, S> extends ContextualDirective<C, S> {
Customize(C context, ServiceShape service) {
super(context, service);
}
}
Loading