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

Avoid regex for smart contract checking #1196

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -34,7 +34,6 @@ public class CompilationEngine(CompilationOptions options)
internal CompilationOptions Options { get; private set; } = options;
private static readonly MetadataReference[] CommonReferences;
private static readonly Dictionary<string, MetadataReference> MetaReferences = [];
internal static readonly Regex s_pattern = new(@"^(Neo\.SmartContract\.Framework\.SmartContract|SmartContract\.Framework\.SmartContract|Framework\.SmartContract|SmartContract|Neo\.SmartContract\.Framework\.Nep17Token|Neo\.SmartContract\.Framework\.TokenContract|Neo.SmartContract.Framework.Nep11Token<.*>)$");
internal readonly ConcurrentDictionary<INamedTypeSymbol, CompilationContext> Contexts = new(SymbolEqualityComparer.Default);

static CompilationEngine()
Expand Down Expand Up @@ -177,7 +176,7 @@ public List<CompilationContext> CompileProject(string csproj, List<INamedTypeSym
{
var classSymbol = semanticModel.GetDeclaredSymbol(classNode);
allClassSymbols.Add(classSymbol);
if (classSymbol is { IsAbstract: false, DeclaredAccessibility: Accessibility.Public } && IsDerivedFromSmartContract(classSymbol, s_pattern))
if (classSymbol is { IsAbstract: false, DeclaredAccessibility: Accessibility.Public } && IsDerivedFromSmartContract(classSymbol))
{
allSmartContracts.Add(classSymbol);
classDependencies[classSymbol] = [];
Expand Down Expand Up @@ -241,7 +240,7 @@ private List<CompilationContext> CompileProjectContracts(Compilation compilation
{
var classSymbol = semanticModel.GetDeclaredSymbol(classNode);
allClassSymbols.Add(classSymbol);
if (classSymbol is { IsAbstract: false, DeclaredAccessibility: Accessibility.Public } && IsDerivedFromSmartContract(classSymbol, s_pattern))
if (classSymbol is { IsAbstract: false, DeclaredAccessibility: Accessibility.Public } && IsDerivedFromSmartContract(classSymbol))
{
allSmartContracts.Add(classSymbol);
classDependencies[classSymbol] = [];
Expand Down Expand Up @@ -319,12 +318,12 @@ void Visit(INamedTypeSymbol classSymbol)
return sorted;
}

internal static bool IsDerivedFromSmartContract(INamedTypeSymbol classSymbol, Regex pattern)
internal static bool IsDerivedFromSmartContract(INamedTypeSymbol classSymbol)
{
var baseType = classSymbol.BaseType;
while (baseType != null)
{
if (pattern.IsMatch(baseType.ToString() ?? string.Empty))
if (baseType.ToString() == "Neo.SmartContract.Framework.SmartContract")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method checks the hierarchy

{
return true;
}
Expand Down
7 changes: 5 additions & 2 deletions src/Neo.Compiler.CSharp/MethodConvert/SourceConvert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Neo.VM;
using System.Linq;
using System.Text.RegularExpressions;

namespace Neo.Compiler;

internal partial class MethodConvert
{
internal static readonly Regex s_pattern = new(@"^(Neo\.SmartContract\.Framework\.SmartContract|SmartContract\.Framework\.SmartContract|Framework\.SmartContract|SmartContract|Neo\.SmartContract\.Framework\.Nep17Token|Neo\.SmartContract\.Framework\.TokenContract|Neo.SmartContract.Framework.Nep11Token<.*>)$");

private void ConvertSource(SemanticModel model)
{
if (SyntaxNode is null) return;
Expand Down Expand Up @@ -158,7 +161,7 @@ internal static bool NeedInstanceConstructor(IMethodSymbol symbol)
if (containingClass == null) return false;
// non-static methods in class
if ((symbol.MethodKind == MethodKind.Constructor || symbol.MethodKind == MethodKind.SharedConstructor)
&& !CompilationEngine.IsDerivedFromSmartContract(containingClass, CompilationEngine.s_pattern))
&& !CompilationEngine.IsDerivedFromSmartContract(containingClass))
// is constructor, and is not smart contract
// typically seen in framework methods
return true;
Expand All @@ -168,7 +171,7 @@ internal static bool NeedInstanceConstructor(IMethodSymbol symbol)
.DeclaringSyntaxReferences.Length == 0)
// No explicit non-static constructor in class
{
if (CompilationEngine.s_pattern.IsMatch(containingClass.BaseType?.ToString() ?? string.Empty))
if (s_pattern.IsMatch(containingClass.BaseType?.ToString() ?? string.Empty))
// class itself is directly inheriting smart contract; cannot have more base classes
shargon marked this conversation as resolved.
Show resolved Hide resolved
return false;
}
Expand Down
Loading