public override void Add(TLDefinition tld, Stream srcStm) { TLVariable vari = tld as TLVariable; if (vari != null) { VariableContents contents = GoGetIt(vari.Name); if (contents.Variable != null) { throw ErrorHelper.CreateError(ReflectorError.kInventoryBase + 4, $"duplicate variable {vari.Name.Name}."); } contents.Variable = vari; return; } TLFunction tlf = tld as TLFunction; if (tlf != null) { VariableContents contents = GoGetIt(tlf.Name); contents.Addressors.Add(tlf); return; } throw ErrorHelper.CreateError(ReflectorError.kInventoryBase + 5, $"expected a top-level function or top-level variable but got a {tld.GetType ().Name}"); }
public override void Add(TLDefinition tld, Stream srcStm) { TLFunction tlf = tld as TLFunction; if (tlf == null) { throw ErrorHelper.CreateError(ReflectorError.kInventoryBase + 7, $"Expected a TLFunction for a property but got a {tld.GetType ().Name}."); } SwiftPropertyType prop = tlf.Signature as SwiftPropertyType; if (prop == null) { throw ErrorHelper.CreateError(ReflectorError.kInventoryBase + 8, $"Expected a function of property type but got a {tlf.Signature.GetType ().Name}."); } PropertyContents contents = null; SwiftName nameToUse = prop.PrivateName ?? prop.Name; if (!values.TryGetValue(nameToUse, out contents)) { contents = new PropertyContents(tlf.Class, nameToUse, sizeofMachinePointer); values.Add(nameToUse, contents); } contents.Add(tlf, prop); }
public static SwiftClassName ToFormalClassName(TLDefinition tld) { TLClassElem elem = tld as TLClassElem; if (elem != null) { if (elem.Class == null) { throw ErrorHelper.CreateError(ReflectorError.kCantHappenBase + 2, $"Expected a top level definition to have a class name."); } return(elem.Class.ClassName); } if (tld is TLProtocolConformanceDescriptor protocolDesc) { if (protocolDesc.ImplementingType is SwiftClassType swiftClass) { return(swiftClass.ClassName); } if (protocolDesc.ImplementingType is SwiftBoundGenericType boundGen) { var baseType = boundGen.BaseType as SwiftClassType; if (baseType != null) { return(baseType.ClassName); } } else if (protocolDesc.ImplementingType is SwiftBuiltInType builtInType) { return(SwiftClassName.FromFullyQualifiedName($"Swift.{builtInType.BuiltInType}", OperatorType.None, "V")); } } throw ErrorHelper.CreateError(ReflectorError.kInventoryBase + 0, $"unknown top level definition {tld.GetType ().Name}"); }
public void Add(TLDefinition tld, Stream srcStm) { TLFunction tlf = tld as TLFunction; if (tlf != null) { if (tlf.Signature.IsExtension) { Extensions.Add(tlf, srcStm); } else if (tlf.IsTopLevelFunction) { if (tlf.Signature is SwiftAddressorType) { Variables.Add(tlf, srcStm); } else if (tlf.Signature is SwiftWitnessTableType) { WitnessTables.Add(tld, srcStm); } else { Functions.Add(tlf, srcStm); } } else { if (tlf.Class.EntityKind == MemberNesting.Protocol) { Protocols.Add(tlf, srcStm); } else { Classes.Add(tld, srcStm); } } } else { if (tld is TLVariable tlvar && ((TLVariable)tld).Class == null) { if (tlvar is TLPropertyDescriptor propDesc) { if (propDesc.ExtensionOn != null) { ExtensionDescriptors.Add(propDesc); } else { PropertyDescriptors.Add(tlvar, srcStm); } } else { Variables.Add(tld, srcStm); } }
public override void Add(TLDefinition tld, Stream srcStm) { ModuleContents module = null; if (!values.TryGetValue(tld.Module, out module)) { module = new ModuleContents(tld.Module, SizeofMachinePointer); values.Add(tld.Module, module); } module.Add(tld, srcStm); }
public override void Add(TLDefinition tld, Stream srcStm) { SwiftName className = ClassInventory.ToClassName(tld); SwiftClassName formalName = ClassInventory.ToFormalClassName(tld); ProtocolContents contents = null; if (!values.TryGetValue(className, out contents)) { contents = new ProtocolContents(formalName, sizeofMachinePointer); values.Add(className, contents); } contents.Add(tld, srcStm); }
public void Add(TLDefinition tld, Stream srcStm) { TLFunction tlf = tld as TLFunction; if (tlf == null) { throw ErrorHelper.CreateError(ReflectorError.kInventoryBase + 9, $"Expected a TLFunction for a witness table but got a {tld.GetType ().Name}."); } if (values.ContainsKey(tlf.MangledName)) { throw ErrorHelper.CreateError(ReflectorError.kInventoryBase + 10, $"Already received witness table entry for {tlf.MangledName}."); } values.Add(tlf.MangledName, tlf); LoadWitnessTable(tlf, srcStm); }
public override void Add(TLDefinition tld, Stream srcStm) { TLFunction tlf = tld as TLFunction; if (tlf == null) { throw ErrorHelper.CreateError(ReflectorError.kInventoryBase + 11, $"expected a top-level function but got a {tld.GetType ().Name}"); } if (Functions.Exists(f => tlf.MangledName == f.MangledName)) { throw ErrorHelper.CreateError(ReflectorError.kInventoryBase + 12, $"duplicate function found for {tlf.MangledName}"); } else { Functions.Add(tlf); } }
public override void Add(TLDefinition tld, Stream srcStm) { TLFunction tlf = tld as TLFunction; if (tlf == null) { throw ErrorHelper.CreateError(ReflectorError.kInventoryBase + 10, $"expected a top-level function but got a {tld.GetType ().Name}"); } OverloadInventory overloads = null; if (!values.TryGetValue(tlf.Name, out overloads)) { overloads = new OverloadInventory(tlf.Name, sizeofMachinePointer); values.Add(tlf.Name, overloads); } overloads.Add(tlf, srcStm); }
public override void Add(TLDefinition tld, Stream srcStm) { // ignoring these - we don't/can't use them if (tld is TLDefaultArgumentInitializer) { return; } SwiftName className = ToClassName(tld); SwiftClassName formalName = ToFormalClassName(tld); ClassContents contents = null; if (!values.TryGetValue(className, out contents)) { contents = new ClassContents(formalName, sizeofMachinePointer); values.Add(className, contents); } contents.Add(tld, srcStm); }
static void TLFunctionsForStream(Stream stm, PlatformName platform, Dictionary <string, string> functions) { List <NListEntry> entries = null; List <MachOFile> macho = MachO.Read(stm, null).ToList(); MachOFile file = macho [0]; List <SymTabLoadCommand> symbols = file.load_commands.OfType <SymTabLoadCommand> ().ToList(); NListEntryType nlet = symbols [0].nlist [0].EntryType; entries = symbols [0].nlist. Where((nle, i) => nle.IsPublic && nle.EntryType == NListEntryType.InSection).ToList(); bool isOldVersion = IsOldVersion(entries); foreach (var entry in entries) { if (!entry.IsSwiftEntryPoint()) { continue; } TLDefinition def = null; try { def = Decomposer.Decompose(entry.str, isOldVersion, Offset(entry)); } catch { } if (def != null) { // this skips over privatized names var tlf = def as TLFunction; if (tlf != null && tlf.Name != null && tlf.Name.Name.Contains("...")) { continue; } if (tlf?.Name != null) { if (!functions.ContainsKey(tlf.Name.Name)) { functions.Add(tlf.Name.Name, tlf.MangledName); } } } } }
public void Add(TLDefinition tld, Stream srcStm) { TLFunction tlf = tld as TLFunction; if (tlf != null) { if (IsConstructor(tlf.Signature, tlf.Class)) { Constructors.Add(tlf, srcStm); } else if (tlf.Signature is SwiftClassConstructorType) { if (ClassConstructor.Values.Count() == 0) { ClassConstructor.Add(tlf, srcStm); } else { throw ErrorHelper.CreateError(ReflectorError.kInventoryBase + 12, $"multiple type metadata accessors for {tlf.Class.ClassName.ToFullyQualifiedName ()}"); } } else if (IsDestructor(tlf.Signature, tlf.Class)) { Destructors.Add(tlf, srcStm); } else if (IsProperty(tlf.Signature, tlf.Class)) { if (IsSubscript(tlf.Signature, tlf.Class)) { if (IsPrivateProperty(tlf.Signature, tlf.Class)) { PrivateSubscripts.Add(tlf); } else { Subscripts.Add(tlf); } } else { if (IsStaticProperty(tlf.Signature, tlf.Class)) { if (IsPrivateProperty(tlf.Signature, tlf.Class)) { StaticPrivateProperties.Add(tlf, srcStm); } else { StaticProperties.Add(tlf, srcStm); } } else { if (IsPrivateProperty(tlf.Signature, tlf.Class)) { PrivateProperties.Add(tlf, srcStm); } else { Properties.Add(tlf, srcStm); } } } } else if (IsMethodOnClass(tlf.Signature, tlf.Class)) { if (tlf is TLMethodDescriptor) { MethodDescriptors.Add(tlf, srcStm); } else { Methods.Add(tlf, srcStm); } } else if (IsStaticMethod(tlf.Signature, tlf.Class)) { StaticFunctions.Add(tlf, srcStm); } else if (IsWitnessTable(tlf.Signature, tlf.Class)) { WitnessTable.Add(tlf, srcStm); } else if (IsInitializer(tlf.Signature, tlf.Class)) { Initializers.Add(tlf, srcStm); } else { FunctionsOfUnknownDestination.Add(tlf); } return; } var meta = tld as TLDirectMetadata; if (meta != null) { if (DirectMetadata != null) { throw ErrorHelper.CreateError(ReflectorError.kInventoryBase + 13, $"duplicate direct metadata in class {DirectMetadata.Class.ClassName.ToFullyQualifiedName ()}"); } DirectMetadata = meta; return; } var lazy = tld as TLLazyCacheVariable; if (lazy != null) { if (LazyCacheVariable != null) { throw ErrorHelper.CreateError(ReflectorError.kInventoryBase + 14, $"duplicate lazy cache variable in class {LazyCacheVariable.Class.ClassName.ToFullyQualifiedName ()}"); } LazyCacheVariable = lazy; return; } var mc = tld as TLMetaclass; if (mc != null) { if (Metaclass != null) { throw ErrorHelper.CreateError(ReflectorError.kInventoryBase + 15, $"duplicate type meta data descriptor in class {Metaclass.Class.ClassName.ToFullyQualifiedName ()}"); } Metaclass = mc; return; } var nom = tld as TLNominalTypeDescriptor; if (nom != null) { if (TypeDescriptor != null) { throw ErrorHelper.CreateError(ReflectorError.kInventoryBase + 16, $"duplicate nominal type descriptor in class {TypeDescriptor.Class.ClassName.ToFullyQualifiedName ()}"); } TypeDescriptor = nom; return; } var tlvar = tld as TLVariable; if (tlvar != null) { if (tlvar is TLPropertyDescriptor tlpropDesc) { PropertyDescriptors.Add(tlpropDesc); } else { Variables.Add(tlvar, srcStm); } return; } var tlprot = tld as TLProtocolConformanceDescriptor; if (tlprot != null) { ProtocolConformanceDescriptors.Add(tlprot); return; } DefinitionsOfUnknownDestination.Add(tld); }
static ModuleInventory FromStreamInto(Stream stm, ModuleInventory inventory, ErrorHandling errors, string fileName = null) { Ex.ThrowOnNull(errors, "errors"); Ex.ThrowOnNull(stm, "stm"); OffsetStream osstm = null; List <NListEntry> entries = null; try { List <MachOFile> macho = MachO.Read(stm, null).ToList(); MachOFile file = macho [0]; List <SymTabLoadCommand> symbols = file.load_commands.OfType <SymTabLoadCommand> ().ToList(); NListEntryType nlet = symbols [0].nlist [0].EntryType; entries = symbols [0].nlist. Where((nle, i) => nle.IsPublic && nle.EntryType == NListEntryType.InSection).ToList(); inventory.Architecture = file.Architecture; osstm = new OffsetStream(stm, file.StartOffset); } catch (Exception e) { errors.Add(ErrorHelper.CreateError(ReflectorError.kCantHappenBase + 58, e, "Unable to retrieve functions from {0}: {1}", fileName ?? "stream", e.Message)); return(inventory); } bool isOldVersion = IsOldVersion(entries); foreach (var entry in entries) { if (!entry.IsSwiftEntryPoint()) { continue; } TLDefinition def = null; try { def = Decomposer.Decompose(entry.str, isOldVersion, Offset(entry)); } catch (RuntimeException e) { var except = new RuntimeException(e.Code, false, e, $"error decomposing {entry.str}: {e.Message}, skipping"); errors.Add(except); } catch (Exception e) { var except = new RuntimeException(ReflectorError.kDecomposeBase + 0, false, e, $"unexpected error handling {entry.str}: {e.Message}, skipping."); errors.Add(except); } if (def != null) { // this skips over privatized names var tlf = def as TLFunction; if (tlf != null && tlf.Name != null && tlf.Name.Name.Contains("...")) { continue; } try { inventory.Add(def, osstm); } catch (RuntimeException e) { e = new RuntimeException(e.Code, e.Error, $"error dispensing top level definition of type {def.GetType ().Name} decomposed from {entry.str}: {e.Message}"); errors.Add(e); } } else { var ex = ErrorHelper.CreateWarning(ReflectorError.kInventoryBase + 18, $"entry {entry.str} uses an unsupported swift feature, skipping."); errors.Add(ex); } } return(inventory); }
public abstract void Add(TLDefinition tlf, Stream srcStm);
public static SwiftName ToClassName(TLDefinition tld) { SwiftClassName cln = ToFormalClassName(tld); return(new SwiftName(cln.ToFullyQualifiedName(), false)); }
public void Add(TLDefinition tld, Stream srcStm) { TLFunction tlf = tld as TLFunction; if (tlf != null) { if (ClassContents.IsWitnessTable(tlf.Signature, tlf.Class)) { WitnessTable.Add(tlf, srcStm); } FunctionsOfUnknownDestination.Add(tlf); return; } TLDirectMetadata meta = tld as TLDirectMetadata; if (meta != null) { if (DirectMetadata != null) { throw ErrorHelper.CreateError(ReflectorError.kInventoryBase + 1, $"duplicate direct metadata in protocol {DirectMetadata.Class.ClassName.ToFullyQualifiedName ()}"); } DirectMetadata = meta; return; } TLMetaclass mc = tld as TLMetaclass; if (mc != null) { if (Metaclass != null) { throw ErrorHelper.CreateError(ReflectorError.kInventoryBase + 2, $"duplicate type meta data descriptor in protocol {Metaclass.Class.ClassName.ToFullyQualifiedName ()}"); } Metaclass = mc; return; } TLProtocolTypeDescriptor ptd = tld as TLProtocolTypeDescriptor; if (ptd != null) { if (TypeDescriptor != null) { throw ErrorHelper.CreateError(ReflectorError.kInventoryBase + 3, $"duplicate protocol type descriptor in protocol {TypeDescriptor.Class.ClassName.ToFullyQualifiedName ()}"); } TypeDescriptor = ptd; return; } if (tld is TLProtocolRequirementsBaseDescriptor baseDescriptor) { BaseDescriptors.Add(baseDescriptor); return; } if (tld is TLBaseConformanceDescriptor baseConformanceDescriptor) { BaseConformanceDescriptors.Add(baseConformanceDescriptor); return; } DefinitionsOfUnknownDestination.Add(tld); }