Ejemplo n.º 1
0
 internal static BuiltinClassInfo GetIteratorTypeFromType(BuiltinClassInfo klass, AnalysisUnit unit) {
     switch (klass.PythonType.TypeId) {
         case BuiltinTypeId.List:
             return unit.ProjectState.ClassInfos[BuiltinTypeId.ListIterator];
         case BuiltinTypeId.Tuple:
             return unit.ProjectState.ClassInfos[BuiltinTypeId.TupleIterator];
         case BuiltinTypeId.Set:
             return unit.ProjectState.ClassInfos[BuiltinTypeId.SetIterator];
         case BuiltinTypeId.Str:
             return unit.ProjectState.ClassInfos[BuiltinTypeId.StrIterator];
         case BuiltinTypeId.Unicode:
             return unit.ProjectState.ClassInfos[BuiltinTypeId.UnicodeIterator];
         case BuiltinTypeId.Bytes:
             return unit.ProjectState.ClassInfos[BuiltinTypeId.BytesIterator];
         case BuiltinTypeId.Generator:
         case BuiltinTypeId.DictKeys:
         case BuiltinTypeId.DictValues:
         case BuiltinTypeId.DictItems:
         case BuiltinTypeId.ListIterator:
         case BuiltinTypeId.TupleIterator:
         case BuiltinTypeId.SetIterator:
         case BuiltinTypeId.StrIterator:
         case BuiltinTypeId.UnicodeIterator:
         case BuiltinTypeId.BytesIterator:
         case BuiltinTypeId.CallableIterator:
             return klass;
         default:
             return null;
     }
 }
Ejemplo n.º 2
0
        public SequenceBuiltinInstanceInfo(BuiltinClassInfo klass, bool sequenceOfSelf, bool supportsMod)
            : base(klass) {
            _supportsMod = supportsMod;

            var seqInfo = klass as SequenceBuiltinClassInfo;
            if (seqInfo != null) {
                _indexTypes = seqInfo.IndexTypes;
            } else if (sequenceOfSelf) {
                _indexTypes = SelfSet;
            } else {
                _indexTypes = AnalysisSet.Empty;
            }
        }
Ejemplo n.º 3
0
        private object GenerateTypeName(AnalysisValue baseClass, bool isRef)
        {
            ClassInfo ci = baseClass as ClassInfo;

            if (ci != null)
            {
                return(GetTypeName(((ProjectEntry)ci.DeclaringModule).GetModuleInfo().Name, ci.ClassDefinition.Name));
            }

            BuiltinClassInfo bci = baseClass as BuiltinClassInfo;

            if (bci != null)
            {
                return(GenerateTypeName(bci._type));
            }

            IterableValue iteri = baseClass as IterableValue;

            if (iteri != null)
            {
                return(GenerateTypeName(iteri.PythonType, isRef, iteri.IndexTypes));
            }

            InstanceInfo ii = baseClass as InstanceInfo;

            if (ii != null)
            {
                return(GenerateTypeName(ii.ClassInfo, isRef));
            }

            BuiltinInstanceInfo bii = baseClass as BuiltinInstanceInfo;

            if (bii != null)
            {
                return(GenerateTypeName(bii.ClassInfo, isRef));
            }

            return(GenerateTypeName(baseClass.PythonType));
        }
 public IterableValue(VariableDef[] indexTypes, BuiltinClassInfo seqType, Node node)
     : base(seqType)
 {
     IndexTypes = indexTypes;
     _node      = node;
 }
Ejemplo n.º 5
0
 public ListInfo(VariableDef[] indexTypes, BuiltinClassInfo seqType, Node node)
     : base(indexTypes, seqType, node)
 {
     EnsureAppend();
 }
Ejemplo n.º 6
0
 public IterBoundBuiltinMethodInfo(IterableInfo iterable, BuiltinMethodInfo method)
     : base(method)
 {
     _indexTypes = iterable.IndexTypes;
     _iterClass = IteratorInfo.GetIteratorTypeFromType(iterable.ClassInfo, iterable.ClassInfo.ProjectState._evalUnit);
 }
Ejemplo n.º 7
0
        private object GetMemberValue(Namespace type, bool isRef)
        {
            switch (type.ResultType)
            {
            case PythonMemberType.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 PythonMemberType.Method:
                BoundMethodInfo mi = type as BoundMethodInfo;
                if (mi != null)
                {
                    return(GenerateFunction(mi.Function));
                }
                return("method");

            case PythonMemberType.Property:
                FunctionInfo prop = type as FunctionInfo;
                if (prop != null)
                {
                    return(GenerateProperty(prop));
                }
                break;

            case PythonMemberType.Class:
                ClassInfo ci = type as ClassInfo;
                if (ci != null)
                {
                    if (isRef)
                    {
                        return(GenerateClassRef(ci));
                    }
                    else
                    {
                        return(GenerateClass(ci));
                    }
                }

                BuiltinClassInfo bci = type as BuiltinClassInfo;
                if (bci != null)
                {
                    return(GenerateClassRef(bci));
                }
                return("type");

            case PythonMemberType.Constant:
                ConstantInfo constantInfo = type as ConstantInfo;
                if (constantInfo != null)
                {
                    return(GenerateConstant(constantInfo));
                }
                break;

            case PythonMemberType.Module:
                if (type is ModuleInfo)
                {
                    return(new Dictionary <string, object>()
                    {
                        { "module_name", ((ModuleInfo)type).Name }
                    });
                }
                else if (type is BuiltinModule)
                {
                    return(new Dictionary <string, object>()
                    {
                        { "module_name", ((BuiltinModule)type).Name }
                    });
                }
                break;

            case PythonMemberType.Instance:
                InstanceInfo instInfo = type as InstanceInfo;
                if (instInfo != null)
                {
                    return(new Dictionary <string, object>()
                    {
                        { "type", GenerateTypeName(instInfo.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.PythonType) }
                });
            }
            return(null);
        }
Ejemplo n.º 8
0
 public ListInfo(VariableDef[] indexTypes, BuiltinClassInfo seqType, Node node, ProjectEntry entry)
     : base(indexTypes, seqType, node, entry) {
 }
Ejemplo n.º 9
0
 public SequenceInfo(VariableDef[] indexTypes, BuiltinClassInfo seqType, Node node, ProjectEntry entry)
     : base(indexTypes, seqType, node) {
     _declaringModule = entry;
     _declaringVersion = entry.AnalysisVersion;
 }
Ejemplo n.º 10
0
 public IteratorInfo(VariableDef[] indexTypes, BuiltinClassInfo iterType, Node node)
     : base(indexTypes, iterType, node) {
 }
Ejemplo n.º 11
0
        private object GetMemberValueInternal(AnalysisValue type, ModuleInfo declModule, bool isRef)
        {
            SpecializedNamespace specialCallable = type as SpecializedNamespace;

            if (specialCallable != null)
            {
                if (specialCallable.Original == null)
                {
                    return(null);
                }
                return(GetMemberValueInternal(specialCallable.Original, declModule, isRef));
            }

            switch (type.MemberType)
            {
            case PythonMemberType.Function:
                FunctionInfo fi = type as FunctionInfo;
                if (fi != null)
                {
                    if (fi.DeclaringModule.GetModuleInfo() != declModule)
                    {
                        return(GenerateFuncRef(fi));
                    }
                    else
                    {
                        return(GenerateFunction(fi));
                    }
                }

                BuiltinFunctionInfo bfi = type as BuiltinFunctionInfo;
                if (bfi != null)
                {
                    return(GenerateFuncRef(bfi));
                }

                return("function");

            case PythonMemberType.Method:
                BoundMethodInfo mi = type as BoundMethodInfo;
                if (mi != null)
                {
                    return(GenerateMethod(mi));
                }
                return("method");

            case PythonMemberType.Property:
                FunctionInfo prop = type as FunctionInfo;
                if (prop != null)
                {
                    return(GenerateProperty(prop));
                }
                break;

            case PythonMemberType.Class:
                ClassInfo ci = type as ClassInfo;
                if (ci != null)
                {
                    if (isRef || ci.DeclaringModule.GetModuleInfo() != declModule)
                    {
                        // TODO: Save qualified name so that classes defined in classes/function can be resolved
                        return(GetTypeRef(ci.DeclaringModule.ModuleName, ci.Name));
                    }
                    else
                    {
                        return(GenerateClass(ci, declModule));
                    }
                }

                BuiltinClassInfo bci = type as BuiltinClassInfo;
                if (bci != null)
                {
                    return(GetTypeRef(bci.PythonType.DeclaringModule.Name, bci.PythonType.Name));
                }
                return("type");

            case PythonMemberType.Constant:
                ConstantInfo constantInfo = type as ConstantInfo;
                if (constantInfo != null)
                {
                    return(GenerateConstant(constantInfo));
                }
                break;

            case PythonMemberType.Module:
                if (type is ModuleInfo)
                {
                    return(GetModuleName(((ModuleInfo)type).Name));
                }
                else if (type is BuiltinModule)
                {
                    return(GetModuleName(((BuiltinModule)type).Name));
                }
                break;

            case PythonMemberType.Instance:
                return(new Dictionary <string, object> {
                    { "type", GenerateTypeName(type, true) }
                });

            default:
                return(new Dictionary <string, object>()
                {
                    { "type", GenerateTypeName(type.PythonType) }
                });
            }
            return(null);
        }
Ejemplo n.º 12
0
 public StarArgsSequenceInfo(VariableDef[] variableDef, BuiltinClassInfo builtinClassInfo, Node node)
     : base(variableDef, builtinClassInfo, node)
 {
 }
Ejemplo n.º 13
0
 public SequenceInfo(VariableDef[] indexTypes, BuiltinClassInfo seqType, Node node)
     : base(indexTypes, seqType, node)
 {
 }
Ejemplo n.º 14
0
 public BaseIteratorValue(BuiltinClassInfo klass) : base(klass) {
 }
Ejemplo n.º 15
0
 public IteratorValue(IterableValue iterable, BuiltinClassInfo iterType)
     : base(iterType) {
     _iterable = iterable;
 }
Ejemplo n.º 16
0
 public SingleIteratorValue(VariableDef indexTypes, BuiltinClassInfo iterType, IPythonProjectEntry declModule)
     : base(iterType) {
     _indexTypes = indexTypes;
     _declModule = declModule;
 }
Ejemplo n.º 17
0
 public FixedIteratorValue(IAnalysisSet iterable, BuiltinClassInfo klass) : base(klass) {
     _iterable = iterable;
 }
Ejemplo n.º 18
0
        public ProjectState(ScriptEngine pythonEngine)
        {
            _pythonEngine      = pythonEngine;
            _projectEntries    = new List <ProjectEntry>();
            _modules           = new Dictionary <string, ModuleReference>();
            _modulesByFilename = new Dictionary <string, ModuleInfo>(StringComparer.OrdinalIgnoreCase);
            _itemCache         = new Dictionary <object, object>();

            var pythonContext = HostingHelpers.GetLanguageContext(_pythonEngine) as PythonContext;

            _codeContextCls = new ModuleContext(new PythonDictionary(), pythonContext).GlobalContext;
            _codeContextCls.ModuleContext.ShowCls = true;

            _codeContext = new ModuleContext(
                new PythonDictionary(),
                HostingHelpers.GetLanguageContext(_pythonEngine) as PythonContext
                ).GlobalContext;

            InitializeBuiltinModules();

            // TODO: Use reflection-only!
            _references = new List <KeyValuePair <Assembly, TopNamespaceTracker> >();
            AddAssembly(LoadAssemblyInfo(typeof(string).Assembly));
            AddAssembly(LoadAssemblyInfo(typeof(Debug).Assembly));


            // cached for quick checks to see if we're a call to clr.AddReference
            SpecializeFunction("clr", "AddReference", (n, unit, args) => AddReference(n, null));
            SpecializeFunction("clr", "AddReferenceByPartialName", (n, unit, args) => AddReference(n, ClrModule.LoadAssemblyByPartialName));
            SpecializeFunction("clr", "AddReferenceByName", (n, unit, args) => AddReference(n, null));
            SpecializeFunction("clr", "AddReferenceToFile", (n, unit, args) => AddReference(n, (s) => ClrModule.LoadAssemblyFromFile(_codeContext, s)));
            SpecializeFunction("clr", "AddReferenceToFileAndPath", (n, unit, args) => AddReference(n, (s) => ClrModule.LoadAssemblyFromFileWithPath(_codeContext, s)));

            try {
                SpecializeFunction("wpf", "LoadComponent", LoadComponent);
            } catch (KeyNotFoundException) {
                // IronPython.Wpf.dll isn't available...
            }

            SpecializeFunction("__builtin__", "range", (n, unit, args) => unit.DeclaringModule.GetOrMakeNodeVariable(n, (nn) => new RangeInfo(ClrModule.GetPythonType(typeof(List)), unit.ProjectState).SelfSet));
            SpecializeFunction("__builtin__", "min", ReturnUnionOfInputs);
            SpecializeFunction("__builtin__", "max", ReturnUnionOfInputs);

            _builtinModule   = (BuiltinModule)Modules["__builtin__"].Module;
            _propertyObj     = GetBuiltin("property");
            _classmethodObj  = GetBuiltin("classmethod");
            _staticmethodObj = GetBuiltin("staticmethod");
            _typeObj         = GetBuiltin("type");
            _intType         = GetBuiltin("int");
            _stringType      = (BuiltinClassInfo)GetBuiltin("str");

            _objectSet = new HashSet <Namespace>(new[] { GetBuiltin("object") });

            _setType       = (BuiltinClassInfo)GetNamespaceFromObjects(TypeCache.Set);
            _rangeFunc     = GetBuiltin("range");
            _frozensetType = GetBuiltin("frozenset");
            _functionType  = GetNamespaceFromObjects(TypeCache.Function);
            _generatorType = (BuiltinClassInfo)GetNamespaceFromObjects(DynamicHelpers.GetPythonTypeFromType(typeof(PythonGenerator)));
            _dictType      = (BuiltinClassInfo)GetNamespaceFromObjects(TypeCache.Dict);
            _boolType      = (BuiltinClassInfo)GetNamespaceFromObjects(TypeCache.Boolean);
            _noneInst      = (ConstantInfo)GetNamespaceFromObjects(new object[] { null });
            _listType      = (BuiltinClassInfo)GetNamespaceFromObjects(TypeCache.List);
            _tupleType     = (BuiltinClassInfo)GetNamespaceFromObjects(TypeCache.PythonTuple);

            _queue = new Queue <AnalysisUnit>();

            _docProvider = CodeContext.LanguageContext.GetService <DocumentationProvider>();
        }
Ejemplo n.º 19
0
 public NumericInstanceInfo(BuiltinClassInfo klass)
     : base(klass) {
 }
Ejemplo n.º 20
0
 public BaseIterableValue(BuiltinClassInfo seqType)
     : base(seqType) {
 }
Ejemplo n.º 21
0
 internal ConstantInfo(BuiltinClassInfo klass, object value, PythonMemberType memberType)
     : base(klass) {
     _value = value;
     _memberType = memberType;
     _builtinInfo = klass.Instance;
 }
Ejemplo n.º 22
0
 public IterableInfo(VariableDef[] indexTypes, BuiltinClassInfo seqType, Node node)
     : base(seqType) {
     _indexTypes = indexTypes;
     _node = node;
 }
Ejemplo n.º 23
0
 public StarArgsSequenceInfo(VariableDef[] variableDef, BuiltinClassInfo builtinClassInfo, Node node, ProjectEntry entry)
     : base(variableDef, builtinClassInfo, node, entry) {
 }
Ejemplo n.º 24
0
 private Dictionary<string, object> GenerateClassRef(BuiltinClassInfo ci)
 {
     return new Dictionary<string, object>() {
         { "type_name", GetTypeName(ci._type.DeclaringModule.Name, ci._type.Name) },
     };
 }
Ejemplo n.º 25
0
 public override bool IsOfType(BuiltinClassInfo klass)
 {
     return _original.IsOfType(klass);
 }
Ejemplo n.º 26
0
 public IterBoundBuiltinMethodInfo(VariableDef[] indexTypes, BuiltinClassInfo iterableClass)
     : base(new IterBuiltinMethodInfo(iterableClass.JType, iterableClass.ProjectState))
 {
     _indexTypes = indexTypes;
     _iterClass = IteratorInfo.GetIteratorTypeFromType(iterableClass, iterableClass.ProjectState._evalUnit);
 }