readonly Dictionary<string, Namespace> all_namespaces;
public RootNamespace (string alias_name)
- : base (null, String.Empty)
+ : base ()
{
this.alias_name = alias_name;
+ RegisterNamespace (this);
all_namespaces = new Dictionary<string, Namespace> ();
all_namespaces.Add ("", this);
report.Error (1681, loc, "The global extern alias cannot be redefined");
}
- public void RegisterNamespace (Namespace child)
+ //
+ // For better error reporting where we try to guess missing using directive
+ //
+ public List<string> FindTypeNamespaces (IMemberContext ctx, string name, int arity)
{
- if (child != this)
- all_namespaces.Add (child.Name, child);
+ List<string> res = null;
+
+ foreach (var ns in all_namespaces) {
+ var type = ns.Value.LookupType (ctx, name, arity, LookupMode.Normal, Location.Null);
+ if (type != null) {
+ if (res == null)
+ res = new List<string> ();
+
+ res.Add (ns.Key);
+ }
+ }
+
+ return res;
}
- public bool IsNamespace (string name)
+ //
+ // For better error reporting where compiler tries to guess missing using directive
+ //
+ public List<string> FindExtensionMethodNamespaces (IMemberContext ctx, string name, int arity)
{
- return all_namespaces.ContainsKey (name);
+ List<string> res = null;
+
+ foreach (var ns in all_namespaces) {
+ var methods = ns.Value.LookupExtensionMethod (ctx, name, arity);
+ if (methods != null) {
+ if (res == null)
+ res = new List<string> ();
+
+ res.Add (ns.Key);
+ }
+ }
+
+ return res;
}
- protected void RegisterNamespace (string dotted_name)
+ public void RegisterNamespace (Namespace child)
{
- if (dotted_name != null && dotted_name.Length != 0 && ! IsNamespace (dotted_name))
- GetNamespace (dotted_name, true);
+ if (child != this)
+ all_namespaces.Add (child.Name, child);
}
public override string GetSignatureForError ()
}
}
- public class GlobalRootNamespace : RootNamespace
+ public sealed class GlobalRootNamespace : RootNamespace
{
public GlobalRootNamespace ()
: base ("global")
//
// Namespace cache for imported and compiled namespaces
//
- // This is an Expression to allow it to be referenced in the
- // compiler parse/intermediate tree during name resolution.
- //
- public class Namespace : FullNamedExpression
+ public class Namespace
{
- Namespace parent;
+ readonly Namespace parent;
string fullname;
protected Dictionary<string, Namespace> namespaces;
protected Dictionary<string, IList<TypeSpec>> types;
List<TypeSpec> extension_method_types;
- Dictionary<string, TypeExpr> cached_types;
- RootNamespace root;
+ Dictionary<string, TypeSpec> cached_types;
bool cls_checked;
- public readonly MemberName MemberName;
-
/// <summary>
/// Constructor Takes the current namespace and the
/// name. This is bootstrapped with parent == null
/// and name = ""
/// </summary>
public Namespace (Namespace parent, string name)
+ : this ()
{
- // Expression members.
- this.eclass = ExprClass.Namespace;
- this.Type = InternalType.Namespace;
- this.loc = Location.Null;
+ if (name == null)
+ throw new ArgumentNullException ("name");
this.parent = parent;
- if (parent != null)
- this.root = parent.root;
- else
- this.root = this as RootNamespace;
-
- if (this.root == null)
- throw new InternalErrorException ("Root namespaces must be created using RootNamespace");
-
- string pname = parent != null ? parent.fullname : "";
+ string pname = parent != null ? parent.fullname : null;
- if (pname == "")
+ if (pname == null)
fullname = name;
else
- fullname = parent.fullname + "." + name;
+ fullname = pname + "." + name;
- if (fullname == null)
- throw new InternalErrorException ("Namespace has a null fullname");
+ while (parent.parent != null)
+ parent = parent.parent;
- if (parent != null && parent.MemberName != MemberName.Null)
- MemberName = new MemberName (parent.MemberName, name, Location.Null);
- else if (name.Length == 0)
- MemberName = MemberName.Null;
- else
- MemberName = new MemberName (name, Location.Null);
-
- namespaces = new Dictionary<string, Namespace> ();
- cached_types = new Dictionary<string, TypeExpr> ();
+ var root = parent as RootNamespace;
+ if (root == null)
+ throw new InternalErrorException ("Root namespaces must be created using RootNamespace");
root.RegisterNamespace (this);
}
+ protected Namespace ()
+ {
+ namespaces = new Dictionary<string, Namespace> ();
+ cached_types = new Dictionary<string, TypeSpec> ();
+ }
+
#region Properties
/// <summary>
#endregion
- protected override Expression DoResolve (ResolveContext ec)
+ public Namespace AddNamespace (MemberName name)
{
- return this;
+ var ns_parent = name.Left == null ? this : AddNamespace (name.Left);
+ return ns_parent.TryAddNamespace (name.Basename);
}
- public void Error_NamespaceDoesNotExist (IMemberContext ctx, string name, int arity, Location loc)
+ Namespace TryAddNamespace (string name)
{
- var retval = LookupType (ctx, name, arity, LookupMode.IgnoreAccessibility, loc);
- if (retval != null) {
- ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (retval.Type);
- ErrorIsInaccesible (ctx, retval.GetSignatureForError (), loc);
- return;
- }
-
- retval = LookupType (ctx, name, -System.Math.Max (1, arity), LookupMode.Probing, loc);
- if (retval != null) {
- Error_TypeArgumentsCannotBeUsed (ctx, retval.Type, arity, loc);
- return;
- }
-
Namespace ns;
- if (arity > 0 && namespaces.TryGetValue (name, out ns)) {
- ns.Error_TypeArgumentsCannotBeUsed (ctx, null, arity, loc);
- return;
- }
- if (this is GlobalRootNamespace) {
- ctx.Module.Compiler.Report.Error (400, loc,
- "The type or namespace name `{0}' could not be found in the global namespace (are you missing an assembly reference?)",
- name);
- } else {
- ctx.Module.Compiler.Report.Error (234, loc,
- "The type or namespace name `{0}' does not exist in the namespace `{1}'. Are you missing an assembly reference?",
- name, GetSignatureForError ());
+ if (!namespaces.TryGetValue (name, out ns)) {
+ ns = new Namespace (this, name);
+ namespaces.Add (name, ns);
}
- }
- public override string GetSignatureForError ()
- {
- return fullname;
+ return ns;
}
- public Namespace AddNamespace (MemberName name)
+ public bool TryGetNamespace (string name, out Namespace ns)
{
- Namespace ns_parent;
- if (name.Left != null) {
- if (parent != null)
- ns_parent = parent.AddNamespace (name.Left);
- else
- ns_parent = AddNamespace (name.Left);
- } else {
- ns_parent = this;
- }
-
- Namespace ns;
- if (!ns_parent.namespaces.TryGetValue (name.Basename, out ns)) {
- ns = new Namespace (ns_parent, name.Basename);
- ns_parent.namespaces.Add (name.Basename, ns);
- }
-
- return ns;
+ return namespaces.TryGetValue (name, out ns);
}
// TODO: Replace with CreateNamespace where MemberName is created for the method call
return found;
}
- public TypeExpr LookupType (IMemberContext ctx, string name, int arity, LookupMode mode, Location loc)
+ public virtual string GetSignatureForError ()
+ {
+ return fullname;
+ }
+
+ public TypeSpec LookupType (IMemberContext ctx, string name, int arity, LookupMode mode, Location loc)
{
if (types == null)
return null;
- TypeExpr te;
- if (arity == 0 && cached_types.TryGetValue (name, out te))
- return te;
+ TypeSpec best = null;
+ if (arity == 0 && cached_types.TryGetValue (name, out best)) {
+ if (best != null || mode != LookupMode.IgnoreAccessibility)
+ return best;
+ }
IList<TypeSpec> found;
if (!types.TryGetValue (name, out found))
return null;
- TypeSpec best = null;
foreach (var ts in found) {
if (ts.Arity == arity) {
if (best == null) {
}
if (best.MemberDefinition.IsImported && ts.MemberDefinition.IsImported) {
+ if (ts.Kind == MemberKind.MissingType)
+ continue;
+
+ if (best.Kind == MemberKind.MissingType) {
+ best = ts;
+ continue;
+ }
+
if (mode == LookupMode.Normal) {
ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (best);
ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (ts);
ctx.Module.Compiler.Report.Error (433, loc, "The imported type `{0}' is defined multiple times", ts.GetSignatureForError ());
}
+
break;
}
if (mode != LookupMode.Normal)
continue;
- if (ts.MemberDefinition.IsImported)
+ if (ts.MemberDefinition.IsImported) {
+ ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (best);
ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (ts);
+ }
ctx.Module.Compiler.Report.Warning (436, 2, loc,
"The type `{0}' conflicts with the imported type of same name'. Ignoring the imported type definition",
}
}
- if (best == null)
- return null;
-
- te = new TypeExpression (best, Location.Null);
-
// TODO MemberCache: Cache more
if (arity == 0 && mode == LookupMode.Normal)
- cached_types.Add (name, te);
+ cached_types.Add (name, best);
- return te;
+ return best;
}
public FullNamedExpression LookupTypeOrNamespace (IMemberContext ctx, string name, int arity, LookupMode mode, Location loc)
Namespace ns;
if (arity == 0 && namespaces.TryGetValue (name, out ns)) {
if (texpr == null)
- return ns;
+ return new NamespaceExpression (ns, loc);
if (mode != LookupMode.Probing) {
- ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (texpr.Type);
+ //ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (texpr.Type);
// ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (ns.loc, "");
ctx.Module.Compiler.Report.Warning (437, 2, loc,
"The type `{0}' conflicts with the imported namespace `{1}'. Using the definition found in the source file",
texpr.GetSignatureForError (), ns.GetSignatureForError ());
}
- if (texpr.Type.MemberDefinition.IsImported)
- return ns;
+ if (texpr.MemberDefinition.IsImported)
+ return new NamespaceExpression (ns, loc);
}
- return texpr;
+ if (texpr == null)
+ return null;
+
+ return new TypeExpression (texpr, loc);
}
//
//
// Looks for extension method in this namespace
//
- public List<MethodSpec> LookupExtensionMethod (IMemberContext invocationContext, TypeSpec extensionType, string name, int arity)
+ public List<MethodSpec> LookupExtensionMethod (IMemberContext invocationContext, string name, int arity)
{
if (extension_method_types == null)
return null;
continue;
}
- var res = ts.MemberCache.FindExtensionMethods (invocationContext, extensionType, name, arity);
+ var res = ts.MemberCache.FindExtensionMethods (invocationContext, name, arity);
if (res == null)
continue;
types = new Dictionary<string, IList<TypeSpec>> (64);
}
- if ((ts.IsStatic || ts.MemberDefinition.IsPartial) && ts.Arity == 0 &&
- (ts.MemberDefinition.DeclaringAssembly == null || ts.MemberDefinition.DeclaringAssembly.HasExtensionMethod)) {
- if (extension_method_types == null)
- extension_method_types = new List<TypeSpec> ();
+ if (ts.IsClass && ts.Arity == 0) {
+ var extension_method_allowed = ts.MemberDefinition.IsImported ? (ts.Modifiers & Modifiers.METHOD_EXTENSION) != 0 : (ts.IsStatic || ts.MemberDefinition.IsPartial);
+ if (extension_method_allowed) {
+ if (extension_method_types == null)
+ extension_method_types = new List<TypeSpec> ();
- extension_method_types.Add (ts);
+ extension_method_types.Add (ts);
+ }
}
var name = ts.Name;
cached_types.Remove (tc.Basename);
}
- public override FullNamedExpression ResolveAsTypeOrNamespace (IMemberContext mc)
- {
- return this;
- }
-
public void SetBuiltinType (BuiltinTypeSpec pts)
{
var found = types[pts.Name];
void CreateUnitSymbolInfo (MonoSymbolFile symwriter)
{
var si = file.CreateSymbolInfo (symwriter);
- comp_unit = new CompileUnitEntry (symwriter, si);;
+ comp_unit = new CompileUnitEntry (symwriter, si);
if (include_files != null) {
foreach (SourceFile include in include_files.Values) {
return Compiler.Settings.IsConditionalSymbolDefined (value);
}
+
+ public override void Accept (StructuralVisitor visitor)
+ {
+ visitor.Visit (this);
+ }
}
public override void AddPartial (TypeDefinition next_part)
{
var existing = ns.LookupType (this, next_part.MemberName.Name, next_part.MemberName.Arity, LookupMode.Probing, Location.Null);
- var td = existing != null ? existing.Type.MemberDefinition as TypeDefinition : null;
+ var td = existing != null ? existing.MemberDefinition as TypeDefinition : null;
AddPartial (next_part, td);
}
MemberCore mc;
if (names_container.DefinedNames.TryGetValue (name, out mc)) {
if (tc is NamespaceContainer && mc is NamespaceContainer) {
- containers.Add (tc);
+ AddTypeContainerMember (tc);
return;
}
}
} else {
names_container.DefinedNames.Add (name, tc);
+
+ var tdef = tc.PartialContainer;
+ if (tdef != null) {
+ //
+ // Same name conflict in different namespace containers
+ //
+ var conflict = ns.GetAllTypes (name);
+ if (conflict != null) {
+ foreach (var e in conflict) {
+ if (e.Arity == mn.Arity) {
+ mc = (MemberCore) e.MemberDefinition;
+ break;
+ }
+ }
+ }
+
+ if (mc != null) {
+ Report.SymbolRelatedToPreviousError (mc);
+ Report.Error (101, tc.Location, "The namespace `{0}' already contains a definition for `{1}'",
+ GetSignatureForError (), mn.GetSignatureForError ());
+ } else {
+ ns.AddType (Module, tdef.Definition);
+ }
+ }
}
base.AddTypeContainer (tc);
-
- var tdef = tc.PartialContainer;
- if (tdef != null)
- ns.AddType (Module, tdef.Definition);
}
public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
ExtensionMethodCandidates candidates;
var container = this;
do {
- candidates = container.LookupExtensionMethodCandidates (invocationContext, extensionType, name, arity, ref position);
+ candidates = container.LookupExtensionMethodCandidates (invocationContext, name, arity, ref position);
if (candidates != null || container.MemberName == null)
return candidates;
while (mn != null) {
++position;
- var methods = container_ns.LookupExtensionMethod (invocationContext, extensionType, name, arity);
+ var methods = container_ns.LookupExtensionMethod (invocationContext, name, arity);
if (methods != null) {
return new ExtensionMethodCandidates (invocationContext, methods, container, position);
}
return null;
}
- ExtensionMethodCandidates LookupExtensionMethodCandidates (IMemberContext invocationContext, TypeSpec extensionType, string name, int arity, ref int position)
+ ExtensionMethodCandidates LookupExtensionMethodCandidates (IMemberContext invocationContext, string name, int arity, ref int position)
{
List<MethodSpec> candidates = null;
if (position == 0) {
++position;
- candidates = ns.LookupExtensionMethod (invocationContext, extensionType, name, arity);
+ candidates = ns.LookupExtensionMethod (invocationContext, name, arity);
if (candidates != null) {
return new ExtensionMethodCandidates (invocationContext, candidates, this, position);
}
++position;
foreach (Namespace n in namespace_using_table) {
- var a = n.LookupExtensionMethod (invocationContext, extensionType, name, arity);
+ var a = n.LookupExtensionMethod (invocationContext, name, arity);
if (a == null)
continue;
public override void GetCompletionStartingWith (string prefix, List<string> results)
{
+ if (Usings == null)
+ return;
+
foreach (var un in Usings) {
if (un.Alias != null)
continue;
if (aliases != null && arity == 0) {
UsingAliasNamespace uan;
if (aliases.TryGetValue (name, out uan)) {
- if (fne != null) {
+ if (fne != null && mode != LookupMode.Probing) {
// TODO: Namespace has broken location
//Report.SymbolRelatedToPreviousError (fne.Location, null);
Compiler.Report.SymbolRelatedToPreviousError (uan.Location, null);
// A using directive imports only types contained in the namespace, it
// does not import any nested namespaces
//
- fne = using_ns.LookupType (this, name, arity, mode, loc);
- if (fne == null)
+ var t = using_ns.LookupType (this, name, arity, mode, loc);
+ if (t == null)
continue;
+ fne = new TypeExpression (t, loc);
if (match == null) {
match = fne;
continue;
return match;
}
- static void MsgtryRef (string s)
- {
- Console.WriteLine (" Try using -r:" + s);
- }
-
- static void MsgtryPkg (string s)
- {
- Console.WriteLine (" Try using -pkg:" + s);
- }
-
- public static void Error_NamespaceNotFound (Location loc, string name, Report Report)
- {
- Report.Error (246, loc, "The type or namespace name `{0}' could not be found. Are you missing a using directive or an assembly reference?",
- name);
-
- switch (name) {
- case "Gtk": case "GtkSharp":
- MsgtryPkg ("gtk-sharp-2.0");
- break;
-
- case "Gdk": case "GdkSharp":
- MsgtryPkg ("gdk-sharp-2.0");
- break;
-
- case "Glade": case "GladeSharp":
- MsgtryPkg ("glade-sharp-2.0");
- break;
-
- case "System.Drawing":
- case "System.Web.Services":
- case "System.Web":
- case "System.Data":
- case "System.Windows.Forms":
- MsgtryRef (name);
- break;
- }
- }
-
protected override void DefineNamespace ()
{
if (namespace_using_table == null)
entry.Define (this);
- Namespace using_ns = entry.ResolvedExpression as Namespace;
+ //
+ // It's needed for repl only, when using clause cannot be resolved don't hold it in
+ // global list which is resolved for each evaluation
+ //
+ if (entry.ResolvedExpression == null) {
+ clauses.RemoveAt (i--);
+ continue;
+ }
+
+ var using_ns = entry.ResolvedExpression as NamespaceExpression;
if (using_ns == null)
continue;
- if (list.Contains (using_ns)) {
+ if (list.Contains (using_ns.Namespace)) {
+ // Ensure we don't report the warning multiple times in repl
+ clauses.RemoveAt (i--);
+
Compiler.Report.Warning (105, 3, entry.Location,
"The using directive for `{0}' appeared previously in this namespace", using_ns.GetSignatureForError ());
} else {
- list.Add (using_ns);
+ list.Add (using_ns.Namespace);
}
}
}
}
- public void EnableUsingClausesRedefinition ()
+ public void EnableRedefinition ()
{
+ is_defined = false;
namespace_using_table = null;
}
return false;
}
+
+ public override void Accept (StructuralVisitor visitor)
+ {
+ visitor.Visit (this);
+ }
}
public class UsingNamespace
public virtual void Define (NamespaceContainer ctx)
{
resolved = expr.ResolveAsTypeOrNamespace (ctx);
- var ns = resolved as Namespace;
+ var ns = resolved as NamespaceExpression;
if (ns == null) {
if (resolved != null) {
ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (resolved.Type);
}
}
}
+
+ public override string ToString()
+ {
+ return resolved.ToString();
+ }
}
public class UsingExternAlias : UsingAliasNamespace
public override void Define (NamespaceContainer ctx)
{
- resolved = ctx.Module.GetRootNamespace (Alias.Value);
- if (resolved == null) {
+ var ns = ctx.Module.GetRootNamespace (Alias.Value);
+ if (ns == null) {
ctx.Module.Compiler.Report.Error (430, Location,
"The extern alias `{0}' was not specified in -reference option",
Alias.Value);
+ return;
}
+
+ resolved = new NamespaceExpression (ns, Location);
}
}