compiler.txt 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. The Internals of the Mono C# Compiler
  2. Miguel de Icaza
  3. ([email protected])
  4. 2002, 2007
  5. * Abstract
  6. The Mono C# compiler is a C# compiler written in C# itself.
  7. Its goals are to provide a free and alternate implementation
  8. of the C# language. The Mono C# compiler generates ECMA CIL
  9. images through the use of the System.Reflection.Emit API which
  10. enable the compiler to be platform independent.
  11. * Overview: How the compiler fits together
  12. The compilation process is managed by the compiler driver (it
  13. lives in driver.cs).
  14. The compiler reads a set of C# source code files, and parses
  15. them. Any assemblies or modules that the user might want to
  16. use with his project are loaded after parsing is done.
  17. Once all the files have been parsed, the type hierarchy is
  18. resolved. First interfaces are resolved, then types and
  19. enumerations.
  20. Once the type hierarchy is resolved, every type is populated:
  21. fields, methods, indexers, properties, events and delegates
  22. are entered into the type system.
  23. At this point the program skeleton has been completed. The
  24. next process is to actually emit the code for each of the
  25. executable methods. The compiler drives this from
  26. RootContext.EmitCode.
  27. Each type then has to populate its methods: populating a
  28. method requires creating a structure that is used as the state
  29. of the block being emitted (this is the EmitContext class) and
  30. then generating code for the topmost statement (the Block).
  31. Code generation has two steps: the first step is the semantic
  32. analysis (Resolve method) that resolves any pending tasks, and
  33. guarantees that the code is correct. The second phase is the
  34. actual code emission. All errors are flagged during in the
  35. "Resolution" process.
  36. After all code has been emitted, then the compiler closes all
  37. the types (this basically tells the Reflection.Emit library to
  38. finish up the types), resources, and definition of the entry
  39. point are done at this point, and the output is saved to
  40. disk.
  41. The following list will give you an idea of where the
  42. different pieces of the compiler live:
  43. Infrastructure:
  44. driver.cs:
  45. This drives the compilation process: loading of
  46. command line options; parsing the inputs files;
  47. loading the referenced assemblies; resolving the type
  48. hierarchy and emitting the code.
  49. codegen.cs:
  50. The state tracking for code generation.
  51. attribute.cs:
  52. Code to do semantic analysis and emit the attributes
  53. is here.
  54. rootcontext.cs:
  55. Keeps track of the types defined in the source code,
  56. as well as the assemblies loaded.
  57. typemanager.cs:
  58. This contains the MCS type system.
  59. report.cs:
  60. Error and warning reporting methods.
  61. support.cs:
  62. Assorted utility functions used by the compiler.
  63. Parsing
  64. cs-tokenizer.cs:
  65. The tokenizer for the C# language, it includes also
  66. the C# pre-processor.
  67. cs-parser.jay, cs-parser.cs:
  68. The parser is implemented using a C# port of the Yacc
  69. parser. The parser lives in the cs-parser.jay file,
  70. and cs-parser.cs is the generated parser.
  71. location.cs:
  72. The `location' structure is a compact representation
  73. of a file, line, column where a token, or a high-level
  74. construct appears. This is used to report errors.
  75. Expressions:
  76. ecore.cs
  77. Basic expression classes, and interfaces most shared
  78. code and static methods are here.
  79. expression.cs:
  80. Most of the different kinds of expressions classes
  81. live in this file.
  82. assign.cs:
  83. The assignment expression got its own file.
  84. constant.cs:
  85. The classes that represent the constant expressions.
  86. literal.cs
  87. Literals are constants that have been entered manually
  88. in the source code, like `1' or `true'. The compiler
  89. needs to tell constants from literals apart during the
  90. compilation process, as literals sometimes have some
  91. implicit extra conversions defined for them.
  92. cfold.cs:
  93. The constant folder for binary expressions.
  94. Statements
  95. statement.cs:
  96. All of the abstract syntax tree elements for
  97. statements live in this file. This also drives the
  98. semantic analysis process.
  99. iterators.cs:
  100. Contains the support for implementing iterators from
  101. the C# 2.0 specification.
  102. Declarations, Classes, Structs, Enumerations
  103. decl.cs
  104. This contains the base class for Members and
  105. Declaration Spaces. A declaration space introduces
  106. new names in types, so classes, structs, delegates and
  107. enumerations derive from it.
  108. class.cs:
  109. Methods for holding and defining class and struct
  110. information, and every member that can be in these
  111. (methods, fields, delegates, events, etc).
  112. The most interesting type here is the `TypeContainer'
  113. which is a derivative of the `DeclSpace'
  114. delegate.cs:
  115. Handles delegate definition and use.
  116. enum.cs:
  117. Handles enumerations.
  118. interface.cs:
  119. Holds and defines interfaces. All the code related to
  120. interface declaration lives here.
  121. parameter.cs:
  122. During the parsing process, the compiler encapsulates
  123. parameters in the Parameter and Parameters classes.
  124. These classes provide definition and resolution tools
  125. for them.
  126. pending.cs:
  127. Routines to track pending implementations of abstract
  128. methods and interfaces. These are used by the
  129. TypeContainer-derived classes to track whether every
  130. method required is implemented.
  131. * The parsing process
  132. All the input files that make up a program need to be read in
  133. advance, because C# allows declarations to happen after an
  134. entity is used, for example, the following is a valid program:
  135. class X : Y {
  136. static void Main ()
  137. {
  138. a = "hello"; b = "world";
  139. }
  140. string a;
  141. }
  142. class Y {
  143. public string b;
  144. }
  145. At the time the assignment expression `a = "hello"' is parsed,
  146. it is not know whether a is a class field from this class, or
  147. its parents, or whether it is a property access or a variable
  148. reference. The actual meaning of `a' will not be discovered
  149. until the semantic analysis phase.
  150. ** The Tokenizer and the pre-processor
  151. The tokenizer is contained in the file `cs-tokenizer.cs', and
  152. the main entry point is the `token ()' method. The tokenizer
  153. implements the `yyParser.yyInput' interface, which is what the
  154. Yacc/Jay parser will use when fetching tokens.
  155. Token definitions are generated by jay during the compilation
  156. process, and those can be references from the tokenizer class
  157. with the `Token.' prefix.
  158. Each time a token is returned, the location for the token is
  159. recorded into the `Location' property, that can be accessed by
  160. the parser. The parser retrieves the Location properties as
  161. it builds its internal representation to allow the semantic
  162. analysis phase to produce error messages that can pin point
  163. the location of the problem.
  164. Some tokens have values associated with it, for example when
  165. the tokenizer encounters a string, it will return a
  166. LITERAL_STRING token, and the actual string parsed will be
  167. available in the `Value' property of the tokenizer. The same
  168. mechanism is used to return integers and floating point
  169. numbers.
  170. C# has a limited pre-processor that allows conditional
  171. compilation, but it is not as fully featured as the C
  172. pre-processor, and most notably, macros are missing. This
  173. makes it simple to implement in very few lines and mesh it
  174. with the tokenizer.
  175. The `handle_preprocessing_directive' method in the tokenizer
  176. handles all the pre-processing, and it is invoked when the '#'
  177. symbol is found as the first token in a line.
  178. The state of the pre-processor is contained in a Stack called
  179. `ifstack', this state is used to track the if/elif/else/endif
  180. nesting and the current state. The state is encoded in the
  181. top of the stack as a number of values `TAKING',
  182. `TAKEN_BEFORE', `ELSE_SEEN', `PARENT_TAKING'.
  183. ** Locations
  184. Locations are encoded as a 32-bit number (the Location
  185. struct) that map each input source line to a linear number.
  186. As new files are parsed, the Location manager is informed of
  187. the new file, to allow it to map back from an int constant to
  188. a file + line number.
  189. Prior to parsing/tokenizing any source files, the compiler
  190. generates a list of all the source files and then reserves the
  191. low N bits of the location to hold the source file, where N is
  192. large enough to hold at least twice as many source files as were
  193. specified on the command line (to allow for a #line in each file).
  194. The upper 32-N bits are the line number in that file.
  195. The token 0 is reserved for ``anonymous'' locations, ie. if we
  196. don't know the location (Location.Null).
  197. The tokenizer also tracks the column number for a token, but
  198. this is currently not being used or encoded. It could
  199. probably be encoded in the low 9 bits, allowing for columns
  200. from 1 to 512 to be encoded.
  201. * The Parser
  202. The parser is written using Jay, which is a port of Berkeley
  203. Yacc to Java, that I later ported to C#.
  204. Many people ask why the grammar of the parser does not match
  205. exactly the definition in the C# specification. The reason is
  206. simple: the grammar in the C# specification is designed to be
  207. consumed by humans, and not by a computer program. Before
  208. you can feed this grammar to a tool, it needs to be simplified
  209. to allow the tool to generate a correct parser for it.
  210. In the Mono C# compiler, we use a class for each of the
  211. statements and expressions in the C# language. For example,
  212. there is a `While' class for the the `while' statement, a
  213. `Cast' class to represent a cast expression and so on.
  214. There is a Statement class, and an Expression class which are
  215. the base classes for statements and expressions.
  216. ** Namespaces
  217. Using list.
  218. * Internal Representation
  219. ** Expressions
  220. Expressions in the Mono C# compiler are represented by the
  221. `Expression' class. This is an abstract class that particular
  222. kinds of expressions have to inherit from and override a few
  223. methods.
  224. The base Expression class contains two fields: `eclass' which
  225. represents the "expression classification" (from the C#
  226. specs) and the type of the expression.
  227. During parsing, the compiler will create the various trees of
  228. expressions. These expressions have to be resolved before they
  229. are can be used. The semantic analysis is implemented by
  230. resolving each of the expressions created during parsing and
  231. creating fully resolved expressions.
  232. A common pattern that you will notice in the compiler is this:
  233. Expression expr;
  234. ...
  235. expr = expr.Resolve (ec);
  236. if (expr == null)
  237. // There was an error, stop processing by returning
  238. The resolution process is implemented by overriding the
  239. `DoResolve' method. The DoResolve method has to set the `eclass'
  240. field and the `type', perform all error checking and computations
  241. that will be required for code generation at this stage.
  242. The return value from DoResolve is an expression. Most of the
  243. time an Expression derived class will return itself (return
  244. this) when it will handle the emission of the code itself, or
  245. it can return a new Expression.
  246. For example, the parser will create an "ElementAccess" class
  247. for:
  248. a [0] = 1;
  249. During the resolution process, the compiler will know whether
  250. this is an array access, or an indexer access. And will
  251. return either an ArrayAccess expression or an IndexerAccess
  252. expression from DoResolve.
  253. All errors must be reported during the resolution phase
  254. (DoResolve) and if an error is detected the DoResolve method
  255. will return null which is used to flag that an error condition
  256. has occurred, this will be used to stop compilation later on.
  257. This means that anyone that calls Expression.Resolve must
  258. check the return value for null which would indicate an error
  259. condition.
  260. The second stage that Expressions participate in is code
  261. generation, this is done by overwriting the "Emit" method of
  262. the Expression class. No error checking must be performed
  263. during this stage.
  264. We take advantage of the distinction between the expressions that
  265. are generated by the parser and the expressions that are the
  266. result of the semantic analysis phase for lambda expressions (more
  267. information in the "Lambda Expressions" section).
  268. But what is important is that expressions and statements that are
  269. generated by the parser should implement the cloning
  270. functionality. This is used lambda expressions require the
  271. compiler to attempt to resolve a given block of code with
  272. different possible types for parameters that have their types
  273. implicitly inferred.
  274. ** Simple Names, MemberAccess
  275. One of the most important classes in the compiler is
  276. "SimpleName" which represents a simple name (from the C#
  277. specification). The names during the resolution time are
  278. bound to field names, parameter names or local variable names.
  279. More complicated expressions like:
  280. Math.Sin
  281. Are composed using the MemberAccess class which contains a
  282. name (Math) and a SimpleName (Sin), this helps driving the
  283. resolution process.
  284. ** Types
  285. The parser creates expressions to represent types during
  286. compilation. For example:
  287. class Sample {
  288. Version vers;
  289. }
  290. That will produce a "SimpleName" expression for the "Version"
  291. word. And in this particular case, the parser will introduce
  292. "Version vers" as a field declaration.
  293. During the resolution process for the fields, the compiler
  294. will have to resolve the word "Version" to a type. This is
  295. done by using the "ResolveAsType" method in Expression instead
  296. of using "Resolve".
  297. ResolveAsType just turns on a different set of code paths for
  298. things like SimpleNames and does a different kind of error
  299. checking than the one used by regular expressions.
  300. ** Constants
  301. Constants in the Mono C# compiler are represented by the
  302. abstract class `Constant'. Constant is in turn derived from
  303. Expression. The base constructor for `Constant' just sets the
  304. expression class to be an `ExprClass.Value', Constants are
  305. born in a fully resolved state, so the `DoResolve' method
  306. only returns a reference to itself.
  307. Each Constant should implement the `GetValue' method which
  308. returns an object with the actual contents of this constant, a
  309. utility virtual method called `AsString' is used to render a
  310. diagnostic message. The output of AsString is shown to the
  311. developer when an error or a warning is triggered.
  312. Constant classes also participate in the constant folding
  313. process. Constant folding is invoked by those expressions
  314. that can be constant folded invoking the functionality
  315. provided by the ConstantFold class (cfold.cs).
  316. Each Constant has to implement a number of methods to convert
  317. itself into a Constant of a different type. These methods are
  318. called `ConvertToXXXX' and they are invoked by the wrapper
  319. functions `ToXXXX'. These methods only perform implicit
  320. numeric conversions. Explicit conversions are handled by the
  321. `Cast' expression class.
  322. The `ToXXXX' methods are the entry point, and provide error
  323. reporting in case a conversion can not be performed.
  324. ** Constant Folding
  325. The C# language requires constant folding to be implemented.
  326. Constant folding is hooked up in the Binary.Resolve method.
  327. If both sides of a binary expression are constants, then the
  328. ConstantFold.BinaryFold routine is invoked.
  329. This routine implements all the binary operator rules, it
  330. is a mirror of the code that generates code for binary
  331. operators, but that has to be evaluated at runtime.
  332. If the constants can be folded, then a new constant expression
  333. is returned, if not, then the null value is returned (for
  334. example, the concatenation of a string constant and a numeric
  335. constant is deferred to the runtime).
  336. ** Side effects
  337. a [i++]++
  338. a [i++] += 5;
  339. ** Statements
  340. *** Invariant meaning in a block
  341. The seemingly small section in the standard entitled
  342. "invariant meaning in a block" has several subtleties
  343. involved, especially when we try to implement the semantics
  344. efficiently.
  345. Most of the semantics are trivial, and basically prevent local
  346. variables from shadowing parameters and other local variables.
  347. However, this notion is not limited to that, but affects all
  348. simple name accesses within a block. And therein lies the rub
  349. -- instead of just worrying about the issue when we arrive at
  350. variable declarations, we need to verify this property at
  351. every use of a simple name within a block.
  352. The key notion that helps us is to note the bi-directional
  353. action of a variable declaration. The declaration together
  354. with anti-shadowing rules can maintain the IMiaB property for
  355. the block containing the declaration and all nested sub
  356. blocks. But, the IMiaB property also forces all surrounding
  357. blocks to avoid using the name. We thus need to maintain a
  358. blacklist of taboo names in all surrounding blocks -- and we
  359. take the expedient of doing so simply: actually maintaining a
  360. (superset of the) blacklist in each block data structure, which
  361. we call the 'known_variable' list.
  362. Because we create the 'known_variable' list during the parse
  363. process, by the time we do simple name resolution, all the
  364. blacklists are fully populated. So, we can just enforce the
  365. rest of the IMiaB property by looking up a couple of lists.
  366. This turns out to be quite efficient: when we used a block
  367. tree walk, a test case took 5-10mins, while with this simple
  368. mildly-redundant data structure, the time taken for the same
  369. test case came down to a couple of seconds.
  370. The IKnownVariable interface is a small wrinkle. Firstly, the
  371. IMiaB also applies to parameter names, especially those of
  372. anonymous methods. Secondly, we need more information than
  373. just the name in the blacklist -- we need the location of the
  374. name and where it's declared. We use the IKnownVariable
  375. interface to abstract out the parser information stored for
  376. local variables and parameters.
  377. * The semantic analysis
  378. Hence, the compiler driver has to parse all the input files.
  379. Once all the input files have been parsed, and an internal
  380. representation of the input program exists, the following
  381. steps are taken:
  382. * The interface hierarchy is resolved first.
  383. As the interface hierarchy is constructed,
  384. TypeBuilder objects are created for each one of
  385. them.
  386. * Classes and structure hierarchy is resolved next,
  387. TypeBuilder objects are created for them.
  388. * Constants and enumerations are resolved.
  389. * Method, indexer, properties, delegates and event
  390. definitions are now entered into the TypeBuilders.
  391. * Elements that contain code are now invoked to
  392. perform semantic analysis and code generation.
  393. * Output Generation
  394. ** Code Generation
  395. The EmitContext class is created any time that IL code is to
  396. be generated (methods, properties, indexers and attributes all
  397. create EmitContexts).
  398. The EmitContext keeps track of the current namespace and type
  399. container. This is used during name resolution.
  400. An EmitContext is used by the underlying code generation
  401. facilities to track the state of code generation:
  402. * The ILGenerator used to generate code for this
  403. method.
  404. * The TypeContainer where the code lives, this is used
  405. to access the TypeBuilder.
  406. * The DeclSpace, this is used to resolve names through
  407. RootContext.LookupType in the various statements and
  408. expressions.
  409. Code generation state is also tracked here:
  410. * CheckState:
  411. This variable tracks the `checked' state of the
  412. compilation, it controls whether we should generate
  413. code that does overflow checking, or if we generate
  414. code that ignores overflows.
  415. The default setting comes from the command line
  416. option to generate checked or unchecked code plus
  417. any source code changes using the checked/unchecked
  418. statements or expressions. Contrast this with the
  419. ConstantCheckState flag.
  420. * ConstantCheckState
  421. The constant check state is always set to `true' and
  422. cant be changed from the command line. The source
  423. code can change this setting with the `checked' and
  424. `unchecked' statements and expressions.
  425. * IsStatic
  426. Whether we are emitting code inside a static or
  427. instance method
  428. * ReturnType
  429. The value that is allowed to be returned or NULL if
  430. there is no return type.
  431. * ReturnLabel
  432. A `Label' used by the code if it must jump to it.
  433. This is used by a few routines that deals with exception
  434. handling.
  435. * HasReturnLabel
  436. Whether we have a return label defined by the toplevel
  437. driver.
  438. * ContainerType
  439. Points to the Type (extracted from the
  440. TypeContainer) that declares this body of code
  441. summary>
  442. * IsConstructor
  443. Whether this is generating code for a constructor
  444. * CurrentBlock
  445. Tracks the current block being generated.
  446. * ReturnLabel;
  447. The location where return has to jump to return the
  448. value
  449. A few variables are used to track the state for checking in
  450. for loops, or in try/catch statements:
  451. * InFinally
  452. Whether we are in a Finally block
  453. * InTry
  454. Whether we are in a Try block
  455. * InCatch
  456. Whether we are in a Catch block
  457. * InUnsafe
  458. Whether we are inside an unsafe block
  459. Methods exposed by the EmitContext:
  460. * EmitTopBlock()
  461. This emits a toplevel block.
  462. This routine is very simple, to allow the anonymous
  463. method support to roll its two-stage version of this
  464. routine on its own.
  465. * NeedReturnLabel ():
  466. This is used to flag during the resolution phase that
  467. the driver needs to initialize the `ReturnLabel'
  468. * Anonymous Methods
  469. The introduction of anonymous methods in the compiler changed
  470. various ways of doing things in the compiler. The most
  471. significant one is the hard split between the resolution phase
  472. and the emission phases of the compiler.
  473. For instance, routines that referenced local variables no
  474. longer can safely create temporary variables during the
  475. resolution phase: they must do so from the emission phase,
  476. since the variable might have been "captured", hence access to
  477. it can not be done with the local-variable operations from the
  478. runtime.
  479. The code emission is in:
  480. EmitTopBlock ()
  481. Which drives the process, it first resolves the topblock, then
  482. emits the required metadata (local variable definitions) and
  483. finally emits the code.
  484. A detailed description of anonymous methods and iterators is
  485. on the new-anonymous-design.txt file in this directory.
  486. * Lambda Expressions
  487. Lambda expressions can come in two forms: those that have implicit
  488. parameter types and those that have explicit parameter types, for
  489. example:
  490. Explicit:
  491. Foo ((int x) => x + 1);
  492. Implicit:
  493. Foo (x => x + 1)
  494. One of the problems that we faced with lambda expressions is
  495. that lambda expressions need to be "probed" with different
  496. types until a working combination is found.
  497. For example:
  498. x => x.i
  499. The above expression could mean vastly different things depending
  500. on the type of "x". The compiler determines the type of "x" (left
  501. hand side "x") at the moment the above expression is "bound",
  502. which means that during the compilation process it will try to
  503. match the above lambda with all the possible types available, for
  504. example:
  505. delegate int di (int x);
  506. delegate string ds (string s);
  507. ..
  508. Foo (di x) {}
  509. Foo (ds x) {}
  510. ...
  511. Foo (x => "string")
  512. In the above example, overload resolution will try "x" as an "int"
  513. and will try "x" as a string. And if one of them "compiles" thats
  514. the one it picks (and it also copes with ambiguities if there was
  515. more than one matching method).
  516. To compile this, we need to hook into the resolution process,
  517. but since the resolution process has side effects (calling
  518. Resolve can either return instances of the resolved expression
  519. type, or can alter field internals) it was necessary to
  520. incorporate a framework to "clone" expressions before we
  521. probe.
  522. The support for cloning was added into Statements and
  523. Expressions and is only necessary for objects of those types
  524. that are created during parsing. It is not necessary to
  525. support these in the classes that are the result of calling
  526. Resolve. This means that SimpleName needs support for
  527. Cloning, but FieldExpr does not need it (SimpleName is created
  528. by the parser, FieldExpr is created during semantic analysis
  529. resolution).
  530. The work happens through the public method called "Clone" that
  531. clones the given Statement or Expression. The base method in
  532. Statement and Expression merely does a MemberwiseCopy of the
  533. elements and then calls the virtual CloneTo method to complete
  534. the copy. By default this method throws an exception, this
  535. is useful to catch cases where we forgot to override CloneTo
  536. for a given Statement/Expression.
  537. With the cloning capability it became possible to call resolve
  538. multiple times (once for each Cloned copy) and based on this
  539. picking the one implementation that would compile and that
  540. would not be ambiguous.
  541. The cloning process is basically a deep copy that happens in the
  542. LambdaExpression class and it clones the top-level block for the
  543. lambda expression. The cloning has the side effect of cloning
  544. the entire containing block as well.
  545. This happens inside this method:
  546. public override bool ImplicitStandardConversionExists (Type delegate_type)
  547. This is used to determine if the current Lambda expression can be
  548. implicitly converted to the given delegate type.
  549. And also happens as a result of the generic method parameter
  550. type inferencing.
  551. ** Lambda Expressions and Cloning
  552. All statements that are created during the parsing method should
  553. implement the CloneTo method:
  554. protected virtual void CloneTo (CloneContext clonectx, Statement target)
  555. This method is called by the Statement.Clone method after it has
  556. done a shallow-copy of all the fields in the statement, and they
  557. should typically Clone any child statements.
  558. Expressions should implement the CloneTo method as well:
  559. protected virtual void CloneTo (CloneContext clonectx, Expression target)
  560. ** Lambda Expressions and Contextual Return
  561. When an expression is parsed as a lambda expression, the parser
  562. inserts a call to a special statement, the contextual return.
  563. The expression:
  564. a => a+1
  565. Is actually compiled as:
  566. a => contextual_return (a+1)
  567. The contextual_return statement will behave differently depending
  568. on the return type of the delegate that the expression will be
  569. converted to.
  570. If the delegate return type is void, the above will basically turn
  571. into an empty operation. Otherwise the above will become
  572. a return statement that can infer return types.
  573. * Miscellaneous
  574. ** Error Processing.
  575. Errors are reported during the various stages of the
  576. compilation process. The compiler stops its processing if
  577. there are errors between the various phases. This simplifies
  578. the code, because it is safe to assume always that the data
  579. structures that the compiler is operating on are always
  580. consistent.
  581. The error codes in the Mono C# compiler are the same as those
  582. found in the Microsoft C# compiler, with a few exceptions
  583. (where we report a few more errors, those are documented in
  584. mcs/errors/errors.txt). The goal is to reduce confusion to
  585. the users, and also to help us track the progress of the
  586. compiler in terms of the errors we report.
  587. The Report class provides error and warning display functions,
  588. and also keeps an error count which is used to stop the
  589. compiler between the phases.
  590. A couple of debugging tools are available here, and are useful
  591. when extending or fixing bugs in the compiler. If the
  592. `--fatal' flag is passed to the compiler, the Report.Error
  593. routine will throw an exception. This can be used to pinpoint
  594. the location of the bug and examine the variables around the
  595. error location.
  596. Warnings can be turned into errors by using the `--werror'
  597. flag to the compiler.
  598. The report class also ignores warnings that have been
  599. specified on the command line with the `--nowarn' flag.
  600. Finally, code in the compiler uses the global variable
  601. RootContext.WarningLevel in a few places to decide whether a
  602. warning is worth reporting to the user or not.
  603. * Debugging the compiler
  604. Sometimes it is convenient to find *how* a particular error
  605. message is being reported from, to do that, you might want to use
  606. the --fatal flag to mcs. The flag will instruct the compiler to
  607. abort with a stack trace execution when the error is reported.
  608. You can use this with -warnaserror to obtain the same effect
  609. with warnings.
  610. * Debugging the Parser.
  611. A useful trick while debugging the parser is to pass the -v
  612. command line option to the compiler.
  613. The -v command line option will dump the various Yacc states
  614. as well as the tokens that are being returned from the
  615. tokenizer to the compiler.
  616. This is useful when tracking down problems when the compiler
  617. is not able to parse an expression correctly.
  618. You can match the states reported with the contents of the
  619. y.output file, a file that contains the parsing tables and
  620. human-readable information about the generated parser.
  621. * Editing the compiler sources
  622. The compiler sources are intended to be edited with 134 columns of width
  623. * Quick Hacks
  624. Once you have a full build of mcs, you can improve your
  625. development time by just issuing make in the `mcs' directory or
  626. using `make qh' in the gmcs directory.