public static FunctionDeclaration FuncFromXElement(XElement elem, ModuleDeclaration module, BaseDeclaration parent) { FunctionDeclaration decl = new FunctionDeclaration { Name = (string)elem.Attribute("name"), Module = module, Parent = parent, Access = TypeDeclaration.AccessibilityFromString((string)elem.Attribute("accessibility")), ReturnTypeName = Ex.ThrowOnNull((string)elem.Attribute("returnType"), "returnType"), IsProperty = elem.BoolAttribute("isProperty"), IsStatic = elem.BoolAttribute("isStatic"), IsFinal = elem.BoolAttribute("isFinal"), OperatorType = OperatorTypeFromElement((string)elem.Attribute("operatorKind")), HasThrows = elem.BoolAttribute("hasThrows"), IsDeprecated = elem.BoolAttribute("isDeprecated"), IsUnavailable = elem.BoolAttribute("isUnavailable"), IsOptional = elem.BoolAttribute("isOptional"), ObjCSelector = (string)elem.Attribute("objcSelector"), IsRequired = elem.BoolAttribute("isRequired"), IsConvenienceInit = elem.BoolAttribute("isConvenienceInit") }; decl.ParameterLists.AddRange(ParameterItem.ParameterListListFromXElement(elem.Element("parameterlists"))); if (decl.IsProperty && (decl.IsSetter || decl.IsSubscriptSetter)) { decl.ParameterLists [decl.ParameterLists.Count - 1] = MassageLastPropertySetterParameterList(decl.ParameterLists.Last()); } return(decl); }
public static List <ParameterItem> ParameterListFromXElement(XElement elem) { var indexed = from pelem in elem.Elements("parameter") orderby(int) pelem.Attribute("index") select ParameterItem.FromXElement(pelem); return(indexed.ToList()); }
public ParameterItem(ParameterItem pi) { PublicName = pi.PublicName; PrivateName = pi.PrivateName; TypeName = pi.TypeName; IsInOut = pi.IsInOut; IsVariadic = pi.IsVariadic; }
public bool EqualsIgnoreNamesPartialMatch(ParameterItem other) { if (other == null) { return(false); } return(this.TypeSpec.EqualsPartialMatch(other.TypeSpec)); }
public bool EqualsIgnoreName(ParameterItem other) { if (other == null) { return(false); } return(IsVariadic == other.IsVariadic && this.TypeSpec.Equals(other.TypeSpec)); }
public static bool AreEqualIgnoreNamesReferencesInvariant(FunctionDeclaration fn1, IList <ParameterItem> pl1, FunctionDeclaration fn2, IList <ParameterItem> pl2, bool matchPartialNames) { if (pl1.Count != pl2.Count) { return(false); } for (int i = 0; i < pl1.Count; i++) { ParameterItem p1 = RecastAsReference(pl1 [i]); ParameterItem p2 = RecastAsReference(pl2 [i]); // Names invariant means TYPE names not parameter names if (!ParameterNamesMatch(p1, p2)) { // we give a pass on matching "self". // this is done because "self" is a keyword in swift // and when matching a wrapper function, we can't call // a parameter "self" but have to call it "thisN" where // N is either an empty string or a number. // This is because there might be a real parameter named "this" // and we had to rename it. // The end result is that we can't use a "this" test, but we // can use a "self" test. var parmName1 = p1.NameIsRequired ? p1.PublicName : p1.PrivateName; var parmName2 = p2.NameIsRequired ? p2.PublicName : p2.PrivateName; if (parmName1 != "self" && parmName2 != "self") { return(false); } } if (fn1.IsTypeSpecGeneric(p1)) { if (!fn2.IsTypeSpecGeneric(p2)) { return(false); } continue; } if (!p1.EqualsIgnoreName(p2)) { if (matchPartialNames) { if (!p1.EqualsIgnoreNamesPartialMatch(p2)) { return(false); } } else { return(false); } } } return(true); }
static ParameterItem SubstituteSelfFromParent(FunctionDeclaration func, ParameterItem p) { if (func.Parent == null || !p.TypeSpec.HasDynamicSelf) { return(p); } p = new ParameterItem(p); p.TypeSpec = p.TypeSpec.ReplaceName("Self", func.Parent.ToFullyQualifiedNameWithGenerics()); return(p); }
static bool ParameterNamesMatch(ParameterItem p1, ParameterItem p2) { // parameters are considered matching if and only if their public names match. // The following are all DISTINCT // public func foo (a b: Int) { } // public func foo (c b: Int) { } // public func foo (b: Int) { } - the public name is b // public func foo (_ b: Int) { } - the public name is null or empty return(p1.PublicName == p2.PublicName); }
public static ParameterItem FromXElement(XElement elem) { ParameterItem pi = new ParameterItem { PublicName = (string)elem.Attribute("publicName"), PrivateName = (string)elem.Attribute("privateName"), TypeName = (string)elem.Attribute("type"), IsVariadic = elem.BoolAttribute("isVariadic"), }; pi.IsInOut = pi.TypeSpec.IsInOut; return(pi); }
static FunctionDeclaration GetAndRemoveSetter(List <FunctionDeclaration> decls, FunctionDeclaration other) { var plToMatch = new List <ParameterItem> (); var selfToMatch = other.Parent; if (other.IsGetter) { // getter - // The arguments to a getter are // arg1, arg2 ... argn // We want to match the type of the return of the getter to the value of the setter // as well as the parameters List <ParameterItem> pl = other.ParameterLists.Last(); ParameterItem item = new ParameterItem(); item.PublicName = ""; item.PrivateName = "retval"; item.TypeSpec = other.ReturnTypeSpec; item.TypeName = other.ReturnTypeName; plToMatch.Add(item); plToMatch.AddRange(pl); } else { // we don't have enough information to match on setter // and since we don't use the materializer, NBD. // materializer. // The arguments to a materializer are // buffer, callbackStoragebuffer, arg1, arg2 ... argn // We have no return to match. Oops. return(null); } for (int i = 0; i < decls.Count; i++) { FunctionDeclaration setter = decls [i]; if (!setter.IsSubscriptGetter) { continue; } List <ParameterItem> targetPl = setter.ParameterLists.Last(); if (ParmsMatch(plToMatch, targetPl)) { decls.RemoveAt(i); return(setter); } } return(null); }
static bool SubscriptParametersMatch(FunctionDeclaration getter, FunctionDeclaration setter) { if (getter.ParameterLists.Count != 2 || setter.ParameterLists.Count != 2) { return(false); } TypeSpec returnType = getter.ReturnTypeSpec; if (getter.ParameterLists [1].Count != setter.ParameterLists [1].Count - 1) { return(false); } if (!returnType.Equals(setter.ParameterLists [1] [0].TypeSpec)) { return(false); } return(ParameterItem.AreEqualIgnoreNamesReferencesInvariant(getter, getter.ParameterLists [1], setter, setter.ParameterLists [1].Skip(1).ToList(), true)); }
public bool MatchesSignature(FunctionDeclaration other, bool ignoreFirstParameterListIfPresent) { if (!TypeSpec.BothNullOrEqual(this.ReturnTypeSpec, other.ReturnTypeSpec)) { return(false); } if (this.ParameterLists.Count != other.ParameterLists.Count) { return(false); } int startIndex = ignoreFirstParameterListIfPresent && this.ParameterLists.Count > 1 ? 1 : 0; for (int i = startIndex; i < this.ParameterLists.Count; i++) { if (!ParameterItem.AreEqualIgnoreNamesReferencesInvariant(this, this.ParameterLists [i], other, other.ParameterLists [i], true)) { return(false); } } return(true); }
static ParameterItem RecastAsReference(ParameterItem p) { if (p.IsInOut) { if (!p.TypeSpec.IsInOut) { p.TypeSpec.IsInOut = true; } return(p); } if (p.TypeSpec is NamedTypeSpec && p.TypeSpec.ContainsGenericParameters) { NamedTypeSpec named = (NamedTypeSpec)p.TypeSpec; // special case - turn UnsafePointer<T> into inout T for matching purposes if (named.Name == "Swift.UnsafePointer" || named.Name == "Swift.UnsafeMutablePointer") { p = new ParameterItem(p); p.TypeSpec = p.TypeSpec.GenericParameters [0]; p.IsInOut = true; p.TypeSpec.IsInOut = true; } } return(p); }
public bool IsTypeSpecGeneric(ParameterItem item) { return(IsTypeSpecGeneric(item.TypeSpec)); }