Skip to content

Commit 1bd223b

Browse files
authored
Merge pull request github#12103 from michaelnebel/csharp/scopedmodfier
C# 11: Scoped parameters and local variables.
2 parents c92fd97 + 0f469ee commit 1bd223b

File tree

21 files changed

+8573
-9
lines changed

21 files changed

+8573
-9
lines changed

csharp/downgrades/cd877b8cc2fb8327499f96fbefd01bb988b2ed63/old.dbscheme

Lines changed: 2078 additions & 0 deletions
Large diffs are not rendered by default.

csharp/downgrades/cd877b8cc2fb8327499f96fbefd01bb988b2ed63/semmlecode.csharp.dbscheme

Lines changed: 2071 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
description: Remove a relation for scoped annotations.
2+
compatibility: backwards
3+
scoped_annotation.rel: delete

csharp/extractor/Semmle.Extraction.CSharp/Entities/CachedSymbol.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,19 @@ protected void PopulateRefKind(TextWriter trapFile, RefKind kind)
5959
}
6060
}
6161

62+
protected void PopulateScopedKind(TextWriter trapFile, ScopedKind kind)
63+
{
64+
switch (kind)
65+
{
66+
case ScopedKind.ScopedRef:
67+
trapFile.scoped_annotation(this, Kinds.ScopedAnnotation.ScopedRef);
68+
break;
69+
case ScopedKind.ScopedValue:
70+
trapFile.scoped_annotation(this, Kinds.ScopedAnnotation.ScopedValue);
71+
break;
72+
}
73+
}
74+
6275
protected void ExtractCompilerGenerated(TextWriter trapFile)
6376
{
6477
if (Symbol.IsImplicitlyDeclared)

csharp/extractor/Semmle.Extraction.CSharp/Entities/Field.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ public override void Populate(TextWriter trapFile)
3333
PopulateRefKind(trapFile, Symbol.RefKind);
3434

3535
var unboundFieldKey = Field.Create(Context, Symbol.OriginalDefinition);
36-
trapFile.fields(this, (Symbol.IsConst ? 2 : 1), Symbol.Name, ContainingType, Type.TypeRef, unboundFieldKey);
36+
var kind = Symbol.IsConst ? VariableKind.Const : VariableKind.None;
37+
trapFile.fields(this, kind, Symbol.Name, ContainingType, Type.TypeRef, unboundFieldKey);
3738

3839
PopulateModifiers(trapFile);
3940

csharp/extractor/Semmle.Extraction.CSharp/Entities/LocalVariable.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,23 @@ public override void Populate(TextWriter trapFile) { }
2323
public void PopulateManual(Expression parent, bool isVar)
2424
{
2525
var trapFile = Context.TrapWriter.Writer;
26-
var (kind, type) = Symbol is ILocalSymbol l
27-
? (l.IsRef ? 3 : l.IsConst ? 2 : 1, l.GetAnnotatedType())
28-
: (1, parent.Type);
29-
trapFile.localvars(this, kind, Symbol.Name, isVar ? 1 : 0, Type.Create(Context, type).TypeRef, parent);
26+
var @var = isVar ? 1 : 0;
3027

3128
if (Symbol is ILocalSymbol local)
3229
{
30+
var kind = local.IsRef ? Kinds.VariableKind.Ref : local.IsConst ? Kinds.VariableKind.Const : Kinds.VariableKind.None;
31+
var type = local.GetAnnotatedType();
32+
trapFile.localvars(this, kind, Symbol.Name, @var, Type.Create(Context, type).TypeRef, parent);
33+
3334
PopulateNullability(trapFile, local.GetAnnotatedType());
35+
PopulateScopedKind(trapFile, local.ScopedKind);
3436
if (local.IsRef)
3537
trapFile.type_annotation(this, Kinds.TypeAnnotation.Ref);
3638
}
39+
else
40+
{
41+
trapFile.localvars(this, Kinds.VariableKind.None, Symbol.Name, @var, Type.Create(Context, parent.Type).TypeRef, parent);
42+
}
3743

3844
trapFile.localvar_location(this, Location);
3945

csharp/extractor/Semmle.Extraction.CSharp/Entities/Parameter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public override void Populate(TextWriter trapFile)
100100
PopulateAttributes();
101101
PopulateNullability(trapFile, Symbol.GetAnnotatedType());
102102
PopulateRefKind(trapFile, Symbol.RefKind);
103+
PopulateScopedKind(trapFile, Symbol.ScopedKind);
103104

104105
if (Symbol.Name != Original.Symbol.Name)
105106
Context.ModelError(Symbol, "Inconsistent parameter declaration");
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Semmle.Extraction.Kinds;
2+
3+
public enum ScopedAnnotation
4+
{
5+
None = 0,
6+
ScopedRef = 1,
7+
ScopedValue = 2
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Semmle.Extraction.Kinds;
2+
3+
public enum VariableKind
4+
{
5+
None = 1,
6+
Const = 2,
7+
Ref = 3
8+
}

csharp/extractor/Semmle.Extraction.CSharp/Tuples.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ internal static void anonymous_types(this TextWriter trapFile, Type type) =>
191191
internal static void field_location(this TextWriter trapFile, Field field, Location location) =>
192192
trapFile.WriteTuple("field_location", field, location);
193193

194-
internal static void fields(this TextWriter trapFile, Field field, int @const, string name, Type declaringType, Type fieldType, Field unboundKey) =>
195-
trapFile.WriteTuple("fields", field, @const, name, declaringType, fieldType, unboundKey);
194+
internal static void fields(this TextWriter trapFile, Field field, VariableKind kind, string name, Type declaringType, Type fieldType, Field unboundKey) =>
195+
trapFile.WriteTuple("fields", field, (int)kind, name, declaringType, fieldType, unboundKey);
196196

197197
internal static void general_type_parameter_constraints(this TextWriter trapFile, TypeParameterConstraints constraints, int hasKind) =>
198198
trapFile.WriteTuple("general_type_parameter_constraints", constraints, hasKind);
@@ -227,8 +227,8 @@ internal static void local_functions(this TextWriter trapFile, LocalFunction fn,
227227
internal static void localvar_location(this TextWriter trapFile, LocalVariable var, Location location) =>
228228
trapFile.WriteTuple("localvar_location", var, location);
229229

230-
internal static void localvars(this TextWriter trapFile, LocalVariable key, int @const, string name, int @var, Type type, Expression expr) =>
231-
trapFile.WriteTuple("localvars", key, @const, name, @var, type, expr);
230+
internal static void localvars(this TextWriter trapFile, LocalVariable key, VariableKind kind, string name, int @var, Type type, Expression expr) =>
231+
trapFile.WriteTuple("localvars", key, (int)kind, name, @var, type, expr);
232232

233233
public static void metadata_handle(this TextWriter trapFile, IEntity entity, Location assembly, int handleValue) =>
234234
trapFile.WriteTuple("metadata_handle", entity, assembly, handleValue);
@@ -462,5 +462,8 @@ internal static void locations_mapped(this System.IO.TextWriter trapFile, NonGen
462462

463463
internal static void file_extraction_mode(this System.IO.TextWriter trapFile, Entities.File file, ExtractorMode mode) =>
464464
trapFile.WriteTuple("file_extraction_mode", file, mode);
465+
466+
internal static void scoped_annotation(this TextWriter trapFile, IEntity element, ScopedAnnotation @scoped) =>
467+
trapFile.WriteTuple("scoped_annotation", element, (int)@scoped);
465468
}
466469
}

0 commit comments

Comments
 (0)