-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use immutable collections in syntax nodes and other utility classes (#…
…107) * change lexer interfaces to use ImmutableArray instead of IReadOnlyList * change TextLocation and SyntaxTrivia to readonly records * changed all syntax nodes to use ImmutableArray * change ClrTypeCache, ClrTypeCacheView and symbol classes to use ImmutableArrays
- Loading branch information
Showing
37 changed files
with
825 additions
and
849 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,86 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using Todl.Compiler.CodeAnalysis.Symbols; | ||
using Todl.Compiler.CodeAnalysis.Syntax; | ||
|
||
namespace Todl.Compiler.CodeAnalysis | ||
namespace Todl.Compiler.CodeAnalysis; | ||
|
||
public sealed class ClrTypeCacheView | ||
{ | ||
public sealed class ClrTypeCacheView | ||
private readonly ClrTypeCache clrTypeCache; | ||
private readonly ImmutableDictionary<string, ClrTypeSymbol> typeAliases; | ||
|
||
internal ClrTypeSymbol ResolveBaseType(string name) | ||
{ | ||
private readonly ClrTypeCache clrTypeCache; | ||
private readonly IDictionary<string, ClrTypeSymbol> typeAliases; | ||
if (string.IsNullOrEmpty(name)) | ||
{ | ||
throw new ArgumentNullException(nameof(name)); | ||
} | ||
|
||
internal ClrTypeSymbol ResolveBaseType(string name) | ||
if (typeAliases.ContainsKey(name)) | ||
{ | ||
if (string.IsNullOrEmpty(name)) | ||
{ | ||
throw new ArgumentNullException(nameof(name)); | ||
} | ||
return typeAliases[name]; | ||
} | ||
|
||
if (typeAliases.ContainsKey(name)) | ||
{ | ||
return typeAliases[name]; | ||
} | ||
return clrTypeCache.Resolve(name); | ||
} | ||
|
||
return clrTypeCache.Resolve(name); | ||
} | ||
public ClrTypeSymbol ResolveType(NameExpression nameExpression) | ||
=> ResolveBaseType(nameExpression.Text.ToString()); | ||
|
||
public ClrTypeSymbol ResolveType(NameExpression nameExpression) | ||
=> ResolveBaseType(nameExpression.Text.ToString()); | ||
public ClrTypeSymbol ResolveType(TypeExpression typeExpression) | ||
{ | ||
var baseType = ResolveType(typeExpression.BaseTypeExpression); | ||
if (!typeExpression.IsArrayType) | ||
{ | ||
return baseType; | ||
} | ||
|
||
public ClrTypeSymbol ResolveType(TypeExpression typeExpression) | ||
if (baseType is null) | ||
{ | ||
var baseType = ResolveType(typeExpression.BaseTypeExpression); | ||
if (!typeExpression.IsArrayType) | ||
{ | ||
return baseType; | ||
} | ||
return null; | ||
} | ||
|
||
if (baseType is null) | ||
{ | ||
return null; | ||
} | ||
var resolvedTypeString = typeExpression.Text.ToString().Replace(typeExpression.BaseTypeExpression.Text.ToString(), baseType.Name); | ||
|
||
var resolvedTypeString = typeExpression.Text.ToString().Replace(typeExpression.BaseTypeExpression.Text.ToString(), baseType.Name); | ||
return new(baseType.ClrType.Assembly.GetType(resolvedTypeString)); | ||
} | ||
|
||
return new(baseType.ClrType.Assembly.GetType(resolvedTypeString)); | ||
private ImmutableDictionary<string, ClrTypeSymbol> ImportTypeAliases(IEnumerable<ImportDirective> importDirectives) | ||
{ | ||
if (importDirectives == null) | ||
{ | ||
throw new ArgumentNullException(nameof(importDirectives)); | ||
} | ||
|
||
private IDictionary<string, ClrTypeSymbol> ImportTypeAliases(IEnumerable<ImportDirective> importDirectives) | ||
var importedTypes = importDirectives.SelectMany(importDirective => | ||
{ | ||
if (importDirectives == null) | ||
{ | ||
throw new ArgumentNullException(nameof(importDirectives)); | ||
} | ||
var importedNamespace = importDirective.Namespace.ToString(); | ||
var types = clrTypeCache | ||
.Types | ||
.Where(t => importedNamespace.Equals(t.Namespace)); | ||
|
||
var importedTypes = importDirectives.SelectMany(importDirective => | ||
if (!importDirective.ImportAll) | ||
{ | ||
var importedNamespace = importDirective.Namespace.ToString(); | ||
var types = clrTypeCache | ||
.Types | ||
.Where(t => importedNamespace.Equals(t.Namespace)); | ||
var importedNames = importDirective | ||
.ImportedNames | ||
.Select(n => $"{importedNamespace}.{n}") | ||
.ToHashSet(); | ||
|
||
if (!importDirective.ImportAll) | ||
{ | ||
var importedNames = importDirective | ||
.ImportedNames | ||
.Select(n => $"{importedNamespace}.{n}") | ||
.ToHashSet(); | ||
|
||
types = types.Where(t => importedNames.Contains(t.Name)); | ||
} | ||
types = types.Where(t => importedNames.Contains(t.Name)); | ||
} | ||
|
||
return types; | ||
}).Distinct(); | ||
return types; | ||
}).Distinct(); | ||
|
||
return importedTypes.ToDictionary(t => t.ClrType.Name); | ||
} | ||
return importedTypes.ToImmutableDictionary(t => t.ClrType.Name); | ||
} | ||
|
||
internal ClrTypeCacheView(ClrTypeCache cache, IEnumerable<ImportDirective> importDirectives) | ||
{ | ||
clrTypeCache = cache; | ||
typeAliases = ImportTypeAliases(importDirectives); | ||
} | ||
internal ClrTypeCacheView(ClrTypeCache cache, IEnumerable<ImportDirective> importDirectives) | ||
{ | ||
clrTypeCache = cache; | ||
typeAliases = ImportTypeAliases(importDirectives); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,8 @@ | ||
using System; | ||
namespace Todl.Compiler.CodeAnalysis.Symbols; | ||
|
||
namespace Todl.Compiler.CodeAnalysis.Symbols | ||
public abstract class VariableSymbol : Symbol | ||
{ | ||
public abstract class VariableSymbol : Symbol | ||
{ | ||
public abstract bool ReadOnly { get; } | ||
public abstract TypeSymbol Type { get; } | ||
public virtual bool Constant => false; | ||
} | ||
public abstract bool ReadOnly { get; } | ||
public abstract TypeSymbol Type { get; } | ||
public virtual bool Constant => false; | ||
} |
Oops, something went wrong.