2 // complete.cs: Expression that are used for completion suggestions.
5 // Miguel de Icaza (miguel@ximian.com)
6 // Marek Safar (marek.safar@gmail.com)
8 // Copyright 2001, 2002, 2003 Ximian, Inc.
9 // Copyright 2003-2009 Novell, Inc.
10 // Copyright 2011 Xamarin Inc
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
17 using System.Collections.Generic;
20 namespace Mono.CSharp {
23 // A common base class for Completing expressions, it
24 // is just a very simple ExpressionStatement
26 public abstract class CompletingExpression : ExpressionStatement
28 public static void AppendResults (List<string> results, string prefix, IEnumerable<string> names)
30 foreach (string name in names) {
34 if (prefix != null && !name.StartsWith (prefix))
37 if (results.Contains (name))
41 results.Add (name.Substring (prefix.Length));
47 public override bool ContainsEmitWithAwait ()
52 public override Expression CreateExpressionTree (ResolveContext ec)
57 public override void EmitStatement (EmitContext ec)
62 public override void Emit (EmitContext ec)
68 public class CompletionSimpleName : CompletingExpression {
71 public CompletionSimpleName (string prefix, Location l)
77 protected override Expression DoResolve (ResolveContext ec)
79 var results = new List<string> ();
81 ec.CurrentMemberDefinition.GetCompletionStartingWith (Prefix, results);
83 throw new CompletionResult (Prefix, results.Distinct ().Select (l => l.Substring (Prefix.Length)).ToArray ());
86 protected override void CloneTo (CloneContext clonectx, Expression t)
92 public class CompletionMemberAccess : CompletingExpression {
97 public CompletionMemberAccess (Expression e, string partial_name, Location l)
101 this.partial_name = partial_name;
104 public CompletionMemberAccess (Expression e, string partial_name, TypeArguments targs, Location l)
108 this.partial_name = partial_name;
112 protected override Expression DoResolve (ResolveContext rc)
114 var sn = expr as SimpleName;
115 const ResolveFlags flags = ResolveFlags.VariableOrValue | ResolveFlags.Type;
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.
123 using (rc.Set (ResolveContext.Options.OmitStructFlowAnalysis)) {
125 expr = sn.LookupNameExpression (rc, MemberLookupRestrictions.ReadAccess | MemberLookupRestrictions.ExactArity);
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
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);
136 } else if (expr is TypeParameterExpr) {
137 expr.Error_UnexpectedKind (rc, flags, sn.Location);
141 expr = expr.Resolve (rc, flags);
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);
155 if (!targs.Resolve (rc))
159 var results = new List<string> ();
160 if (expr is Namespace) {
161 Namespace nexpr = expr as Namespace;
162 string namespaced_partial;
164 if (partial_name == null)
165 namespaced_partial = nexpr.Name;
167 namespaced_partial = nexpr.Name + "." + partial_name;
169 rc.CurrentMemberDefinition.GetCompletionStartingWith (namespaced_partial, results);
170 if (partial_name != null)
171 results = results.Select (l => l.Substring (partial_name.Length)).ToList ();
173 var r = MemberCache.GetCompletitionMembers (rc, expr_type, partial_name).Select (l => l.Name);
174 AppendResults (results, partial_name, r);
177 throw new CompletionResult (partial_name == null ? "" : partial_name, results.Distinct ().ToArray ());
180 protected override void CloneTo (CloneContext clonectx, Expression t)
182 CompletionMemberAccess target = (CompletionMemberAccess) t;
185 target.targs = targs.Clone ();
187 target.expr = expr.Clone (clonectx);
191 public class CompletionElementInitializer : CompletingExpression {
194 public CompletionElementInitializer (string partial_name, Location l)
196 this.partial_name = partial_name;
200 protected override Expression DoResolve (ResolveContext ec)
202 var members = MemberCache.GetCompletitionMembers (ec, ec.CurrentInitializerVariable.Type, partial_name);
204 // TODO: Does this mean exact match only ?
205 // if (partial_name != null && results.Count > 0 && result [0] == "")
206 // throw new CompletionResult ("", new string [] { "=" });
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);
215 throw new CompletionResult (partial_name == null ? "" : partial_name, results.Distinct ().ToArray ());
218 protected override void CloneTo (CloneContext clonectx, Expression t)