Skip to content

Commit 2a206f9

Browse files
authored
Read/store ILCustomAttrs, ILSecurityDecls, ILTypeDef sensibly (#4597)
* Read/store ILCustomAttrs, ILSecurityDecls, ILTypeDef sensibly (#4597) * hide representation of ILPreTypeDef * fix build * remove unnecessary use of lists with repeated tail-appends * add comment * fix test
1 parent b6dfbb6 commit 2a206f9

25 files changed

+757
-670
lines changed

src/absil/il.fs

Lines changed: 274 additions & 146 deletions
Large diffs are not rendered by default.

src/absil/il.fsi

Lines changed: 190 additions & 269 deletions
Large diffs are not rendered by default.

src/absil/ilmorph.fs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ let morphILTypesInILInstr ((factualty,fformalty)) i =
200200
| ILToken.ILField fr -> I_ldtoken (ILToken.ILField (conv_fspec fr))
201201
| x -> x
202202

203-
let return_typ2typ ilg f (r:ILReturn) = {r with Type=f r.Type; CustomAttrs=cattrs_typ2typ ilg f r.CustomAttrs}
204-
let param_typ2typ ilg f (p: ILParameter) = {p with Type=f p.Type; CustomAttrs=cattrs_typ2typ ilg f p.CustomAttrs}
203+
let return_typ2typ ilg f (r:ILReturn) = {r with Type=f r.Type; CustomAttrsStored= storeILCustomAttrs (cattrs_typ2typ ilg f r.CustomAttrs)}
204+
let param_typ2typ ilg f (p: ILParameter) = {p with Type=f p.Type; CustomAttrsStored= storeILCustomAttrs (cattrs_typ2typ ilg f p.CustomAttrs)}
205205

206206
let morphILMethodDefs f (m:ILMethodDefs) = mkILMethods (List.map f m.AsList)
207207
let fdefs_fdef2fdef f (m:ILFieldDefs) = mkILFields (List.map f m.AsList)
@@ -287,14 +287,14 @@ and tdefs_typ2typ_ilmbody2ilmbody_mdefs2mdefs ilg enc fs tdefs =
287287
// --------------------------------------------------------------------
288288

289289
let manifest_typ2typ ilg f (m : ILAssemblyManifest) =
290-
{ m with CustomAttrs = cattrs_typ2typ ilg f m.CustomAttrs }
290+
{ m with CustomAttrsStored = storeILCustomAttrs (cattrs_typ2typ ilg f m.CustomAttrs) }
291291

292292
let morphILTypeInILModule_ilmbody2ilmbody_mdefs2mdefs ilg ((ftype: ILModuleDef -> (ILTypeDef list * ILTypeDef) option -> ILMethodDef option -> ILType -> ILType),fmdefs) m =
293293

294294
let ftdefs = tdefs_typ2typ_ilmbody2ilmbody_mdefs2mdefs ilg [] (ftype m,fmdefs m)
295295

296296
{ m with TypeDefs=ftdefs m.TypeDefs;
297-
CustomAttrs=cattrs_typ2typ ilg (ftype m None None) m.CustomAttrs;
297+
CustomAttrsStored= storeILCustomAttrs (cattrs_typ2typ ilg (ftype m None None) m.CustomAttrs);
298298
Manifest=Option.map (manifest_typ2typ ilg (ftype m None None)) m.Manifest }
299299

300300
let module_instr2instr_typ2typ ilg fs x =

src/absil/ilprint.fs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,11 +1021,10 @@ let goutput_manifest env os m =
10211021
output_sqstring os m.Name;
10221022
output_string os " { \n";
10231023
output_string os ".hash algorithm "; output_i32 os m.AuxModuleHashAlgorithm; output_string os "\n";
1024-
goutput_custom_attrs env os m.CustomAttrs;
1025-
goutput_security_decls env os m.SecurityDecls;
1026-
(output_option output_publickey) os m.PublicKey;
1027-
(output_option output_ver) os m.Version;
1028-
(output_option output_locale) os m.Locale;
1024+
goutput_custom_attrs env os m.CustomAttrs
1025+
(output_option output_publickey) os m.PublicKey
1026+
(output_option output_ver) os m.Version
1027+
(output_option output_locale) os m.Locale
10291028
output_string os " } \n"
10301029

10311030

src/absil/ilread.fs

Lines changed: 88 additions & 55 deletions
Large diffs are not rendered by default.

src/absil/ilreflect.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1431,7 +1431,7 @@ let buildGenParamsPass1b cenv emEnv (genArgs : Type array) (gps : ILGenericParam
14311431
| _ -> failwith "buildGenParam: multiple base types"
14321432
);
14331433
// set interface constraints (interfaces that instances of gp must meet)
1434-
gpB.SetInterfaceConstraints(Array.ofList interfaceTs);
1434+
gpB.SetInterfaceConstraints(Array.ofList interfaceTs)
14351435
gp.CustomAttrs |> emitCustomAttrs cenv emEnv (wrapCustomAttr gpB.SetCustomAttribute)
14361436

14371437
let flags = GenericParameterAttributes.None

src/fsharp/CompileOps.fs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3781,13 +3781,15 @@ let MakeILResource rname bytes =
37813781
{ Name = rname
37823782
Location = ILResourceLocation.LocalOut bytes
37833783
Access = ILResourceAccess.Public
3784-
CustomAttrs = emptyILCustomAttrs }
3784+
CustomAttrsStored = storeILCustomAttrs emptyILCustomAttrs
3785+
MetadataIndex = NoMetadataIdx }
37853786

37863787
let PickleToResource inMem file g scope rname p x =
37873788
{ Name = rname
37883789
Location = (let bytes = pickleObjWithDanglingCcus inMem file g scope p x in ILResourceLocation.LocalOut bytes)
37893790
Access = ILResourceAccess.Public
3790-
CustomAttrs = emptyILCustomAttrs }
3791+
CustomAttrsStored = storeILCustomAttrs emptyILCustomAttrs
3792+
MetadataIndex = NoMetadataIdx }
37913793

37923794
let GetSignatureData (file, ilScopeRef, ilModule, byteReader) : PickledDataWithReferences<PickledCcuInfo> =
37933795
unpickleObjWithDanglingCcus file ilScopeRef ilModule unpickleCcuInfo byteReader
@@ -5205,11 +5207,11 @@ module private ScriptPreprocessClosure =
52055207
let allRootDiagnostics = allRootDiagnostics |> List.filter (fst >> isRootRange)
52065208

52075209
let result : LoadClosure =
5208-
{ SourceFiles = List.groupByFirst sourceFiles
5209-
References = List.groupByFirst references
5210+
{ SourceFiles = List.groupBy fst sourceFiles |> List.map (map2Of2 (List.map snd))
5211+
References = List.groupBy fst references |> List.map (map2Of2 (List.map snd))
52105212
UnresolvedReferences = unresolvedReferences
52115213
Inputs = sourceInputs
5212-
NoWarns = List.groupByFirst globalNoWarns
5214+
NoWarns = List.groupBy fst globalNoWarns |> List.map (map2Of2 (List.map snd))
52135215
OriginalLoadReferences = tcConfig.loadedSources
52145216
ResolutionDiagnostics = resolutionDiagnostics
52155217
AllRootFileDiagnostics = allRootDiagnostics
@@ -5430,7 +5432,7 @@ let TypeCheckOneInputEventually
54305432
let m = qualNameOfFile.Range
54315433
TcOpenDecl tcSink tcGlobals amap m m tcEnv prefixPath
54325434

5433-
let res = (EmptyTopAttrs, [], tcEnv, tcEnv, tcState.tcsTcImplEnv, RootSigsAndImpls(rootSigs, rootImpls, allSigModulTyp, allImplementedSigModulTyp), tcState.tcsCcuType, createsGeneratedProvidedTypes)
5435+
let res = (EmptyTopAttrs, None, tcEnv, tcEnv, tcState.tcsTcImplEnv, RootSigsAndImpls(rootSigs, rootImpls, allSigModulTyp, allImplementedSigModulTyp), tcState.tcsCcuType, createsGeneratedProvidedTypes)
54345436
return res
54355437

54365438
| ParsedInput.ImplFile (ParsedImplFileInput(filename, _, qualNameOfFile, _, _, _, _) as file) ->
@@ -5490,7 +5492,7 @@ let TypeCheckOneInputEventually
54905492
if verbose then dprintf "done TypeCheckOneInputEventually...\n"
54915493

54925494
let topSigsAndImpls = RootSigsAndImpls(rootSigs, rootImpls, allSigModulTyp, allImplementedSigModulTyp)
5493-
let res = (topAttrs, [implFile], tcEnvAtEnd, tcSigEnv, tcImplEnv, topSigsAndImpls, ccuType, createsGeneratedProvidedTypes)
5495+
let res = (topAttrs, Some implFile, tcEnvAtEnd, tcSigEnv, tcImplEnv, topSigsAndImpls, ccuType, createsGeneratedProvidedTypes)
54945496
return res }
54955497

54965498
return (tcEnvAtEnd, topAttrs, implFiles),
@@ -5502,7 +5504,7 @@ let TypeCheckOneInputEventually
55025504
tcsRootSigsAndImpls = topSigsAndImpls }
55035505
with e ->
55045506
errorRecovery e range0
5505-
return (tcState.TcEnvFromSignatures, EmptyTopAttrs, []), tcState
5507+
return (tcState.TcEnvFromSignatures, EmptyTopAttrs, None), tcState
55065508
}
55075509

55085510
/// Typecheck a single file (or interactive entry into F# Interactive)
@@ -5518,7 +5520,7 @@ let TypeCheckMultipleInputsFinish(results, tcState: TcState) =
55185520
let tcEnvsAtEndFile, topAttrs, implFiles = List.unzip3 results
55195521

55205522
let topAttrs = List.foldBack CombineTopAttrs topAttrs EmptyTopAttrs
5521-
let implFiles = List.concat implFiles
5523+
let implFiles = List.choose id implFiles
55225524
// This is the environment required by fsi.exe when incrementally adding definitions
55235525
let tcEnvAtEndOfLastFile = (match tcEnvsAtEndFile with h :: _ -> h | _ -> tcState.TcEnvFromSignatures)
55245526

src/fsharp/CompileOps.fsi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -724,10 +724,10 @@ val GetInitialTcState:
724724
/// Check one input, returned as an Eventually computation
725725
val TypeCheckOneInputEventually :
726726
checkForErrors:(unit -> bool) * TcConfig * TcImports * TcGlobals * Ast.LongIdent option * NameResolution.TcResultsSink * TcState * Ast.ParsedInput
727-
-> Eventually<(TcEnv * TopAttribs * TypedImplFile list) * TcState>
727+
-> Eventually<(TcEnv * TopAttribs * TypedImplFile option) * TcState>
728728

729729
/// Finish the checking of multiple inputs
730-
val TypeCheckMultipleInputsFinish: (TcEnv * TopAttribs * 'T list) list * TcState -> (TcEnv * TopAttribs * 'T list) * TcState
730+
val TypeCheckMultipleInputsFinish: (TcEnv * TopAttribs * 'T option) list * TcState -> (TcEnv * TopAttribs * 'T list) * TcState
731731
732732
/// Finish the checking of a closed set of inputs
733733
val TypeCheckClosedInputSetFinish: TypedImplFile list * TcState -> TcState * TypedImplFile list

src/fsharp/IlxGen.fs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3454,7 +3454,8 @@ and GenGenericParam cenv eenv (tp:Typar) =
34543454

34553455
Constraints = subTypeConstraints
34563456
Variance=NonVariant
3457-
CustomAttrs = mkILCustomAttrs (GenAttrs cenv eenv tp.Attribs)
3457+
CustomAttrsStored = storeILCustomAttrs (mkILCustomAttrs (GenAttrs cenv eenv tp.Attribs))
3458+
MetadataIndex = NoMetadataIdx
34583459
HasReferenceTypeConstraint=refTypeConstraint
34593460
HasNotNullableValueTypeConstraint=notNullableValueTypeConstraint
34603461
HasDefaultConstructorConstraint= defaultConstructorConstraint }
@@ -3474,7 +3475,8 @@ and GenSlotParam m cenv eenv (TSlotParam(nm,ty,inFlag,outFlag,optionalFlag,attri
34743475
IsIn=inFlag || inFlag2
34753476
IsOut=outFlag || outFlag2
34763477
IsOptional=optionalFlag || optionalFlag2
3477-
CustomAttrs= mkILCustomAttrs (GenAttrs cenv eenv attribs) }
3478+
CustomAttrsStored = storeILCustomAttrs (mkILCustomAttrs (GenAttrs cenv eenv attribs))
3479+
MetadataIndex = NoMetadataIdx }
34783480

34793481
and GenFormalSlotsig m cenv eenv (TSlotSig(_,typ,ctps,mtps,paraml,returnTy)) =
34803482
let paraml = List.concat paraml
@@ -5005,7 +5007,8 @@ and GenParams cenv eenv (mspec:ILMethodSpec) (attribs:ArgReprInfo list) (implVal
50055007
IsIn=inFlag
50065008
IsOut=outFlag
50075009
IsOptional=optionalFlag
5008-
CustomAttrs= mkILCustomAttrs (GenAttrs cenv eenv attribs) }
5010+
CustomAttrsStored = storeILCustomAttrs (mkILCustomAttrs (GenAttrs cenv eenv attribs))
5011+
MetadataIndex = NoMetadataIdx }
50095012

50105013
param, takenNames)
50115014
|> fst
@@ -5014,7 +5017,8 @@ and GenReturnInfo cenv eenv ilRetTy (retInfo : ArgReprInfo) : ILReturn =
50145017
let marshal,attrs = GenMarshal cenv retInfo.Attribs
50155018
{ Type=ilRetTy
50165019
Marshal=marshal
5017-
CustomAttrs= mkILCustomAttrs (GenAttrs cenv eenv attrs) }
5020+
CustomAttrsStored= storeILCustomAttrs (mkILCustomAttrs (GenAttrs cenv eenv attrs))
5021+
MetadataIndex = NoMetadataIdx }
50185022

50195023
and GenPropertyForMethodDef compileAsInstance tref mdef (v:Val) (memberInfo:ValMemberInfo) ilArgTys ilPropTy ilAttrs compiledName =
50205024
let name = match compiledName with | Some n -> n | _ -> v.PropertyName in (* chop "get_" *)

src/fsharp/InternalCollections.fs

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -198,37 +198,3 @@ type internal MruCache<'Token, 'Key,'Value when 'Value : not struct>(keepStrongl
198198
member bc.Resize(tok, newKeepStrongly, ?newKeepMax) =
199199
cache.Resize(tok, newKeepStrongly, ?newKeepMax=newKeepMax)
200200

201-
/// List helpers
202-
[<Sealed>]
203-
type internal List =
204-
/// Return a new list with one element for each unique 'Key. Multiple 'TValues are flattened.
205-
/// The original order of the first instance of 'Key is preserved.
206-
static member groupByFirst( l : ('Key * 'Value) list) : ('Key * 'Value list) list =
207-
let nextIndex = ref 0
208-
let result = System.Collections.Generic.List<'Key * System.Collections.Generic.List<'Value>>()
209-
let keyToIndex = Dictionary<'Key,int>(HashIdentity.Structural)
210-
let indexOfKey(key) =
211-
match keyToIndex.TryGetValue(key) with
212-
| true, v -> v
213-
| false, _ ->
214-
keyToIndex.Add(key,!nextIndex)
215-
nextIndex := !nextIndex + 1
216-
!nextIndex - 1
217-
218-
for kv in l do
219-
let index = indexOfKey(fst kv)
220-
if index>= result.Count then
221-
let k,vs = fst kv,System.Collections.Generic.List<'Value>()
222-
vs.Add(snd kv)
223-
result.Add(k,vs)
224-
else
225-
let _,vs = result.[index]
226-
vs.Add(snd kv)
227-
228-
result |> Seq.map(fun (k,vs) -> k,vs |> List.ofSeq ) |> List.ofSeq
229-
230-
/// Return each distinct item in the list using reference equality.
231-
static member referenceDistinct( l : 'T list) : 'T list when 'T : not struct =
232-
let set = System.Collections.Generic.Dictionary<'T,bool>(HashIdentity.Reference)
233-
l |> List.iter(fun i->set.Add(i,true))
234-
set |> Seq.map(fun kv->kv.Key) |> List.ofSeq

0 commit comments

Comments
 (0)