Example #1
0
        private object GenerateFunction(BuiltinFunctionInfo bfi)
        {
            string name = bfi.Function.DeclaringModule.Name;

            if (bfi.Function.DeclaringType != null)
            {
                name += "." + bfi.Function.DeclaringType.Name;
            }
            name += "." + bfi.Function.Name;

            return(new Dictionary <string, object>()
            {
                { "func_name", name }
            });
        }
Example #2
0
        /// <summary>
        /// Replaces a built-in function (specified by module name and function name) with a customized
        /// delegate which provides specific behavior for handling when that function is called.
        ///
        /// Currently this just provides a hook when the function is called - it could be expanded
        /// to providing the interpretation of when the function is called as well.
        /// </summary>
        private void SpecializeFunction(string moduleName, string name, Func <CallExpression, AnalysisUnit, ISet <Namespace>[], ISet <Namespace> > dlg)
        {
            var module = Modules[moduleName];

            BuiltinModule builtin = module.Module as BuiltinModule;

            Debug.Assert(builtin != null);
            if (builtin != null)
            {
                foreach (var v in builtin[name])
                {
                    BuiltinFunctionInfo funcInfo = v as BuiltinFunctionInfo;
                    if (funcInfo != null)
                    {
                        builtin[name] = new SpecializedBuiltinFunction(this, funcInfo.Function, dlg).SelfSet;
                        break;
                    }
                }
            }
        }
Example #3
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);
        }
Example #4
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);
        }
Example #5
0
        private object GenerateFunction(BuiltinFunctionInfo bfi)
        {
            string name = bfi.Function.DeclaringModule.Name;
            if (bfi.Function.DeclaringType != null) {
                name += "." + bfi.Function.DeclaringType.Name;
            }
            name += "." + bfi.Function.Name;

            return new Dictionary<string, object>() {
                { "func_name", MemoizeString(name) }
            };
        }
Example #6
0
 internal NewFunction(BuiltinFunctionInfo function, JAnalyzer projectState)
     : base(function.Function, projectState)
 {
 }