~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to contrib/ICSharpCode.NRefactory.CSharp/Parser/mcs/driver.cs

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