mcs/error recovery: handle errors caused by closing braces after a statement expressi...
[mono.git] / mcs / mcs / complete.cs
index 9ee16f9fd0da1afeb71f900d36594fd0f1e3c271..a1bb8ec00f48c249eebd4ec1928ab0110c9dce1e 100644 (file)
@@ -7,75 +7,80 @@
 //
 // Copyright 2001, 2002, 2003 Ximian, Inc.
 // Copyright 2003-2009 Novell, Inc.
+// Copyright 2011 Xamarin Inc
 //
 // Completion* classes derive from ExpressionStatement as this allows
 // them to pass through the parser in many conditions that require
 // statements even when the expression is incomplete (for example
 // completing inside a lambda
 //
+using System.Collections.Generic;
+using System.Linq;
+
 namespace Mono.CSharp {
-       using System;
-       using System.Collections;
-       using System.Reflection;
-       using System.Reflection.Emit;
-       using System.Text;
 
        //
        // A common base class for Completing expressions, it
        // is just a very simple ExpressionStatement
        //
-       public abstract class CompletingExpression : ExpressionStatement {
-               public override void EmitStatement (EmitContext ec)
+       public abstract class CompletingExpression : ExpressionStatement
+       {
+               public static void AppendResults (List<string> results, string prefix, IEnumerable<string> names)
                {
-                       // Do nothing
+                       foreach (string name in names) {
+                               if (name == null)
+                                       continue;
+
+                               if (prefix != null && !name.StartsWith (prefix))
+                                       continue;
+
+                               if (results.Contains (name))
+                                       continue;
+
+                               if (prefix != null)
+                                       results.Add (name.Substring (prefix.Length));
+                               else
+                                       results.Add (name);
+                       }
                }
 
-               public override void Emit (EmitContext ec)
+               public override bool ContainsEmitWithAwait ()
                {
-                       // Do nothing
+                       return false;
                }
 
-               public override Expression CreateExpressionTree (EmitContext ec)
+               public override Expression CreateExpressionTree (ResolveContext ec)
                {
                        return null;
                }
+
+               public override void EmitStatement (EmitContext ec)
+               {
+                       // Do nothing
+               }
+
+               public override void Emit (EmitContext ec)
+               {
+                       // Do nothing
+               }
        }
        
        public class CompletionSimpleName : CompletingExpression {
-               string prefix;
+               public string Prefix;
                
                public CompletionSimpleName (string prefix, Location l)
                {
                        this.loc = l;
-                       this.prefix = prefix;
-               }
-
-               void AppendResults (ArrayList results, string prefix, IEnumerable names)
-               {
-                       foreach (string name in names){
-                               if (!name.StartsWith (prefix))
-                                       continue;
-
-                               if (results.Contains (name))
-                                       continue;
-
-                               if (prefix != null)
-                                       results.Add (name.Substring (prefix.Length));
-                               else
-                                       results.Add (name);
-                       }
-
+                       this.Prefix = prefix;
                }
                
-               public override Expression DoResolve (EmitContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
-                       ArrayList results = new ArrayList ();
+                       var results = new List<string> ();
+
+                       ec.CurrentMemberDefinition.GetCompletionStartingWith (Prefix, results);
 
-                       AppendResults (results, prefix, Evaluator.GetVarNames ());
-                       AppendResults (results, prefix, ec.TypeContainer.NamespaceEntry.CompletionGetTypesStartingWith (ec.TypeContainer, prefix));
-                       AppendResults (results, prefix, Evaluator.GetUsingList ());
-                       
-                       throw new CompletionResult (prefix, (string []) results.ToArray (typeof (string)));
+                       throw new CompletionResult (Prefix, results.Distinct ().Select (l => l.Substring (Prefix.Length)).ToArray ());
                }
 
                protected override void CloneTo (CloneContext clonectx, Expression t)
@@ -88,30 +93,6 @@ namespace Mono.CSharp {
                Expression expr;
                string partial_name;
                TypeArguments targs;
-
-               static MemberFilter CollectingFilter = new MemberFilter (Match);
-
-               static bool Match (MemberInfo m, object filter_criteria)
-               {
-                       if (m is FieldInfo){
-                               if (((FieldInfo) m).IsSpecialName)
-                                       return false;
-                               
-                       }
-                       if (m is MethodInfo){
-                               if (((MethodInfo) m).IsSpecialName)
-                                       return false;
-                       }
-
-                       if (filter_criteria == null)
-                               return true;
-                       
-                       string n = (string) filter_criteria;
-                       if (m.Name.StartsWith (n))
-                               return true;
-                       
-                       return false;
-               }
                
                public CompletionMemberAccess (Expression e, string partial_name, Location l)
                {
@@ -128,19 +109,17 @@ namespace Mono.CSharp {
                        this.targs = targs;
                }
                
-               public override Expression DoResolve (EmitContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
-                       SimpleName original = expr as SimpleName;
                        Expression expr_resolved = expr.Resolve (ec,
-                               ResolveFlags.VariableOrValue | ResolveFlags.Type |
-                               ResolveFlags.Intermediate | ResolveFlags.DisableStructFlowAnalysis);
+                               ResolveFlags.VariableOrValue | ResolveFlags.Type);
 
                        if (expr_resolved == null)
                                return null;
 
-                       Type expr_type = expr_resolved.Type;
-                       if (expr_type.IsPointer || expr_type == TypeManager.void_type || expr_type == TypeManager.null_type || expr_type == TypeManager.anonymous_method_type) {
-                               Unary.Error_OperatorCannotBeApplied (loc, ".", expr_type);
+                       TypeSpec expr_type = expr_resolved.Type;
+                       if (expr_type.IsPointer || expr_type.Kind == MemberKind.Void || expr_type == InternalType.NullLiteral || expr_type == InternalType.AnonymousMethod) {
+                               expr_resolved.Error_OperatorCannotBeApplied (ec, loc, ".", expr_type);
                                return null;
                        }
 
@@ -149,27 +128,25 @@ namespace Mono.CSharp {
                                        return null;
                        }
 
-                       ArrayList results = new ArrayList ();
-                       MemberInfo [] result = expr_type.FindMembers (MemberTypes.All, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public, CollectingFilter, partial_name);
-                       foreach (MemberInfo r in result){
-                               string name;
+                       var results = new List<string> ();
+                       if (expr_resolved is Namespace){
+                               Namespace nexpr = expr_resolved as Namespace;
+                               string namespaced_partial;
 
-                               MethodBase rasb = r as MethodBase;
-                               if (rasb != null && rasb.IsSpecialName)
-                                       continue;
-                               
                                if (partial_name == null)
-                                       name = r.Name;
-                               else {
-                                       name = r.Name.Substring (partial_name.Length);
-                               }
-                               
-                               if (results.Contains (name))
-                                       continue;
-                               results.Add (name);
+                                       namespaced_partial = nexpr.Name;
+                               else
+                                       namespaced_partial = nexpr.Name + "." + partial_name;
+
+                               ec.CurrentMemberDefinition.GetCompletionStartingWith (namespaced_partial, results);
+                               if (partial_name != null)
+                                       results = results.Select (l => l.Substring (partial_name.Length)).ToList ();
+                       } else {
+                               var r = MemberCache.GetCompletitionMembers (ec, expr_type, partial_name).Select (l => l.Name);
+                               AppendResults (results, partial_name, r);
                        }
 
-                       throw new CompletionResult (partial_name == null ? "" : partial_name, (string []) results.ToArray (typeof (string)));
+                       throw new CompletionResult (partial_name == null ? "" : partial_name, results.Distinct ().ToArray ());
                }
 
                protected override void CloneTo (CloneContext clonectx, Expression t)
@@ -182,4 +159,38 @@ namespace Mono.CSharp {
                        target.expr = expr.Clone (clonectx);
                }
        }
-}
\ No newline at end of file
+
+       public class CompletionElementInitializer : CompletingExpression {
+               string partial_name;
+               
+               public CompletionElementInitializer (string partial_name, Location l)
+               {
+                       this.partial_name = partial_name;
+                       this.loc = l;
+               }
+               
+               protected override Expression DoResolve (ResolveContext ec)
+               {
+                       var members = MemberCache.GetCompletitionMembers (ec, ec.CurrentInitializerVariable.Type, partial_name);
+
+// TODO: Does this mean exact match only ?
+//                     if (partial_name != null && results.Count > 0 && result [0] == "")
+//                             throw new CompletionResult ("", new string [] { "=" });
+
+                       var results = members.Where (l => (l.Kind & (MemberKind.Field | MemberKind.Property)) != 0).Select (l => l.Name).ToList ();
+                       if (partial_name != null) {
+                               var temp = new List<string> ();
+                               AppendResults (temp, partial_name, results);
+                               results = temp;
+                       }
+
+                       throw new CompletionResult (partial_name == null ? "" : partial_name, results.Distinct ().ToArray ());
+               }
+
+               protected override void CloneTo (CloneContext clonectx, Expression t)
+               {
+                       // Nothing
+               }
+       }
+       
+}