Skip to content

Commit

Permalink
Remove the notNull helper from the compiler.
Browse files Browse the repository at this point in the history
  • Loading branch information
FeepingCreature committed Oct 12, 2023
1 parent 5cf4428 commit 0304ee2
Show file tree
Hide file tree
Showing 33 changed files with 422 additions and 499 deletions.
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fi

FLAGS="$JFLAG -I$($LLVM_CONFIG --includedir) -L-L$($LLVM_CONFIG --libdir)"

TAG=v0.4.0
TAG=v0.4.2
NEAT=.cache/bootstrap/"$TAG"/neat-"$TAG"-gcc/neat

if [ ! -f "$NEAT" ]
Expand Down
7 changes: 7 additions & 0 deletions src/backend/base.nt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ module backend.base;
import helpers;
import polyhash;

bottom die() {
import c.stdlib : exit;

print("Internal compiler error.");
exit(1);
}

abstract class Backend
{
abstract BackendModule createModule(
Expand Down
11 changes: 2 additions & 9 deletions src/backend/llvm.nt
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,7 @@ class LLVMBackendFunction : BackendFunction, PhiCapable {
}

override int callFuncPtr(BackendType type, int callReg, int[] args) {
auto backendFpType = type.instanceOf(BackendFunctionPointerType).notNull;
auto backendFpType = type.instanceOf(BackendFunctionPointerType)? else die;

return callImpl(retType=backendFpType.ret, fpType=backendFpType, callReg, args);
}
Expand Down Expand Up @@ -1219,7 +1219,7 @@ class LLVMBackendFunction : BackendFunction, PhiCapable {
} else {
auto amd64Type = toAmd64ParamType(this.ret_);
if (amd64Type && !amd64Type.same(this.ret_)) {
auto amd64Type = amd64Type.notNull;
auto amd64Type = amd64Type? else die;
auto llAlloca = llRegToAlloca(this.ret_, reg);
auto llAmd64Type = this.mod.translate(amd64Type, false);
auto llAmd64PtrType = this.mod.translate(new BackendPointerType(amd64Type, this.mod.platform), false);
Expand Down Expand Up @@ -1306,10 +1306,3 @@ class LLVMBackendFunction : BackendFunction, PhiCapable {
bool isPointer(BackendType type) {
return type.instanceOf(BackendPointerType) || type.instanceOf(BackendFunctionPointerType);
}

private template notNull(T) {
nonnullable T notNull(nullable T arg) {
if (auto a = arg) return a;
assert(false);
}
}
27 changes: 13 additions & 14 deletions src/main.nt
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ class ModulePreProcessorImpl : ModulePreProcessor
this(this.visitors) { }

override void process(ASTModuleBase module_) {
auto module_ = module_.instanceOf(ASTModule).notNull;
auto module_ = module_.instanceOf(ASTModule)? else die;

[visitor.visit(module_) for visitor in visitors];
}
Expand Down Expand Up @@ -323,7 +323,7 @@ class ModulePostProcessorImpl : ModulePostProcessor

override void process(ModuleBase module_)
{
auto module_ = module_.instanceOf(Module).notNull;
auto module_ = module_.instanceOf(Module)? else die;

queue(module_);
}
Expand Down Expand Up @@ -427,11 +427,10 @@ class SimpleModulePostProcessor : ModulePostProcessor
settings.loopPass, preProcessor, postProcessor);
auto warmContext = makeWarmContext(settings.compiler, framePointer=null, compilingModule=module_);
auto simpleContext = Context(module_, warmContext, coldContext);
auto fun = module_.lookup(name, simpleContext, LookupReason.identifier, locRange)?;
locRange.assert(!!fun, () => "undefined function '$(name)'")?;
auto fun = fun.instanceOf(Function);
locRange.assert(!!fun, () => "'$(name)' is not a function")?;
return fun.notNull;
auto fun = module_.lookup(name, simpleContext, LookupReason.identifier, locRange)??
else return locRange.fail("undefined function '$(name)'");
return fun.instanceOf(Function)?
else return locRange.fail("'$(name)' is not a function");
}

class ParserHelperImpl : ParserHelper
Expand Down Expand Up @@ -521,11 +520,11 @@ Package[] resolvePackages(ASTPackage[] astPackages, string execPath) {
break;
}
}
if (!foundDep) {
auto foundDep = foundDep? else ({
print("Missing package dependency: $dep");
assert(false);
}
result[i].addDependency(foundDep.notNull);
die;
});
result[i].addDependency(foundDep);
}
}
return result;
Expand Down Expand Up @@ -911,11 +910,11 @@ ASTModule builtinSymbols(Options options) {
auto defaultImports = [builtins];
auto compiler = new CompilerImpl(
defaultImports, packages, options.cFlags, workPool, options.fileIdTable);
auto settings = new BuildSettings(compiler, options.backend.notNull,
auto settings = new BuildSettings(compiler, options.backend? else die,
x86_32=options.x86_32, caching=options.caching, versions=options.allVersions,
platformFlags=options.platformFlags, optimize=options.optimize, forMacro=false,
extraCFlags=options.extraCFlags, pool=workPool, loopPass=loopPass);
auto macroSettings = new BuildSettings(compiler, options.macroBackend.notNull,
auto macroSettings = new BuildSettings(compiler, options.macroBackend? else die,
x86_32=options.macro_x86_32, caching=options.caching, versions=options.allMacroVersions,
platformFlags=options.macroPlatformFlags, optimize=false, forMacro=true,
extraCFlags = options.macroExtraCFlags, pool=workPool, loopPass=loopPass);
Expand Down Expand Up @@ -1031,7 +1030,7 @@ long long int compiler_hash_mult() { return $(mult)LL; }
if (!options.fileIdOutput.empty) {
mut string content;
// FIXME something goes wrong with the refcounting if I directly access pinnedFileIds.keys here
auto pinnedFileIds = compiler.fileIdTable.instanceOf(FileIdTableImpl).notNull.pinnedFileIds;
auto pinnedFileIds = compiler.fileIdTable.instanceOf(FileIdTableImpl)?.pinnedFileIds else die;
for (id in pinnedFileIds.keys) {
auto name = compiler.fileIdTable.toFileName(id);
content ~= "--file-id-pin $id $name\n";
Expand Down
36 changes: 17 additions & 19 deletions src/neat/array.nt
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class Array : Type
auto result = stmt.compile(context)?;
return compiler.statementExpression(result.statement, source);
}
return compiler.exprWithTemporaryExpr(context.getUniqueId, source, &do_)?.notNull;
return compiler.exprWithTemporaryExpr(context.getUniqueId, source, &do_)?? else die;
}

override nullable Statement endLifetime(Context context, Expression value)
Expand Down Expand Up @@ -311,7 +311,7 @@ class ArrayFreeze : Expression
Expression value;

this(this.value, Platform platform) {
auto arrayType = this.value.type.instanceOf(Array).notNull;
auto arrayType = this.value.type.instanceOf(Array)? else die;
this.type = new Array(arrayType.pendingElementType, platform, mutableElements=false);
this.info = value.info;
}
Expand Down Expand Up @@ -340,7 +340,7 @@ class ArrayPointer : Expression
{
int arrayReg = this.arrayValue.emit(output);

return getArrayPtr(output, this.arrayValue.type.instanceOf(Array).notNull, arrayReg);
return getArrayPtr(output, this.arrayValue.type.instanceOf(Array)? else die, arrayReg);
}

override void hash(Hash hash) { hash.adds("ArrayPointer"); this.arrayValue.hash(hash); }
Expand Down Expand Up @@ -513,8 +513,8 @@ class ArraySlice : Expression
Expression arrayEqual(Context context, Expression left, Expression right) {
auto compiler = context.compiler;

Array leftArray = left.type.instanceOf(Array).notNull;
Array rightArray = right.type.instanceOf(Array).notNull;
Array leftArray = left.type.instanceOf(Array)? else die;
Array rightArray = right.type.instanceOf(Array)? else die;

Parameter[] params = [
Parameter.simple("left", leftArray),
Expand Down Expand Up @@ -549,7 +549,7 @@ Expression arrayEqual(Context context, Expression left, Expression right) {
Expression releaseArray(Context context, Expression value) {
auto compiler = context.compiler;

Array array = value.type.instanceOf(Array).notNull;
Array array = value.type.instanceOf(Array)? else die;
Parameter[] params = [Parameter.simple("value", array)];

ASTStatement body_()
Expand All @@ -573,7 +573,7 @@ Expression releaseArray(Context context, Expression value) {
(Expression | fail Error) arrayCat(Context context, mut Expression left, mut Expression right, LocRange locRange) {
auto compiler = context.compiler;

Array leftArray = left.type.instanceOf(Array).notNull;
Array leftArray = left.type.instanceOf(Array)? else die;

mut (nullable Expression | :unknown) ltr_ = :unknown;
(nullable Expression | fail Error) ltr() {
Expand Down Expand Up @@ -605,7 +605,7 @@ Expression releaseArray(Context context, Expression value) {
(Expression left, Expression right | fail Error) merge() {
if (sameType || appendElementType) return (left, right);
if (auto rtl = rtl?) return (left, rtl);
return (ltr?.notNull, right);
return (ltr?? else die, right);
}
auto = merge?;

Expand Down Expand Up @@ -662,14 +662,13 @@ Expression releaseArray(Context context, Expression value) {
return compiler.call(
context, astFn, [
ASTArgument(astLeft, "", __RANGE__), ASTArgument(astRight, "", __RANGE__)],
locRange, false)?
.notNull;
locRange, false)?? else die;
}

(Statement | fail Error) arrayCatAssign(Context context, Reference left, Expression right, LocRange locRange) {
auto compiler = context.compiler;

Array leftArray = left.type.instanceOf(Array).notNull;
Array leftArray = left.type.instanceOf(Array)? else die;
bool appendElementType = leftArray.elementType.same(right.type)
|| leftArray.elementType.implicitConvertFrom(context, right, locRange)?;
bool appendArrays = leftArray.same(right.type);
Expand Down Expand Up @@ -761,8 +760,7 @@ Expression releaseArray(Context context, Expression value) {
context, astFn, [
ASTArgument(astLeftPtr, "", __RANGE__), ASTArgument(astRight, "", __RANGE__)],
locRange, false)?
.notNull
.(compiler.exprStatement(that));
.(compiler.exprStatement(that? else die));
}

(nullable ASTSymbol | fail Error) parseArrayLiteral(Parser parser, LexicalContext lexicalContext)
Expand All @@ -777,8 +775,8 @@ Expression releaseArray(Context context, Expression value) {
while (true) {
if (parser.acceptToken(TokenType.rsquarebracket)) break;
auto from = parser.from;
ASTSymbol value = lexicalContext.compiler.parseExpression(parser, lexicalContext)?
.notNull;
ASTSymbol value = lexicalContext.compiler.parseExpression(parser, lexicalContext)??
else return parser.fail("array literal member expected");
values ~= ASTArrayLiteralElement(value, parser.to(from));
if (parser.acceptToken(TokenType.rsquarebracket)) break;
parser.expectToken(TokenType.comma)?;
Expand Down Expand Up @@ -871,7 +869,7 @@ class ASTArrayLiteral : ASTSymbol
for (i, element in this.elements) {
merger.add(values[i], element.locRange, context)?;
}
auto mergeType = merger.type(context).notNull;
auto mergeType = merger.type(context)? else die;
for (i in 0 .. this.elements.length) {
auto elemLocRange = this.elements[i].value.locRange;
if (auto castValue = context.compiler.implicitConvertTo(context, values[i], mergeType, elemLocRange)?) {
Expand Down Expand Up @@ -901,7 +899,7 @@ class ArrayLiteral : ArrayAllocation
override int emit(Generator output)
{
auto targetReg = super.emit(output);
auto ptrReg = getArrayPtr(output, this.type.instanceOf(Array).notNull, targetReg);
auto ptrReg = getArrayPtr(output, this.type.instanceOf(Array)? else die, targetReg);
auto backendElementType = elementType.emit(output.platform);
for (i, value in values) {
int offsetReg = output.fun.wordLiteral(output.platform, i);
Expand Down Expand Up @@ -943,7 +941,7 @@ class ASTArrayLength : ASTSymbol
() => "'\$' can only be used inside an array index context")?;

return new ArrayLength(
surroundingArraySymbol.instanceOf(Expression).notNull,
surroundingArraySymbol.instanceOf(Expression)? else die,
context.nativeWordType);
}

Expand All @@ -960,7 +958,7 @@ class ArrayCastExpr : Expression
this(this.source, this.target) { this.type = target; this.info = source.info; }

override int emit(Generator generator) {
Array sourceArray = source.type.instanceOf(Array).notNull;
Array sourceArray = source.type.instanceOf(Array)? else die;
auto sourceArrayElemBk = sourceArray.elementType.emit(generator.platform);
int sourceSize = sourceArrayElemBk.size;
auto targetArrayElemBk = target.elementType.emit(generator.platform);
Expand Down
27 changes: 14 additions & 13 deletions src/neat/base.nt
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,10 @@ struct Context

(void | fail Error) assert2(bool test, LocRange locRange, string msg) {
if (test) return;
return fail(locRange, msg);
}

Error fail(LocRange locRange, string msg) {
return new Error(warmContext.errorStack ~ locRange, msg);
}

Expand Down Expand Up @@ -1275,7 +1279,7 @@ abstract class FunctionDeclarationBase : Symbol
*/
nullable Type retWIP;

Type ret() { return this.retWIP.notNull; }
Type ret() { return this.retWIP? else die; }

Parameter[] params;

Expand Down Expand Up @@ -1486,6 +1490,8 @@ class ParseStatementArgs : MacroArgs
}
}

bottom die() { import backend.base : die; die; }

interface ImportModuleBaseTask : ITask
{
(ASTModuleBase | fail Error) module_();
Expand All @@ -1502,7 +1508,7 @@ class ParseImportArgs : MacroArgs
}
(ASTModuleBase | ImportModuleBaseTask | fail Error) getModule(CompilerBase compiler) {
return this.module_.case(
nullable ASTModuleBase base: base.notNull,
nullable ASTModuleBase base: base? else die,
ImportModuleBaseTask task: task,
Error err: err);
}
Expand Down Expand Up @@ -1745,10 +1751,6 @@ struct LocRange {

}

(bottom | fail Error) die(string message) {
return new Error([this], message);
}

// This should have been caught earlier.
void hardCheck(bool flag, string message) {
if (flag) return;
Expand All @@ -1757,6 +1759,12 @@ struct LocRange {
exit(1);
}

bottom die(string message) {
print("$(toString): Internal compiler error!");
print(message);
exit(1);
}

BackendLocation toBackendLoc(FileIdTable fileIdTable) {
mut BackendLocation ret;
ret.file = fileIdTable.toFileName(fileId);
Expand All @@ -1770,13 +1778,6 @@ struct LocRange {
}
}

template notNull(T) {
nonnullable T notNull(nullable T arg) {
if (auto a = arg) return a;
assert(false);
}
}

bottom exitWithError(Error err, nullable FileIdTable fileIdTable = null) {
printError(err, fileIdTable);
exit(1);
Expand Down
Loading

0 comments on commit 0304ee2

Please sign in to comment.