Skip to content

Commit 1461978

Browse files
T-GroCopilot
andcommitted
Rename Display→MemberRepresentation, dissolve ExtensionAndMiscHelpers, split LoopAndConstantOptimization
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 1f29259 commit 1461978

12 files changed

Lines changed: 738 additions & 739 deletions

src/Compiler/TypedTree/TypedTreeOps.Attributes.fs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,6 +1353,42 @@ module internal AttributeHelpers =
13531353
let ValRefIsCompiledAsInstanceMember g (vref: ValRef) =
13541354
ValSpecIsCompiledAsInstance g vref.Deref
13551355

1356+
let tryFindExtensionAttribute (g: TcGlobals) (attribs: Attrib list) : Attrib option =
1357+
tryFindEntityAttribByFlag g WellKnownEntityAttributes.ExtensionAttribute attribs
1358+
1359+
let tryAddExtensionAttributeIfNotAlreadyPresentForModule
1360+
(g: TcGlobals)
1361+
(tryFindExtensionAttributeIn: (Attrib list -> Attrib option) -> Attrib option)
1362+
(moduleEntity: Entity)
1363+
: Entity =
1364+
if Option.isSome (tryFindExtensionAttribute g moduleEntity.Attribs) then
1365+
moduleEntity
1366+
else
1367+
match tryFindExtensionAttributeIn (tryFindExtensionAttribute g) with
1368+
| None -> moduleEntity
1369+
| Some extensionAttrib ->
1370+
{ moduleEntity with
1371+
entity_attribs = moduleEntity.EntityAttribs.Add(extensionAttrib, WellKnownEntityAttributes.ExtensionAttribute)
1372+
}
1373+
1374+
let tryAddExtensionAttributeIfNotAlreadyPresentForType
1375+
(g: TcGlobals)
1376+
(tryFindExtensionAttributeIn: (Attrib list -> Attrib option) -> Attrib option)
1377+
(moduleOrNamespaceTypeAccumulator: ModuleOrNamespaceType ref)
1378+
(typeEntity: Entity)
1379+
: Entity =
1380+
if Option.isSome (tryFindExtensionAttribute g typeEntity.Attribs) then
1381+
typeEntity
1382+
else
1383+
match tryFindExtensionAttributeIn (tryFindExtensionAttribute g) with
1384+
| None -> typeEntity
1385+
| Some extensionAttrib ->
1386+
moduleOrNamespaceTypeAccumulator.Value.AllEntitiesByLogicalMangledName.TryFind(typeEntity.LogicalName)
1387+
|> Option.iter (fun e ->
1388+
e.entity_attribs <- e.EntityAttribs.Add(extensionAttrib, WellKnownEntityAttributes.ExtensionAttribute))
1389+
1390+
typeEntity
1391+
13561392
[<AutoOpen>]
13571393
module internal ByrefAndSpanHelpers =
13581394

src/Compiler/TypedTree/TypedTreeOps.Attributes.fsi

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,23 @@ module internal AttributeHelpers =
293293

294294
val ValRefIsCompiledAsInstanceMember: TcGlobals -> ValRef -> bool
295295

296+
val tryFindExtensionAttribute: g: TcGlobals -> attribs: Attrib list -> Attrib option
297+
298+
/// Add an System.Runtime.CompilerServices.ExtensionAttribute to the module Entity if found via predicate and not already present.
299+
val tryAddExtensionAttributeIfNotAlreadyPresentForModule:
300+
g: TcGlobals ->
301+
tryFindExtensionAttributeIn: ((Attrib list -> Attrib option) -> Attrib option) ->
302+
moduleEntity: Entity ->
303+
Entity
304+
305+
/// Add an System.Runtime.CompilerServices.ExtensionAttribute to the type Entity if found via predicate and not already present.
306+
val tryAddExtensionAttributeIfNotAlreadyPresentForType:
307+
g: TcGlobals ->
308+
tryFindExtensionAttributeIn: ((Attrib list -> Attrib option) -> Attrib option) ->
309+
moduleOrNamespaceTypeAccumulator: ModuleOrNamespaceType ref ->
310+
typeEntity: Entity ->
311+
Entity
312+
296313

297314
[<AutoOpen>]
298315
module internal ByrefAndSpanHelpers =

src/Compiler/TypedTree/TypedTreeOps.ExprConstruction.fs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,6 +1314,40 @@ module internal TypeTesters =
13141314
else
13151315
isResumableCodeTy g ty
13161316

1317+
let ComputeUseMethodImpl g (v: Val) =
1318+
v.ImplementedSlotSigs
1319+
|> List.exists (fun slotsig ->
1320+
let oty = slotsig.DeclaringType
1321+
let otcref = tcrefOfAppTy g oty
1322+
let tcref = v.MemberApparentEntity
1323+
1324+
// REVIEW: it would be good to get rid of this special casing of Compare and GetHashCode
1325+
isInterfaceTy g oty
1326+
&&
1327+
1328+
(let isCompare =
1329+
tcref.GeneratedCompareToValues.IsSome
1330+
&& (typeEquiv g oty g.mk_IComparable_ty
1331+
|| tyconRefEq g g.system_GenericIComparable_tcref otcref)
1332+
1333+
not isCompare)
1334+
&&
1335+
1336+
(let isGenericEquals =
1337+
tcref.GeneratedHashAndEqualsWithComparerValues.IsSome
1338+
&& tyconRefEq g g.system_GenericIEquatable_tcref otcref
1339+
1340+
not isGenericEquals)
1341+
&&
1342+
1343+
(let isStructural =
1344+
(tcref.GeneratedCompareToWithComparerValues.IsSome
1345+
&& typeEquiv g oty g.mk_IStructuralComparable_ty)
1346+
|| (tcref.GeneratedHashAndEqualsWithComparerValues.IsSome
1347+
&& typeEquiv g oty g.mk_IStructuralEquatable_ty)
1348+
1349+
not isStructural))
1350+
13171351

13181352
[<AutoOpen>]
13191353
module internal CommonContainers =

src/Compiler/TypedTree/TypedTreeOps.ExprConstruction.fsi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,9 @@ module internal TypeTesters =
645645
/// The delegate type ResumableCode, or any function returning this a delegate type
646646
val isReturnsResumableCodeTy: TcGlobals -> TType -> bool
647647

648+
/// Determine if a value is a method implementing an interface dispatch slot using a private method impl
649+
val ComputeUseMethodImpl: g: TcGlobals -> v: Val -> bool
650+
648651
[<AutoOpen>]
649652
module internal CommonContainers =
650653

src/Compiler/TypedTree/TypedTreeOps.FreeVars.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ module internal FreeTypeVars =
493493
accFreeInTypesLeftToRight g false true emptyFreeTyparsLeftToRight ty |> List.rev
494494

495495
[<AutoOpen>]
496-
module internal Display =
496+
module internal MemberRepresentation =
497497

498498
//--------------------------------------------------------------------------
499499
// Values representing member functions on F# types

src/Compiler/TypedTree/TypedTreeOps.FreeVars.fsi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ module internal FreeTypeVars =
117117
val freeInModuleTy: ModuleOrNamespaceType -> FreeTyvars
118118

119119
[<AutoOpen>]
120-
module internal Display =
120+
module internal MemberRepresentation =
121121

122122
val GetMemberTypeInFSharpForm:
123123
TcGlobals -> SynMemberFlags -> ValReprInfo -> TType -> range -> Typars * CurriedArgInfos * TType * ArgReprInfo

src/Compiler/TypedTree/TypedTreeOps.Remap.fs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,4 +1676,20 @@ module internal TypeEquivalence =
16761676
let measureEquiv g m1 m2 =
16771677
measureAEquiv g TypeEquivEnv.EmptyIgnoreNulls m1 m2
16781678

1679+
/// An immutable mapping from witnesses to some data.
1680+
///
1681+
/// Note: this uses an immutable HashMap/Dictionary with an IEqualityComparer that captures TcGlobals, see EmptyTraitWitnessInfoHashMap
1682+
type TraitWitnessInfoHashMap<'T> = ImmutableDictionary<TraitWitnessInfo, 'T>
1683+
1684+
/// Create an empty immutable mapping from witnesses to some data
1685+
let EmptyTraitWitnessInfoHashMap g : TraitWitnessInfoHashMap<'T> =
1686+
ImmutableDictionary.Create(
1687+
{ new IEqualityComparer<_> with
1688+
member _.Equals(a, b) =
1689+
nullSafeEquality a b (fun a b -> traitKeysAEquiv g TypeEquivEnv.EmptyIgnoreNulls a b)
1690+
1691+
member _.GetHashCode(a) = hash a.MemberName
1692+
}
1693+
)
1694+
16791695

src/Compiler/TypedTree/TypedTreeOps.Remap.fsi

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,4 +559,12 @@ module internal TypeEquivalence =
559559
/// Check the equivalence of two units-of-measure
560560
val measureEquiv: TcGlobals -> Measure -> Measure -> bool
561561

562+
/// An immutable mapping from witnesses to some data.
563+
///
564+
/// Note: this uses an immutable HashMap/Dictionary with an IEqualityComparer that captures TcGlobals, see EmptyTraitWitnessInfoHashMap
565+
type TraitWitnessInfoHashMap<'T> = ImmutableDictionary<TraitWitnessInfo, 'T>
566+
567+
/// Create an empty immutable mapping from witnesses to some data
568+
val EmptyTraitWitnessInfoHashMap: TcGlobals -> TraitWitnessInfoHashMap<'T>
569+
562570

src/Compiler/TypedTree/TypedTreeOps.Remapping.fs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,43 @@ module internal SignatureOps =
724724
seqEntity.entity_flags.IsStructRecordOrUnionType
725725
)))
726726

727+
/// Matches a ModuleOrNamespaceContents that is empty from a signature printing point of view.
728+
/// Signatures printed via the typed tree in NicePrint don't print TMDefOpens or TMDefDo.
729+
/// This will match anything that does not have any types or bindings.
730+
[<return: Struct>]
731+
let (|EmptyModuleOrNamespaces|_|) (moduleOrNamespaceContents: ModuleOrNamespaceContents) =
732+
match moduleOrNamespaceContents with
733+
| TMDefs(defs = defs) ->
734+
let mdDefsLength =
735+
defs
736+
|> List.count (function
737+
| ModuleOrNamespaceContents.TMDefRec _
738+
| ModuleOrNamespaceContents.TMDefs _ -> true
739+
| _ -> false)
740+
741+
let emptyModuleOrNamespaces =
742+
defs
743+
|> List.choose (function
744+
| ModuleOrNamespaceContents.TMDefRec _ as defRec
745+
| ModuleOrNamespaceContents.TMDefs(defs = [ ModuleOrNamespaceContents.TMDefRec _ as defRec ]) ->
746+
match defRec with
747+
| TMDefRec(bindings = [ ModuleOrNamespaceBinding.Module(mspec, ModuleOrNamespaceContents.TMDefs(defs = defs)) ]) ->
748+
defs
749+
|> List.forall (function
750+
| ModuleOrNamespaceContents.TMDefOpens _
751+
| ModuleOrNamespaceContents.TMDefDo _
752+
| ModuleOrNamespaceContents.TMDefRec(isRec = true; tycons = []; bindings = []) -> true
753+
| _ -> false)
754+
|> fun isEmpty -> if isEmpty then Some mspec else None
755+
| _ -> None
756+
| _ -> None)
757+
758+
if mdDefsLength = emptyModuleOrNamespaces.Length then
759+
ValueSome emptyModuleOrNamespaces
760+
else
761+
ValueNone
762+
| _ -> ValueNone
763+
727764
[<AutoOpen>]
728765
module internal ExprFreeVars =
729766

src/Compiler/TypedTree/TypedTreeOps.Remapping.fsi

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,13 @@ module internal SignatureOps =
121121
/// Updates the IsPrefixDisplay to false for the Microsoft.FSharp.Collections.seq`1 entity
122122
val updateSeqTypeIsPrefix: fsharpCoreMSpec: ModuleOrNamespace -> unit
123123

124+
/// Matches a ModuleOrNamespaceContents that is empty from a signature printing point of view.
125+
/// Signatures printed via the typed tree in NicePrint don't print TMDefOpens or TMDefDo.
126+
/// This will match anything that does not have any types or bindings.
127+
[<return: Struct>]
128+
val (|EmptyModuleOrNamespaces|_|):
129+
moduleOrNamespaceContents: ModuleOrNamespaceContents -> ModuleOrNamespace list voption
130+
124131
[<AutoOpen>]
125132
module internal ExprFreeVars =
126133

0 commit comments

Comments
 (0)