public void AddTypeParameter(TypeParameter p) { p.Container = new Property(); p.Container.Container = this; if (TypeParameters == null) { TypeParameters = new List <TypeParameter>(); } TypeParameters.Add(p); }
protected static MemberName MakeMemberName (MemberBase host, string name, int unique_id, TypeParameter[] tparams, Location loc) { string host_name = host == null ? null : host is InterfaceMemberBase ? ((InterfaceMemberBase)host).GetFullName (host.MemberName) : host.Name; string tname = MakeName (host_name, "c", name, unique_id); TypeParameters args = null; if (tparams != null) { args = new TypeParameters (); foreach (TypeParameter tparam in tparams) args.Add (new TypeParameterName (tparam.Name, null, loc)); } return new MemberName (tname, args, loc); }
/// <summary> /// Initializes a type with its parent model and the XML node is represents. /// </summary> /// <param name="model">The parent model containing this type.</param> /// <param name="node">The XML node represented by this type.</param> /// <exception cref="FormatException">The name attribute has an invalid format.</exception> public Type(XmlDocumentationModel model, XmlNode node) : base(model, node) { // ReSharper disable once PossibleNullReferenceException var name = node.Attributes["name"].Value; if (!name.StartsWith("T:")) { throw new FormatException($"Bad type name format: {name}"); } name = name.Substring(2); if (name.StartsWith(Model.AssemblyName + ".")) { name = name.Substring(Model.AssemblyName.Length + 1); } var parts = name.Split(new[] { "." }, StringSplitOptions.None); Name = parts[parts.Length - 1]; var potentialContainingTypeName = $"T:{Model.AssemblyName}.{string.Join(".", parts.Take(parts.Length - 1))}"; var potentialContainingTypeId = potentialContainingTypeName; if (Model.Members.ContainsKey(potentialContainingTypeId)) { var containingType = (Type)Model.Members[potentialContainingTypeId]; NamespaceStrings = containingType.NamespaceStrings; containingType.NestedTypes.Add(this); ContainingType = containingType; } else { NamespaceStrings = parts.Take(parts.Length - 1).Select(p => Regex.Replace(p, "`(\\d+)", "")).ToArray(); } var summary = node.SelectSingleNode("summary"); if (summary != null) { Documentation = new Documentation(summary); } var paramTypes = node.SelectNodes("typeparam"); if (paramTypes != null) { foreach (XmlNode paramType in paramTypes) { TypeParameters.Add(new TypeParameter(paramType)); } } }
protected static MemberName MakeMemberName (MemberBase host, string name, int unique_id, TypeParameters tparams, Location loc) { string host_name = host == null ? null : host is InterfaceMemberBase ? ((InterfaceMemberBase)host).GetFullName (host.MemberName) : host.MemberName.Name; string tname = MakeName (host_name, "c", name, unique_id); TypeParameters args = null; if (tparams != null) { args = new TypeParameters (tparams.Count); // Type parameters will be filled later when we have TypeContainer // instance, for now we need only correct arity to create valid name for (int i = 0; i < tparams.Count; ++i) args.Add ((TypeParameter) null); } return new MemberName (tname, args, loc); }
private void GetParameters() { // get instance parameters List <Parameter> instanceParameters = new List <Parameter>(); foreach (Parameter parameter in NestedFamilyInstance.Parameters) { if (NestedFamilyInstance.Document.FamilyManager.CanElementParameterBeAssociated(parameter)) { instanceParameters.Add(parameter); } } instanceParameters.Sort((p1, p2) => string.Compare(p1.Definition.Name, p2.Definition.Name, StringComparison.Ordinal)); instanceParameters.ForEach(parameter => { InstanceParameters.Add(new NestedFamilyParameterModel(parameter, this, true, _mainViewModel)); }); // get type parameters List <Parameter> typeParameters = new List <Parameter>(); foreach (Parameter parameter in NestedFamilyInstance.Symbol.Parameters) { if (NestedFamilyInstance.Document.FamilyManager.CanElementParameterBeAssociated(parameter)) { typeParameters.Add(parameter); } } typeParameters.Sort((p1, p2) => string.Compare(p1.Definition.Name, p2.Definition.Name, StringComparison.Ordinal)); typeParameters.ForEach(parameter => { TypeParameters.Add(new NestedFamilyParameterModel(parameter, this, false, _mainViewModel)); }); }
// // Creates a host for the anonymous method // AnonymousMethodMethod DoCreateMethodHost (EmitContext ec) { // // Anonymous method body can be converted to // // 1, an instance method in current scope when only `this' is hoisted // 2, a static method in current scope when neither `this' nor any variable is hoisted // 3, an instance method in compiler generated storey when any hoisted variable exists // Modifiers modifiers; TypeDefinition parent = null; var src_block = Block.Original.Explicit; if (src_block.HasCapturedVariable || src_block.HasCapturedThis) { parent = storey = FindBestMethodStorey (); if (storey == null) { var sm = src_block.ParametersBlock.TopBlock.StateMachine; // // Remove hoisted this demand when simple instance method is enough (no hoisted variables only this) // if (src_block.HasCapturedThis && src_block.ParametersBlock.StateMachine == null) { src_block.ParametersBlock.TopBlock.RemoveThisReferenceFromChildrenBlock (src_block); // // Special case where parent class is used to emit instance method // because currect storey is of value type (async host) and we don't // want to create another childer storey to host this reference only // if (sm != null && sm.Kind == MemberKind.Struct) parent = sm.Parent.PartialContainer; } // // For iterators we can host everything in one class // if (sm is IteratorStorey) parent = storey = sm; } modifiers = storey != null ? Modifiers.INTERNAL : Modifiers.PRIVATE; } else { if (ec.CurrentAnonymousMethod != null) parent = storey = ec.CurrentAnonymousMethod.Storey; modifiers = Modifiers.STATIC | Modifiers.PRIVATE; } if (parent == null) parent = ec.CurrentTypeDefinition.Parent.PartialContainer; string name = CompilerGeneratedContainer.MakeName (parent != storey ? block_name : null, "m", null, ec.Module.CounterAnonymousMethods++); MemberName member_name; if (storey == null && ec.CurrentTypeParameters != null) { var hoisted_tparams = ec.CurrentTypeParameters; var type_params = new TypeParameters (hoisted_tparams.Count); for (int i = 0; i < hoisted_tparams.Count; ++i) { type_params.Add (hoisted_tparams[i].CreateHoistedCopy (null)); } member_name = new MemberName (name, type_params, Location); } else { member_name = new MemberName (name, Location); } return new AnonymousMethodMethod (parent, this, storey, new TypeExpression (ReturnType, Location), modifiers, member_name, parameters); }
// // Creates a proxy base method call inside this container for hoisted base member calls // public MethodSpec CreateHoistedBaseCallProxy (ResolveContext rc, MethodSpec method) { Method proxy_method; // // One proxy per base method is enough // if (hoisted_base_call_proxies == null) { hoisted_base_call_proxies = new Dictionary<MethodSpec, Method> (); proxy_method = null; } else { hoisted_base_call_proxies.TryGetValue (method, out proxy_method); } if (proxy_method == null) { string name = CompilerGeneratedContainer.MakeName (method.Name, null, "BaseCallProxy", hoisted_base_call_proxies.Count); MemberName member_name; TypeArguments targs = null; TypeSpec return_type = method.ReturnType; var local_param_types = method.Parameters.Types; if (method.IsGeneric) { // // Copy all base generic method type parameters info // var hoisted_tparams = method.GenericDefinition.TypeParameters; var tparams = new TypeParameters (); targs = new TypeArguments (); targs.Arguments = new TypeSpec[hoisted_tparams.Length]; for (int i = 0; i < hoisted_tparams.Length; ++i) { var tp = hoisted_tparams[i]; var local_tp = new TypeParameter (tp, null, new MemberName (tp.Name, Location), null); tparams.Add (local_tp); targs.Add (new SimpleName (tp.Name, Location)); targs.Arguments[i] = local_tp.Type; } member_name = new MemberName (name, tparams, Location); // // Mutate any method type parameters from original // to newly created hoisted version // var mutator = new TypeParameterMutator (hoisted_tparams, tparams); return_type = mutator.Mutate (return_type); local_param_types = mutator.Mutate (local_param_types); } else { member_name = new MemberName (name); } var base_parameters = new Parameter[method.Parameters.Count]; for (int i = 0; i < base_parameters.Length; ++i) { var base_param = method.Parameters.FixedParameters[i]; base_parameters[i] = new Parameter (new TypeExpression (local_param_types [i], Location), base_param.Name, base_param.ModFlags, null, Location); base_parameters[i].Resolve (this, i); } var cloned_params = ParametersCompiled.CreateFullyResolved (base_parameters, method.Parameters.Types); if (method.Parameters.HasArglist) { cloned_params.FixedParameters[0] = new Parameter (null, "__arglist", Parameter.Modifier.NONE, null, Location); cloned_params.Types[0] = Module.PredefinedTypes.RuntimeArgumentHandle.Resolve (); } // Compiler generated proxy proxy_method = new Method (this, new TypeExpression (return_type, Location), Modifiers.PRIVATE | Modifiers.COMPILER_GENERATED | Modifiers.DEBUGGER_HIDDEN, member_name, cloned_params, null); var block = new ToplevelBlock (Compiler, proxy_method.ParameterInfo, Location) { IsCompilerGenerated = true }; var mg = MethodGroupExpr.CreatePredefined (method, method.DeclaringType, Location); mg.InstanceExpression = new BaseThis (method.DeclaringType, Location); if (targs != null) mg.SetTypeArguments (rc, targs); // Get all the method parameters and pass them as arguments var real_base_call = new Invocation (mg, block.GetAllParametersArguments ()); Statement statement; if (method.ReturnType.Kind == MemberKind.Void) statement = new StatementExpression (real_base_call); else statement = new Return (real_base_call, Location); block.AddStatement (statement); proxy_method.Block = block; members.Add (proxy_method); proxy_method.Define (); proxy_method.PrepareEmit (); hoisted_base_call_proxies.Add (method, proxy_method); } return proxy_method.Spec; }
string[] CreateTypeParameters (TypeParameters parentAllTypeParameters) { string[] names; int parent_offset = 0; if (parentAllTypeParameters != null) { if (CurrentTypeParameters == null) { all_type_parameters = parentAllTypeParameters; return parentAllTypeParameters.GetAllNames (); } names = new string[parentAllTypeParameters.Count + CurrentTypeParameters.Count]; all_type_parameters = new TypeParameters (names.Length); all_type_parameters.Add (parentAllTypeParameters); parent_offset = all_type_parameters.Count; for (int i = 0; i < parent_offset; ++i) names[i] = all_type_parameters[i].MemberName.Name; } else { names = new string[CurrentTypeParameters.Count]; } for (int i = 0; i < CurrentTypeParameters.Count; ++i) { if (all_type_parameters != null) all_type_parameters.Add (MemberName.TypeParameters[i]); var name = CurrentTypeParameters[i].MemberName.Name; names[parent_offset + i] = name; for (int ii = 0; ii < parent_offset + i; ++ii) { if (names[ii] != name) continue; var tp = CurrentTypeParameters[i]; var conflict = all_type_parameters[ii]; tp.WarningParentNameConflict (conflict); } } if (all_type_parameters == null) all_type_parameters = CurrentTypeParameters; return names; }
// // Creates a host for the anonymous method // AnonymousMethodMethod DoCreateMethodHost (EmitContext ec) { // // Anonymous method body can be converted to // // 1, an instance method in current scope when only `this' is hoisted // 2, a static method in current scope when neither `this' nor any variable is hoisted // 3, an instance method in compiler generated storey when any hoisted variable exists // Modifiers modifiers; TypeDefinition parent = null; var src_block = Block.Original.Explicit; if (src_block.HasCapturedVariable || src_block.HasCapturedThis) { parent = storey = FindBestMethodStorey (); if (storey == null) { var top_block = src_block.ParametersBlock.TopBlock; var sm = top_block.StateMachine; if (src_block.HasCapturedThis) { // // Remove hoisted 'this' request when simple instance method is // enough. No hoisted variables only 'this' and don't need to // propagate this to value type state machine. // StateMachine sm_parent; var pb = src_block.ParametersBlock; do { sm_parent = pb.StateMachine; pb = pb.Parent == null ? null : pb.Parent.ParametersBlock; } while (sm_parent == null && pb != null); if (sm_parent == null) { top_block.RemoveThisReferenceFromChildrenBlock (src_block); } else if (sm_parent.Kind == MemberKind.Struct) { // // Special case where parent class is used to emit instance method // because currect storey is of value type (async host) and we cannot // use ldftn on non-boxed instances either to share mutated state // parent = sm_parent.Parent.PartialContainer; } else if (sm is IteratorStorey) { // // For iterators we can host everything in one class // parent = storey = sm; } } } modifiers = storey != null ? Modifiers.INTERNAL : Modifiers.PRIVATE; } else { if (ec.CurrentAnonymousMethod != null) parent = storey = ec.CurrentAnonymousMethod.Storey; modifiers = Modifiers.STATIC | Modifiers.PRIVATE; } if (parent == null) parent = ec.CurrentTypeDefinition.Parent.PartialContainer; string name = CompilerGeneratedContainer.MakeName (parent != storey ? block_name : null, "m", null, parent.PartialContainer.CounterAnonymousMethods++); MemberName member_name; if (storey == null && ec.CurrentTypeParameters != null) { var hoisted_tparams = ec.CurrentTypeParameters; var type_params = new TypeParameters (hoisted_tparams.Count); for (int i = 0; i < hoisted_tparams.Count; ++i) { type_params.Add (hoisted_tparams[i].CreateHoistedCopy (null)); } member_name = new MemberName (name, type_params, Location); } else { member_name = new MemberName (name, Location); } return new AnonymousMethodMethod (parent, this, storey, new TypeExpression (ReturnType, Location), modifiers, member_name, parameters); }
string[] CreateTypeParameters () { string[] names; int parent_offset = 0; var parent_all = Parent.all_type_parameters; if (parent_all != null) { if (CurrentTypeParameters == null) { all_type_parameters = Parent.all_type_parameters; return Parent.all_tp_builders.Select (l => l.Name).ToArray (); } names = new string[parent_all.Count + CurrentTypeParameters.Count]; all_type_parameters = new TypeParameters (names.Length); all_type_parameters.Add (Parent.all_type_parameters); parent_offset = all_type_parameters.Count; for (int i = 0; i < parent_offset; ++i) names[i] = all_type_parameters[i].MemberName.Name; } else { names = new string[CurrentTypeParameters.Count]; } for (int i = 0; i < CurrentTypeParameters.Count; ++i) { if (all_type_parameters != null) all_type_parameters.Add (MemberName.TypeParameters[i]); var name = CurrentTypeParameters[i].MemberName.Name; names[parent_offset + i] = name; for (int ii = 0; ii < parent_offset + i; ++ii) { if (names[ii] != name) continue; var tp = CurrentTypeParameters[i]; var conflict = all_type_parameters[ii]; tp.WarningParentNameConflict (conflict); } } if (all_type_parameters == null) all_type_parameters = CurrentTypeParameters; return names; }
void case_364() #line 2999 "cs-parser.jay" { var tparams = new TypeParameters (); tparams.Add ((TypeParameter)yyVals[0+yyTop]); yyVal = tparams; locationListStack.Push (new List<Location> ()); }
// // Creates a host for the anonymous method // AnonymousMethodMethod DoCreateMethodHost (EmitContext ec) { // // Anonymous method body can be converted to // // 1, an instance method in current scope when only `this' is hoisted // 2, a static method in current scope when neither `this' nor any variable is hoisted // 3, an instance method in compiler generated storey when any hoisted variable exists // Modifiers modifiers; var src_block = Block.Original.Explicit; if (src_block.HasCapturedVariable || src_block.HasCapturedThis) { storey = FindBestMethodStorey (); // // For iterators we can host everything in one class // if (storey == null && Block.TopBlock.StateMachine is IteratorStorey) storey = block.TopBlock.StateMachine; modifiers = storey != null ? Modifiers.INTERNAL : Modifiers.PRIVATE; } else { if (ec.CurrentAnonymousMethod != null) storey = ec.CurrentAnonymousMethod.Storey; modifiers = Modifiers.STATIC | Modifiers.PRIVATE; } var parent = storey != null ? storey : ec.CurrentTypeDefinition.Parent.PartialContainer; string name = CompilerGeneratedContainer.MakeName (parent != storey ? block_name : null, "m", null, ec.Module.CounterAnonymousMethods++); MemberName member_name; if (storey == null && ec.CurrentTypeParameters != null) { var hoisted_tparams = ec.CurrentTypeParameters; var type_params = new TypeParameters (hoisted_tparams.Count); for (int i = 0; i < hoisted_tparams.Count; ++i) { type_params.Add (hoisted_tparams[i].CreateHoistedCopy (null)); } member_name = new MemberName (name, type_params, Location); } else { member_name = new MemberName (name, Location); } return new AnonymousMethodMethod (parent, this, storey, new TypeExpression (ReturnType, Location), modifiers, member_name, parameters); }
public TypeDoc AddTypeParameter(TypeParameterDoc typeParameter) => WithTypeParameters(TypeParameters.Add(typeParameter));
void case_357() { var tparams = new TypeParameters (); tparams.Add ((TypeParameter)yyVals[0+yyTop]); yyVal = tparams; }
/// <summary> /// Initializes a method with its parent model and the XML node is represents. /// </summary> /// <param name="model">The parent model containing this method.</param> /// <param name="node">The XML node represented by this method.</param> /// <exception cref="FormatException">The name attribute has an invalid format.</exception> public Method(XmlDocumentationModel model, XmlNode node) : base(model, node) { // ReSharper disable once PossibleNullReferenceException var name = node.Attributes["name"].Value; if (!name.StartsWith("M:")) { throw new FormatException($"Bad method name format: {name}"); } name = name.Substring(2); if (!name.StartsWith(Model.AssemblyName + ".")) { throw new FormatException($"Bad method name format: {name}"); } name = name.Substring(Model.AssemblyName.Length + 1); var parentheseIdx = name.IndexOf("("); if (parentheseIdx > 0) { var parametersPart = name.Substring(parentheseIdx + 1).TrimEnd(')'); ParseParameters(parametersPart); name = name.Substring(0, parentheseIdx); } var parts = name.Split(new[] { "." }, StringSplitOptions.None); Name = parts[parts.Length - 1]; var containingTypeName = Model.AssemblyName + "." + string.Join(".", parts.Take(parts.Length - 1)); var containingTypeId = "T:" + containingTypeName; var containingType = (Type)Model.Members[containingTypeId]; ContainingType = containingType; var summary = node.SelectSingleNode("summary"); if (summary != null) { Documentation = new Documentation(summary); } var paramNodes = node.SelectNodes("param"); if (paramNodes != null) { var i = 0; foreach (XmlNode paramNode in paramNodes) { if (i < Parameters.Count) { Parameters[i].Documentation = new Documentation(paramNode); Parameters[i].Name = paramNode.Attributes?["name"]?.Value; } i++; } } var paramTypes = node.SelectNodes("typeparam"); if (paramTypes != null) { foreach (XmlNode paramType in paramTypes) { TypeParameters.Add(new TypeParameter(paramType)); } } var thrownExceptionNodes = node.SelectNodes("exception"); if (thrownExceptionNodes != null) { foreach (XmlNode thrownExceptionNode in thrownExceptionNodes) { ThrownExceptions.Add(new ThrownException(thrownExceptionNode)); } } }
public static AnonymousTypeClass Create (TypeContainer parent, IList<AnonymousTypeParameter> parameters, Location loc) { string name = ClassNamePrefix + parent.Module.CounterAnonymousTypes++; ParametersCompiled all_parameters; TypeParameters tparams = null; SimpleName[] t_args; if (parameters.Count == 0) { all_parameters = ParametersCompiled.EmptyReadOnlyParameters; t_args = null; } else { t_args = new SimpleName[parameters.Count]; tparams = new TypeParameters (); Parameter[] ctor_params = new Parameter[parameters.Count]; for (int i = 0; i < parameters.Count; ++i) { AnonymousTypeParameter p = parameters[i]; for (int ii = 0; ii < i; ++ii) { if (parameters[ii].Name == p.Name) { parent.Compiler.Report.Error (833, parameters[ii].Location, "`{0}': An anonymous type cannot have multiple properties with the same name", p.Name); p = new AnonymousTypeParameter (null, "$" + i.ToString (), p.Location); parameters[i] = p; break; } } t_args[i] = new SimpleName ("<" + p.Name + ">__T", p.Location); tparams.Add (new TypeParameter (i, new MemberName (t_args[i].Name, p.Location), null, null, Variance.None)); ctor_params[i] = new Parameter (t_args[i], p.Name, Parameter.Modifier.NONE, null, p.Location); } all_parameters = new ParametersCompiled (ctor_params); } // // Create generic anonymous type host with generic arguments // named upon properties names // AnonymousTypeClass a_type = new AnonymousTypeClass (parent.Module, new MemberName (name, tparams, loc), parameters, loc); Constructor c = new Constructor (a_type, name, Modifiers.PUBLIC | Modifiers.DEBUGGER_HIDDEN, null, all_parameters, loc); c.Block = new ToplevelBlock (parent.Module.Compiler, c.ParameterInfo, loc); // // Create fields and constructor body with field initialization // bool error = false; for (int i = 0; i < parameters.Count; ++i) { AnonymousTypeParameter p = parameters [i]; Field f = new Field (a_type, t_args [i], Modifiers.PRIVATE | Modifiers.READONLY | Modifiers.DEBUGGER_HIDDEN, new MemberName ("<" + p.Name + ">", p.Location), null); if (!a_type.AddField (f)) { error = true; continue; } c.Block.AddStatement (new StatementExpression ( new SimpleAssign (new MemberAccess (new This (p.Location), f.Name), c.Block.GetParameterReference (i, p.Location)))); ToplevelBlock get_block = new ToplevelBlock (parent.Module.Compiler, p.Location); get_block.AddStatement (new Return ( new MemberAccess (new This (p.Location), f.Name), p.Location)); Property prop = new Property (a_type, t_args [i], Modifiers.PUBLIC, new MemberName (p.Name, p.Location), null); prop.Get = new Property.GetMethod (prop, 0, null, p.Location); prop.Get.Block = get_block; a_type.AddMember (prop); } if (error) return null; a_type.AddConstructor (c); return a_type; }
// // Creates a host for the anonymous method // AnonymousMethodMethod DoCreateMethodHost (EmitContext ec) { // // Anonymous method body can be converted to // // 1, an instance method in current scope when only `this' is hoisted // 2, a static method in current scope when neither `this' nor any variable is hoisted // 3, an instance method in compiler generated storey when any hoisted variable exists // Modifiers modifiers; if (Block.HasCapturedVariable || Block.HasCapturedThis) { storey = FindBestMethodStorey (); modifiers = storey != null ? Modifiers.INTERNAL : Modifiers.PRIVATE; } else { if (ec.CurrentAnonymousMethod != null) storey = ec.CurrentAnonymousMethod.Storey; modifiers = Modifiers.STATIC | Modifiers.PRIVATE; } TypeContainer parent = storey != null ? storey : ec.CurrentTypeDefinition.Parent.PartialContainer; MemberCore mc = ec.MemberContext as MemberCore; string name = CompilerGeneratedClass.MakeName (parent != storey ? block_name : null, "m", null, unique_id++); MemberName member_name; if (storey == null && ec.CurrentTypeParameters != null) { var hoisted_tparams = ec.CurrentTypeParameters; var type_params = new TypeParameters (hoisted_tparams.Count); for (int i = 0; i < hoisted_tparams.Count; ++i) { type_params.Add (hoisted_tparams[i].CreateHoistedCopy (null)); } member_name = new MemberName (name, type_params, Location); } else { member_name = new MemberName (name, Location); } string real_name = String.Format ( "{0}~{1}{2}", mc.GetSignatureForError (), GetSignatureForError (), parameters.GetSignatureForError ()); return new AnonymousMethodMethod (parent, this, storey, new TypeExpression (ReturnType, Location), modifiers, real_name, member_name, parameters); }
void case_347() #line 2854 "cs-parser.jay" { var tparams = new TypeParameters (); tparams.Add ((TypeParameter)yyVals[0+yyTop]); yyVal = tparams; }