public static AssociatedTypeDeclaration FromXElement(XElement elem) { var assocType = new AssociatedTypeDeclaration(); assocType.Name = NameAttribute(elem); var superClassElem = elem.Element("superclass"); if (superClassElem != null) { var superClassName = NameAttribute(superClassElem); if (superClassName != null) { assocType.SuperClass = TypeSpecParser.Parse(superClassName); } } var defaultDefn = elem.Attribute("defaulttype"); if (defaultDefn != null) { assocType.DefaultType = TypeSpecParser.Parse((string)defaultDefn); } if (elem.Element("conformingprotocols") != null) { var conforming = from conform in elem.Element("conformingprotocols").Elements() select TypeSpecParser.Parse(NameAttribute (conform)) as NamedTypeSpec; assocType.ConformingProtocols.AddRange(conforming); } return(assocType); }
public AssociatedTypeDeclaration AssociatedTypeDeclarationFromGenericWithFullPath(NamedTypeSpec named, TypeMapper typeMap) { AssociatedTypeDeclaration assoc = null; OwningProtocolAndAssociateTypeFromGenericWithFullPath(named, typeMap, out assoc); return(assoc); }
public static TypeDeclaration TypeFromXElement(XElement elem, ModuleDeclaration module, BaseDeclaration parent /* can be null */) { var decl = FromKind((string)elem.Attribute("kind")); bool isUnrooted = elem.Attribute("module") != null; decl.Module = module; decl.Parent = parent; if (isUnrooted) { decl.IsUnrooted = true; decl.fullUnrootedName = (string)elem.Attribute("name"); decl.unrootedName = decl.fullUnrootedName.NameWithoutModule(); decl.Name = decl.fullUnrootedName.Contains('.') ? decl.fullUnrootedName.Substring(decl.fullUnrootedName.LastIndexOf('.') + 1) : decl.fullUnrootedName; } else { decl.Name = (string)elem.Attribute("name"); } decl.Access = AccessibilityFromString((string)elem.Attribute("accessibility")); decl.IsObjC = elem.BoolAttribute("isObjC"); decl.IsFinal = elem.BoolAttribute("isFinal"); decl.IsDeprecated = elem.BoolAttribute("isDeprecated"); decl.IsUnavailable = elem.BoolAttribute("isUnavailable"); decl.InnerClasses.AddRange(InnerFoo <ClassDeclaration> (elem, "innerclasses", module, decl)); decl.InnerStructs.AddRange(InnerFoo <StructDeclaration> (elem, "innerstructs", module, decl)); decl.InnerEnums.AddRange(InnerFoo <EnumDeclaration> (elem, "innerenums", module, decl)); if (elem.Element("members") != null) { var members = from mem in elem.Element("members").Elements() select Member.FromXElement(mem, module, decl) as Member; decl.Members.AddRange(members); } if (elem.Element("inherits") != null) { var inherits = from inherit in elem.Element("inherits").Elements() select SwiftReflector.SwiftXmlReflection.Inheritance.FromXElement(inherit) as Inheritance; decl.Inheritance.AddRange(inherits); } EnumDeclaration edecl = decl as EnumDeclaration; if (edecl != null) { var enumElements = (from enumElement in elem.Element("elements").Elements() select new EnumElement((string)enumElement.Attribute("name"), (string)enumElement.Attribute("type"), (long?)enumElement.Attribute("intValue"))).ToList();; edecl.Elements.AddRange(enumElements); if (elem.Attribute("rawType") != null) { edecl.RawTypeName = (string)elem.Attribute("rawType"); } } var protoDecl = decl as ProtocolDeclaration; if (protoDecl != null) { if (elem.Element("associatedtypes") != null) { var assocElements = from assocElem in elem.Element("associatedtypes").Elements() select AssociatedTypeDeclaration.FromXElement(assocElem); protoDecl.AssociatedTypes.AddRange(assocElements); } } return(decl); }
ProtocolDeclaration OwningProtocolAndAssociateTypeFromGenericWithFullPath(NamedTypeSpec named, TypeMapper typeMap, out AssociatedTypeDeclaration assoc) { assoc = null; if (named.Name.Contains(".")) { var parts = named.Name.Split('.'); if (IsTypeSpecGeneric(parts [0])) { // I make assertions about why things can't happen. Here's why: // If we have func foo<T:SomeProtocol>(a:T b:T.Foo) // it means that if the T part of T.Foo is a generic, then T HAS to be // constrained to a protocol with associated types // If T.Foo is a path to an associated type, then it var depthIndex = GetGenericDepthAndIndex(parts [0]); if (depthIndex.Item1 < 0 || depthIndex.Item2 < 0) { return(null); } var genDecl = GetGeneric(depthIndex.Item1, depthIndex.Item2); if (genDecl.Constraints.Count != 1) // pretty sure this can't ever happen { return(null); } var inh = genDecl.Constraints [0] as InheritanceConstraint; if (inh == null) // pretty sure this also can't ever happen { return(null); } var entity = typeMap.GetEntityForTypeSpec(inh.InheritsTypeSpec); if (entity == null) // Also can't happen { return(null); } if (entity.EntityType != EntityType.Protocol) { return(null); // Also can't happen } var protocol = entity.Type as ProtocolDeclaration; if (protocol != null && protocol.HasAssociatedTypes && (assoc = protocol.AssociatedTypeNamed(parts [1])) != null) { return(protocol); } } } return(null); }