ECMA-335 (CLI) 标准 读书笔记——总结CLI类型系统(上)
?
??? 標(biāo)準(zhǔn)的第四部指出,CLI的核心是Kernel Profile,而Kernel Profile由Runtime infrastructure library和Base Class Library (BCL) 組成。
?
?
·???????? Runtime infrastructure library 向CLI提供了編譯器需要的服務(wù)和從特定格式的文件流中動(dòng)態(tài)調(diào)用類型的能力,CLI規(guī)范的第二部中指定了這些特定的文件格式。
·???????? BCL是現(xiàn)代編程語言的一個(gè)簡單的運(yùn)行時(shí)庫,它作為C#語言運(yùn)行時(shí)庫的標(biāo)準(zhǔn),同時(shí)也作為CLI標(biāo)準(zhǔn)庫的一部分。它提供了類型來描述CLI的內(nèi)建數(shù)據(jù)類型,簡單的文件存取,定制屬性,安全屬性,字符串處理,格式化,流,集合等等。
??
這里我們可以看出支持CLI核心特征的Runtime infrastructure library主要由指令系統(tǒng)構(gòu)成,而BCL則主要由類型系統(tǒng)構(gòu)成。所以我們研究CLI的核心就要從它的指令系統(tǒng)和類型系統(tǒng)入手。
?
我們先來總結(jié)一下CLI的類型系統(tǒng)。
通用類型系統(tǒng)的概述請參考:ECMA- 335(CLI)標(biāo)準(zhǔn) 讀書筆記(第一部:概念和架構(gòu) 第8章~8.2)
類型分為值類型和引用類型。
值類型分為內(nèi)建值類型和用戶定義類型。
引用類型分為自描述、接口、指針和內(nèi)建引用類型。
?
類型系統(tǒng)里最核心的是內(nèi)建類型,包括內(nèi)建值類型和引用類型。其它的特征則可以根據(jù)不同的系統(tǒng)平臺(tái)要求在實(shí)現(xiàn)時(shí)進(jìn)行裁減。
?
第二部 第7章 類型和簽名
元數(shù)據(jù)提供了定義和引用類型的機(jī)制。第10章描述了與類型定義相關(guān)的元數(shù)據(jù),這里不區(qū)分類型是類,接口還是值類型。
用于引用類型的機(jī)制分為以下兩部分:
·???????? 用戶定義類型的邏輯描述,該類型被引用但沒定義在當(dāng)前模塊中,它們被存儲(chǔ)于元數(shù)據(jù)的表中(22.38章)。
·???????? 編碼一個(gè)或更多類型引用的簽名,除了多種修飾語外。語法非終結(jié)符的Type描述了簽名中的獨(dú)特入口。簽名的編碼在23.1.16章詳述。
7.1 內(nèi)建類型
下面的表詳細(xì)列出了CLI中所有的內(nèi)建類型(包括指針類型):
| Type ::= | Description | Clause |
| ‘!’ Int32 | Generic parameter in a type definition, accessed by index from 0 | 9.1 |
| | ‘!!’ Int32 | Generic parameter in a method definition, accessed by index from 0 | 9.2 |
| | bool | Boolean | 7.2 |
| | char | 16-bit Unicode code point | 7.2 |
| | class TypeReference | User defined reference type | 7.3 |
| | float32 | 32-bit floating-point number | 7.2 |
| | float64 | 64-bit floating-point number | 7.2 |
| | int8 | Signed 8-bit integer | 7.2 |
| | int16 | Signed 16-bit integer | 7.2 |
| | int32 | Signed 32-bit integer | 7.2 |
| | int64 | Signed 64-bit integer | 7.2 |
| | method CallConv Type ‘*’ ‘(’ Parameters ‘)’ | Method pointer | 14.5 |
| | native int | 32- or 64-bit signed integer whose size is platform-specific | 7.2 |
| | native unsigned int | 32- or 64-bit unsigned integer whose size is platform-specific | 7.2 |
| | object | See System. Object in Partition IV | ? |
| | string | See System.String in Partition IV | ? |
| | Type ‘&’ | Managed pointer to Type. Type shall not be a managed pointer type or typedref | 14.4 |
| | Type ‘*’ | Unmanaged pointer to Type | 14.4 |
| | Type ‘<’ GenArgs ‘>’ | Instantiation of generic type | 9.4 |
| | Type ‘[’ [ Bound [ ‘,’ Bound ]*] ‘]’ | Array of Type with optional rank (number of dimensions) and bounds. | 14.1and 14.2 |
| | Type modopt ‘(’ TypeReference ‘)’ | Custom modifier that can be ignored by the caller. | 7.1.1 |
| | Type modreq ‘(’ TypeReference ‘)’ | Custom modifier that the caller shall understand. | 7.1.1 |
| | Type pinned | For local variables only. The garbage collector shall not move the referenced value. | 7.1.2 |
| | typedref | Typed reference (i.e., a value of type System.TypedReference), created by mkrefany and used by refanytype or refanyval. | 7.2 |
| | valuetype TypeReference | (Unboxed) user defined value type | 13 |
| | unsigned int8 | Unsigned 8-bit integer | 7.2 |
| | ?unsigned intl6 | Unsigned l6-bit integer | 7.2 |
| | ?unsigned int32 | Unsigned 32-bit integer | 7.2? |
| | ?unsigned int64 | Unsigned 64-bit integer | 7.2? |
| | ?void | No type. Only allowed as a return type or as part of void * | 7.2 |
?
7.4 本地?cái)?shù)據(jù)類型
CLI的一些實(shí)現(xiàn)將運(yùn)行于現(xiàn)有的操作系統(tǒng)或運(yùn)行時(shí)平臺(tái)上,這些平臺(tái)指定的數(shù)據(jù)類型要執(zhí)行某些特定的功能。元數(shù)據(jù)允許通過指定CLI的內(nèi)建和用戶定義類型如何被轉(zhuǎn)換成本地?cái)?shù)據(jù)或者從本地?cái)?shù)據(jù)轉(zhuǎn)換過來的方式,與這些本地?cái)?shù)據(jù)類型進(jìn)行交互。這些轉(zhuǎn)換信息能被(使用關(guān)鍵詞marshal)用于:
- 方法的返回類型,規(guī)定了一個(gè)本地?cái)?shù)據(jù)類型實(shí)際被返回的和應(yīng)該被轉(zhuǎn)換回指定的CLI數(shù)據(jù)類型。
- 方法的參數(shù),規(guī)定了被調(diào)用者提供的CLI數(shù)據(jù)類型應(yīng)該被轉(zhuǎn)換成指定的本地?cái)?shù)據(jù)類型。(如果參數(shù)通過引用傳遞,更新的值應(yīng)該在調(diào)用結(jié)束后,從本地?cái)?shù)據(jù)類型被轉(zhuǎn)換回CLI類型。)
- 用戶定義類型字段,規(guī)定了任何企圖傳遞包含該字段的對象的時(shí)候,平臺(tái)方法都應(yīng)該作一份這個(gè)對象的拷貝,用指定的本地?cái)?shù)據(jù)類型替換這些字段。(如果對象通過引用被傳遞,更新的值應(yīng)該在調(diào)用結(jié)束后被轉(zhuǎn)換回來。)
下面的表中列出了CLI支持的所有本地類型,并提供了每個(gè)類型的描述。(更多完整的描述請看第四部分中enum的定義System.Runtime.Interopservices.UnmanagedType,它提供了用于編碼這些類型的實(shí)際值。)所有0-63的編碼值被保留用于向后兼容所有已經(jīng)存在的CLI的執(zhí)行。64-127的值被留作將來用于這個(gè)和相關(guān)標(biāo)準(zhǔn)上。
| NativeType ::= | Description | Name in the class library enum type UnmanagedType |
| ‘[’ ‘]’ | Native array. Type and size are determined at runtime from the actual marshaled array. | LPArray |
| |bool | Boolean. 4-byte integer value where any non- zero value represents TRUE, and 0 represents FALSE. | Bool |
| | float32 | 32-bit floating-point number. | R4 |
| | float64 | 64-bit floating-point number. | R8 |
| | [unsigned] int | Signed or unsigned integer, sized to hold a pointer on the platform | SysUInt or SysInt |
| | [unsigned] int8 | Signed or unsigned 8-bit integer | U1 or I1 |
| | [unsigned] int16 | Signed or unsigned 16-bit integer | U2 or I2 |
| | [unsigned] int32 | Signed or unsigned 32-bit integer | U4 or I4 |
| | [unsigned] int64 | Signed or unsigned 64-bit integer | U8 or I8 |
| |lpstr | A pointer to a null-terminated array of ANSI characters. The code page is implementation- specific. | LPStr |
| |lpwstr | A pointer to a null-terminated array of Unicode characters. The character encoding is implementation-specific. | LPWStr |
| | method | A function pointer. | FunctionPtr |
| | NativeType ‘[’ ‘]’ | Array of NativeType. The length is determined at runtime by the size of the actual marshaled array. | LPArray |
| | NativeType ‘[’ Int32 ‘]’ | Array of Native Type of length lnt32. | LPArray |
| | NativeType ‘[’ ‘+’ Int32 ‘]’ | Array of NativeType with runtime supplied element size. The int32 specifies a parameter to the current method (counting from parameter number 0) that, at runtime, will contain the size of an element of the array in bytes. Can only be applied to methods, not fields. | LPArray |
| | NativeType ‘[’ Int32 ‘+’ Int32 ‘]’ | Array of NativeType with runtime supplied element size. The first int32 specifies the number of elements in the array. The second 1nt32 specifies which parameter to the current method (counting from parameter number 0) will specify the additional number of elements in the array. Can only be applied to methods, not fields | LPArray |
?
[例子:
.method int32 M1( int32 marshal(int32), bool[] marshal(bool[5]) )
方法 M1 帶了兩個(gè)參數(shù): 一個(gè)int32和一個(gè)帶有5個(gè) bool的數(shù)組.
.method int32 M2( int32 marshal(int32), bool[] marshal(bool[+1]) )
方法M2帶了兩個(gè)參數(shù): 一個(gè) int32和一個(gè) bool型數(shù)組: 數(shù)組元素的個(gè)數(shù)由第一個(gè)參數(shù)的值給定。
.method int32 M3( int32 marshal(int32), bool[] marshal(bool[7+1]) )
方法M3帶了兩個(gè)參數(shù): 一個(gè)int32和一個(gè) bool型數(shù)組: 數(shù)組元素的個(gè)數(shù)由第一個(gè)參數(shù)的值加上7給定.]
?
接下篇:ECMA-335 (CLI) 標(biāo)準(zhǔn) 讀書筆記——總結(jié)CLI類型系統(tǒng)(下)
轉(zhuǎn)載于:https://www.cnblogs.com/cubean/archive/2010/04/07/1706657.html
總結(jié)
以上是生活随笔為你收集整理的ECMA-335 (CLI) 标准 读书笔记——总结CLI类型系统(上)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 腾达 PR204 无线路由器与其他无线功
- 下一篇: 很漂亮的50个模板