11//----------------------------------------------------------------------------
22//
3- // Copyright (c) 2002-2010 Microsoft Corporation.
3+ // Copyright (c) 2002-2011 Microsoft Corporation.
44//
55// This source code is subject to terms and conditions of the Apache License, Version 2.0. A
66// copy of the license can be found in the License.html file at the root of this distribution.
@@ -341,6 +341,20 @@ type ILAssemblyRef(data) =
341341 assemRefLocale= locale; }
342342
343343static member FromAssembly ( assembly : System.Reflection.Assembly ) =
344+ // SILVERLIGHT-TODO: Needs to account for empty versions, public keys
345+ #if SILVERLIGHT
346+ let fullName = assembly.ToString()
347+ let components = fullName.Split([| ',' |])
348+ // name, version, culture, public key token
349+ let nm = components.[ 0 ]
350+ let v = System.Version( components.[ 1 ]. Split([| '=' |]).[ 1 ])
351+ let version : ILVersionInfo option = Some(( uint16 v.Major, uint16 v.Minor, uint16 v.Build, uint16 v.Revision))
352+ let keyString = components.[ 3 ]. Split([| '=' |]).[ 1 ]
353+ let keyBytes = Bytes.stringAsUtf8NullTerminated keyString
354+ let key = Some( PublicKeyToken( keyBytes.[ 0 .. keyBytes.Length-2 ])) // remove null terminator
355+ ILAssemblyRef.Create( nm, None, key, false , version, None)
356+
357+ #else
344358let aname = assembly.GetName()
345359let locale = None
346360//match aname.CultureInfo with
@@ -361,6 +375,7 @@ type ILAssemblyRef(data) =
361375| v-> Some( uint16 v.Major, uint16 v.Minor, uint16 v.Build, uint16 v.Revision)
362376
363377 ILAssemblyRef.Create( aname.Name, None, publicKey, false , version, locale)
378+ #endif
364379
365380member aref.QualifiedName =
366381let b = new System.Text.StringBuilder( 100 )
@@ -757,6 +772,9 @@ type ILAttribElem =
757772type ILAttributeNamedArg = ( string* ILType* bool* ILAttribElem)
758773type ILAttribute =
759774{ Method: ILMethodSpec ;
775+ #if SILVERLIGHT
776+ Arguments: ILAttribElem list * ILAttributeNamedArg list
777+ #endif
760778 Data: byte [] }
761779
762780[<NoEquality; NoComparison>]
@@ -1829,9 +1847,6 @@ let mkILFieldRef(tref,nm,ty) = { EnclosingTypeRef=tref; Name=nm; Type=ty}
18291847
18301848let mkILFieldSpec ( tref , ty ) = { FieldRef= tref; EnclosingType= ty}
18311849
1832- let mkILFieldSpec_in_tspec ( tspec : ILTypeSpec , boxity , nm , ty ) =
1833- mkILFieldSpec( mkILFieldRef( tspec.TypeRef, nm, ty), mkILTy boxity tspec)
1834-
18351850let mkILFieldSpecInTy ( typ : ILType , nm , fty ) =
18361851 mkILFieldSpec( mkILFieldRef( typ.TypeRef, nm, fty), typ)
18371852
@@ -2243,6 +2258,7 @@ let tname_RuntimeFieldHandle = "System.RuntimeFieldHandle"
22432258type ILGlobals =
22442259{ mscorlibScopeRef: ILScopeRef ;
22452260 mscorlibAssemblyName: string ;
2261+ noDebugData: bool ;
22462262 tref_Object: ILTypeRef
22472263; tspec_Object: ILTypeSpec
22482264; typ_Object: ILType
@@ -2311,7 +2327,10 @@ type ILGlobals =
23112327; typ_StreamingContext: ILType
23122328; tref_SecurityPermissionAttribute: ILTypeRef
23132329; tspec_Exception: ILTypeSpec
2314- ; typ_Exception: ILType }
2330+ ; typ_Exception: ILType
2331+ ; mutable generatedAttribsCache: ILAttribute list
2332+ ; mutable debuggerBrowsableNeverAttributeCache: ILAttribute option
2333+ ; mutable debuggerTypeProxyAttributeCache: ILAttribute option }
23152334override x.ToString () = " <ILGlobals>"
23162335
23172336let mkNormalCall mspec = I_ call( Normalcall, mspec, None)
@@ -2323,7 +2342,7 @@ let ldarg_1 = I_ldarg 1us
23232342let tname_CompilerGeneratedAttribute = " System.Runtime.CompilerServices.CompilerGeneratedAttribute"
23242343let tname_DebuggableAttribute = " System.Diagnostics.DebuggableAttribute"
23252344
2326- let mkILGlobals mscorlibScopeRef mscorlib_assembly_name_option =
2345+ let mkILGlobals mscorlibScopeRef mscorlib_assembly_name_optionnoDebugData =
23272346let mscorlibAssemblyName =
23282347match mscorlib_ assembly_ name_ optionwith
23292348| Some name-> name
@@ -2473,8 +2492,9 @@ let mkILGlobals mscorlibScopeRef mscorlib_assembly_name_option =
24732492let tref_RuntimeFieldHandle = mkILTyRef( mscorlibScopeRef, tname_ RuntimeFieldHandle)
24742493let tspec_RuntimeFieldHandle = mkILTySpec( tref_ RuntimeFieldHandle, emptyILGenericArgs)
24752494let typ_RuntimeFieldHandle = ILType.Value tspec_ RuntimeFieldHandle
2476- { mscorlibScopeRef= mscorlibScopeRef
2477- ; mscorlibAssemblyName= mscorlibAssemblyName
2495+ { mscorlibScopeRef= mscorlibScopeRef
2496+ ; mscorlibAssemblyName= mscorlibAssemblyName
2497+ ; noDebugData= noDebugData
24782498; tref_ Object= tref_ Object
24792499; tspec_ Object= tspec_ Object
24802500; typ_ Object= typ_ Object
@@ -2544,15 +2564,18 @@ let mkILGlobals mscorlibScopeRef mscorlib_assembly_name_option =
25442564; typ_ StreamingContext= typ_ StreamingContext
25452565; tref_ SecurityPermissionAttribute= tref_ SecurityPermissionAttribute
25462566; tspec_ Exception= tspec_ Exception
2547- ; typ_ Exception= typ_ Exception}
2567+ ; typ_ Exception= typ_ Exception
2568+ ; generatedAttribsCache= []
2569+ ; debuggerBrowsableNeverAttributeCache= None
2570+ ; debuggerTypeProxyAttributeCache= None}
25482571
25492572
25502573(* NOTE: ecma_ prefix refers to the standard "mscorlib"*)
25512574let ecmaPublicKey = PublicKeyToken( Bytes.ofInt32Array[| 0xde ; 0xad ; 0xbe ; 0xef ; 0xca ; 0xfe ; 0xfa ; 0xce |])
25522575
25532576let ecmaMscorlibScopeRef = ILScopeRef.Assembly( ILAssemblyRef.Create( " mscorlib" , None, Some ecmaPublicKey, true , None, None))
25542577
2555- let ecmaILGlobals = mkILGlobals ecmaMscorlibScopeRef None
2578+ let ecmaILGlobals = mkILGlobals ecmaMscorlibScopeRef Nonefalse
25562579
25572580let mkInitializeArrayMethSpec ilg =
25582581 mkILNonGenericStaticMethSpecInTy( mkILNonGenericBoxedTy( mkILTyRef( ilg.mscorlibScopeRef, " System.Runtime.CompilerServices.RuntimeHelpers" )), " InitializeArray" , [ ilg.typ_ Array; ilg.typ_ RuntimeFieldHandle], ILType.Void)
@@ -3859,7 +3882,11 @@ let sigptr_get_intarray n (bytes:byte[]) sigptr =
38593882
38603883let sigptr_get_string n bytes sigptr =
38613884let intarray , sigptr = sigptr_ get_ intarray n bytes sigptr
3885+ #if SILVERLIGHT
3886+ System.Text.Encoding.UTF8.GetString( intarray, 0 , intarray.Length), sigptr
3887+ #else
38623888 System.Text.Encoding.UTF8.GetString intarray, sigptr
3889+ #endif
38633890
38643891let sigptr_get_z_i32 bytes sigptr =
38653892let b0 , sigptr = sigptr_ get_ byte bytes sigptr
@@ -4084,6 +4111,11 @@ let encodeCustomAttrNamedArg ilg (nm, ty, prop, elem) =
40844111yield ! encodeCustomAttrValue ilg ty elem|]
40854112
40864113let mkILCustomAttribMethRef ( ilg : ILGlobals ) ( mspec : ILMethodSpec , fixedArgs : list < _ >, namedArgs : list < _ >) =
4114+ #if SILVERLIGHT
4115+ { Method= mspec;
4116+ Arguments= fixedArgs, namedArgs
4117+ Data= [||] }
4118+ #else
40874119let argtys = mspec.MethodRef.ArgTypes
40884120let args =
40894121[| yield ! [| 0x01 uy; 0x00 uy; |]
@@ -4095,6 +4127,7 @@ let mkILCustomAttribMethRef (ilg: ILGlobals) (mspec:ILMethodSpec, fixedArgs: lis
40954127
40964128{ Method= mspec;
40974129 Data= args}
4130+ #endif
40984131
40994132let mkILCustomAttribute ilg ( tref , argtys , argvs , propvs ) =
41004133 mkILCustomAttribMethRef ilg( mkILNonGenericCtorMethSpec( tref, argtys), argvs, propvs)
@@ -4125,21 +4158,42 @@ let typ_DebuggerBrowsableState ilg =
41254158 ILType.Value( mkILTySpec( tref_ DebuggerBrowsableState, emptyILGenericArgs))
41264159
41274160let mkCompilerGeneratedAttribute ilg = mkILCustomAttribute ilg( tref_ CompilerGeneratedAttribute ilg,[],[],[])
4161+ let mkDebuggerNonUserCodeAttribute ilg = mkILCustomAttribute ilg( tref_ DebuggerNonUserCodeAttribute ilg,[],[],[])
41284162let mkDebuggerHiddenAttribute ilg = mkILCustomAttribute ilg( tref_ DebuggerHiddenAttribute ilg,[],[],[])
41294163let mkDebuggerDisplayAttribute ilg s = mkILCustomAttribute ilg( tref_ DebuggerDisplayAttribute ilg,[ ilg.typ_ String],[ ILAttribElem.String( Some s)],[])
4130- let mkDebuggerTypeProxyAttribute ilg ( ty : ILType ) = mkILCustomAttribute ilg( tref_ DebuggerTypeProxyAttribute ilg,[ ilg.typ_ Type],[ ILAttribElem.TypeRef( Some ty.TypeRef)],[])
4164+ let mkDebuggerTypeProxyAttribute ilg ( ty : ILType ) =
4165+ match ilg.debuggerTypeProxyAttributeCachewith
4166+ | None->
4167+ let res = mkILCustomAttribute ilg( tref_ DebuggerTypeProxyAttribute ilg,[ ilg.typ_ Type],[ ILAttribElem.TypeRef( Some ty.TypeRef)],[])
4168+ ilg.debuggerTypeProxyAttributeCache<- Some res
4169+ res
4170+ | Some res-> res
4171+
41314172let mkDebuggerBrowsableAttribute ilg n = mkILCustomAttribute ilg( tref_ DebuggerBrowsableAttribute ilg,[ typ_ DebuggerBrowsableState ilg],[ ILAttribElem.Int32 n],[])
4132- let mkDebuggerBrowsableNeverAttribute ilg = mkDebuggerBrowsableAttribute ilg0
4133- let mkDebuggerBrowsableCollapsedAttribute ilg = mkDebuggerBrowsableAttribute ilg2
4134- let mkDebuggerBrowsableRootHiddenAttribute ilg = mkDebuggerBrowsableAttribute ilg3
4135- let mkDebuggerNonUserCodeAttribute ilg = mkILCustomAttribute ilg( tref_ DebuggerNonUserCodeAttribute ilg,[],[],[])
4173+ let mkDebuggerBrowsableNeverAttribute ilg =
4174+ match ilg.debuggerBrowsableNeverAttributeCachewith
4175+ | None->
4176+ let res = mkDebuggerBrowsableAttribute ilg0
4177+ ilg.debuggerBrowsableNeverAttributeCache<- Some res
4178+ res
4179+ | Some res-> res
41364180let mkDebuggerStepThroughAttribute ilg = mkILCustomAttribute ilg( tref_ DebuggerStepThroughAttribute ilg,[],[],[])
41374181let mkDebuggableAttribute ilg ( jitTracking , jitOptimizerDisabled ) =
41384182 mkILCustomAttribute ilg( tref_ DebuggableAttribute ilg,[ ilg.typ_ Bool; ilg.typ_ Bool], [ ILAttribElem.Bool jitTracking; ILAttribElem.Bool jitOptimizerDisabled],[])
41394183
41404184
41414185// Bug 2129. Requests attributes to be added to compiler generated methods
4142- let add_generated_attrs ilg ( attrs : ILAttributes ) = mkILCustomAttrs( attrs.AsList@ [ mkCompilerGeneratedAttribute ilg; mkDebuggerNonUserCodeAttribute ilg])
4186+ let add_generated_attrs ilg ( attrs : ILAttributes ) =
4187+ let attribs =
4188+ match ilg.generatedAttribsCachewith
4189+ | [] ->
4190+ let res = [ if not ilg.noDebugDatathen
4191+ yield mkCompilerGeneratedAttribute ilg
4192+ yield mkDebuggerNonUserCodeAttribute ilg]
4193+ ilg.generatedAttribsCache<- res
4194+ res
4195+ | res-> res
4196+ mkILCustomAttrs( attrs.AsList@ attribs)
41434197
41444198let addMethodGeneratedAttrs ilg ( mdef : ILMethodDef ) = { mdefwith CustomAttrs= add_ generated_ attrs ilg mdef.CustomAttrs}
41454199let addPropertyGeneratedAttrs ilg ( pdef : ILPropertyDef ) = { pdefwith CustomAttrs= add_ generated_ attrs ilg pdef.CustomAttrs}
@@ -4548,9 +4602,14 @@ let parseILVersion (vstr : string) =
45484602 vstr<- System.String.Join( " ." , versionComponents) ;
45494603
45504604let version = System.Version( vstr)
4551- let zero16 n = if n< 0 sthen 0 uselse uint16( n)
45524605let zero32 n = if n< 0 then 0 uselse uint16( n)
4553- ( zero32 version.Major, zero32 version.Minor, zero32 version.Build, zero16 version.MinorRevision);;
4606+ // since the minor revision will be -1 if none is specified, we need to truncate to 0 to not break existing code
4607+ #if SILVERLIGHT
4608+ let minorRevision = if versionComponents.Length< 4 then 0 uselse uint16( version.Revision)
4609+ #else
4610+ let minorRevision = if versionComponents.Length< 4 then 0 uselse uint16( version.MinorRevision)
4611+ #endif
4612+ ( zero32 version.Major, zero32 version.Minor, zero32 version.Build, minorRevision);;
45544613
45554614
45564615let compareILVersions ( a1 , a2 , a3 , a4 ) (( b1 , b2 , b3 , b4 ) : ILVersionInfo ) =