/// <summary> /// Constructor Takes the current namespace and the /// name. This is bootstrapped with parent == null /// and name = "" /// </summary> public Namespace(Namespace parent, string name) { // Expression members. this.eclass = ExprClass.Namespace; this.Type = InternalType.FakeInternalType; this.loc = Location.Null; 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 : ""; if (pname == "") fullname = name; else fullname = parent.fullname + "." + name; if (fullname == null) throw new InternalErrorException ("Namespace has a null fullname"); if (parent != null && parent.MemberName != MemberName.Null) MemberName = new MemberName (parent.MemberName, name); else if (name.Length == 0) MemberName = MemberName.Null; else MemberName = new MemberName (name); namespaces = new Dictionary<string, Namespace> (); cached_types = new Dictionary<string, TypeExpr> (); root.RegisterNamespace (this); }
protected void ImportTypes (MetaType[] types, Namespace targetNamespace, bool hasExtensionTypes) { Namespace ns = targetNamespace; string prev_namespace = null; foreach (var t in types) { if (t == null) continue; // Be careful not to trigger full parent type loading if (t.MemberType == MemberTypes.NestedType) continue; if (t.Name[0] == '<') continue; var it = CreateType (t, null, new DynamicTypeReader (t), true); if (it == null) continue; if (prev_namespace != t.Namespace) { ns = t.Namespace == null ? targetNamespace : targetNamespace.GetNamespace (t.Namespace, true); prev_namespace = t.Namespace; } ns.AddType (module, it); if (it.IsStatic && hasExtensionTypes && HasAttribute (CustomAttributeData.GetCustomAttributes (t), "ExtensionAttribute", CompilerServicesNamespace)) { it.SetExtensionMethodContainer (); } } }
private NamespaceEntry (NamespaceEntry parent, CompilationUnit file, Namespace ns, bool slave) { this.parent = parent; this.file = file; this.IsImplicit = true; this.ns = ns; this.SlaveDeclSpace = slave ? new RootDeclSpace (this) : null; }
public Namespace Resolve (IMemberContext rc) { if (resolved != null) return resolved; FullNamedExpression fne = name.GetTypeExpression ().ResolveAsTypeStep (rc, false); if (fne == null) return null; resolved = fne as Namespace; if (resolved == null) { rc.Compiler.Report.SymbolRelatedToPreviousError (fne.Type); rc.Compiler.Report.Error (138, Location, "`{0}' is a type not a namespace. A using namespace directive can only be applied to namespaces", GetSignatureForError ()); } return resolved; }
public NamespaceContainer (MemberName name, ModuleContainer module, NamespaceContainer parent, CompilationSourceFile sourceFile) { this.module = module; this.parent = parent; this.file = sourceFile; this.loc = name == null ? Location.Null : name.Location; if (parent != null) ns = parent.NS.GetNamespace (name.GetName (), true); else if (name != null) ns = module.GlobalRootNamespace.GetNamespace (name.GetName (), true); else ns = module.GlobalRootNamespace; SlaveDeclSpace = new RootDeclSpace (module, this); }
public NamespaceEntry (ModuleContainer ctx, NamespaceEntry parent, CompilationUnit file, string name) { this.ctx = ctx; this.parent = parent; this.file = file; entries.Add (this); if (parent != null) ns = parent.NS.GetNamespace (name, true); else if (name != null) ns = ctx.GlobalRootNamespace.GetNamespace (name, true); else ns = ctx.GlobalRootNamespace; SlaveDeclSpace = new RootDeclSpace (ctx, this); }
public NamespaceContainer(MemberName name, NamespaceContainer parent) : base(parent, name, null, MemberKind.Namespace) { this.Parent = parent; this.ns = parent.NS.AddNamespace (name); containers = new List<TypeContainer> (); }
public static TypeSpec Resolve(ModuleContainer module, MemberKind kind, string ns, string name, int arity, bool reportErrors) { Namespace type_ns = module.GlobalRootNamespace.GetNamespace(ns, true); var found = type_ns.GetAllTypes(name); if (found == null) { if (reportErrors) { module.Compiler.Report.Error(518, "The predefined type `{0}.{1}' is not defined or imported", ns, name); } return(null); } TypeSpec best_match = null; foreach (var candidate in found) { if (candidate.Kind != kind) { if (candidate.Kind == MemberKind.Struct && kind == MemberKind.Void && candidate.MemberDefinition is TypeContainer) { // Void is declared as struct but we keep it internally as // special kind, the swap will be done by caller } else { continue; } } if (candidate.Arity != arity) { continue; } if ((candidate.Modifiers & Modifiers.INTERNAL) != 0 && !candidate.MemberDefinition.IsInternalAsPublic(module.DeclaringAssembly)) { continue; } if (best_match == null) { best_match = candidate; continue; } var other_match = best_match; if (!best_match.MemberDefinition.IsImported && module.Compiler.BuiltinTypes.Object.MemberDefinition.DeclaringAssembly == candidate.MemberDefinition.DeclaringAssembly) { best_match = candidate; } string location; if (best_match.MemberDefinition is MemberCore) { location = ((MemberCore)best_match.MemberDefinition).Location.Name; } else { var assembly = (ImportedAssemblyDefinition)best_match.MemberDefinition.DeclaringAssembly; location = Path.GetFileName(assembly.Location); } module.Compiler.Report.SymbolRelatedToPreviousError(other_match); module.Compiler.Report.SymbolRelatedToPreviousError(candidate); module.Compiler.Report.Warning(1685, 1, "The predefined type `{0}.{1}' is defined multiple times. Using definition from `{2}'", ns, name, location); break; } if (best_match == null && reportErrors) { Location loc; if (found[0].MemberDefinition is MemberCore) { loc = ((MemberCore)found[0].MemberDefinition).Location; } else { loc = Location.Null; module.Compiler.Report.SymbolRelatedToPreviousError(found[0]); } module.Compiler.Report.Error(520, loc, "The predefined type `{0}.{1}' is not declared correctly", ns, name); } return(best_match); }
protected override Expression DoResolve(ResolveContext ec) { Expression expr_resolved = expr.Resolve(ec, ResolveFlags.VariableOrValue | ResolveFlags.Type); if (expr_resolved == null) { return(null); } TypeSpec expr_type = expr_resolved.Type; if (expr_type.IsPointer || expr_type.Kind == MemberKind.Void || expr_type == InternalType.NullLiteral || expr_type == InternalType.AnonymousMethod) { Unary.Error_OperatorCannotBeApplied(ec, loc, ".", expr_type); return(null); } if (targs != null) { if (!targs.Resolve(ec)) { return(null); } } var results = new List <string> (); if (expr_resolved is Namespace) { Namespace nexpr = expr_resolved as Namespace; string namespaced_partial; if (partial_name == null) { namespaced_partial = nexpr.Name; } else { namespaced_partial = nexpr.Name + "." + partial_name; } #if false Console.WriteLine("Workign with: namespaced partial {0}", namespaced_partial); foreach (var x in ec.TypeContainer.NamespaceEntry.CompletionGetTypesStartingWith(ec.TypeContainer, namespaced_partial)) { Console.WriteLine(" {0}", x); } #endif CompletionSimpleName.AppendResults( results, partial_name, ec.CurrentMemberDefinition.Parent.NamespaceEntry.CompletionGetTypesStartingWith(namespaced_partial)); } 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, results.Distinct().ToArray()); }
/// /// Does extension methods look up to find a method which matches name and extensionType. /// Search starts from this namespace and continues hierarchically up to top level. /// public ExtensionMethodGroupExpr LookupExtensionMethod(Type extensionType, ClassOrStruct currentClass, string name, Location loc) { ArrayList candidates = null; if (currentClass != null) { 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); if (a == null) { continue; } if (candidates == null) { candidates = a; } else { candidates.AddRange(a); } } if (candidates != null) { return(new ExtensionMethodGroupExpr(candidates, parent, extensionType, loc)); } 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)); }
TypeExpr CreateSiteType(EmitContext ec, Arguments arguments, int dyn_args_count, bool is_statement) { int default_args = is_statement ? 1 : 2; var module = ec.Module; bool has_ref_out_argument = false; var targs = new TypeExpression[dyn_args_count + default_args]; targs [0] = new TypeExpression(module.PredefinedTypes.CallSite.TypeSpec, loc); for (int i = 0; i < dyn_args_count; ++i) { Argument a = arguments [i]; if (a.ArgType == Argument.AType.Out || a.ArgType == Argument.AType.Ref) { has_ref_out_argument = true; } var t = a.Type; // Convert any internal type like dynamic or null to object if (t.Kind == MemberKind.InternalCompilerType) { t = TypeManager.object_type; } targs [i + 1] = new TypeExpression(t, loc); } TypeExpr del_type = null; if (!has_ref_out_argument) { string d_name = is_statement ? "Action" : "Func"; TypeExpr te = null; Namespace type_ns = module.GlobalRootNamespace.GetNamespace("System", true); if (type_ns != null) { te = type_ns.LookupType(module, d_name, dyn_args_count + default_args, true, Location.Null); } if (te != null) { if (!is_statement) { targs [targs.Length - 1] = new TypeExpression(type, loc); } del_type = new GenericTypeExpr(te.Type, new TypeArguments(targs), loc); } } // // Create custom delegate when no appropriate predefined one is found // if (del_type == null) { TypeSpec rt = is_statement ? TypeManager.void_type : type; Parameter[] p = new Parameter [dyn_args_count + 1]; p[0] = new Parameter(targs [0], "p0", Parameter.Modifier.NONE, null, loc); var site = ec.CreateDynamicSite(); int index = site.Types == null ? 0 : site.Types.Count; if (site.Mutator != null) { rt = site.Mutator.Mutate(rt); } for (int i = 1; i < dyn_args_count + 1; ++i) { var t = targs[i]; if (site.Mutator != null) { t.Type = site.Mutator.Mutate(t.Type); } p[i] = new Parameter(t, "p" + i.ToString("X"), arguments[i - 1].Modifier, null, loc); } Delegate d = new Delegate(site.NamespaceEntry, site, new TypeExpression(rt, loc), Modifiers.INTERNAL | Modifiers.COMPILER_GENERATED, new MemberName("Container" + index.ToString("X")), new ParametersCompiled(p), null); d.CreateType(); d.DefineType(); d.Define(); d.Emit(); var inflated = site.AddDelegate(d); del_type = new TypeExpression(inflated, loc); } TypeExpr site_type = new GenericTypeExpr(module.PredefinedTypes.CallSiteGeneric.TypeSpec, new TypeArguments(del_type), loc); return(site_type); }
TypeSpec FindDocumentedTypeNonArray(MemberCore mc, string identifier, DeclSpace ds, string cref) { var types = module.Compiler.BuiltinTypes; switch (identifier) { case "int": return(types.Int); case "uint": return(types.UInt); case "short": return(types.Short); case "ushort": return(types.UShort); case "long": return(types.Long); case "ulong": return(types.ULong); case "float": return(types.Float); case "double": return(types.Double); case "char": return(types.Char); case "decimal": return(types.Decimal); case "byte": return(types.Byte); case "sbyte": return(types.SByte); case "object": return(types.Object); case "bool": return(types.Bool); case "string": return(types.String); case "void": return(types.Void); } FullNamedExpression e = ds.LookupNamespaceOrType(identifier, 0, mc.Location, false); if (e != null) { if (!(e is TypeExpr)) { return(null); } return(e.Type); } int index = identifier.LastIndexOf('.'); if (index < 0) { return(null); } var nsName = identifier.Substring(0, index); var typeName = identifier.Substring(index + 1); Namespace ns = ds.NamespaceEntry.NS.GetNamespace(nsName, false); ns = ns ?? mc.Module.GlobalRootNamespace.GetNamespace(nsName, false); if (ns != null) { var te = ns.LookupType(mc, typeName, 0, true, mc.Location); if (te != null) { return(te.Type); } } int warn; TypeSpec parent = FindDocumentedType(mc, identifier.Substring(0, index), ds, cref); if (parent == null) { return(null); } // no need to detect warning 419 here var ts = FindDocumentedMember(mc, parent, identifier.Substring(index + 1), null, ds, out warn, cref, false, null) as TypeSpec; if (ts != null) { return(ts); } return(null); }
void ImportTypes (Type[] types, Namespace targetNamespace, Type extension_type) { Namespace ns = targetNamespace; string prev_namespace = null; foreach (var t in types) { if (t == null) continue; // Be careful not to trigger full parent type loading if (t.MemberType == MemberTypes.NestedType) continue; if (t.Name[0] == '<') continue; var it = CreateType (t, null, t, 0, true); if (it == null) continue; if (prev_namespace != t.Namespace) { ns = t.Namespace == null ? targetNamespace : targetNamespace.GetNamespace (t.Namespace, true); prev_namespace = t.Namespace; } ns.AddType (it); if (it.IsStatic && extension_type != null && t.IsDefined (extension_type, false)) { it.SetExtensionMethodContainer (); } } }
protected override Expression DoResolve(ResolveContext rc) { var sn = expr as SimpleName; const ResolveFlags flags = ResolveFlags.VariableOrValue | ResolveFlags.Type; if (sn != null) { expr = sn.LookupNameExpression(rc, MemberLookupRestrictions.ReadAccess | MemberLookupRestrictions.ExactArity); // // Resolve expression which does have type set as we need expression type // with disable flow analysis as we don't know whether left side expression // is used as variable or type // if (expr is VariableReference || expr is ConstantExpr || expr is Linq.TransparentMemberAccess) { expr = expr.Resolve(rc); } else if (expr is TypeParameterExpr) { expr.Error_UnexpectedKind(rc, flags, sn.Location); expr = null; } } else { expr = expr.Resolve(rc, flags); } if (expr == null) { return(null); } TypeSpec expr_type = expr.Type; if (expr_type.IsPointer || expr_type.Kind == MemberKind.Void || expr_type == InternalType.NullLiteral || expr_type == InternalType.AnonymousMethod) { expr.Error_OperatorCannotBeApplied(rc, loc, ".", expr_type); return(null); } if (targs != null) { if (!targs.Resolve(rc)) { return(null); } } var results = new List <string> (); if (expr is Namespace) { Namespace nexpr = expr as Namespace; string namespaced_partial; if (partial_name == null) { namespaced_partial = nexpr.Name; } else { namespaced_partial = nexpr.Name + "." + partial_name; } rc.CurrentMemberDefinition.GetCompletionStartingWith(namespaced_partial, results); if (partial_name != null) { results = results.Select(l => l.Substring(partial_name.Length)).ToList(); } } else { var r = MemberCache.GetCompletitionMembers(rc, expr_type, partial_name).Select(l => l.Name); AppendResults(results, partial_name, r); } throw new CompletionResult(partial_name == null ? "" : partial_name, results.Distinct().ToArray()); }
/// <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() { if (name == null) throw new ArgumentNullException ("name"); this.parent = parent; string pname = parent != null ? parent.fullname : null; if (pname == null) fullname = name; else fullname = pname + "." + name; while (parent.parent != null) parent = parent.parent; var root = parent as RootNamespace; if (root == null) throw new InternalErrorException ("Root namespaces must be created using RootNamespace"); root.RegisterNamespace (this); }
private NamespaceEntry(ModuleContainer ctx, NamespaceEntry parent, CompilationUnit file, Namespace ns, bool slave) { this.ctx = ctx; this.parent = parent; this.file = file; this.IsImplicit = true; this.ns = ns; this.SlaveDeclSpace = slave ? new RootDeclSpace(ctx, this) : null; }
public bool TryGetNamespace(string name, out Namespace ns) { return namespaces.TryGetValue (name, out ns); }
FullNamedExpression Lookup(string name, int arity, LookupMode mode, Location loc) { // // Check whether it's in the namespace. // FullNamedExpression fne = ns.LookupTypeOrNamespace(this, name, arity, mode, loc); // // Check aliases. // if (aliases != null && arity == 0) { UsingAliasNamespace uan; if (aliases.TryGetValue(name, out uan)) { if (fne != null) { // TODO: Namespace has broken location //Report.SymbolRelatedToPreviousError (fne.Location, null); Compiler.Report.SymbolRelatedToPreviousError(uan.Location, null); Compiler.Report.Error(576, loc, "Namespace `{0}' contains a definition with same name as alias `{1}'", GetSignatureForError(), name); } return(uan.ResolvedExpression); } } if (fne != null) { return(fne); } if (IsImplicit) { return(null); } // // Check using entries. // FullNamedExpression match = null; foreach (Namespace using_ns in namespace_using_table) { // // 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) { continue; } if (match == null) { match = fne; continue; } // Prefer types over namespaces var texpr_fne = fne as TypeExpr; var texpr_match = match as TypeExpr; if (texpr_fne != null && texpr_match == null) { match = fne; continue; } else if (texpr_fne == null) { continue; } // It can be top level accessibility only var better = Namespace.IsImportedTypeOverride(module, texpr_match.Type, texpr_fne.Type); if (better == null) { if (mode == LookupMode.Normal) { Compiler.Report.SymbolRelatedToPreviousError(texpr_match.Type); Compiler.Report.SymbolRelatedToPreviousError(texpr_fne.Type); Compiler.Report.Error(104, loc, "`{0}' is an ambiguous reference between `{1}' and `{2}'", name, texpr_match.GetSignatureForError(), texpr_fne.GetSignatureForError()); } return(match); } if (better == texpr_fne.Type) { match = texpr_fne; } } return(match); }
public NamespaceEntry (ModuleContainer module, NamespaceEntry parent, CompilationSourceFile sourceFile, string name) { this.module = module; this.parent = parent; this.file = sourceFile; if (parent != null) ns = parent.NS.GetNamespace (name, true); else if (name != null) ns = module.GlobalRootNamespace.GetNamespace (name, true); else ns = module.GlobalRootNamespace; SlaveDeclSpace = new RootDeclSpace (module, this); }
public void Define() { if (resolved) { return; } // FIXME: Because we call Define from bottom not top if (parent != null) { parent.Define(); } namespace_using_table = empty_namespaces; resolved = true; if (clauses != null) { var list = new List <Namespace> (clauses.Count); bool post_process_using_aliases = false; for (int i = 0; i < clauses.Count; ++i) { var entry = clauses[i]; if (entry.Alias != null) { if (aliases == null) { aliases = new Dictionary <string, UsingAliasNamespace> (); } // // Aliases are not available when resolving using section // except extern aliases // if (entry is UsingExternAlias) { entry.Define(this); if (entry.ResolvedExpression != null) { aliases.Add(entry.Alias.Name, (UsingExternAlias)entry); } clauses.RemoveAt(i--); } else { post_process_using_aliases = true; } continue; } entry.Define(this); Namespace using_ns = entry.ResolvedExpression as Namespace; if (using_ns == null) { continue; } if (list.Contains(using_ns)) { 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); } } namespace_using_table = list.ToArray(); if (post_process_using_aliases) { for (int i = 0; i < clauses.Count; ++i) { var entry = clauses[i]; if (entry.Alias != null) { entry.Define(this); if (entry.ResolvedExpression != null) { aliases.Add(entry.Alias.Name, (UsingAliasNamespace)entry); } clauses.RemoveAt(i--); } } } } }
// // Extension methods look up for dotted namespace names // public IList<MethodSpec> LookupExtensionMethod (IMemberContext invocationContext, TypeSpec extensionType, string name, int arity, out Namespace scope) { // // Inspect parent namespaces in namespace expression // scope = this; do { var candidates = scope.LookupExtensionMethod (invocationContext, extensionType, name, arity); if (candidates != null) return candidates; scope = scope.Parent; } while (scope != null); return null; }
// // Extension methods look up for dotted namespace names // public IList <MethodSpec> LookupExtensionMethod(IMemberContext invocationContext, TypeSpec extensionType, string name, int arity, out Namespace scope) { // // Inspect parent namespaces in namespace expression // scope = this; do { var candidates = scope.LookupExtensionMethod(invocationContext, extensionType, name, arity); if (candidates != null) { return(candidates); } scope = scope.Parent; } while (scope != null); return(null); }
private NamespaceEntry (ModuleContainer ctx, NamespaceEntry parent, CompilationUnit file, Namespace ns, bool slave) { this.ctx = ctx; this.parent = parent; this.file = file; this.IsImplicit = true; this.ns = ns; this.SlaveDeclSpace = slave ? new RootDeclSpace (ctx, this) : null; }
private FullNamedExpression Lookup(string name, int arity, Location loc, bool ignore_cs0104) { // // Check whether it's in the namespace. // FullNamedExpression fne = ns.Lookup(this, name, arity, loc); // // Check aliases. // if (using_aliases != null && arity == 0) { foreach (UsingAliasEntry ue in using_aliases) { if (ue.Alias == name) { if (fne != null) { if (Doppelganger != null) { // TODO: Namespace has broken location //Report.SymbolRelatedToPreviousError (fne.Location, null); Compiler.Report.SymbolRelatedToPreviousError(ue.Location, null); Compiler.Report.Error(576, loc, "Namespace `{0}' contains a definition with same name as alias `{1}'", GetSignatureForError(), name); } else { return(fne); } } return(ue.Resolve(Doppelganger ?? this, Doppelganger == null)); } } } if (fne != null) { if (!((fne.Type.Modifiers & Modifiers.INTERNAL) != 0 && !fne.Type.MemberDefinition.IsInternalAsPublic(module.DeclaringAssembly))) { return(fne); } } if (IsImplicit) { return(null); } // // Check using entries. // FullNamedExpression match = null; foreach (Namespace using_ns in GetUsingTable()) { // A using directive imports only types contained in the namespace, it // does not import any nested namespaces fne = using_ns.LookupType(this, name, arity, false, loc); if (fne == null) { continue; } if (match == null) { match = fne; continue; } // Prefer types over namespaces var texpr_fne = fne as TypeExpr; var texpr_match = match as TypeExpr; if (texpr_fne != null && texpr_match == null) { match = fne; continue; } else if (texpr_fne == null) { continue; } if (ignore_cs0104) { return(match); } // It can be top level accessibility only var better = Namespace.IsImportedTypeOverride(module, texpr_match.Type, texpr_fne.Type); if (better == null) { Compiler.Report.SymbolRelatedToPreviousError(texpr_match.Type); Compiler.Report.SymbolRelatedToPreviousError(texpr_fne.Type); Compiler.Report.Error(104, loc, "`{0}' is an ambiguous reference between `{1}' and `{2}'", name, texpr_match.GetSignatureForError(), texpr_fne.GetSignatureForError()); return(match); } if (better == texpr_fne.Type) { match = texpr_fne; } } return(match); }
private NamespaceContainer (ModuleContainer module, NamespaceContainer parent, CompilationSourceFile file, Namespace ns, bool slave) { this.module = module; this.parent = parent; this.file = file; this.IsImplicit = true; this.ns = ns; this.SlaveDeclSpace = slave ? new RootDeclSpace (module, this) : null; }
private NamespaceEntry(ModuleContainer module, NamespaceEntry parent, CompilationSourceFile file, Namespace ns, bool slave) { this.module = module; this.parent = parent; this.file = file; this.IsImplicit = true; this.ns = ns; this.SlaveDeclSpace = slave ? new RootDeclSpace(module, this) : null; }
public NamespaceEntry (NamespaceEntry parent, CompilationUnit file, string name) { this.parent = parent; this.file = file; entries.Add (this); if (parent != null) ns = parent.NS.GetNamespace (name, true); else if (name != null) ns = GlobalRootNamespace.Instance.GetNamespace (name, true); else ns = GlobalRootNamespace.Instance; SlaveDeclSpace = new RootDeclSpace (this); }
protected void EmitCall(EmitContext ec, Expression binder, Arguments arguments, bool isStatement) { // // This method generates all internal infrastructure for a dynamic call. The // reason why it's quite complicated is the mixture of dynamic and anonymous // methods. Dynamic itself requires a temporary class (ContainerX) and anonymous // methods can generate temporary storey as well (AnonStorey). Handling MVAR // type parameters rewrite is non-trivial in such case as there are various // combinations possible therefore the mutator is not straightforward. Secondly // we need to keep both MVAR(possibly VAR for anon storey) and type VAR to emit // correct Site field type and its access from EmitContext. // int dyn_args_count = arguments == null ? 0 : arguments.Count; int default_args = isStatement ? 1 : 2; var module = ec.Module; bool has_ref_out_argument = false; var targs = new TypeExpression[dyn_args_count + default_args]; targs[0] = new TypeExpression(module.PredefinedTypes.CallSite.TypeSpec, loc); TypeExpression[] targs_for_instance = null; TypeParameterMutator mutator; var site_container = ec.CreateDynamicSite(); if (context_mvars != null) { TypeParameters tparam; TypeContainer sc = site_container; do { tparam = sc.CurrentTypeParameters; sc = sc.Parent; } while (tparam == null); mutator = new TypeParameterMutator(context_mvars, tparam); if (!ec.IsAnonymousStoreyMutateRequired) { targs_for_instance = new TypeExpression[targs.Length]; targs_for_instance[0] = targs[0]; } } else { mutator = null; } for (int i = 0; i < dyn_args_count; ++i) { Argument a = arguments[i]; if (a.ArgType == Argument.AType.Out || a.ArgType == Argument.AType.Ref) { has_ref_out_argument = true; } var t = a.Type; // Convert any internal type like dynamic or null to object if (t.Kind == MemberKind.InternalCompilerType) { t = ec.BuiltinTypes.Object; } if (targs_for_instance != null) { targs_for_instance[i + 1] = new TypeExpression(t, loc); } if (mutator != null) { t = t.Mutate(mutator); } targs[i + 1] = new TypeExpression(t, loc); } TypeExpr del_type = null; TypeExpr del_type_instance_access = null; if (!has_ref_out_argument) { string d_name = isStatement ? "Action" : "Func"; TypeSpec te = null; Namespace type_ns = module.GlobalRootNamespace.GetNamespace("System", true); if (type_ns != null) { te = type_ns.LookupType(module, d_name, dyn_args_count + default_args, LookupMode.Normal, loc); } if (te != null) { if (!isStatement) { var t = type; if (t.Kind == MemberKind.InternalCompilerType) { t = ec.BuiltinTypes.Object; } if (targs_for_instance != null) { targs_for_instance[targs_for_instance.Length - 1] = new TypeExpression(t, loc); } if (mutator != null) { t = t.Mutate(mutator); } targs[targs.Length - 1] = new TypeExpression(t, loc); } del_type = new GenericTypeExpr(te, new TypeArguments(targs), loc); if (targs_for_instance != null) { del_type_instance_access = new GenericTypeExpr(te, new TypeArguments(targs_for_instance), loc); } else { del_type_instance_access = del_type; } } } // // Create custom delegate when no appropriate predefined delegate has been found // Delegate d; if (del_type == null) { TypeSpec rt = isStatement ? ec.BuiltinTypes.Void : type; Parameter[] p = new Parameter[dyn_args_count + 1]; p[0] = new Parameter(targs[0], "p0", Parameter.Modifier.NONE, null, loc); var site = ec.CreateDynamicSite(); int index = site.Containers == null ? 0 : site.Containers.Count; if (mutator != null) { rt = mutator.Mutate(rt); } for (int i = 1; i < dyn_args_count + 1; ++i) { p[i] = new Parameter(targs[i], "p" + i.ToString("X"), arguments[i - 1].Modifier, null, loc); } d = new Delegate(site, new TypeExpression(rt, loc), Modifiers.INTERNAL | Modifiers.COMPILER_GENERATED, new MemberName("Container" + index.ToString("X")), new ParametersCompiled(p), null); d.CreateContainer(); d.DefineContainer(); d.Define(); d.PrepareEmit(); site.AddTypeContainer(d); // // Add new container to inflated site container when the // member cache already exists // if (site.CurrentType is InflatedTypeSpec && index > 0) { site.CurrentType.MemberCache.AddMember(d.CurrentType); } del_type = new TypeExpression(d.CurrentType, loc); if (targs_for_instance != null) { del_type_instance_access = null; } else { del_type_instance_access = del_type; } } else { d = null; } var site_type_decl = new GenericTypeExpr(module.PredefinedTypes.CallSiteGeneric.TypeSpec, new TypeArguments(del_type), loc); var field = site_container.CreateCallSiteField(site_type_decl, loc); if (field == null) { return; } if (del_type_instance_access == null) { var dt = d.CurrentType.DeclaringType.MakeGenericType(module, context_mvars.Types); del_type_instance_access = new TypeExpression(MemberCache.GetMember(dt, d.CurrentType), loc); } var instanceAccessExprType = new GenericTypeExpr(module.PredefinedTypes.CallSiteGeneric.TypeSpec, new TypeArguments(del_type_instance_access), loc); if (instanceAccessExprType.ResolveAsType(ec.MemberContext) == null) { return; } bool inflate_using_mvar = context_mvars != null && ec.IsAnonymousStoreyMutateRequired; TypeSpec gt; if (inflate_using_mvar || context_mvars == null) { gt = site_container.CurrentType; } else { gt = site_container.CurrentType.MakeGenericType(module, context_mvars.Types); } // When site container already exists the inflated version has to be // updated manually to contain newly created field if (gt is InflatedTypeSpec && site_container.AnonymousMethodsCounter > 1) { var tparams = gt.MemberDefinition.TypeParametersCount > 0 ? gt.MemberDefinition.TypeParameters : TypeParameterSpec.EmptyTypes; var inflator = new TypeParameterInflator(module, gt, tparams, gt.TypeArguments); gt.MemberCache.AddMember(field.InflateMember(inflator)); } FieldExpr site_field_expr = new FieldExpr(MemberCache.GetMember(gt, field), loc); BlockContext bc = new BlockContext(ec.MemberContext, null, ec.BuiltinTypes.Void); Arguments args = new Arguments(1); args.Add(new Argument(binder)); StatementExpression s = new StatementExpression(new SimpleAssign(site_field_expr, new Invocation(new MemberAccess(instanceAccessExprType, "Create"), args))); using (ec.With(BuilderContext.Options.OmitDebugInfo, true)) { if (s.Resolve(bc)) { Statement init = new If(new Binary(Binary.Operator.Equality, site_field_expr, new NullLiteral(loc)), s, loc); init.Emit(ec); } args = new Arguments(1 + dyn_args_count); args.Add(new Argument(site_field_expr)); if (arguments != null) { int arg_pos = 1; foreach (Argument a in arguments) { if (a is NamedArgument) { // Name is not valid in this context args.Add(new Argument(a.Expr, a.ArgType)); } else { args.Add(a); } if (inflate_using_mvar && a.Type != targs[arg_pos].Type) { a.Expr.Type = targs[arg_pos].Type; } ++arg_pos; } } Expression target = new DelegateInvocation(new MemberAccess(site_field_expr, "Target", loc).Resolve(bc), args, false, loc).Resolve(bc); if (target != null) { target.Emit(ec); } } }
protected void ImportTypes (MetaType[] types, Namespace targetNamespace, bool importExtensionTypes) { Namespace ns = targetNamespace; string prev_namespace = null; foreach (var t in types) { if (t == null) continue; // Be careful not to trigger full parent type loading if (t.MemberType == MemberTypes.NestedType) continue; if (t.Name[0] == '<') continue; var it = CreateType (t, null, new DynamicTypeReader (t), true); if (it == null) continue; if (prev_namespace != t.Namespace) { ns = t.Namespace == null ? targetNamespace : targetNamespace.GetNamespace (t.Namespace, true); prev_namespace = t.Namespace; } // Cannot rely on assembly level Extension attribute or static modifier because they // are not followed by other compilers (e.g. F#). if (it.IsClass && it.Arity == 0 && importExtensionTypes && HasAttribute (CustomAttributeData.GetCustomAttributes (t), "ExtensionAttribute", CompilerServicesNamespace)) { it.SetExtensionMethodContainer (); } ns.AddType (module, it); } }
public static TypeSpec Resolve (ModuleContainer module, MemberKind kind, string ns, string name, int arity, bool required, bool reportErrors) { // // Cannot call it with true because it could create non-existent namespaces for // predefined types. It's set to true only for build-in types which all must // exist therefore it does not matter, for predefined types we don't want to create // fake namespaces when type is optional and does not exist (e.g. System.Linq). // Namespace type_ns = module.GlobalRootNamespace.GetNamespace (ns, required); IList<TypeSpec> found = null; if (type_ns != null) found = type_ns.GetAllTypes (name); if (found == null) { if (reportErrors) module.Compiler.Report.Error (518, "The predefined type `{0}.{1}' is not defined or imported", ns, name); return null; } TypeSpec best_match = null; foreach (var candidate in found) { if (candidate.Kind != kind) { if (candidate.Kind == MemberKind.Struct && kind == MemberKind.Void && candidate.MemberDefinition is TypeContainer) { // Void is declared as struct but we keep it internally as // special kind, the swap will be done by caller } else { continue; } } if (candidate.Arity != arity) continue; if ((candidate.Modifiers & Modifiers.INTERNAL) != 0 && !candidate.MemberDefinition.IsInternalAsPublic (module.DeclaringAssembly)) continue; if (best_match == null) { best_match = candidate; continue; } var other_match = best_match; if (!best_match.MemberDefinition.IsImported && module.Compiler.BuiltinTypes.Object.MemberDefinition.DeclaringAssembly == candidate.MemberDefinition.DeclaringAssembly) { best_match = candidate; } string location; if (best_match.MemberDefinition is MemberCore) { location = ((MemberCore) best_match.MemberDefinition).Location.Name; } else { var assembly = (ImportedAssemblyDefinition) best_match.MemberDefinition.DeclaringAssembly; location = Path.GetFileName (assembly.Location); } module.Compiler.Report.SymbolRelatedToPreviousError (other_match); module.Compiler.Report.SymbolRelatedToPreviousError (candidate); module.Compiler.Report.Warning (1685, 1, "The predefined type `{0}.{1}' is defined multiple times. Using definition from `{2}'", ns, name, location); break; } if (best_match == null && reportErrors) { var found_member = found[0]; if (found_member.Kind == MemberKind.MissingType) { // CSC: should be different error number module.Compiler.Report.Error (518, "The predefined type `{0}.{1}' is defined in an assembly that is not referenced.", ns, name); } else { Location loc; if (found_member.MemberDefinition is MemberCore) { loc = ((MemberCore) found_member.MemberDefinition).Location; } else { loc = Location.Null; module.Compiler.Report.SymbolRelatedToPreviousError (found_member); } module.Compiler.Report.Error (520, loc, "The predefined type `{0}.{1}' is not declared correctly", ns, name); } } return best_match; }
public void RegisterNamespace(Namespace child) { if (child != this) all_namespaces.Add (child.Name, child); }
// // Processes "see" or "seealso" elements. // Checks cref attribute. // private static void HandleXrefCommon(MemberCore mc, DeclSpace ds, XmlElement xref, Report Report) { string cref = xref.GetAttribute("cref").Trim(wsChars); // when, XmlReader, "if (cref == null)" if (!xref.HasAttribute("cref")) { return; } if (cref.Length == 0) { Report.Warning(1001, 1, mc.Location, "Identifier expected"); } // ... and continue until CS1584. else { // Additional symbols for < and > are allowed for easier XML typing cref = cref.Replace('{', '<').Replace('}', '>'); } string signature; // "x:" are stripped string name; // method invokation "(...)" are removed string parameters; // method parameter list // When it found '?:' ('T:' 'M:' 'F:' 'P:' 'E:' etc.), // MS ignores not only its member kind, but also // the entire syntax correctness. Nor it also does // type fullname resolution i.e. "T:List(int)" is kept // as T:List(int), not // T:System.Collections.Generic.List<System.Int32> if (cref.Length > 2 && cref [1] == ':') { return; } else { signature = cref; } // Also note that without "T:" any generic type // indication fails. int parens_pos = signature.IndexOf('('); int brace_pos = parens_pos >= 0 ? -1 : signature.IndexOf('['); if (parens_pos > 0 && signature [signature.Length - 1] == ')') { name = signature.Substring(0, parens_pos).Trim(wsChars); parameters = signature.Substring(parens_pos + 1, signature.Length - parens_pos - 2).Trim(wsChars); } else if (brace_pos > 0 && signature [signature.Length - 1] == ']') { name = signature.Substring(0, brace_pos).Trim(wsChars); parameters = signature.Substring(brace_pos + 1, signature.Length - brace_pos - 2).Trim(wsChars); } else { name = signature; parameters = null; } Normalize(mc, ref name, Report); string identifier = GetBodyIdentifierFromName(name); // Check if identifier is valid. // This check is not necessary to mark as error, but // csc specially reports CS1584 for wrong identifiers. string [] name_elems = identifier.Split('.'); for (int i = 0; i < name_elems.Length; i++) { string nameElem = GetBodyIdentifierFromName(name_elems [i]); if (i > 0) { Normalize(mc, ref nameElem, Report); } if (!Tokenizer.IsValidIdentifier(nameElem) && nameElem.IndexOf("operator") < 0) { Report.Warning(1584, 1, mc.Location, "XML comment on `{0}' has syntactically incorrect cref attribute `{1}'", mc.GetSignatureForError(), cref); xref.SetAttribute("cref", "!:" + signature); return; } } // check if parameters are valid Type [] parameter_types; if (parameters == null) { parameter_types = null; } else if (parameters.Length == 0) { parameter_types = Type.EmptyTypes; } else { string [] param_list = parameters.Split(','); ArrayList plist = new ArrayList(); for (int i = 0; i < param_list.Length; i++) { string param_type_name = param_list [i].Trim(wsChars); Normalize(mc, ref param_type_name, Report); Type param_type = FindDocumentedType(mc, param_type_name, ds, cref, Report); if (param_type == null) { Report.Warning(1580, 1, mc.Location, "Invalid type for parameter `{0}' in XML comment cref attribute `{1}'", (i + 1).ToString(), cref); return; } plist.Add(param_type); } parameter_types = plist.ToArray(typeof(Type)) as Type []; } Type type = FindDocumentedType(mc, name, ds, cref, Report); if (type != null // delegate must not be referenced with args && (!TypeManager.IsDelegateType(type) || parameter_types == null)) { string result = GetSignatureForDoc(type) + (brace_pos < 0 ? String.Empty : signature.Substring(brace_pos)); xref.SetAttribute("cref", "T:" + result); return; // a type } int period = name.LastIndexOf('.'); if (period > 0) { string typeName = name.Substring(0, period); string member_name = name.Substring(period + 1); Normalize(mc, ref member_name, Report); type = FindDocumentedType(mc, typeName, ds, cref, Report); int warn_result; if (type != null) { FoundMember fm = FindDocumentedMember(mc, type, member_name, parameter_types, ds, out warn_result, cref, true, name, Report); if (warn_result > 0) { return; } if (!fm.IsEmpty) { MemberInfo mi = fm.Member; // we cannot use 'type' directly // to get its name, since mi // could be from DeclaringType // for nested types. xref.SetAttribute("cref", GetMemberDocHead(mi.MemberType) + GetSignatureForDoc(fm.Type) + "." + member_name + GetParametersFormatted(mi)); return; // a member of a type } } } else { int warn_result; FoundMember fm = FindDocumentedMember(mc, ds.TypeBuilder, name, parameter_types, ds, out warn_result, cref, true, name, Report); if (warn_result > 0) { return; } if (!fm.IsEmpty) { MemberInfo mi = fm.Member; // we cannot use 'type' directly // to get its name, since mi // could be from DeclaringType // for nested types. xref.SetAttribute("cref", GetMemberDocHead(mi.MemberType) + GetSignatureForDoc(fm.Type) + "." + name + GetParametersFormatted(mi)); return; // local member name } } // It still might be part of namespace name. Namespace ns = ds.NamespaceEntry.NS.GetNamespace(name, false); if (ns != null) { xref.SetAttribute("cref", "N:" + ns.GetSignatureForError()); return; // a namespace } if (GlobalRootNamespace.Instance.IsNamespace(name)) { xref.SetAttribute("cref", "N:" + name); return; // a namespace } Report.Warning(1574, 1, mc.Location, "XML comment on `{0}' has cref attribute `{1}' that could not be resolved", mc.GetSignatureForError(), cref); xref.SetAttribute("cref", "!:" + name); }
// TODO: Replace with CreateNamespace where MemberName is created for the method call public Namespace GetNamespace(string name, bool create) { int pos = name.IndexOf ('.'); Namespace ns; string first; if (pos >= 0) first = name.Substring (0, pos); else first = name; if (!namespaces.TryGetValue (first, out ns)) { if (!create) return null; ns = new Namespace (this, first); namespaces.Add (first, ns); } if (pos >= 0) ns = ns.GetNamespace (name.Substring (pos + 1), create); return ns; }
public Namespace AddNamespace (MemberName name) { 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; }
Namespace TryAddNamespace(string name) { Namespace ns; if (!namespaces.TryGetValue (name, out ns)) { ns = new Namespace (this, name); namespaces.Add (name, ns); } return ns; }
public NamespaceContainer (MemberName name, NamespaceContainer parent) : base (parent, name, null, MemberKind.Namespace) { this.Parent = parent; this.ns = parent.NS.AddNamespace (name); containers = new List<TypeContainer> (); var topParent = this; while (topParent.Parent != null) { topParent = topParent.Parent; } compSourceFile = topParent as CompilationSourceFile; }
protected NamespaceContainer(ModuleContainer parent) : base(parent, null, null, MemberKind.Namespace) { ns = parent.GlobalRootNamespace; containers = new List<TypeContainer> (2); }
private static TypeSpec FindDocumentedTypeNonArray(MemberCore mc, string identifier, DeclSpace ds, string cref, Report r) { switch (identifier) { case "int": return(TypeManager.int32_type); case "uint": return(TypeManager.uint32_type); case "short": return(TypeManager.short_type);; case "ushort": return(TypeManager.ushort_type); case "long": return(TypeManager.int64_type); case "ulong": return(TypeManager.uint64_type);; case "float": return(TypeManager.float_type);; case "double": return(TypeManager.double_type); case "char": return(TypeManager.char_type);; case "decimal": return(TypeManager.decimal_type);; case "byte": return(TypeManager.byte_type);; case "sbyte": return(TypeManager.sbyte_type);; case "object": return(TypeManager.object_type);; case "bool": return(TypeManager.bool_type);; case "string": return(TypeManager.string_type);; case "void": return(TypeManager.void_type);; } FullNamedExpression e = ds.LookupNamespaceOrType(identifier, 0, mc.Location, false); if (e != null) { if (!(e is TypeExpr)) { return(null); } return(e.Type); } int index = identifier.LastIndexOf('.'); if (index < 0) { return(null); } var nsName = identifier.Substring(0, index); var typeName = identifier.Substring(index + 1); Namespace ns = ds.NamespaceEntry.NS.GetNamespace(nsName, false); ns = ns ?? mc.Module.GlobalRootNamespace.GetNamespace(nsName, false); if (ns != null) { var te = ns.LookupType(mc.Compiler, typeName, 0, true, mc.Location); if (te != null) { return(te.Type); } } int warn; TypeSpec parent = FindDocumentedType(mc, identifier.Substring(0, index), ds, cref, r); if (parent == null) { return(null); } // no need to detect warning 419 here var ts = FindDocumentedMember(mc, parent, identifier.Substring(index + 1), null, ds, out warn, cref, false, null, r) as TypeSpec; if (ts != null) { return(ts); } return(null); }