📄 namespace.cs
字号:
} internal Namespace resolved; public Namespace Resolve () { if (resolved != null) return resolved; DeclSpace root = RootContext.Tree.Types; root.NamespaceEntry = NamespaceEntry; FullNamedExpression fne = Expr.ResolveAsTypeStep (root.EmitContext, false); root.NamespaceEntry = null; if (fne == null) { Error_NamespaceNotFound (Location, Name.ToString ()); return null; } resolved = fne as Namespace; if (resolved == null) { Report.Error (138, Location, "`{0} is a type not a namespace. A using namespace directive can only be applied to namespaces", Name.ToString ()); } return resolved; } } public abstract class AliasEntry { public readonly string Name; public readonly NamespaceEntry NamespaceEntry; public readonly Location Location; protected AliasEntry (NamespaceEntry entry, string name, Location loc) { Name = name; NamespaceEntry = entry; Location = loc; } protected FullNamedExpression resolved; bool error; public FullNamedExpression Resolve () { if (resolved != null || error) return resolved; resolved = DoResolve (); if (resolved == null) error = true; return resolved; } protected abstract FullNamedExpression DoResolve (); } public class LocalAliasEntry : AliasEntry { public readonly Expression Alias; public LocalAliasEntry (NamespaceEntry entry, string name, MemberName alias, Location loc) : base (entry, name, loc) { Alias = alias.GetTypeExpression (); } protected override FullNamedExpression DoResolve () { DeclSpace root = RootContext.Tree.Types; root.NamespaceEntry = NamespaceEntry; resolved = Alias.ResolveAsTypeStep (root.EmitContext, false); root.NamespaceEntry = null; if (resolved == null) Error_NamespaceNotFound (Location, Alias.ToString ()); return resolved; } } public class ExternAliasEntry : AliasEntry { public ExternAliasEntry (NamespaceEntry entry, string name, Location loc) : base (entry, name, loc) { } protected override FullNamedExpression DoResolve () { resolved = RootNamespace.GetRootNamespace (Name); if (resolved == null) Report.Error (430, Location, "The extern alias '" + Name + "' was not specified in a /reference option"); return resolved; } } public NamespaceEntry (NamespaceEntry parent, SourceFile file, string name, Location loc) { this.parent = parent; this.file = file; this.IsImplicit = false; entries.Add (this); this.ID = entries.Count; if (parent != null) ns = parent.NS.GetNamespace (name, true); else if (name != null) ns = RootNamespace.Global.GetNamespace (name, true); else ns = RootNamespace.Global; } private NamespaceEntry (NamespaceEntry parent, SourceFile file, Namespace ns) { this.parent = parent; this.file = file; // no need to add self to 'entries', since we don't have any aliases or using entries. this.ID = -1; this.IsImplicit = true; this.ns = ns; } // // According to section 16.3.1 (using-alias-directive), the namespace-or-type-name is // resolved as if the immediately containing namespace body has no using-directives. // // Section 16.3.2 says that the same rule is applied when resolving the namespace-name // in the using-namespace-directive. // // To implement these rules, the expressions in the using directives are resolved using // the "doppelganger" (ghostly bodiless duplicate). // NamespaceEntry doppelganger; NamespaceEntry Doppelganger { get { if (!IsImplicit && doppelganger == null) doppelganger = new NamespaceEntry (ImplicitParent, file, ns); return doppelganger; } } public readonly int ID; public readonly bool IsImplicit; public Namespace NS { get { return ns; } } public NamespaceEntry Parent { get { return parent; } } public NamespaceEntry ImplicitParent { get { if (parent == null) return null; if (implicit_parent == null) { implicit_parent = (parent.NS == ns.Parent) ? parent : new NamespaceEntry (parent, file, ns.Parent); } return implicit_parent; } } /// <summary> /// Records a new namespace for resolving name references /// </summary> public void Using (MemberName name, Location loc) { if (DeclarationFound){ Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations"); return; } if (name.Equals (ns.MemberName)) return; if (using_clauses == null) using_clauses = new ArrayList (); foreach (UsingEntry old_entry in using_clauses) { if (name.Equals (old_entry.Name)) { if (RootContext.WarningLevel >= 3) Report.Warning (105, loc, "The using directive for `{0}' appeared previously in this namespace", name); return; } } UsingEntry ue = new UsingEntry (Doppelganger, name, loc); using_clauses.Add (ue); } public void UsingAlias (string name, MemberName alias, Location loc) { if (DeclarationFound){ Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations"); return; } if (aliases == null) aliases = new Hashtable (); if (aliases.Contains (name)) { AliasEntry ae = (AliasEntry)aliases [name]; Report.SymbolRelatedToPreviousError (ae.Location, ae.Name); Report.Error (1537, loc, "The using alias `" + name + "' appeared previously in this namespace"); return; } if (RootContext.Version == LanguageVersion.Default && name == "global" && RootContext.WarningLevel >= 2) Report.Warning (440, loc, "An alias named `global' will not be used when resolving 'global::';" + " the global namespace will be used instead"); aliases [name] = new LocalAliasEntry (Doppelganger, name, alias, loc); } public void UsingExternalAlias (string name, Location loc) { if (UsingFound || DeclarationFound) { Report.Error (439, loc, "An extern alias declaration must precede all other elements"); return; } if (aliases == null) aliases = new Hashtable (); if (aliases.Contains (name)) { AliasEntry ae = (AliasEntry) aliases [name]; Report.SymbolRelatedToPreviousError (ae.Location, ae.Name); Report.Error (1537, loc, "The using alias `" + name + "' appeared previously in this namespace"); return; } if (name == "global") { Report.Error (1681, loc, "You cannot redefine the global extern alias"); return; } aliases [name] = new ExternAliasEntry (Doppelganger, name, loc); } public FullNamedExpression LookupNamespaceOrType (DeclSpace ds, string name, Location loc, bool ignore_cs0104) { // Precondition: Only simple names (no dots) will be looked up with this function. FullNamedExpression resolved = null; for (NamespaceEntry curr_ns = this; curr_ns != null; curr_ns = curr_ns.ImplicitParent) { if ((resolved = curr_ns.Lookup (ds, name, loc, ignore_cs0104)) != null) break; } return resolved; } static void Error_AmbiguousTypeReference (Location loc, string name, FullNamedExpression t1, FullNamedExpression t2) { Report.Error (104, loc, "`{0}' is an ambiguous reference between `{1}' and `{2}'", name, t1.FullName, t2.FullName); } // Looks-up a alias named @name in this and surrounding namespace declarations public FullNamedExpression LookupAlias (string name) { AliasEntry entry = null; // We use Parent rather than ImplicitParent since we know implicit namespace declarations // cannot have using entries. for (NamespaceEntry n = this; n != null; n = n.Parent) { if (n.aliases == null) continue; entry = n.aliases [name] as AliasEntry; if (entry != null) return entry.Resolve (); } return null; } private FullNamedExpression Lookup (DeclSpace ds, string name, Location loc, bool ignore_cs0104) { // // Check whether it's in the namespace. // FullNamedExpression fne = NS.Lookup (ds, name, loc); if (fne != null) return fne; if (IsImplicit) return null; // // Check aliases. // if (aliases != null) { AliasEntry entry = aliases [name] as AliasEntry; if (entry != null) return entry.Resolve (); } // // Check using entries. // FullNamedExpression match = null; foreach (Namespace using_ns in GetUsingTable ()) { match = using_ns.Lookup (ds, name, loc); if (match == null || !(match is TypeExpr)) continue; if (fne != null) { if (!ignore_cs0104) Error_AmbiguousTypeReference (loc, name, fne, match); return null; } fne = match; } return fne; } // Our cached computation. readonly Namespace [] empty_namespaces = new Namespace [0]; Namespace [] namespace_using_table; Namespace [] GetUsingTable () { if (namespace_using_table != null) return namespace_using_table; if (using_clauses == null) { namespace_using_table = empty_namespaces; return namespace_using_table; } ArrayList list = new ArrayList (using_clauses.Count); foreach (UsingEntry ue in using_clauses) { Namespace using_ns = ue.Resolve (); if (using_ns == null) continue; list.Add (using_ns); } namespace_using_table = new Namespace [list.Count]; list.CopyTo (namespace_using_table, 0); return namespace_using_table; } readonly string [] empty_using_list = new string [0]; public int SymbolFileID { get { if (symfile_id == 0 && file.SourceFileEntry != null) { int parent_id = parent == null ? 0 : parent.SymbolFileID; string [] using_list = empty_using_list; if (using_clauses != null) { using_list = new string [using_clauses.Count]; for (int i = 0; i < using_clauses.Count; i++) using_list [i] = ((UsingEntry) using_clauses [i]).Name.ToString (); } symfile_id = CodeGen.SymbolWriter.DefineNamespace (ns.Name, file.SourceFileEntry, using_list, parent_id); } return symfile_id; } } 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.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"); break; case "Gdk": case "GdkSharp": MsgtryPkg ("gdk-sharp"); break; case "Glade": case "GladeSharp": MsgtryPkg ("glade-sharp"); break; case "System.Drawing": case "System.Web.Services": case "System.Web": case "System.Data": case "System.Windows.Forms": MsgtryRef (name); break; } } /// <summary> /// Used to validate that all the using clauses are correct /// after we are finished parsing all the files. /// </summary> void VerifyUsing () { if (using_clauses != null) { foreach (UsingEntry ue in using_clauses) ue.Resolve (); } if (aliases != null) { foreach (DictionaryEntry de in aliases) ((AliasEntry) de.Value).Resolve (); } } /// <summary> /// Used to validate that all the using clauses are correct /// after we are finished parsing all the files. /// </summary> static public void VerifyAllUsing () { foreach (NamespaceEntry entry in entries) entry.VerifyUsing (); } public string GetSignatureForError () { return ns.GetSignatureForError (); } public override string ToString () { return ns.ToString (); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -