~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to src/addins/CSharpBinding/MonoDevelop.CSharp.Parser/mcs/driver.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// driver.cs: The compiler command line driver.
 
3
//
 
4
// Authors:
 
5
//   Miguel de Icaza (miguel@gnu.org)
 
6
//   Marek Safar (marek.safar@gmail.com)
 
7
//
 
8
// Dual licensed under the terms of the MIT X11 or GNU GPL
 
9
//
 
10
// Copyright 2001, 2002, 2003 Ximian, Inc (http://www.ximian.com)
 
11
// Copyright 2004, 2005, 2006, 2007, 2008 Novell, Inc
 
12
//
 
13
 
 
14
using System;
 
15
using System.Reflection;
 
16
using System.Reflection.Emit;
 
17
using System.Collections.Generic;
 
18
using System.IO;
 
19
using System.Text;
 
20
using System.Globalization;
 
21
using System.Diagnostics;
 
22
 
 
23
namespace Mono.CSharp
 
24
{
 
25
        /// <summary>
 
26
        ///    The compiler driver.
 
27
        /// </summary>
 
28
        class Driver
 
29
        {
 
30
                readonly CompilerContext ctx;
 
31
 
 
32
                public Driver (CompilerContext ctx)
 
33
                {
 
34
                        this.ctx = ctx;
 
35
                }
 
36
 
 
37
                Report Report {
 
38
                        get {
 
39
                                return ctx.Report;
 
40
                        }
 
41
                }
 
42
 
 
43
                void tokenize_file (CompilationSourceFile file)
 
44
                {
 
45
                        Stream input;
 
46
 
 
47
                        try {
 
48
                                input = File.OpenRead (file.Name);
 
49
                        } catch {
 
50
                                Report.Error (2001, "Source file `" + file.Name + "' could not be found");
 
51
                                return;
 
52
                        }
 
53
 
 
54
                        using (input){
 
55
                                SeekableStreamReader reader = new SeekableStreamReader (input, ctx.Settings.Encoding);
 
56
                                Tokenizer lexer = new Tokenizer (reader, file, ctx);
 
57
                                int token, tokens = 0, errors = 0;
 
58
 
 
59
                                while ((token = lexer.token ()) != Token.EOF){
 
60
                                        tokens++;
 
61
                                        if (token == Token.ERROR)
 
62
                                                errors++;
 
63
                                }
 
64
                                Console.WriteLine ("Tokenized: " + tokens + " found " + errors + " errors");
 
65
                        }
 
66
                        
 
67
                        return;
 
68
                }
 
69
 
 
70
                void Parse (ModuleContainer module)
 
71
                {
 
72
                        Location.Initialize (module.Compiler.SourceFiles);
 
73
 
 
74
                        bool tokenize_only = module.Compiler.Settings.TokenizeOnly;
 
75
                        var sources = module.Compiler.SourceFiles;
 
76
                        for (int i = 0; i < sources.Count; ++i) {
 
77
                                if (tokenize_only) {
 
78
                                        tokenize_file (sources[i]);
 
79
                                } else {
 
80
                                        Parse (sources[i], module);
 
81
                                }
 
82
                        }
 
83
                }
 
84
 
 
85
                public void Parse (CompilationSourceFile file, ModuleContainer module)
 
86
                {
 
87
                        Stream input;
 
88
 
 
89
                        try {
 
90
                                input = File.OpenRead (file.Name);
 
91
                        } catch {
 
92
                                Report.Error (2001, "Source file `{0}' could not be found", file.Name);
 
93
                                return;
 
94
                        }
 
95
 
 
96
                        // Check 'MZ' header
 
97
                        if (input.ReadByte () == 77 && input.ReadByte () == 90) {
 
98
                                Report.Error (2015, "Source file `{0}' is a binary file and not a text file", file.Name);
 
99
                                input.Close ();
 
100
                                return;
 
101
                        }
 
102
 
 
103
                        input.Position = 0;
 
104
                        SeekableStreamReader reader = new SeekableStreamReader (input, ctx.Settings.Encoding);
 
105
 
 
106
                        Parse (reader, file, module);
 
107
                        reader.Dispose ();
 
108
                        input.Close ();
 
109
                }       
 
110
                
 
111
                public void Parse (SeekableStreamReader reader, CompilationSourceFile file, ModuleContainer module)
 
112
                {
 
113
                        file.NamespaceContainer = new NamespaceContainer (null, module, null, file);
 
114
 
 
115
                        CSharpParser parser = new CSharpParser (reader, file);
 
116
                        parser.parse ();
 
117
                }
 
118
                
 
119
                public static int Main (string[] args)
 
120
                {
 
121
                        Location.InEmacs = Environment.GetEnvironmentVariable ("EMACS") == "t";
 
122
 
 
123
                        var r = new Report (new ConsoleReportPrinter ());
 
124
                        CommandLineParser cmd = new CommandLineParser (r);
 
125
                        var settings = cmd.ParseArguments (args);
 
126
                        if (settings == null || r.Errors > 0)
 
127
                                return 1;
 
128
 
 
129
                        if (cmd.HasBeenStopped)
 
130
                                return 0;
 
131
 
 
132
                        Driver d = new Driver (new CompilerContext (settings, r));
 
133
 
 
134
                        if (d.Compile () && d.Report.Errors == 0) {
 
135
                                if (d.Report.Warnings > 0) {
 
136
                                        Console.WriteLine ("Compilation succeeded - {0} warning(s)", d.Report.Warnings);
 
137
                                }
 
138
                                Environment.Exit (0);
 
139
                                return 0;
 
140
                        }
 
141
                        
 
142
                        
 
143
                        Console.WriteLine("Compilation failed: {0} error(s), {1} warnings",
 
144
                                d.Report.Errors, d.Report.Warnings);
 
145
                        Environment.Exit (1);
 
146
                        return 1;
 
147
                }
 
148
 
 
149
                public static string GetPackageFlags (string packages, Report report)
 
150
                {
 
151
                        ProcessStartInfo pi = new ProcessStartInfo ();
 
152
                        pi.FileName = "pkg-config";
 
153
                        pi.RedirectStandardOutput = true;
 
154
                        pi.UseShellExecute = false;
 
155
                        pi.Arguments = "--libs " + packages;
 
156
                        Process p = null;
 
157
                        try {
 
158
                                p = Process.Start (pi);
 
159
                        } catch (Exception e) {
 
160
                                if (report == null)
 
161
                                        throw;
 
162
 
 
163
                                report.Error (-27, "Couldn't run pkg-config: " + e.Message);
 
164
                                return null;
 
165
                        }
 
166
                        
 
167
                        if (p.StandardOutput == null) {
 
168
                                if (report == null)
 
169
                                        throw new ApplicationException ("Specified package did not return any information");
 
170
 
 
171
                                report.Warning (-27, 1, "Specified package did not return any information");
 
172
                                p.Close ();
 
173
                                return null;
 
174
                        }
 
175
 
 
176
                        string pkgout = p.StandardOutput.ReadToEnd ();
 
177
                        p.WaitForExit ();
 
178
                        if (p.ExitCode != 0) {
 
179
                                if (report == null)
 
180
                                        throw new ApplicationException (pkgout);
 
181
 
 
182
                                report.Error (-27, "Error running pkg-config. Check the above output.");
 
183
                                p.Close ();
 
184
                                return null;
 
185
                        }
 
186
 
 
187
                        p.Close ();
 
188
                        return pkgout;
 
189
                }
 
190
 
 
191
                //
 
192
                // Main compilation method
 
193
                //
 
194
                public bool Compile ()
 
195
                {
 
196
                        var settings = ctx.Settings;
 
197
 
 
198
                        //
 
199
                        // If we are an exe, require a source file for the entry point or
 
200
                        // if there is nothing to put in the assembly, and we are not a library
 
201
                        //
 
202
                        if (settings.FirstSourceFile == null &&
 
203
                                ((settings.Target == Target.Exe || settings.Target == Target.WinExe || settings.Target == Target.Module) ||
 
204
                                settings.Resources == null)) {
 
205
                                Report.Error (2008, "No files to compile were specified");
 
206
                                return false;
 
207
                        }
 
208
 
 
209
                        TimeReporter tr = new TimeReporter (settings.Timestamps);
 
210
                        ctx.TimeReporter = tr;
 
211
                        tr.StartTotal ();
 
212
 
 
213
                        var module = new ModuleContainer (ctx);
 
214
                        RootContext.ToplevelTypes = module;
 
215
 
 
216
                        tr.Start (TimeReporter.TimerType.ParseTotal);
 
217
                        Parse (module);
 
218
                        tr.Stop (TimeReporter.TimerType.ParseTotal);
 
219
 
 
220
                        if (Report.Errors > 0)
 
221
                                return false;
 
222
 
 
223
                        if (settings.TokenizeOnly || settings.ParseOnly) {
 
224
                                tr.StopTotal ();
 
225
                                tr.ShowStats ();
 
226
                                return true;
 
227
                        }
 
228
 
 
229
                        var output_file = settings.OutputFile;
 
230
                        string output_file_name;
 
231
                        if (output_file == null) {
 
232
                                var source_file = settings.FirstSourceFile;
 
233
 
 
234
                                if (source_file == null) {
 
235
                                        Report.Error (1562, "If no source files are specified you must specify the output file with -out:");
 
236
                                        return false;
 
237
                                }
 
238
 
 
239
                                output_file_name = source_file.Name;
 
240
                                int pos = output_file_name.LastIndexOf ('.');
 
241
 
 
242
                                if (pos > 0)
 
243
                                        output_file_name = output_file_name.Substring (0, pos);
 
244
                                
 
245
                                output_file_name += settings.TargetExt;
 
246
                                output_file = output_file_name;
 
247
                        } else {
 
248
                                output_file_name = Path.GetFileName (output_file);
 
249
                        }
 
250
 
 
251
#if STATIC
 
252
                        var importer = new StaticImporter (module);
 
253
                        var references_loader = new StaticLoader (importer, ctx);
 
254
 
 
255
                        tr.Start (TimeReporter.TimerType.AssemblyBuilderSetup);
 
256
                        var assembly = new AssemblyDefinitionStatic (module, references_loader, output_file_name, output_file);
 
257
                        assembly.Create (references_loader.Domain);
 
258
                        tr.Stop (TimeReporter.TimerType.AssemblyBuilderSetup);
 
259
 
 
260
                        // Create compiler types first even before any referenced
 
261
                        // assembly is loaded to allow forward referenced types from
 
262
                        // loaded assembly into compiled builder to be resolved
 
263
                        // correctly
 
264
                        tr.Start (TimeReporter.TimerType.CreateTypeTotal);
 
265
                        module.CreateType ();
 
266
                        importer.AddCompiledAssembly (assembly);
 
267
                        tr.Stop (TimeReporter.TimerType.CreateTypeTotal);
 
268
 
 
269
                        references_loader.LoadReferences (module);
 
270
 
 
271
                        tr.Start (TimeReporter.TimerType.PredefinedTypesInit);
 
272
                        if (!ctx.BuiltinTypes.CheckDefinitions (module))
 
273
                                return false;
 
274
 
 
275
                        tr.Stop (TimeReporter.TimerType.PredefinedTypesInit);
 
276
 
 
277
                        references_loader.LoadModules (assembly, module.GlobalRootNamespace);
 
278
#else
 
279
                        var assembly = new AssemblyDefinitionDynamic (module, output_file_name, output_file);
 
280
                        module.SetDeclaringAssembly (assembly);
 
281
 
 
282
                        var importer = new ReflectionImporter (module, ctx.BuiltinTypes);
 
283
                        assembly.Importer = importer;
 
284
 
 
285
                        var loader = new DynamicLoader (importer, ctx);
 
286
                        loader.LoadReferences (module);
 
287
 
 
288
                        if (!ctx.BuiltinTypes.CheckDefinitions (module))
 
289
                                return false;
 
290
 
 
291
                        if (!assembly.Create (AppDomain.CurrentDomain, AssemblyBuilderAccess.Save))
 
292
                                return false;
 
293
 
 
294
                        module.CreateType ();
 
295
 
 
296
                        loader.LoadModules (assembly, module.GlobalRootNamespace);
 
297
#endif
 
298
                        module.InitializePredefinedTypes ();
 
299
 
 
300
                        tr.Start (TimeReporter.TimerType.UsingResolve);
 
301
                        foreach (var source_file in ctx.SourceFiles) {
 
302
                                source_file.NamespaceContainer.Resolve ();
 
303
                        }
 
304
                        tr.Stop (TimeReporter.TimerType.UsingResolve);
 
305
 
 
306
                        tr.Start (TimeReporter.TimerType.ModuleDefinitionTotal);
 
307
                        module.Define ();
 
308
                        tr.Stop (TimeReporter.TimerType.ModuleDefinitionTotal);
 
309
 
 
310
                        if (Report.Errors > 0)
 
311
                                return false;
 
312
 
 
313
                        if (settings.DocumentationFile != null) {
 
314
                                var doc = new DocumentationBuilder (module);
 
315
                                doc.OutputDocComment (output_file, settings.DocumentationFile);
 
316
                        }
 
317
 
 
318
                        assembly.Resolve ();
 
319
                        
 
320
                        if (Report.Errors > 0)
 
321
                                return false;
 
322
 
 
323
 
 
324
                        tr.Start (TimeReporter.TimerType.EmitTotal);
 
325
                        assembly.Emit ();
 
326
                        tr.Stop (TimeReporter.TimerType.EmitTotal);
 
327
 
 
328
                        if (Report.Errors > 0){
 
329
                                return false;
 
330
                        }
 
331
 
 
332
                        tr.Start (TimeReporter.TimerType.CloseTypes);
 
333
                        module.CloseType ();
 
334
                        tr.Stop (TimeReporter.TimerType.CloseTypes);
 
335
 
 
336
                        tr.Start (TimeReporter.TimerType.Resouces);
 
337
                        assembly.EmbedResources ();
 
338
                        tr.Stop (TimeReporter.TimerType.Resouces);
 
339
 
 
340
                        if (Report.Errors > 0)
 
341
                                return false;
 
342
 
 
343
                        assembly.Save ();
 
344
 
 
345
#if STATIC
 
346
                        references_loader.Dispose ();
 
347
#endif
 
348
                        tr.StopTotal ();
 
349
                        tr.ShowStats ();
 
350
 
 
351
                        return Report.Errors == 0;
 
352
                }
 
353
        }
 
354
 
 
355
        public class CompilerCompilationUnit {
 
356
                public ModuleContainer ModuleCompiled { get; set; }
 
357
                public LocationsBag LocationsBag { get; set; }
 
358
                public UsingsBag UsingsBag { get; set; }
 
359
                public SpecialsBag SpecialsBag { get; set; }
 
360
        }
 
361
        
 
362
        //
 
363
        // This is the only public entry point
 
364
        //
 
365
        public class CompilerCallableEntryPoint : MarshalByRefObject {
 
366
                
 
367
                public static bool InvokeCompiler (string [] args, TextWriter error)
 
368
                {
 
369
                        try {
 
370
                                var r = new Report (new StreamReportPrinter (error));
 
371
                                CommandLineParser cmd = new CommandLineParser (r, error);
 
372
                                var setting = cmd.ParseArguments (args);
 
373
                                if (setting == null || r.Errors > 0)
 
374
                                        return false;
 
375
 
 
376
                                var d = new Driver (new CompilerContext (setting, r));
 
377
                                return d.Compile ();
 
378
                        } finally {
 
379
                                Reset ();
 
380
                        }
 
381
                }
 
382
 
 
383
                public static int[] AllWarningNumbers {
 
384
                        get {
 
385
                                return Report.AllWarnings;
 
386
                        }
 
387
                }
 
388
 
 
389
                public static void Reset ()
 
390
                {
 
391
                        Reset (true);
 
392
                }
 
393
 
 
394
                public static void PartialReset ()
 
395
                {
 
396
                        Reset (false);
 
397
                }
 
398
                
 
399
                public static void Reset (bool full_flag)
 
400
                {
 
401
                        CSharpParser.yacc_verbose_flag = 0;
 
402
                        Location.Reset ();
 
403
                        
 
404
                        if (!full_flag)
 
405
                                return;
 
406
 
 
407
                        AnonymousTypeClass.Reset ();
 
408
                        AnonymousMethodBody.Reset ();
 
409
                        AnonymousMethodStorey.Reset ();
 
410
                        SymbolWriter.Reset ();
 
411
                        Switch.Reset ();
 
412
                        Linq.QueryBlock.TransparentParameter.Reset ();
 
413
                        TypeInfo.Reset ();
 
414
                }
 
415
                
 
416
                public static CompilerCompilationUnit ParseFile (string[] args, Stream input, string inputFile, TextWriter reportStream)
 
417
                {
 
418
                        return ParseFile (args, input, inputFile, new StreamReportPrinter (reportStream));
 
419
                }
 
420
                
 
421
                internal static object parseLock = new object ();
 
422
 
 
423
                public static CompilerCompilationUnit ParseFile (string[] args, Stream input, string inputFile, ReportPrinter reportPrinter)
 
424
                {
 
425
                        lock (parseLock) {
 
426
                                try {
 
427
                                        //                                     Driver d = Driver.Create (args, false, null, reportPrinter);
 
428
                                        //                                     if (d == null)
 
429
                                        //                                             return null;
 
430
       
 
431
                                        var r = new Report (reportPrinter);
 
432
                                        CommandLineParser cmd = new CommandLineParser (r, Console.Out);
 
433
                                        var setting = cmd.ParseArguments (args);
 
434
                                        if (setting == null || r.Errors > 0)
 
435
                                                return null;
 
436
 
 
437
                                        CompilerContext ctx = new CompilerContext (setting, r);
 
438
                                        
 
439
                                        var files = new List<CompilationSourceFile> ();
 
440
                                        var unit = new CompilationSourceFile (inputFile, inputFile, 0);
 
441
                                        var module = new ModuleContainer (ctx);
 
442
                                        unit.NamespaceContainer = new NamespaceContainer (null, module, null, unit);
 
443
                                        files.Add (unit);
 
444
                                        Location.Initialize (files);
 
445
 
 
446
                                        // TODO: encoding from driver
 
447
                                        SeekableStreamReader reader = new SeekableStreamReader (input, Encoding.Default);
 
448
                                
 
449
                                        RootContext.ToplevelTypes = module;
 
450
                                        
 
451
                                        CSharpParser parser = new CSharpParser (reader, unit);
 
452
                                        parser.Lexer.TabSize = 1;
 
453
                                        parser.Lexer.sbag = new SpecialsBag ();
 
454
                                        parser.LocationsBag = new LocationsBag ();
 
455
                                        parser.UsingsBag = new UsingsBag ();
 
456
                                        parser.parse ();
 
457
                                
 
458
                                        return new CompilerCompilationUnit () { ModuleCompiled = RootContext.ToplevelTypes, LocationsBag = parser.LocationsBag, UsingsBag = parser.UsingsBag, SpecialsBag = parser.Lexer.sbag };
 
459
                                } finally {
 
460
                                        Reset ();
 
461
                                }
 
462
                        }
 
463
                }
 
464
        }
 
465
}