mcs/error recovery: handle errors caused by closing braces after a statement expressi...
[mono.git] / mcs / mcs / complete.cs
1 //
2 // complete.cs: Expression that are used for completion suggestions.
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //   Marek Safar (marek.safar@gmail.com)
7 //
8 // Copyright 2001, 2002, 2003 Ximian, Inc.
9 // Copyright 2003-2009 Novell, Inc.
10 // Copyright 2011 Xamarin Inc
11 //
12 // Completion* classes derive from ExpressionStatement as this allows
13 // them to pass through the parser in many conditions that require
14 // statements even when the expression is incomplete (for example
15 // completing inside a lambda
16 //
17 using System.Collections.Generic;
18 using System.Linq;
19
20 namespace Mono.CSharp {
21
22         //
23         // A common base class for Completing expressions, it
24         // is just a very simple ExpressionStatement
25         //
26         public abstract class CompletingExpression : ExpressionStatement
27         {
28                 public static void AppendResults (List<string> results, string prefix, IEnumerable<string> names)
29                 {
30                         foreach (string name in names) {
31                                 if (name == null)
32                                         continue;
33
34                                 if (prefix != null && !name.StartsWith (prefix))
35                                         continue;
36
37                                 if (results.Contains (name))
38                                         continue;
39
40                                 if (prefix != null)
41                                         results.Add (name.Substring (prefix.Length));
42                                 else
43                                         results.Add (name);
44                         }
45                 }
46
47                 public override bool ContainsEmitWithAwait ()
48                 {
49                         return false;
50                 }
51
52                 public override Expression CreateExpressionTree (ResolveContext ec)
53                 {
54                         return null;
55                 }
56
57                 public override void EmitStatement (EmitContext ec)
58                 {
59                         // Do nothing
60                 }
61
62                 public override void Emit (EmitContext ec)
63                 {
64                         // Do nothing
65                 }
66         }
67         
68         public class CompletionSimpleName : CompletingExpression {
69                 public string Prefix;
70                 
71                 public CompletionSimpleName (string prefix, Location l)
72                 {
73                         this.loc = l;
74                         this.Prefix = prefix;
75                 }
76                 
77                 protected override Expression DoResolve (ResolveContext ec)
78                 {
79                         var results = new List<string> ();
80
81                         ec.CurrentMemberDefinition.GetCompletionStartingWith (Prefix, results);
82
83                         throw new CompletionResult (Prefix, results.Distinct ().Select (l => l.Substring (Prefix.Length)).ToArray ());
84                 }
85
86                 protected override void CloneTo (CloneContext clonectx, Expression t)
87                 {
88                         // Nothing
89                 }
90         }
91         
92         public class CompletionMemberAccess : CompletingExpression {
93                 Expression expr;
94                 string partial_name;
95                 TypeArguments targs;
96                 
97                 public CompletionMemberAccess (Expression e, string partial_name, Location l)
98                 {
99                         this.expr = e;
100                         this.loc = l;
101                         this.partial_name = partial_name;
102                 }
103
104                 public CompletionMemberAccess (Expression e, string partial_name, TypeArguments targs, Location l)
105                 {
106                         this.expr = e;
107                         this.loc = l;
108                         this.partial_name = partial_name;
109                         this.targs = targs;
110                 }
111                 
112                 protected override Expression DoResolve (ResolveContext ec)
113                 {
114                         Expression expr_resolved = expr.Resolve (ec,
115                                 ResolveFlags.VariableOrValue | ResolveFlags.Type);
116
117                         if (expr_resolved == null)
118                                 return null;
119
120                         TypeSpec expr_type = expr_resolved.Type;
121                         if (expr_type.IsPointer || expr_type.Kind == MemberKind.Void || expr_type == InternalType.NullLiteral || expr_type == InternalType.AnonymousMethod) {
122                                 expr_resolved.Error_OperatorCannotBeApplied (ec, loc, ".", expr_type);
123                                 return null;
124                         }
125
126                         if (targs != null) {
127                                 if (!targs.Resolve (ec))
128                                         return null;
129                         }
130
131                         var results = new List<string> ();
132                         if (expr_resolved is Namespace){
133                                 Namespace nexpr = expr_resolved as Namespace;
134                                 string namespaced_partial;
135
136                                 if (partial_name == null)
137                                         namespaced_partial = nexpr.Name;
138                                 else
139                                         namespaced_partial = nexpr.Name + "." + partial_name;
140
141                                 ec.CurrentMemberDefinition.GetCompletionStartingWith (namespaced_partial, results);
142                                 if (partial_name != null)
143                                         results = results.Select (l => l.Substring (partial_name.Length)).ToList ();
144                         } else {
145                                 var r = MemberCache.GetCompletitionMembers (ec, expr_type, partial_name).Select (l => l.Name);
146                                 AppendResults (results, partial_name, r);
147                         }
148
149                         throw new CompletionResult (partial_name == null ? "" : partial_name, results.Distinct ().ToArray ());
150                 }
151
152                 protected override void CloneTo (CloneContext clonectx, Expression t)
153                 {
154                         CompletionMemberAccess target = (CompletionMemberAccess) t;
155
156                         if (targs != null)
157                                 target.targs = targs.Clone ();
158
159                         target.expr = expr.Clone (clonectx);
160                 }
161         }
162
163         public class CompletionElementInitializer : CompletingExpression {
164                 string partial_name;
165                 
166                 public CompletionElementInitializer (string partial_name, Location l)
167                 {
168                         this.partial_name = partial_name;
169                         this.loc = l;
170                 }
171                 
172                 protected override Expression DoResolve (ResolveContext ec)
173                 {
174                         var members = MemberCache.GetCompletitionMembers (ec, ec.CurrentInitializerVariable.Type, partial_name);
175
176 // TODO: Does this mean exact match only ?
177 //                      if (partial_name != null && results.Count > 0 && result [0] == "")
178 //                              throw new CompletionResult ("", new string [] { "=" });
179
180                         var results = members.Where (l => (l.Kind & (MemberKind.Field | MemberKind.Property)) != 0).Select (l => l.Name).ToList ();
181                         if (partial_name != null) {
182                                 var temp = new List<string> ();
183                                 AppendResults (temp, partial_name, results);
184                                 results = temp;
185                         }
186
187                         throw new CompletionResult (partial_name == null ? "" : partial_name, results.Distinct ().ToArray ());
188                 }
189
190                 protected override void CloneTo (CloneContext clonectx, Expression t)
191                 {
192                         // Nothing
193                 }
194         }
195         
196 }