namespace Mono.CSharp {
public class RootNamespace : Namespace {
+ //
+ // Points to Mono's GetNamespaces method, an
+ // optimization when running on Mono to fetch all the
+ // namespaces in an assembly
+ //
static MethodInfo get_namespaces_method;
string alias_name;
- Assembly referenced_assembly;
+ protected Assembly [] referenced_assemblies;
Hashtable all_namespaces;
- static Hashtable root_namespaces;
+ static ListDictionary root_namespaces;
public static GlobalRootNamespace Global;
static RootNamespace ()
public static void Reset ()
{
- root_namespaces = new Hashtable ();
+ root_namespaces = new ListDictionary ();
Global = new GlobalRootNamespace ();
root_namespaces ["global"] = Global;
}
- protected RootNamespace (string alias_name, Assembly assembly)
+ protected RootNamespace (string alias_name)
: base (null, String.Empty)
{
this.alias_name = alias_name;
- referenced_assembly = assembly;
+ referenced_assemblies = new Assembly [0];
all_namespaces = new Hashtable ();
all_namespaces.Add ("", this);
+ }
+
+ public void AddAssemblyReference (Assembly a)
+ {
+ foreach (Assembly assembly in referenced_assemblies) {
+ if (a == assembly)
+ return;
+ }
- if (referenced_assembly != null)
- ComputeNamespaces (this.referenced_assembly);
+ // How to test whether attribute exists without loading the assembly :-(
+#if NET_2_1
+ const string SystemCore = "System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e";
+#else
+ const string SystemCore = "System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
+#endif
+ if (TypeManager.extension_attribute_type == null &&
+ a.FullName == SystemCore) {
+ TypeManager.extension_attribute_type = a.GetType("System.Runtime.CompilerServices.ExtensionAttribute");
+ }
+
+ int top = referenced_assemblies.Length;
+ Assembly [] n = new Assembly [top + 1];
+ referenced_assemblies.CopyTo (n, 0);
+ n [top] = a;
+ referenced_assemblies = n;
}
- public static void DefineRootNamespace (string name, Assembly assembly)
+ public static void DefineRootNamespace (string alias, Assembly assembly)
{
- if (name == "global") {
+ if (alias == "global") {
NamespaceEntry.Error_GlobalNamespaceRedefined (Location.Null);
return;
}
- RootNamespace retval = GetRootNamespace (name);
- if (retval == null || retval.referenced_assembly != assembly)
- root_namespaces [name] = new RootNamespace (name, assembly);
+
+ RootNamespace retval = GetRootNamespace (alias);
+ if (retval == null) {
+ retval = new RootNamespace (alias);
+ root_namespaces.Add (alias, retval);
+ }
+
+ retval.AddAssemblyReference (assembly);
}
public static RootNamespace GetRootNamespace (string name)
return (RootNamespace) root_namespaces [name];
}
+ public static void ComputeNamespaces ()
+ {
+ foreach (RootNamespace rn in root_namespaces.Values) {
+ foreach (Assembly a in rn.referenced_assemblies) {
+ try {
+ rn.ComputeNamespaces (a);
+ } catch (TypeLoadException e) {
+ Report.Error (11, Location.Null, e.Message);
+ } catch (System.IO.FileNotFoundException) {
+ Report.Error (12, Location.Null, "An assembly `{0}' is used without being referenced",
+ a.FullName);
+ }
+ }
+ }
+ }
+
public virtual Type LookupTypeReflection (string name, Location loc)
{
- return GetTypeInAssembly (referenced_assembly, name);
+ Type found_type = null;
+
+ foreach (Assembly a in referenced_assemblies) {
+ Type t = GetTypeInAssembly (a, name);
+ if (t == null)
+ continue;
+
+ if (found_type == null) {
+ found_type = t;
+ continue;
+ }
+
+ Report.SymbolRelatedToPreviousError (found_type);
+ Report.SymbolRelatedToPreviousError (t);
+ Report.Error (433, loc, "The imported type `{0}' is defined multiple times", name);
+
+ return found_type;
+ }
+
+ return found_type;
}
public void RegisterNamespace (Namespace child)
protected void ComputeNamespaces (Assembly assembly)
{
- // How to test whether attribute exists without loading the assembly :-(
-#if NET_2_1
- const string SystemCore = "System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
-#else
- const string SystemCore = "System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
-#endif
- if (TypeManager.extension_attribute_type == null &&
- assembly.FullName == SystemCore) {
- TypeManager.extension_attribute_type = assembly.GetType("System.Runtime.CompilerServices.ExtensionAttribute");
- }
-
bool contains_extension_methods = TypeManager.extension_attribute_type != null &&
assembly.IsDefined(TypeManager.extension_attribute_type, false);
}
public class GlobalRootNamespace : RootNamespace {
- Assembly [] assemblies;
Module [] modules;
public GlobalRootNamespace ()
- : base ("global", null)
+ : base ("global")
{
- assemblies = new Assembly [0];
}
public Assembly [] Assemblies {
- get { return assemblies; }
+ get { return referenced_assemblies; }
}
public Module [] Modules {
get { return modules; }
}
- public void AddAssemblyReference (Assembly a)
- {
- foreach (Assembly assembly in assemblies) {
- if (a == assembly)
- return;
- }
-
- int top = assemblies.Length;
- Assembly [] n = new Assembly [top + 1];
- assemblies.CopyTo (n, 0);
- n [top] = a;
- assemblies = n;
-
- ComputeNamespaces (a);
- }
-
public void AddModuleReference (Module m)
{
int top = modules != null ? modules.Length : 0;
public override Type LookupTypeReflection (string name, Location loc)
{
- Type found_type = null;
-
- foreach (Assembly a in assemblies) {
- Type t = GetTypeInAssembly (a, name);
- if (t == null)
- continue;
-
- if (found_type == null) {
- found_type = t;
- continue;
- }
-
- Report.SymbolRelatedToPreviousError (found_type);
- Report.SymbolRelatedToPreviousError (t);
- Report.Error (433, loc, "The imported type `{0}' is defined multiple times", name);
-
- return found_type;
- }
+ Type found_type = base.LookupTypeReflection (name, loc);
if (modules != null) {
foreach (Module module in modules) {
return ns;
}
+ public bool HasDefinition (string name)
+ {
+ return declspaces != null && declspaces [name] != null;
+ }
+
TypeExpr LookupType (string name, Location loc)
{
if (cached_types.Contains (name))
//
tdecl.DefineType ();
t = tdecl.TypeBuilder;
+
+ if (RootContext.EvalMode){
+ // Replace the TypeBuilder with a System.Type, as
+ // Reflection.Emit fails otherwise (we end up pretty
+ // much with Random type definitions later on).
+ Type tt = t.Assembly.GetType (t.Name);
+ if (tt != null)
+ t = tt;
+ }
}
}
string lookup = t != null ? t.FullName : (fullname.Length == 0 ? name : fullname + "." + name);
///
/// Looks for extension method in this namespace
///
- public ArrayList LookupExtensionMethod (Type extensionType, ClassOrStruct currentClass, string name, NamespaceEntry ns)
+ public ArrayList LookupExtensionMethod (Type extensionType, ClassOrStruct currentClass, string name)
{
ArrayList found = null;
declspaces.Add (name, ds);
}
+ public void RemoveDeclSpace (string name)
+ {
+ declspaces.Remove (name);
+ }
+
/// <summary>
/// The qualified name of the current namespace
/// </summary>
}
return resolved;
}
+
+ public override string ToString ()
+ {
+ return Name;
+ }
}
class UsingAliasEntry {
return fne;
}
+
+ public override string ToString ()
+ {
+ return Alias;
+ }
+
}
class LocalUsingAliasEntry : UsingAliasEntry {
public override FullNamedExpression Resolve (IResolveContext rc)
{
- if (resolved != null)
+ if (resolved != null || value == null)
return (FullNamedExpression)resolved;
resolved = value.GetTypeExpression ().ResolveAsTypeStep (rc, false);
- if (resolved == null)
+ if (resolved == null) {
+ value = null;
return null;
+ }
// FIXME: This is quite wrong, the accessibility is not global
if (resolved.Type != null) {
return (FullNamedExpression)resolved;
}
+
+ public override string ToString ()
+ {
+ return String.Format ("{0} = {1}", Alias, value.GetSignatureForError ());
+ }
}
Namespace ns;
NamespaceEntry parent, implicit_parent;
- SourceFile file;
+ CompilationUnit file;
int symfile_id;
// Namespace using import block
entries = new ArrayList ();
}
- public NamespaceEntry (NamespaceEntry parent, SourceFile file, string name)
+ public NamespaceEntry (NamespaceEntry parent, CompilationUnit file, string name)
{
this.parent = parent;
this.file = file;
SlaveDeclSpace = new RootDeclSpace (this);
}
- private NamespaceEntry (NamespaceEntry parent, SourceFile file, Namespace ns, bool slave)
+ private NamespaceEntry (NamespaceEntry parent, CompilationUnit file, Namespace ns, bool slave)
{
this.parent = parent;
this.file = file;
this.SlaveDeclSpace = slave ? new RootDeclSpace (this) : null;
}
+ //
+ // Populates the Namespace with some using declarations, used by the
+ // eval mode.
+ //
+ public void Populate (ArrayList source_using_aliases, ArrayList source_using_clauses)
+ {
+ foreach (UsingAliasEntry uae in source_using_aliases){
+ if (using_aliases == null)
+ using_aliases = new ArrayList ();
+
+ using_aliases.Add (uae);
+ }
+
+ foreach (UsingEntry ue in source_using_clauses){
+ if (using_clauses == null)
+ using_clauses = new ArrayList ();
+
+ using_clauses.Add (ue);
+ }
+ }
+
+ //
+ // Extracts the using alises and using clauses into a couple of
+ // arrays that might already have the same information; Used by the
+ // C# Eval mode.
+ //
+ public void Extract (ArrayList out_using_aliases, ArrayList out_using_clauses)
+ {
+ if (using_aliases != null){
+ foreach (UsingAliasEntry uae in using_aliases){
+ bool replaced = false;
+
+ for (int i = 0; i < out_using_aliases.Count; i++){
+ UsingAliasEntry out_uea = (UsingAliasEntry) out_using_aliases [i];
+
+ if (out_uea.Alias == uae.Alias){
+ out_using_aliases [i] = uae;
+ replaced = true;
+ break;
+ }
+ }
+ if (!replaced)
+ out_using_aliases.Add (uae);
+ }
+ }
+
+ if (using_clauses != null){
+ foreach (UsingEntry ue in using_clauses){
+ bool found = false;
+
+ foreach (UsingEntry out_ue in out_using_clauses)
+ if (out_ue.Name == ue.Name){
+ found = true;
+ break;
+ }
+ if (!found)
+ out_using_clauses.Add (ue);
+ }
+ }
+ }
+
//
// 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.
{
ArrayList candidates = null;
if (currentClass != null) {
- candidates = ns.LookupExtensionMethod (extensionType, currentClass, name, this);
+ candidates = ns.LookupExtensionMethod (extensionType, currentClass, name);
if (candidates != null)
return new ExtensionMethodGroupExpr (candidates, this, extensionType, loc);
}
foreach (Namespace n in GetUsingTable ()) {
- ArrayList a = n.LookupExtensionMethod (extensionType, null, name, this);
+ ArrayList a = n.LookupExtensionMethod (extensionType, null, name);
if (a == null)
continue;
if (parent == null)
return null;
+ //
+ // Inspect parent namespaces in namespace expression
+ //
+ Namespace parent_ns = ns.Parent;
+ do {
+ candidates = parent_ns.LookupExtensionMethod (extensionType, null, name);
+ if (candidates != null)
+ return new ExtensionMethodGroupExpr (candidates, parent, extensionType, loc);
+
+ parent_ns = parent_ns.Parent;
+ } while (parent_ns != null);
+
+ //
+ // Continue in parent scope
+ //
return parent.LookupExtensionMethod (extensionType, currentClass, name, loc);
}
// Check whether it's in the namespace.
//
FullNamedExpression fne = ns.Lookup (ds, name, loc);
- if (fne != null)
- return fne;
//
// Check aliases.
//
if (using_aliases != null) {
foreach (UsingAliasEntry ue in using_aliases) {
- if (ue.Alias == name)
+ if (ue.Alias == name) {
+ if (fne != null) {
+ if (Doppelganger != null) {
+ // TODO: Namespace has broken location
+ //Report.SymbolRelatedToPreviousError (fne.Location, null);
+ Report.SymbolRelatedToPreviousError (ue.Location, null);
+ Report.Error (576, loc,
+ "Namespace `{0}' contains a definition with same name as alias `{1}'",
+ GetSignatureForError (), name);
+ } else {
+ return fne;
+ }
+ }
+
return ue.Resolve (Doppelganger);
+ }
}
}
+ if (fne != null)
+ return fne;
+
if (IsImplicit)
return null;
using_list [i] = ((UsingEntry) using_clauses [i]).MemberName.GetTypeName ();
}
- symfile_id = SymbolWriter.DefineNamespace (ns.Name, file.SourceFileEntry, using_list, parent_id);
+ symfile_id = SymbolWriter.DefineNamespace (ns.Name, file.CompileUnitEntry, using_list, parent_id);
}
return symfile_id;
}
public static void Error_NamespaceNotFound (Location loc, string name)
{
+ if (RootContext.EvalMode){
+ // Do not report this, it might be an error on the eval side, and we are lax there.
+ return;
+ }
+
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);