62f3ebd7aac631ec5d6d4f6ca18650e2b3606b94
[mono.git] / doc / c-sharp
1 * MCS: The Ximian C# compiler
2
3         MCS began as an experiment to learn the features of C# by
4         writing a large C# program.  MCS is currently able to parse C#
5         programs and create an internal tree representation of the
6         program.  MCS can parse itself.  
7
8         Work is progressing quickly on various fronts in the C#
9         compiler.  Recently I started using the System.Reflection API
10         to load system type definitions and avoid self-population of
11         types in the compiler and dropped my internal Type
12         representation in favor of using the CLI's System.Type.  
13
14 ** Phases of the compiler
15
16         The compiler has a number of phases:
17
18         <ul>
19                 * Lexical analyzer: hand-coded lexical analyzer that
20                   provides tokens to the parser.
21
22                 * The Parser: the parser is implemented using Jay (A
23                   Berkeley Yacc port to Java, that I ported to C#).
24                   The parser does minimal work and syntax checking,
25                   and only constructs a parsed tree.
26
27                   Each language element gets its own class.  The code
28                   convention is to use an uppercase name for the
29                   language element.  So a C# class and its associated
30                   information is kept in a "Class" class, a "struct"
31                   in a "Struct" class and so on.  Statements derive
32                   from the "Statement" class, and Expressions from the
33                   Expr class.
34
35                 * Parent class resolution: before the actual code
36                   generation, we need to resolve the parents and
37                   interfaces for interface, classe and struct
38                   definitions.
39
40                 * Semantic analysis: since C# can not resolve in a
41                   top-down pass what identifiers actually mean, we
42                   have to postpone this decision until the above steps
43                   are finished.
44
45                 * Code generation: nothing done so far, but I do not
46                   expect this to be hard, as I will just use
47                   System.Reflection.Emit to generate the code. 
48         
49         </ul>
50
51 <a name="tasks">
52 ** Current pending tasks
53
54         Simple tasks:
55
56         <ul>
57                 * Array declarations are currently being ignored, 
58
59                 * PInvoke declarations are not supported.
60
61                 * Pre-processing is not supported.
62
63                 * Attribute declarations and passing currently ignored.
64
65                 * Compiler does not pass around line/col information from tokenizer for error reporting.
66
67                 * Jay does not work correctly with `error'
68                   productions, making parser errors hard to point.  It
69                   would be best to port the Bison-To-Java compiler to
70                   become Bison-to-C# compiler (bjepson@oreilly.com
71                   might have more information)
72         </ul>
73
74         Critical tasks:
75
76         <ul>
77                 * Resolve "base" classes and "base" interfaces for
78                   classes, structs and interfaces.
79
80                   Once this is done, we can actually do the semantic
81                   analysis, because otherwise we do not know who our
82                   parents are.
83         </ul>
84
85 ** Questions and Answers
86
87 Q: Why not write a C# front-end for GCC?
88
89 A: I wanted to learn about C#, and this was an exercise in this
90    task.  The resulting compiler is highly object-oriented, which has
91    lead to a very nice, easy to follow and simple implementation of
92    the compiler.
93
94    I found that the design of this compiler is very similar to
95    Guavac's implementation.
96
97    Targeting the CIL/MSIL byte codes would require to re-architecting
98    GCC, as GCC is mostly designed to be used for register machines.
99    
100    The GCC Java engine that generates Java byte codes cheats: it does
101    not use the GCC backend; it has a special backend just for Java, so
102    you can not really generate Java bytecodes from the other languages
103    supported by GCC. 
104
105 Q: If your C# compiler is written in C#, how do you plan on getting
106    this working on a non-Microsoft environment.
107
108    We will do this through an implementation of the CLI Virtual
109    Execution System for Unix (our JIT engine). 
110
111 Q: Do you use Bison?
112
113 A: No, currently I am using Jay which is a port of Berkeley Yacc to
114    Java that I later ported to C#.  This means that error recovery is
115    not as nice as I would like to, and for some reason error
116    productions are not being caught.  
117
118    In the future I want to port one of the Bison/Java ports to C# for
119    the parser.
120
121 Q: How do I compile it?
122
123 A: Compiling MCS currently requires you to run my port of <a
124    href="http://primates.ximian.com/~miguel/code/jay.cs.tar.gz">Jay to
125    C#</a> on a Unix system to generate the parser, and then you need
126    to use Microsoft's .NET csc.exe compiler to compile the compiler.
127
128    You only need to compile the compiler compiler (C code), the
129    samples are Java samples that I did not port, and you do not need
130    them.
131
132    It might be simple to port Jay.cs to Windows, but I have not tried
133    this. 
134
135 You might also want to look at the <a href="faq.html#gcc">GCC</a>
136 section on the main FAQ