From 872ff555dc401d4b5cf3777473d955a814d43949 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 19 Nov 2023 12:34:22 +0000 Subject: Add CheckReturnValue annotation --- .../compiler/patio/PatioCodeGenContext.java | 54 ++++++++++++++++++++++ .../shedlang/compiler/util/CheckReturnValue.java | 11 +++++ 2 files changed, 65 insertions(+) create mode 100644 src/main/java/org/shedlang/compiler/util/CheckReturnValue.java (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenContext.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenContext.java index fd45f29d..da417cf5 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenContext.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenContext.java @@ -14,6 +14,7 @@ import org.shedlang.compiler.patio.ast.types.PatioType; import org.shedlang.compiler.sources.Source; import org.shedlang.compiler.typechecker.TypeInfos; import org.shedlang.compiler.types.*; +import org.shedlang.compiler.util.CheckReturnValue; import org.shedlang.compiler.util.P; import java.util.ArrayList; @@ -35,6 +36,7 @@ public record PatioCodeGenContext( ) { public static final GlobalContext EMPTY = forTypeInfos(TypeInfos.EMPTY); + @CheckReturnValue public static GlobalContext forTypeInfos(TypeInfos typeInfos) { var structTags = generateStructTags(typeInfos); var effectTags = generateEffectTags(typeInfos); @@ -46,6 +48,7 @@ public record PatioCodeGenContext( ); } + @CheckReturnValue private static PMap generateStructTags(TypeInfos typeInfos) { var tags = P.map(); var nextTag = 0; @@ -56,6 +59,7 @@ public record PatioCodeGenContext( return tags; } + @CheckReturnValue private static PMap generateEffectTags(TypeInfos typeInfos) { var tags = P.map(); var nextTag = 0; @@ -66,18 +70,21 @@ public record PatioCodeGenContext( return tags; } + @CheckReturnValue public NamespaceTypeInfo namespaceTypeInfo(NamespaceName namespaceName) { return typeInfos.namespaceTypeInfo(namespaceName) // TODO: throw better error .orElseThrow(() -> new RuntimeException("No type info for: " + namespaceName)); } + @CheckReturnValue public StructTypeInfo structTypeInfo(StructType type) { return typeInfos.structTypeInfo(type) // TODO: throw better error .orElseThrow(() -> new RuntimeException("No type info for: " + type)); } + @CheckReturnValue public int structTag(StructType type) { var tag = this.structTags.get(type); if (tag == null) { @@ -88,12 +95,14 @@ public record PatioCodeGenContext( } } + @CheckReturnValue public UserDefinedEffectInfo userDefinedEffectInfo(UserDefinedEffect effect) { return typeInfos.userDefinedEffectInfo(effect) // TODO: throw better error .orElseThrow(() -> new RuntimeException("No effect info for: " + effect)); } + @CheckReturnValue public int effectTag(UserDefinedEffect effect) { var tag = this.effectTags.get(effect); if (tag == null) { @@ -110,6 +119,7 @@ public record PatioCodeGenContext( ) { public static CompilationUnitContext EMPTY = new CompilationUnitContext(OrderedPMap.empty()); + @CheckReturnValue public CompilationUnitContext addElement(PatioCompilationUnitElement element) { // TODO: handle existing element with name. return new CompilationUnitContext( @@ -117,10 +127,12 @@ public record PatioCodeGenContext( ); } + @CheckReturnValue private Result toResult(T value) { return new Result<>(value, this); } + @CheckReturnValue public Result allocateCompilationUnitName( PatioCodeGenCompilationUnitElementSpace space, Optional name @@ -134,6 +146,7 @@ public record PatioCodeGenContext( return toResult(fullName); } + @CheckReturnValue public CompilationUnitContext addCompilationUnitElement( PatioCompilationUnitElement element ) { @@ -160,6 +173,7 @@ public record PatioCodeGenContext( P.map() ); + @CheckReturnValue public Result temporaryPop() { return new FunctionContext( this.temporaryStack.minus(0), @@ -170,6 +184,7 @@ public record PatioCodeGenContext( ).toResult(this.temporaryStack.get(0)); } + @CheckReturnValue public FunctionContext temporaryPush(PatioLocalRef value) { return new FunctionContext( this.temporaryStack.plus(value), @@ -180,6 +195,7 @@ public record PatioCodeGenContext( ); } + @CheckReturnValue public FunctionContext addInstruction(PatioInstruction instruction) { return new FunctionContext( this.temporaryStack, @@ -190,6 +206,7 @@ public record PatioCodeGenContext( ); } + @CheckReturnValue public Result allocateLocal( PatioCodeGenLocalSpace space, Optional name, @@ -211,6 +228,7 @@ public record PatioCodeGenContext( ).toResult(new PatioLocalRef(fullName)); } + @CheckReturnValue public Result>> addStackIrLocal( String name, PatioLocalFactory createLocal @@ -245,6 +263,7 @@ public record PatioCodeGenContext( return context.toResult(this.waitingForStackIrLocal.getOrDefault(name, P.vector())); } + @CheckReturnValue public PatioLocalRef stackIrLocalNameToLocal(String name) { var local = this.stackIrNameToLocal.get(name); @@ -256,12 +275,14 @@ public record PatioCodeGenContext( return local; } + @CheckReturnValue public Optional stackIrLocalNameToLocalOrNone(String name) { var local = this.stackIrNameToLocal.get(name); return Optional.ofNullable(local); } + @CheckReturnValue public FunctionContext addWaitingForShedLocal( String name, Function func @@ -292,6 +313,7 @@ public record PatioCodeGenContext( FunctionContext.EMPTY ); + @CheckReturnValue public static PatioCodeGenContext forTypeInfos(TypeInfos typeInfos) { return new PatioCodeGenContext( GlobalContext.forTypeInfos(typeInfos), @@ -301,26 +323,32 @@ public record PatioCodeGenContext( ); } + @CheckReturnValue public NamespaceTypeInfo namespaceTypeInfo(NamespaceName namespaceName) { return this.globalContext.namespaceTypeInfo(namespaceName); } + @CheckReturnValue public StructTypeInfo structTypeInfo(StructType type) { return this.globalContext.structTypeInfo(type); } + @CheckReturnValue public int structTag(StructType type) { return globalContext.structTag(type); } + @CheckReturnValue public UserDefinedEffectInfo userDefinedEffectInfo(UserDefinedEffect effect) { return this.globalContext.userDefinedEffectInfo(effect); } + @CheckReturnValue public int effectTag(UserDefinedEffect effect) { return globalContext.effectTag(effect); } + @CheckReturnValue public PatioCodeGenResult allocateCompilationUnitElementName( PatioCodeGenCompilationUnitElementSpace space, String name @@ -333,6 +361,7 @@ public record PatioCodeGenContext( ); } + @CheckReturnValue public PatioCodeGenResult allocateCompilationUnitElementName( PatioCodeGenCompilationUnitElementSpace space ) { @@ -344,11 +373,13 @@ public record PatioCodeGenContext( ); } + @CheckReturnValue public PatioCodeGenContext addDataElementMutable(String name, PatioDataLayout layout, Source source) { var dataElement = new PatioDataElementMutable(name, layout, source); return addCompilationUnitElement(dataElement); } + @CheckReturnValue public PatioCodeGenContext addCompilationUnitElement( PatioCompilationUnitElement element ) { @@ -357,12 +388,14 @@ public record PatioCodeGenContext( ); } + @CheckReturnValue public PatioCodeGenResult temporaryPop() { var result = this.functionContext.temporaryPop(); return withFunctionContextResult(result); } + @CheckReturnValue public PatioCodeGenContext temporaryPopIntoLocal(PatioLocalRef target, Source source) { var result = this.functionContext.temporaryPop(); @@ -370,16 +403,19 @@ public record PatioCodeGenContext( .addInstruction(new PatioLocalStore(target, result.value, source)); } + @CheckReturnValue public PatioCodeGenContext temporaryPush(PatioLocalRef value) { var result = this.functionContext.temporaryPush(value); return withFunctionContext(result); } + @CheckReturnValue public PatioCodeGenContext addInstruction(PatioInstruction instruction) { return withFunctionContext(this.functionContext.addInstruction(instruction)); } + @CheckReturnValue public PatioCodeGenResult allocateParam( PatioCodeGenLocalSpace space, PatioType type @@ -394,6 +430,7 @@ public record PatioCodeGenContext( return withFunctionContextResult(result); } + @CheckReturnValue public PatioCodeGenResult allocateParam( PatioCodeGenLocalSpace space, String name, @@ -409,6 +446,7 @@ public record PatioCodeGenContext( return withFunctionContextResult(result); } + @CheckReturnValue public PatioCodeGenResult allocateVal( PatioCodeGenLocalSpace space, PatioType type @@ -423,6 +461,7 @@ public record PatioCodeGenContext( return withFunctionContextResult(result); } + @CheckReturnValue public PatioCodeGenResult allocateVal( PatioCodeGenLocalSpace space, String name, @@ -438,12 +477,14 @@ public record PatioCodeGenContext( return withFunctionContextResult(result); } + @CheckReturnValue public PatioCodeGenContext addStackIrParam( String name ) { return addStackIrLocal(name, (l, c) -> c, PatioLocal.Param::new); } + @CheckReturnValue public PatioCodeGenContext addStackIrVal( String name, BiFunction setLocal @@ -451,6 +492,7 @@ public record PatioCodeGenContext( return addStackIrLocal(name, setLocal, PatioLocal.Val::new); } + @CheckReturnValue public PatioCodeGenContext addStackIrVarIfMissing( String name ) { @@ -462,6 +504,7 @@ public record PatioCodeGenContext( } } + @CheckReturnValue public PatioCodeGenContext addStackIrLocal( String name, BiFunction setLocal, @@ -481,6 +524,7 @@ public record PatioCodeGenContext( return context; } + @CheckReturnValue public PatioCodeGenContext onceStackIrLocalDefined( String name, Function func @@ -496,10 +540,12 @@ public record PatioCodeGenContext( } } + @CheckReturnValue public PatioLocalRef stackIrLocalNameToLocal(String name) { return this.functionContext.stackIrLocalNameToLocal(name); } + @CheckReturnValue public PatioCodeGenContext createFunction( String name, PatioVisibility visibility, @@ -533,6 +579,7 @@ public record PatioCodeGenContext( ); } + @CheckReturnValue public PatioCodeGenContext enterBlock() { return new PatioCodeGenContext( this.globalContext, @@ -548,6 +595,7 @@ public record PatioCodeGenContext( ); } + @CheckReturnValue public PatioCodeGenResult> exitBlock() { if (!this.functionContext.temporaryStack.isEmpty()) { throw new RuntimeException("temporary stack must be empty at the end of a block"); @@ -567,26 +615,32 @@ public record PatioCodeGenContext( ).toResult(this.functionContext.instructions); } + @CheckReturnValue private PatioCodeGenContext withCompilationUnitContext(CompilationUnitContext compilationUnitContext) { return new PatioCodeGenContext(this.globalContext, compilationUnitContext, this.blockStack, this.functionContext); } + @CheckReturnValue private PatioCodeGenResult withCompilationUnitContextResult(CompilationUnitContext.Result result) { return withCompilationUnitContext(result.context).toResult(result.value); } + @CheckReturnValue private PatioCodeGenContext withFunctionContext(FunctionContext functionContext) { return new PatioCodeGenContext(this.globalContext, this.compilationUnitContext, this.blockStack, functionContext); } + @CheckReturnValue private PatioCodeGenResult withFunctionContextResult(FunctionContext.Result result) { return withFunctionContext(result.context).toResult(result.value); } + @CheckReturnValue public PatioCodeGenResult toResult(T value) { return new PatioCodeGenResult<>(value, this); } + @CheckReturnValue public PatioCompilationUnit toCompilationUnit(Source source) { return new PatioCompilationUnit( new ArrayList<>(this.compilationUnitContext.elementsByName.values()), diff --git a/src/main/java/org/shedlang/compiler/util/CheckReturnValue.java b/src/main/java/org/shedlang/compiler/util/CheckReturnValue.java new file mode 100644 index 00000000..2e5ad19a --- /dev/null +++ b/src/main/java/org/shedlang/compiler/util/CheckReturnValue.java @@ -0,0 +1,11 @@ +package org.shedlang.compiler.util; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.SOURCE) +public @interface CheckReturnValue { +} -- cgit v1.2.3