The following document is based on a Microsoft® developer document describing the stages of implementation of the C# compiler. It is included here as a possible guide to understanding portions of the C# source code.
This document outlines the stages of COMPILER::CompileAll()
Read Incremental Information
----------------------------
- Reads incremental file
- Checks file list and command line arguments
Declare
-------
- ParseTopLevel
- Create NSDECLSYMs
- Create AGGSYMs
- Changed sources
- Sets access && kind(isClass, isInterface, isEnum, isStruct, isDelegate)
- if type list changed, do full rebuild
- Unchanged sources & metadata
- Sets access && approximates kind (can't set isEnum/isStruct/isDelegate until base is resolved)
All types in the compilation are known, however:
- The kind (class/struct/etc.) is not necessarily correct
- Types have no members or bases
Check for change in type list. Change will cause a full rebuild:
- number of types
- File a type was declared in
- Accessibility of a type
- Add or remove a type
Resolve Inheritance
-------------------
- Resolves namespace using clauses
- Sets isResolvingBaseClasses
- Binds to base class & implemented interfaces and resolve recursively
- Sets kind for types imported from metadata(and declared in unchanged sources)
- Set hasResolvedBaseClasses
- If base has changed, set full rebuild
All types have:
- Base class & interface lists are set
- Kind (isEnum/isStruct, is set)
- Namespaces have using clauses resolved
It is now legal to lookup types in any context
Check for changes in inheritance hierarchy.
If any change is detected, stop resolving the inheritance hierarchy,
before the change is made and recover to a full rebuild
Define
------
- Special classes (object, attributeusage, conditional, obsolete) have members defined
- Declare global (module & assembly) attributes
- Set isDefined bit on AGGSYMs
- Define aggregate members NON-recursively
- Does NOT define property accessors
- Does NOT check explicit interface implementations
- Uses 'name' attribute for indexers
- Sets special compile time attributes
- Obsolete attribute
- Attribute usage attribute
- Conditional attribute
- Evaluates compile time constants
- Might cause other types to be defined on demand
- Might cause other constants to be evaluated on demand
- Causes files to be marked as changed if they are required to be
in defined state to evaluate a constant
Changed files have all public members declared(except property accessors).
Public (non-private to be precise) interface is diffed against metadata.
If a type has changed, all dependant files are added to the changed list.
If the changed threshold is reached (50% of files have been read from source),
recover to a full rebuild.
At this point we've made the final decision for either an incremental or a full rebuild.
If incremental rebuild, read member definitions for all unchanged files from metadata.
Prepare
-------
- Prepare base classes and implemented interfaces
- For metadata & unchanged sources reads members from metaimport
- Sets isPrepared
- Builds abstract method list
- Builds implemented interface list
- Sets isOverride bit on imported methods
- For changed sources
- Check definitions against inherited members (new & override)
- Define property accessors
- Check explicit interface implementations
- For structs resolves layout
CLS
---
- CLS checking on all members performed
Emit (EmitTokens & Compile)
----
- Initializes emitter iface
- Tokens for all members are emitted
- Assembly options emitted to alink
- Members compiled
- Attributes emitted
- Method bodies emitted
- Finish output file
- Do resources
- Finish assembly emit
Write Incremental Information
-----------------------------
- Writes incremental information file
Marking a Source Input File as Changed
---------------------------------------
Things that cause a file to be marked as changed:
- The file time stamp has changed since the last rebuild
- A file this file is dependant on has had an interface change
- This file defines a constant which a changed file requires to evaluate one of its constants
- Backing out to a full rebuild
State a file can be in:
- Undeclared
- The file is fresh, we simply declare as normal
- Declared
- The text contents of the file are unchanged
- Declarations for the types in the file have been read in from old metadata
- The list of types (and their namespaces) contained in this file can not have changed
- Parse file
- Declare file
- Creates new NSDECLSYMs for the file
- Matches the new parse trees to the existing AGGSYM declarations
- Re-parents the AGGGSYMs to the new NSDECLSYMs
- Leaves orphan NSDECLSYMs from the metadata import
- Bases resolved
- Same as declared except:
- We know that the type list hasn't changed, so the base list for this type can't have changed
- We never import the member definitions for a source file from
metadata until we know we are never going to back out to a full rebuild
Backing Out to a Full Rebuild
-----------------------------
To back out to a full rebuild, all remaining unchanged files are marked as changed.
This forces them to be brought up to the current stage.
Emitting Metadata & Incremental Update
--------------------------------------
- Stages of metadata
- Has type list changed
- New types
- Deleted types
- Accessibility
- Kind (enum, struct, class, delegate)
- Base class
- Implemented interfaces
- Has interface changed of type
- Foreach public member token in metadata
- Find matching symbol (unless property accessor, as they are not created yet)
- Diff md and symbol
- Count public members from metadata
- Count public members from symbols (add uncreated property accessors to count)
- Diff counts of public members
- Match existing tokens to symbols
- Reemit tokens in changed files
- Delete tokens which are nolonger present
- Add new tokens
- Update tokens which haven't changed
Types
-----
Has Interface Changed
---------------------
- Has type list changed checked during ResolveBase
- Don't add or delete types
- Check custom attributess
- Attribute Usage
- Obsolete
- CLS compliant
Reemit
------
- Delete related tokens
- Custom Attributes
- Permissions tdHasSecurity
- Class Layout
- MethodImpls
- Set flags with SetTypeDefProps
- Base, ifaces remain unchanged
- Emit Custom Attributes
- User custom attributes
- Pseudo-custom attributes
- StructLayout tdLayoutMask
- Guid
- ComImport tdImport
- InterfaceType
- ClassInterface
- Serializable tdSerializable
- Compiler Generated
- DefaultMember
- StructLayout on structs
Parameters
----------
- Has changed checked by method
- Don't add or delete parameter tokens
- All parameter tokens must have the 'name' member set
- Return value tokens are optional - only emitted if they have CAs
Reemit
------
- Delete related tokens
- Custom Attributes
- Field Marshal
- DefineParamProps - set flags & clear PCAs
- Emit Custom Attributes
- user custom attributes
- Pseudo-custom attributes
- In pdIn
- Out pdOut
- Optional pdOptional
- Marshal pdHasFieldMarshal
- Compiler generated CAs
- ParamArray
- Out
Methods
-------
Emit
----
- EmitMethodDef
- sets flags, signature, name, parent
- Emits MethodImpls if required
- EmitMethodInfo
- sets MethodImpl flags
- Emit Custom Attributes
- Emit Method RVA
HasChanged
----------
- Find existing symbol based on:
- Name
- Return type
- Parameter types
- Check match on
- Accessibility
- Paramarray
- Varargs
- Static
- Virtual/override/abstract/new/sealed
- Operator/implicit/explicit
- Custom Attributes
- Conditional
- Obsolete
- CLS
Reemit
------
- Find existing symbol based on:
- Name
- Return type
- Parameter types
- Is static
- Is varargs
- Delete related tokens
- Custom Attributes
- PInvoke Map
- Permissions
- Set flags with SetMethodProps
- Note: can't reset signature
- add MethodImpl tokens
- Emit Custom Attributes
- user custom attributes
- Pseudo-custom attributes
- DllImport mdPinvokeImpl
- MethodImpl miPreserveSig, miInternalCall, miSynchronized, miNoInlining
- PreserveSig miPreserveSig
- Compiler Generated CAs
Method Impls
------------
- Done during method emit
- Deleted as related tokens of the type
- Added again during method reemit
Fields
------
Emit
----
- EmitMembVarDef
- Name, flags, signature, parent, constant value
- Emit Custom Attributes
- User custom attributes
- Pseudo-custom attributes
- NonSerialized fdNotSerialized
- Marshal fdHasFieldMarshal
- FieldOffset
- Compiler Generated CAs
- Decimal Constant
HasChanged
----------
- find existing symbol based on
- Name
- Type
- Check match on
- Accessibility
- Static/const/readonly
- Constant value
- Volatile
- Custom Attributes
- Obsolete
- CLS
Reemit
------
- Find existing symbol based on
- Name
- Type
- Static
- Volatile
- Delete related tokens
- Custom Attributes
- Set flags & constant value
- Emit Custom Attributes
Properties
----------
Emit
----
- Nothing emitted for explicit property impls
- Method impls only
- EmitPropertyDef
- Parent, name, get, set, signature
- Emit Custom Attributes
- User custom attributes
HasChanged
----------
- Find existing symbol based on
- Name
- Argument types
- Check match on
- Not checked - checked by accessors
- Accessibility
- Return type
- Varargs
- Param array
- Indexer name
- Accessor count
- Custom Attributes
- Obsolete
- CLS
Reemit
------
- Find existing sym base on:
- Name
- Indexer name
- Argument types
- Return types
- Is static
- Delete related tokens
- Custom Attributes
- Reset
- Flags
- Get/set tokens
- Emit Custom Attributes
Events
------
Emit
----
- Nothing emitted for explicit event impls
- EmitEventDef
- Parent, name, type, add, remove
- Emit Custom Attributes
- User custom attributes
HasChanged
----------
- Find existing symbol based on
- Name
- Check match on
- Type
- Access
- Custom Attributes
- Obsolete
- CLS
Reemit
------
- Find existing symbol based on
- Name
- Delete related tokens
- Custom Attributes
- Reset
- Flags
- Type
- Accessor tokens
- Emit Custom Attributes
- User custom attributes
Assembly
--------
Emit
----
- Emit Custom Attributes
- User custom attributes
- Pseudo-custom attributes
- Compiler Generated custom attributes
- Security Permission
- Debuggable
HasChanged
----------
- Check for match on CLS compliant
- Force full build on change
Reemit
------
- delete related tokens
- Custom Attributes
- Security Permissions
- Emit Custom Attributes
Module
------
Emit
----
- Emit Custom Attributes
- user custom attributes
- Compiler Generated custom attributes
- UnverifiableCode
HasChanged
----------
- Check for match on CLS compliant
- Force full build on change
Reemit
------
- Delete related tokens
- Custom Attributes
- Emit Custom Attributes
Private Implementation Details
------------------------------
Emit
----
- Track tokens by input file
Reemit
------
- Delete all tokens for all changed input files
Copyright (c) 2002 Microsoft Corporation