internal ProjectEntry(JAnalyzer state, string moduleName, string filePath, IAnalysisCookie cookie) { _projectState = state; _moduleName = moduleName ?? ""; _filePath = filePath; _cookie = cookie; _myScope = new ModuleInfo(_moduleName, this, state.Interpreter.CreateModuleContext()); _unit = new AnalysisUnit(_tree, _myScope.Scope); AnalysisLog.NewUnit(_unit); }
private object GetMemberValue(Namespace type, ModuleInfo declModule, bool isRef) { SpecializedNamespace specialCallable = type as SpecializedNamespace; if (specialCallable != null) { return GetMemberValue(specialCallable.Original, declModule, isRef); } switch (type.MemberType) { case JMemberType.Function: FunctionInfo fi = type as FunctionInfo; if (fi != null) { return GenerateFunction(fi); } BuiltinFunctionInfo bfi = type as BuiltinFunctionInfo; if (bfi != null) { return GenerateFunction(bfi); } return "function"; case JMemberType.Method: BoundMethodInfo mi = type as BoundMethodInfo; if (mi != null) { return GenerateFunction(mi.Function); } return "method"; case JMemberType.Property: FunctionInfo prop = type as FunctionInfo; if (prop != null) { return GenerateProperty(prop); } break; case JMemberType.Class: ClassInfo ci = type as ClassInfo; if (ci != null) { if (isRef || ci.DeclaringModule.MyScope != declModule) { return GenerateClassRef(ci); } else { return GenerateClass(ci, declModule); } } BuiltinClassInfo bci = type as BuiltinClassInfo; if (bci != null) { return GenerateClassRef(bci); } return "type"; case JMemberType.Constant: ConstantInfo constantInfo = type as ConstantInfo; if (constantInfo != null) { return GenerateConstant(constantInfo); } break; case JMemberType.Module: if (type is ModuleInfo) { return GetModuleName(((ModuleInfo)type).Name); } else if (type is BuiltinModule) { return GetModuleName(((BuiltinModule)type).Name); } break; case JMemberType.Instance: InstanceInfo instInfo = type as InstanceInfo; if (instInfo != null) { return new Dictionary<string, object>() { { "type" , GenerateTypeName(instInfo.ClassInfo) } }; } BuiltinInstanceInfo builtinInst = type as BuiltinInstanceInfo; if (builtinInst != null) { return new Dictionary<string, object>() { { "type" , GenerateTypeName(builtinInst.ClassInfo) } }; } SequenceInfo seqInfo = type as SequenceInfo; if (seqInfo != null) { return new Dictionary<string, object>() { { "type" , GenerateTypeName(seqInfo.ClassInfo) } }; } break; default: return new Dictionary<string, object>() { { "type" , GenerateTypeName(type.JType) } }; } return null; }
private Dictionary<string, object> SerializeModule(ModuleInfo moduleInfo) { return new Dictionary<string, object>() { { "members", GenerateMembers(moduleInfo) }, { "doc", MemoizeString(moduleInfo.Documentation) }, { "children", GenerateChildModules(moduleInfo) }, { "filename", moduleInfo.ProjectEntry.FilePath } }; }
private string GetMemberKind(VariableDef variableDef, ModuleInfo declModule, bool isRef) { if (variableDef.Types.Count == 1) { return GetMemberKind(variableDef.Types.First(), declModule, isRef); } else if (variableDef.Types.Count == 0) { // typed to object return "data"; } else { return "multiple"; } }
private object GetMemberValue(VariableDef variableDef, ModuleInfo declModule, bool isRef) { if (variableDef.Types.Count == 1) { var type = variableDef.Types.First(); var res = GetMemberValue(type, declModule, isRef); if (res == null) { _errors.Add(String.Format("Cannot save single member: {0}", variableDef.Types.First())); } return res; } else if (variableDef.Types.Count == 0) { return new Dictionary<string, object>() { { "type", _objectType } }; } else { List<object> res = new List<object>(); foreach (var type in variableDef.Types) { res.Add( new Dictionary<string, object>() { { "kind", GetMemberKind(type, declModule, isRef) }, { "value", GetMemberValue(type, declModule, isRef) } } ); } return new Dictionary<string, object>() { {"members", res.ToArray() } }; } }
private Dictionary<string, object> GenerateMembers(ModuleInfo moduleInfo) { Dictionary<string, object> res = new Dictionary<string, object>(); foreach (var keyValue in moduleInfo.Scope.Variables) { res[keyValue.Key] = GenerateMember(keyValue.Value, moduleInfo); } return res; }
private object GetClassMembers(ClassInfo ci, ModuleInfo declModule) { Dictionary<string, object> memTable = new Dictionary<string, object>(); foreach (var keyValue in ci.Scope.Variables) { memTable[keyValue.Key] = GenerateMember(keyValue.Value, declModule, true); } if (ci.Instance.InstanceAttributes != null) { foreach (var keyValue in ci.Instance.InstanceAttributes) { memTable[keyValue.Key] = GenerateMember(keyValue.Value, declModule, true); } } return memTable; }
private Dictionary<string, object> GenerateClass(ClassInfo ci, ModuleInfo declModule) { return new Dictionary<string, object>() { { "mro", GetClassMro(ci) }, { "bases", GetClassBases(ci) }, { "members" , GetClassMembers(ci, declModule) }, { "doc", MemoizeString(ci.Documentation) }, { "builtin", false }, { "location", GenerateLocation(ci.Location) } }; }
private object GenerateMember(VariableDef variableDef, ModuleInfo declModule, bool isRef = false) { Dictionary<string, object> memberEntry = new Dictionary<string, object>() { {"kind", GetMemberKind(variableDef, declModule, isRef) }, {"value", GetMemberValue(variableDef, declModule, isRef) } }; return memberEntry; }
private object[] GenerateChildModules(ModuleInfo moduleInfo) { List<object> res = new List<object>(); foreach (var keyValue in moduleInfo.GetChildrenPackages(null)) { res.Add(keyValue.Key); } return res.ToArray(); }
private static string GetMemberKind(Namespace type, ModuleInfo declModule, bool isRef) { SpecializedNamespace specialCallable = type as SpecializedNamespace; if (specialCallable != null) { return GetMemberKind(specialCallable.Original, declModule, isRef); } switch (type.MemberType) { case JMemberType.Function: if (type is BuiltinFunctionInfo) { return "func_ref"; } return "function"; case JMemberType.Method: return "method"; case JMemberType.Property: return "property"; case JMemberType.Class: if (isRef || type is BuiltinClassInfo || (type is ClassInfo && ((ClassInfo)type).DeclaringModule.MyScope != declModule)) { return "typeref"; } return "type"; case JMemberType.Module: return "moduleref"; case JMemberType.Instance: default: return "data"; } }
/// <summary> /// Adds an ephemeral reference for the declaring module. Ephemeral references are modules /// referenced via import statements but that for a module that we don't know actually exists. /// As long as there are ephemeral references to the name we want to provide import completion /// for that module. But once those have been removed we want to stop displaying those names. /// /// Therefore we track the version of a module that accessed it and as long as the latest /// analyzed version knows about the module we'll include it in the analysis. /// </summary> /// <param name="module"></param> public void AddEphemeralReference(ModuleInfo module) { if (EphmeralReferences == null) { EphmeralReferences = new Dictionary<ModuleInfo, int>(); } EphmeralReferences[module] = module.ProjectEntry.AnalysisVersion; }
public ModuleScope(ModuleInfo moduleInfo) : base(moduleInfo, null) { }
public void AddChildPackage(ModuleInfo childPackage, AnalysisUnit curUnit) { string realName = childPackage.Name; int lastDot; if ((lastDot = childPackage.Name.LastIndexOf('.')) != -1) { realName = childPackage.Name.Substring(lastDot + 1); } childPackage.ParentPackage = this; Scope.SetVariable(childPackage.ProjectEntry.Tree, curUnit, realName, childPackage.SelfSet, false); if (_packageModules == null) { _packageModules = new Dictionary<string, WeakReference>(); } _packageModules[realName] = childPackage.WeakModule; }