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

« back to all changes in this revision

Viewing changes to src/addins/CSharpBinding/MonoDevelop.CSharp.Parser/TypeSystemProvider.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:
34
34
using Mono.CSharp;
35
35
using System.Linq;
36
36
using ICSharpCode.NRefactory;
 
37
using MonoDevelop.CSharp.Refactoring.CodeActions;
 
38
using MonoDevelop.Core;
 
39
using ICSharpCode.NRefactory.CSharp.Resolver;
37
40
 
38
41
namespace MonoDevelop.CSharp.Parser
39
42
{
40
 
        public class TypeSystemParser : ITypeSystemParser
 
43
        public class TypeSystemParser : MonoDevelop.Ide.TypeSystem.TypeSystemParser
41
44
        {
42
 
                public ParsedDocument Parse (bool storeAst, string fileName, System.IO.TextReader content, MonoDevelop.Projects.Project project = null)
 
45
                public override ParsedDocument Parse (bool storeAst, string fileName, System.IO.TextReader content, MonoDevelop.Projects.Project project = null)
43
46
                {
44
47
                        var parser = new ICSharpCode.NRefactory.CSharp.CSharpParser (GetCompilerArguments (project));
45
48
                        parser.GenerateTypeSystemMode = !storeAst;
59
62
                                        if (comment != null) {
60
63
                                                VisitComment (result, comment, tagComments);
61
64
                                        } else {
62
 
                                                if (storeAst)
63
 
                                                        VisitPreprocessorDirective (result, special as SpecialsBag.PreProcessorDirective);
 
65
                                                if (storeAst) {
 
66
                                                        var ppd = special as SpecialsBag.PreProcessorDirective;
 
67
                                                        if  (ppd != null)
 
68
                                                                VisitPreprocessorDirective (result, ppd);
 
69
                                                }
64
70
                                        }
65
71
                                }
66
72
                        };
76
82
 
77
83
                        result.LastWriteTimeUtc = pf.LastWriteTime.Value;
78
84
                        result.ParsedFile = pf;
79
 
                        result.Add (GenerateFoldings (unit, result));
 
85
                        result.Add (GetSemanticTags (unit));
 
86
 
 
87
                        result.CreateRefactoringContext = (doc, token) => new MDRefactoringContext (doc, doc.Editor.Caret.Location, token);
 
88
                        result.CreateRefactoringContextWithEditor = (data, resolver, token) => new MDRefactoringContext ((DotNetProject)project, data, result, (CSharpAstResolver)resolver, TextLocation.Empty, token);
 
89
 
80
90
                        if (storeAst) {
81
91
                                result.Ast = unit;
 
92
                                result.Add (GenerateFoldings (unit, result));
82
93
                        }
83
94
                        return result;
84
95
                }
85
96
                
86
 
                IEnumerable<FoldingRegion> GenerateFoldings (CompilationUnit unit, ParsedDocument doc)
 
97
                IEnumerable<FoldingRegion> GenerateFoldings (SyntaxTree unit, ParsedDocument doc)
87
98
                {
88
99
                        foreach (var fold in doc.ConditionalRegions.ToFolds ())
89
100
                                yield return fold;
96
107
                        foreach (var fold in visitor.Foldings)
97
108
                                yield return fold;
98
109
                }
99
 
                
 
110
 
100
111
                class FoldingVisitor : DepthFirstAstVisitor<object, object>
101
112
                {
102
113
                        public readonly List<FoldingRegion> Foldings = new List<FoldingRegion> ();
117
128
                                        Foldings.Add (new FoldingRegion (new DomRegion (firstChild.StartLocation, node.EndLocation), FoldType.Undefined));
118
129
                                }
119
130
                        }
120
 
                        public override object VisitCompilationUnit (CompilationUnit unit, object data)
 
131
                        public override object VisitSyntaxTree (SyntaxTree unit, object data)
121
132
                        {
122
133
                                AddUsings (unit);
123
 
                                return base.VisitCompilationUnit (unit, data);
124
 
                        }
125
 
 
 
134
                                return base.VisitSyntaxTree (unit, data);
 
135
                        }
 
136
 
 
137
                        static TextLocation CorrectEnd (AstNode token)
 
138
                        {
 
139
                                return new TextLocation (token.EndLocation.Line, token.EndLocation.Column + 1);
 
140
                        }
 
141
 
 
142
                        static bool LastToken(AstNode arg)
 
143
                        {
 
144
                                return !(arg is NewLineNode || arg is WhitespaceNode || arg is ICSharpCode.NRefactory.CSharp.Comment);
 
145
                        }
 
146
                
126
147
                        public override object VisitNamespaceDeclaration (NamespaceDeclaration namespaceDeclaration, object data)
127
148
                        {
128
149
                                AddUsings (namespaceDeclaration);
129
 
                                if (!namespaceDeclaration.RBraceToken.IsNull)
130
 
                                        Foldings.Add (new FoldingRegion (new DomRegion (namespaceDeclaration.LBraceToken.GetPrevNode ().EndLocation, namespaceDeclaration.RBraceToken.EndLocation), FoldType.Undefined));
 
150
                                if (!namespaceDeclaration.RBraceToken.IsNull && namespaceDeclaration.LBraceToken.StartLocation.Line != namespaceDeclaration.RBraceToken.StartLocation.Line)
 
151
                                        Foldings.Add (new FoldingRegion (new DomRegion (namespaceDeclaration.LBraceToken.GetPrevNode (LastToken).EndLocation, CorrectEnd (namespaceDeclaration.RBraceToken)), FoldType.Undefined));
131
152
                                return base.VisitNamespaceDeclaration (namespaceDeclaration, data);
132
153
                        }
133
154
                        
134
155
                        public override object VisitTypeDeclaration (TypeDeclaration typeDeclaration, object data)
135
156
                        {
136
 
                                if (!typeDeclaration.RBraceToken.IsNull)
137
 
                                        Foldings.Add (new FoldingRegion (new DomRegion (typeDeclaration.LBraceToken.GetPrevNode ().EndLocation, typeDeclaration.RBraceToken.StartLocation), FoldType.Type));
 
157
                                if (!typeDeclaration.RBraceToken.IsNull && typeDeclaration.LBraceToken.StartLocation.Line != typeDeclaration.RBraceToken.StartLocation.Line)
 
158
                                        Foldings.Add (new FoldingRegion (new DomRegion (typeDeclaration.LBraceToken.GetPrevNode (LastToken).EndLocation, CorrectEnd (typeDeclaration.RBraceToken)), FoldType.Type));
138
159
                                return base.VisitTypeDeclaration (typeDeclaration, data);
139
160
                        }
140
161
                        
141
162
                        public override object VisitMethodDeclaration (MethodDeclaration methodDeclaration, object data)
142
163
                        {
143
 
                                if (!methodDeclaration.Body.IsNull)
144
 
                                        Foldings.Add (new FoldingRegion (new DomRegion (methodDeclaration.Body.LBraceToken.GetPrevNode ().EndLocation, methodDeclaration.Body.RBraceToken.StartLocation), FoldType.Member));
 
164
                                if (!methodDeclaration.Body.IsNull && methodDeclaration.Body.LBraceToken.StartLocation.Line != methodDeclaration.Body.RBraceToken.StartLocation.Line)
 
165
                                        Foldings.Add (new FoldingRegion (new DomRegion (methodDeclaration.Body.LBraceToken.GetPrevNode (LastToken).EndLocation, CorrectEnd (methodDeclaration.Body.RBraceToken)), FoldType.Member));
145
166
                                return base.VisitMethodDeclaration (methodDeclaration, data);
146
167
                        }
147
168
                        
148
169
                        public override object VisitConstructorDeclaration (ConstructorDeclaration constructorDeclaration, object data)
149
170
                        {
150
 
                                if (!constructorDeclaration.Body.IsNull)
151
 
                                        Foldings.Add (new FoldingRegion (new DomRegion (constructorDeclaration.Body.LBraceToken.GetPrevNode ().EndLocation, constructorDeclaration.Body.RBraceToken.StartLocation), FoldType.Member));
 
171
                                if (!constructorDeclaration.Body.IsNull && constructorDeclaration.Body.LBraceToken.StartLocation.Line != constructorDeclaration.Body.RBraceToken.StartLocation.Line)
 
172
                                        Foldings.Add (new FoldingRegion (new DomRegion (constructorDeclaration.Body.LBraceToken.GetPrevNode (LastToken).EndLocation, CorrectEnd (constructorDeclaration.Body.RBraceToken)), FoldType.Member));
152
173
                                return base.VisitConstructorDeclaration (constructorDeclaration, data);
153
174
                        }
154
175
                        
155
176
                        public override object VisitDestructorDeclaration (DestructorDeclaration destructorDeclaration, object data)
156
177
                        {
157
 
                                if (!destructorDeclaration.Body.IsNull)
158
 
                                        Foldings.Add (new FoldingRegion (new DomRegion (destructorDeclaration.Body.LBraceToken.GetPrevNode ().EndLocation, destructorDeclaration.Body.RBraceToken.StartLocation), FoldType.Member));
 
178
                                if (!destructorDeclaration.Body.IsNull && destructorDeclaration.Body.LBraceToken.StartLocation.Line != destructorDeclaration.Body.RBraceToken.StartLocation.Line)
 
179
                                        Foldings.Add (new FoldingRegion (new DomRegion (destructorDeclaration.Body.LBraceToken.GetPrevNode (LastToken).EndLocation, CorrectEnd (destructorDeclaration.Body.RBraceToken)), FoldType.Member));
159
180
                                return base.VisitDestructorDeclaration (destructorDeclaration, data);
160
181
                        }
161
182
                        
162
183
                        public override object VisitOperatorDeclaration (OperatorDeclaration operatorDeclaration, object data)
163
184
                        {
164
 
                                if (!operatorDeclaration.Body.IsNull)
165
 
                                        Foldings.Add (new FoldingRegion (new DomRegion (operatorDeclaration.Body.LBraceToken.GetPrevNode ().EndLocation, operatorDeclaration.Body.RBraceToken.StartLocation), FoldType.Member));
 
185
                                if (!operatorDeclaration.Body.IsNull && operatorDeclaration.Body.LBraceToken.StartLocation.Line != operatorDeclaration.Body.RBraceToken.StartLocation.Line)
 
186
                                        Foldings.Add (new FoldingRegion (new DomRegion (operatorDeclaration.Body.LBraceToken.GetPrevNode (LastToken).EndLocation, CorrectEnd (operatorDeclaration.Body.RBraceToken)), FoldType.Member));
166
187
                                return base.VisitOperatorDeclaration (operatorDeclaration, data);
167
188
                        }
168
189
                        
169
190
                        public override object VisitPropertyDeclaration (PropertyDeclaration propertyDeclaration, object data)
170
191
                        {
171
 
                                if (!propertyDeclaration.LBraceToken.IsNull)
172
 
                                        Foldings.Add (new FoldingRegion (new DomRegion (propertyDeclaration.LBraceToken.GetPrevNode ().EndLocation, propertyDeclaration.RBraceToken.StartLocation), FoldType.Member));
 
192
                                if (!propertyDeclaration.LBraceToken.IsNull && propertyDeclaration.LBraceToken.StartLocation.Line != propertyDeclaration.RBraceToken.StartLocation.Line)
 
193
                                        Foldings.Add (new FoldingRegion (new DomRegion (propertyDeclaration.LBraceToken.GetPrevNode (LastToken).EndLocation, CorrectEnd (propertyDeclaration.RBraceToken)), FoldType.Member));
173
194
                                return base.VisitPropertyDeclaration (propertyDeclaration, data);
174
195
                        }
175
196
                        
176
197
                        public override object VisitIndexerDeclaration (IndexerDeclaration indexerDeclaration, object data)
177
198
                        {
178
 
                                if (!indexerDeclaration.LBraceToken.IsNull)
179
 
                                        Foldings.Add (new FoldingRegion (new DomRegion (indexerDeclaration.LBraceToken.GetPrevNode ().EndLocation, indexerDeclaration.RBraceToken.StartLocation), FoldType.Member));
 
199
                                if (!indexerDeclaration.LBraceToken.IsNull && indexerDeclaration.LBraceToken.StartLocation.Line != indexerDeclaration.RBraceToken.StartLocation.Line)
 
200
                                        Foldings.Add (new FoldingRegion (new DomRegion (indexerDeclaration.LBraceToken.GetPrevNode (LastToken).EndLocation, CorrectEnd (indexerDeclaration.RBraceToken)), FoldType.Member));
180
201
                                return base.VisitIndexerDeclaration (indexerDeclaration, data);
181
202
                        }
182
203
                        
183
204
                        public override object VisitCustomEventDeclaration (CustomEventDeclaration eventDeclaration, object data)
184
205
                        {
185
 
                                if (!eventDeclaration.LBraceToken.IsNull)
186
 
                                        Foldings.Add (new FoldingRegion (new DomRegion (eventDeclaration.LBraceToken.GetPrevNode ().EndLocation, eventDeclaration.RBraceToken.StartLocation), FoldType.Member));
 
206
                                if (!eventDeclaration.LBraceToken.IsNull && eventDeclaration.LBraceToken.StartLocation.Line != eventDeclaration.RBraceToken.StartLocation.Line)
 
207
                                        Foldings.Add (new FoldingRegion (new DomRegion (eventDeclaration.LBraceToken.GetPrevNode (LastToken).EndLocation, CorrectEnd (eventDeclaration.RBraceToken)), FoldType.Member));
187
208
                                return base.VisitCustomEventDeclaration (eventDeclaration, data);
188
209
                        }
189
210
                        
190
211
                        public override object VisitSwitchStatement (SwitchStatement switchStatement, object data)
191
212
                        {
192
 
                                if (!switchStatement.RBraceToken.IsNull)
193
 
                                        Foldings.Add (new FoldingRegion (new DomRegion (switchStatement.LBraceToken.GetPrevNode ().EndLocation, switchStatement.RBraceToken.StartLocation), FoldType.Member));
 
213
                                if (!switchStatement.RBraceToken.IsNull && switchStatement.LBraceToken.StartLocation.Line != switchStatement.RBraceToken.StartLocation.Line)
 
214
                                        Foldings.Add (new FoldingRegion (new DomRegion (switchStatement.LBraceToken.GetPrevNode (LastToken).EndLocation, CorrectEnd (switchStatement.RBraceToken)), FoldType.Member));
194
215
                                return base.VisitSwitchStatement (switchStatement, data);
195
216
                        }
196
217
                        
197
218
                        public override object VisitBlockStatement (BlockStatement blockStatement, object data)
198
219
                        {
199
220
                                if (!(blockStatement.Parent is EntityDeclaration) && blockStatement.EndLocation.Line - blockStatement.StartLocation.Line > 2) {
200
 
                                        Foldings.Add (new FoldingRegion (new DomRegion (blockStatement.GetPrevNode ().EndLocation, blockStatement.RBraceToken.StartLocation), FoldType.Undefined));
 
221
                                        Foldings.Add (new FoldingRegion (new DomRegion (blockStatement.GetPrevNode (LastToken).EndLocation, CorrectEnd (blockStatement.RBraceToken)), FoldType.Undefined));
201
222
                                }
202
223
                                
203
224
                                return base.VisitBlockStatement (blockStatement, data);
204
225
                        }
205
226
                }
206
 
                
 
227
 
 
228
                static IEnumerable<Tag> GetSemanticTags (SyntaxTree unit)
 
229
                {
 
230
                        var visitor = new SemanticTagVisitor ();
 
231
                        unit.AcceptVisitor (visitor);
 
232
                        foreach (var fold in visitor.Tags)
 
233
                                yield return fold;
 
234
                }
 
235
 
 
236
                public class SemanticTagVisitor : DepthFirstAstVisitor
 
237
                {
 
238
                        public List<Tag> Tags =  new List<Tag> ();
 
239
 
 
240
                        public override void VisitThrowStatement (ThrowStatement throwStatement)
 
241
                        {
 
242
                                var createExpression = throwStatement.Expression as ObjectCreateExpression;
 
243
                                if (createExpression == null)
 
244
                                        return;
 
245
                                var st = createExpression.Type as SimpleType;
 
246
                                var mt = createExpression.Type as MemberType;
 
247
                                if (st != null && st.Identifier == "NotImplementedException" ||
 
248
                                    mt != null && mt.MemberName == "NotImplementedException" && mt.Target.ToString () == "System") {
 
249
 
 
250
                                        if (createExpression.Arguments.Any ()) {
 
251
                                                Tags.Add (new Tag ("High", GettextCatalog.GetString ("NotImplementedException({0}) thrown.", createExpression.Arguments.First ().ToString ()), new DomRegion (throwStatement.StartLocation, throwStatement.EndLocation)));
 
252
                                        } else {
 
253
                                                Tags.Add (new Tag ("High", GettextCatalog.GetString ("NotImplementedException thrown."), new DomRegion (throwStatement.StartLocation, throwStatement.EndLocation)));
 
254
                                        }
 
255
                                }
 
256
                        }
 
257
                }
 
258
 
207
259
                void VisitMcsUnit ()
208
260
                {
209
261
                }
234
286
                        foreach (string tag in tagComments) {
235
287
                                if (!trimmedContent.StartsWith (tag))
236
288
                                        continue;
237
 
                                result.Add (new MonoDevelop.Ide.TypeSystem.Tag (tag, comment.Content, cmt.Region));
 
289
                                result.Add (new Tag (tag, comment.Content, cmt.Region));
238
290
                        }
239
291
                }
240
292
                
321
373
                                break;
322
374
                        }
323
375
                }
324
 
                
325
 
                public static CompilerSettings GetCompilerArguments (MonoDevelop.Projects.Project project)
 
376
 
 
377
                public static ICSharpCode.NRefactory.CSharp.CompilerSettings GetCompilerArguments (MonoDevelop.Projects.Project project)
326
378
                {
327
 
                        var compilerArguments = new CompilerSettings ();
328
 
                        compilerArguments.TabSize = 1;
 
379
                        var compilerArguments = new ICSharpCode.NRefactory.CSharp.CompilerSettings ();
 
380
        ///             compilerArguments.TabSize = 1;
329
381
 
330
382
                        if (project == null || MonoDevelop.Ide.IdeApp.Workspace == null) {
331
 
                                compilerArguments.Unsafe = true;
 
383
                                compilerArguments.AllowUnsafeBlocks = true;
332
384
                                return compilerArguments;
333
385
                        }
334
386
 
340
392
                                
341
393
                        if (!string.IsNullOrEmpty (par.DefineSymbols)) {
342
394
                                foreach (var sym in par.DefineSymbols.Split (';', ',', ' ', '\t').Where (s => !string.IsNullOrWhiteSpace (s)))
343
 
                                        compilerArguments.AddConditionalSymbol (sym);
 
395
                                        compilerArguments.ConditionalSymbols.Add (sym);
344
396
                        }
345
397
                        
346
 
                        compilerArguments.Unsafe = par.UnsafeCode;
347
 
                        compilerArguments.Version = ConvertLanguageVersion (par.LangVersion);
348
 
                        compilerArguments.Checked = par.GenerateOverflowChecks;
 
398
                        compilerArguments.AllowUnsafeBlocks = par.UnsafeCode;
 
399
                        compilerArguments.LanguageVersion = ConvertLanguageVersion (par.LangVersion);
 
400
                        compilerArguments.CheckForOverflow = par.GenerateOverflowChecks;
349
401
                        compilerArguments.WarningLevel = par.WarningLevel;
350
 
                        compilerArguments.EnhancedWarnings = par.TreatWarningsAsErrors;
 
402
                        compilerArguments.TreatWarningsAsErrors = par.TreatWarningsAsErrors;
351
403
                        if (!string.IsNullOrEmpty (par.NoWarnings)) {
352
404
                                foreach (var warning in par.NoWarnings.Split (';', ',', ' ', '\t')) {
353
405
                                        int w;
356
408
                                        } catch (Exception) {
357
409
                                                continue;
358
410
                                        }
359
 
                                        compilerArguments.SetIgnoreWarning (w);
 
411
                                        compilerArguments.DisabledWarnings.Add (w);
360
412
                                }
361
413
                        }
362
414
                        
363
415
                        return compilerArguments;
364
416
                }
365
417
                
366
 
                static Mono.CSharp.LanguageVersion ConvertLanguageVersion (LangVersion ver)
 
418
                internal static Version ConvertLanguageVersion (LangVersion ver)
367
419
                {
368
420
                        switch (ver) {
369
421
                        case LangVersion.Default:
370
 
                                return Mono.CSharp.LanguageVersion.Default;
 
422
                                return new Version (5, 0, 0, 0);
371
423
                        case LangVersion.ISO_1:
372
 
                                return Mono.CSharp.LanguageVersion.ISO_1;
 
424
                                return new Version (1, 0, 0, 0);
373
425
                        case LangVersion.ISO_2:
374
 
                                return Mono.CSharp.LanguageVersion.ISO_2;
 
426
                                return new Version (2, 0, 0, 0);
 
427
                        case LangVersion.Version3:
 
428
                                return new Version (3, 0, 0, 0);
 
429
                        case LangVersion.Version4:
 
430
                                return new Version (4, 0, 0, 0);
 
431
                        case LangVersion.Version5:
 
432
                                return new Version (5, 0, 0, 0);
375
433
                        }
376
 
                        return Mono.CSharp.LanguageVersion.Default;
 
434
                        return new Version (5, 0, 0, 0);;
377
435
                }
378
436
        }
379
437