Merge pull request #600 from tr8dr/master
[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 rc)
113                 {
114                         var sn = expr as SimpleName;
115                         const ResolveFlags flags = ResolveFlags.VariableOrValue | ResolveFlags.Type;
116
117                         //
118                         // Resolve the expression with flow analysis turned off, we'll do the definite
119                         // assignment checks later.  This is because we don't know yet what the expression
120                         // will resolve to - it may resolve to a FieldExpr and in this case we must do the
121                         // definite assignment check on the actual field and not on the whole struct.
122                         //
123                         using (rc.Set (ResolveContext.Options.OmitStructFlowAnalysis)) {
124                                 if (sn != null) {
125                                         expr = sn.LookupNameExpression (rc, MemberLookupRestrictions.ReadAccess | MemberLookupRestrictions.ExactArity);
126
127                                         //
128                                         // Resolve expression which does have type set as we need expression type
129                                         // with disable flow analysis as we don't know whether left side expression
130                                         // is used as variable or type
131                                         //
132                                         if (expr is VariableReference || expr is ConstantExpr || expr is Linq.TransparentMemberAccess) {
133                                                 using (rc.With (ResolveContext.Options.DoFlowAnalysis, false)) {
134                                                         expr = expr.Resolve (rc);
135                                                 }
136                                         } else if (expr is TypeParameterExpr) {
137                                                 expr.Error_UnexpectedKind (rc, flags, sn.Location);
138                                                 expr = null;
139                                         }
140                                 } else {
141                                         expr = expr.Resolve (rc, flags);
142                                 }
143                         }
144
145                         if (expr == null)
146                                 return null;
147
148                         TypeSpec expr_type = expr.Type;
149                         if (expr_type.IsPointer || expr_type.Kind == MemberKind.Void || expr_type == InternalType.NullLiteral || expr_type == InternalType.AnonymousMethod) {
150                                 expr.Error_OperatorCannotBeApplied (rc, loc, ".", expr_type);
151                                 return null;
152                         }
153
154                         if (targs != null) {
155                                 if (!targs.Resolve (rc))
156                                         return null;
157                         }
158
159                         var results = new List<string> ();
160                         if (expr is Namespace) {
161                                 Namespace nexpr = expr as Namespace;
162                                 string namespaced_partial;
163
164                                 if (partial_name == null)
165                                         namespaced_partial = nexpr.Name;
166                                 else
167                                         namespaced_partial = nexpr.Name + "." + partial_name;
168
169                                 rc.CurrentMemberDefinition.GetCompletionStartingWith (namespaced_partial, results);
170                                 if (partial_name != null)
171                                         results = results.Select (l => l.Substring (partial_name.Length)).ToList ();
172                         } else {
173                                 var r = MemberCache.GetCompletitionMembers (rc, expr_type, partial_name).Select (l => l.Name);
174                                 AppendResults (results, partial_name, r);
175                         }
176
177                         throw new CompletionResult (partial_name == null ? "" : partial_name, results.Distinct ().ToArray ());
178                 }
179
180                 protected override void CloneTo (CloneContext clonectx, Expression t)
181                 {
182                         CompletionMemberAccess target = (CompletionMemberAccess) t;
183
184                         if (targs != null)
185                                 target.targs = targs.Clone ();
186
187                         target.expr = expr.Clone (clonectx);
188                 }
189         }
190
191         public class CompletionElementInitializer : CompletingExpression {
192                 string partial_name;
193                 
194                 public CompletionElementInitializer (string partial_name, Location l)
195                 {
196                         this.partial_name = partial_name;
197                         this.loc = l;
198                 }
199                 
200                 protected override Expression DoResolve (ResolveContext ec)
201                 {
202                         var members = MemberCache.GetCompletitionMembers (ec, ec.CurrentInitializerVariable.Type, partial_name);
203
204 // TODO: Does this mean exact match only ?
205 //                      if (partial_name != null && results.Count > 0 && result [0] == "")
206 //                              throw new CompletionResult ("", new string [] { "=" });
207
208                         var results = members.Where (l => (l.Kind & (MemberKind.Field | MemberKind.Property)) != 0).Select (l => l.Name).ToList ();
209                         if (partial_name != null) {
210                                 var temp = new List<string> ();
211                                 AppendResults (temp, partial_name, results);
212                                 results = temp;
213                         }
214
215                         throw new CompletionResult (partial_name == null ? "" : partial_name, results.Distinct ().ToArray ());
216                 }
217
218                 protected override void CloneTo (CloneContext clonectx, Expression t)
219                 {
220                         // Nothing
221                 }
222         }
223         
224 }