Skip to content

Commit

Permalink
Merge pull request #3 from praeclarum/nonulls
Browse files Browse the repository at this point in the history
Use C# 8.0 Nullable References to squash bugs
  • Loading branch information
praeclarum committed Dec 11, 2018
2 parents fd8bccb + 7dd6787 commit cdd3992
Show file tree
Hide file tree
Showing 50 changed files with 1,428 additions and 1,401 deletions.
34 changes: 18 additions & 16 deletions CLanguage.sln
@@ -1,11 +1,10 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26124.0
# Visual Studio Version 16
VisualStudioVersion = 16.0.28315.86
MinimumVisualStudioVersion = 15.0.26124.0
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CLanguage", "CLanguage\CLanguage.csproj", "{A53988E1-9F38-44E6-8B29-8C3F1C4CD487}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CLanguage", "CLanguage\CLanguage.csproj", "{A53988E1-9F38-44E6-8B29-8C3F1C4CD487}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CLanguageTests", "CLanguageTests\CLanguageTests.csproj", "{EACAC407-784A-4EC8-B876-1AFC26DE6050}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CLanguageTests", "CLanguageTests\CLanguageTests.csproj", "{EACAC407-784A-4EC8-B876-1AFC26DE6050}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CLanguage.Editor.Mac", "Editor\Mac\CLanguage.Editor.Mac.csproj", "{13FC4538-EA48-4F84-9F1A-109701F9285F}"
EndProject
Expand All @@ -20,22 +19,19 @@ Global
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A53988E1-9F38-44E6-8B29-8C3F1C4CD487}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A53988E1-9F38-44E6-8B29-8C3F1C4CD487}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A53988E1-9F38-44E6-8B29-8C3F1C4CD487}.Debug|x64.ActiveCfg = Debug|x64
{A53988E1-9F38-44E6-8B29-8C3F1C4CD487}.Debug|x64.Build.0 = Debug|x64
{A53988E1-9F38-44E6-8B29-8C3F1C4CD487}.Debug|x86.ActiveCfg = Debug|x86
{A53988E1-9F38-44E6-8B29-8C3F1C4CD487}.Debug|x86.Build.0 = Debug|x86
{A53988E1-9F38-44E6-8B29-8C3F1C4CD487}.Debug|x64.ActiveCfg = Debug|Any CPU
{A53988E1-9F38-44E6-8B29-8C3F1C4CD487}.Debug|x64.Build.0 = Debug|Any CPU
{A53988E1-9F38-44E6-8B29-8C3F1C4CD487}.Debug|x86.ActiveCfg = Debug|Any CPU
{A53988E1-9F38-44E6-8B29-8C3F1C4CD487}.Debug|x86.Build.0 = Debug|Any CPU
{A53988E1-9F38-44E6-8B29-8C3F1C4CD487}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A53988E1-9F38-44E6-8B29-8C3F1C4CD487}.Release|Any CPU.Build.0 = Release|Any CPU
{A53988E1-9F38-44E6-8B29-8C3F1C4CD487}.Release|x64.ActiveCfg = Release|x64
{A53988E1-9F38-44E6-8B29-8C3F1C4CD487}.Release|x64.Build.0 = Release|x64
{A53988E1-9F38-44E6-8B29-8C3F1C4CD487}.Release|x86.ActiveCfg = Release|x86
{A53988E1-9F38-44E6-8B29-8C3F1C4CD487}.Release|x86.Build.0 = Release|x86
{A53988E1-9F38-44E6-8B29-8C3F1C4CD487}.Release|x64.ActiveCfg = Release|Any CPU
{A53988E1-9F38-44E6-8B29-8C3F1C4CD487}.Release|x64.Build.0 = Release|Any CPU
{A53988E1-9F38-44E6-8B29-8C3F1C4CD487}.Release|x86.ActiveCfg = Release|Any CPU
{A53988E1-9F38-44E6-8B29-8C3F1C4CD487}.Release|x86.Build.0 = Release|Any CPU
{EACAC407-784A-4EC8-B876-1AFC26DE6050}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EACAC407-784A-4EC8-B876-1AFC26DE6050}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EACAC407-784A-4EC8-B876-1AFC26DE6050}.Debug|x64.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -73,6 +69,12 @@ Global
{E2A5AB72-C825-47F7-96BD-D102192C4B99}.Release|x86.ActiveCfg = Release|x86
{E2A5AB72-C825-47F7-96BD-D102192C4B99}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F3DB415B-9904-423A-84F7-3B65A76BDB28}
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
Policies = $0
$0.TextStylePolicy = $1
Expand Down
5 changes: 3 additions & 2 deletions CLanguage/CLanguage.csproj
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>1.0.0</Version>
Expand All @@ -11,7 +11,8 @@
<RepositoryUrl>https://github.com/praeclarum/CLanguage.git</RepositoryUrl>
<TargetFramework>netstandard2.0</TargetFramework>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<LangVersion>latest</LangVersion>
<LangVersion>8.0</LangVersion>
<NullableReferenceTypes>true</NullableReferenceTypes>
</PropertyGroup>

<ItemGroup>
Expand Down
8 changes: 4 additions & 4 deletions CLanguage/CLanguageService.cs
Expand Up @@ -27,7 +27,7 @@ public static TranslationUnit ParseTranslationUnit (string code, Report.Printer
return ParseTranslationUnit (code, new Report (printer));
}

public static Interpreter.CInterpreter CreateInterpreter (string code, MachineInfo machineInfo = null, Report.Printer printer = null)
public static Interpreter.CInterpreter CreateInterpreter (string code, MachineInfo? machineInfo = null, Report.Printer? printer = null)
{
var exe = Compile (code, machineInfo, printer);

Expand All @@ -36,7 +36,7 @@ public static Interpreter.CInterpreter CreateInterpreter (string code, MachineIn
return i;
}

public static Interpreter.Executable Compile (string code, MachineInfo machineInfo = null, Report.Printer printer = null)
public static Interpreter.Executable Compile (string code, MachineInfo? machineInfo = null, Report.Printer? printer = null)
{
var report = new Report (printer);

Expand All @@ -50,7 +50,7 @@ public static Interpreter.Executable Compile (string code, MachineInfo machineIn
return exe;
}

public static ColorSpan[] Colorize (string code, MachineInfo machineInfo = null, Report.Printer printer = null)
public static ColorSpan[] Colorize (string code, MachineInfo? machineInfo = null, Report.Printer? printer = null)
{
var report = new Report (printer);

Expand Down Expand Up @@ -124,7 +124,7 @@ SyntaxColor GetTokenColor (Token token)
if (token.Kind < 128 || Lexer.OperatorTokens.Contains (token.Kind))
return SyntaxColor.Operator;
else if (Lexer.KeywordTokens.Contains (token.Kind)) {
switch ((string)token.Value) {
switch (token.StringValue) {
case "unsigned":
case "signed":
return SyntaxColor.Type;
Expand Down
2 changes: 1 addition & 1 deletion CLanguage/Compiler/BlockContext.cs
Expand Up @@ -21,7 +21,7 @@ public BlockContext (Block block, MachineInfo machineInfo, Report report, Compil
Block = block;
}

public override ResolvedVariable TryResolveVariable (string name, CType[] argTypes)
public override ResolvedVariable? TryResolveVariable (string name, CType[]? argTypes)
{
foreach (var s in Block.Statements) {
if (s is MultiDeclaratorStatement mds && mds.InitDeclarators != null) {
Expand Down
24 changes: 12 additions & 12 deletions CLanguage/Compiler/CCompiler.cs
Expand Up @@ -54,7 +54,7 @@ void ProcessDocument (Document document)
}
}

Token[] Include (string filePath, bool relative)
Token[]? Include (string filePath, bool relative)
{
return null;
}
Expand Down Expand Up @@ -135,13 +135,16 @@ Executable CompileExecutable ()
// Compile functions
//
foreach (var (f, pc) in functionsToCompile) {
var body = f.Body;
if (body == null)
continue;
var fc = new FunctionContext (exe, f, pc);
AddStatementDeclarations (fc);
f.Body.Emit (fc);
body.Emit (fc);
f.LocalVariables.AddRange (fc.LocalVariables);

// Make sure it returns
if (f.Body.Statements.Count == 0 || !f.Body.AlwaysReturns) {
if (body.Statements.Count == 0 || !body.AlwaysReturns) {
if (f.FunctionType.ReturnType.IsVoid) {
fc.Emit (OpCode.Return);
}
Expand Down Expand Up @@ -176,7 +179,7 @@ void AddStatementDeclarations (Statement statement, BlockContext context)
}
}
else {
var ctype = context.MakeCType (multi.Specifiers, idecl.Declarator, idecl.Initializer, block);
CType ctype = context.MakeCType (multi.Specifiers, idecl.Declarator, idecl.Initializer, block);
var name = idecl.Declarator.DeclaredIdentifier;

if (ctype is CFunctionType ftype && !HasStronglyBoundPointer (idecl.Declarator)) {
Expand Down Expand Up @@ -209,7 +212,7 @@ void AddStatementDeclarations (Statement statement, BlockContext context)
}
}
//var init = GetInitExpression(idecl.Initializer);
block.AddVariable (name, ctype);
block.AddVariable (name, ctype ?? CBasicType.SignedInt);
}

if (idecl.Initializer != null) {
Expand Down Expand Up @@ -268,16 +271,13 @@ Expression GetInitializerExpression (Initializer init)
var e = GetInitializerExpression (i);

if (i.Designation == null || i.Designation.Designators.Count == 0) {
var ie = new StructureExpression.Item ();
ie.Expression = GetInitializerExpression (i);
var ie = new StructureExpression.Item (null, GetInitializerExpression (i));
sexpr.Items.Add (ie);
}
else {

foreach (var d in i.Designation.Designators) {
var ie = new StructureExpression.Item ();
ie.Field = d.ToString ();
ie.Expression = e;
var ie = new StructureExpression.Item (d.ToString (), e);
sexpr.Items.Add (ie);
}
}
Expand All @@ -290,14 +290,14 @@ Expression GetInitializerExpression (Initializer init)
}
}

FunctionDeclarator GetFunctionDeclarator (Declarator d)
FunctionDeclarator? GetFunctionDeclarator (Declarator? d)
{
if (d == null) return null;
else if (d is FunctionDeclarator) return (FunctionDeclarator)d;
else return GetFunctionDeclarator (d.InnerDeclarator);
}

bool HasStronglyBoundPointer (Declarator d)
bool HasStronglyBoundPointer (Declarator? d)
{
if (d == null) return false;
else if (d is PointerDeclarator && ((PointerDeclarator)d).StrongBinding) return true;
Expand Down
47 changes: 23 additions & 24 deletions CLanguage/Compiler/EmitContext.cs
Expand Up @@ -8,8 +8,8 @@ namespace CLanguage.Compiler
{
public abstract class EmitContext
{
public EmitContext ParentContext { get; }
public CompiledFunction FunctionDecl { get; private set; }
public EmitContext? ParentContext { get; }
public CompiledFunction? FunctionDecl { get; private set; }

public Report Report { get; private set; }

Expand All @@ -20,17 +20,17 @@ protected EmitContext (EmitContext parentContext)
{
}

protected EmitContext (MachineInfo machineInfo, Report report, CompiledFunction fdecl, EmitContext parentContext)
protected EmitContext (MachineInfo machineInfo, Report report, CompiledFunction? fdecl, EmitContext? parentContext)
{
MachineInfo = machineInfo ?? throw new ArgumentNullException (nameof (machineInfo));
Report = report ?? throw new ArgumentNullException (nameof (report));
FunctionDecl = fdecl;
MachineInfo = machineInfo ?? (parentContext?.MachineInfo ?? throw new ArgumentNullException (nameof (machineInfo)));
Report = report ?? (parentContext?.Report ?? throw new ArgumentNullException (nameof (report)));
FunctionDecl = fdecl ?? parentContext?.FunctionDecl;
ParentContext = parentContext;
}

public virtual CType ResolveTypeName (TypeName typeName)
{
return MakeCType (typeName.Specifiers, typeName.Declarator, null, null);
return MakeCType (typeName.Specifiers, typeName.Declarator, null, new Block (VariableScope.Global));
}

public virtual CType ResolveTypeName (string typeName)
Expand All @@ -43,7 +43,7 @@ public virtual CType ResolveTypeName (string typeName)
return CBasicType.SignedInt;
}

public ResolvedVariable ResolveVariable (string name, CType[] argTypes)
public ResolvedVariable ResolveVariable (string name, CType[]? argTypes)
{
var v = TryResolveVariable (name, argTypes);
if (v != null)
Expand All @@ -52,7 +52,7 @@ public ResolvedVariable ResolveVariable (string name, CType[] argTypes)
return new ResolvedVariable (VariableScope.Global, 0, CBasicType.SignedInt);
}

public virtual ResolvedVariable TryResolveVariable (string name, CType[] argTypes)
public virtual ResolvedVariable? TryResolveVariable (string name, CType[]? argTypes)
{
var r = ParentContext?.ResolveVariable (name, argTypes);
if (r != null)
Expand All @@ -71,8 +71,7 @@ public virtual ResolvedVariable ResolveMethodFunction (CStructType structType, C
if (r != null)
return r;

Report.Error (9000, $"No definition for '{structType.Name}::{method.Name}' found");
return null;
throw new Exception ("Cannot resolve method function");
}

public virtual void BeginBlock (Block b) { }
Expand Down Expand Up @@ -173,13 +172,13 @@ public int GetInstructionOffset (CBasicType aType)
throw new NotSupportedException ("Arithmetic on type '" + aType + "'");
}

public CType MakeCType (DeclarationSpecifiers specs, Declarator decl, Initializer init, Block block)
public CType MakeCType (DeclarationSpecifiers specs, Declarator? decl, Initializer? init, Block block)
{
var type = MakeCType (specs, init, block);
return MakeCType (type, decl, init, block);
}

CType MakeCType (CType type, Declarator decl, Initializer init, Block block)
CType MakeCType (CType type, Declarator? decl, Initializer? init, Block block)
{
if (decl is IdentifierDeclarator) {
// This is the name
Expand All @@ -193,7 +192,7 @@ CType MakeCType (CType type, Declarator decl, Initializer init, Block block)
isPointerToFunc = type is CFunctionType;
}

var p = pdecl.Pointer;
Pointer? p = pdecl.Pointer;
while (p != null) {
type = new CPointerType (type);
type.TypeQualifiers = p.TypeQualifiers;
Expand All @@ -213,8 +212,8 @@ CType MakeCType (CType type, Declarator decl, Initializer init, Block block)
type = ((CPointerType)type).InnerType;
}
}
else if (decl is ArrayDeclarator) {
var adecl = (ArrayDeclarator)decl;
else if (decl is ArrayDeclarator startAdecl) {
var adecl = (ArrayDeclarator?)startAdecl;

while (adecl != null) {
int? len = null;
Expand All @@ -231,7 +230,7 @@ CType MakeCType (CType type, Declarator decl, Initializer init, Block block)
}
}
type = new CArrayType (type, len);
adecl = adecl.InnerDeclarator as ArrayDeclarator;
adecl = adecl?.InnerDeclarator as ArrayDeclarator;
if (adecl != null && adecl.InnerDeclarator != null) {
if (adecl.InnerDeclarator is IdentifierDeclarator) {
}
Expand Down Expand Up @@ -260,7 +259,9 @@ CType MakeCFunctionType (CType returnType, Declarator decl, Block block)
var name = decl.DeclaredIdentifier;
var ftype = new CFunctionType (returnType, isInstance);
foreach (var pdecl in fdecl.Parameters) {
var pt = MakeCType (pdecl.DeclarationSpecifiers, pdecl.Declarator, null, block);
var pt = pdecl.DeclarationSpecifiers != null
? MakeCType (pdecl.DeclarationSpecifiers, pdecl.Declarator, null, block)
: CBasicType.SignedInt;
if (!pt.IsVoid) {
ftype.AddParameter (pdecl.Name, pt);
}
Expand All @@ -271,7 +272,7 @@ CType MakeCFunctionType (CType returnType, Declarator decl, Block block)
return type;
}

public CType MakeCType (DeclarationSpecifiers specs, Initializer init, Block block)
public CType MakeCType (DeclarationSpecifiers specs, Initializer? init, Block block)
{
//
// Infer types
Expand All @@ -296,7 +297,7 @@ public CType MakeCType (DeclarationSpecifiers specs, Initializer init, Block blo
else {
var sign = Signedness.Signed;
var size = "";
TypeSpecifier trueTs = null;
TypeSpecifier? trueTs = null;

foreach (var ts in specs.TypeSpecifiers) {
if (ts.Name == "unsigned") {
Expand Down Expand Up @@ -330,8 +331,7 @@ public CType MakeCType (DeclarationSpecifiers specs, Initializer init, Block blo
var structTs = specs.TypeSpecifiers.FirstOrDefault (x => x.Kind == TypeSpecifierKind.Struct || x.Kind == TypeSpecifierKind.Class);
if (structTs != null) {
if (structTs.Body != null) {
var st = new CStructType ();
st.Name = structTs.Name;
var st = new CStructType (structTs.Name);
foreach (var s in structTs.Body.Statements) {
AddStructMember (st, s, block);
}
Expand All @@ -357,8 +357,7 @@ public CType MakeCType (DeclarationSpecifiers specs, Initializer init, Block blo
if (enumTs != null) {
var enumName = specs.TypeSpecifiers[0].Name;
if (enumTs.Body != null) {
var et = new CEnumType ();
et.Name = enumTs.Name;
var et = new CEnumType (enumTs.Name);
var enumContext = new EnumContext (enumTs, et, this);
foreach (var s in enumTs.Body.Statements) {
AddEnumMember (et, s, block, enumContext);
Expand Down
4 changes: 2 additions & 2 deletions CLanguage/Compiler/EnumContext.cs
Expand Up @@ -18,7 +18,7 @@ public EnumContext (TypeSpecifier enumTs, CEnumType et, EmitContext emitContext)
this.emitContext = emitContext;
}

public override ResolvedVariable TryResolveVariable (string name, CType[] argTypes)
public override ResolvedVariable? TryResolveVariable (string name, CType[]? argTypes)
{
var r = et.Members.FirstOrDefault (x => x.Name == name);
if (r != null) {
Expand All @@ -28,4 +28,4 @@ public override ResolvedVariable TryResolveVariable (string name, CType[] argTyp
return base.TryResolveVariable (name, argTypes);
}
}
}
}

0 comments on commit cdd3992

Please sign in to comment.