public static void ImportExtensionMethodsIntoPackage(Package package, Type type) { VerifyNoMissingSymbols(package); var isbuiltin = type.Assembly == Assembly.GetExecutingAssembly(); var extendedType = (Type)package.Dict["T"].CheckedValue; var restrictedImport = type.GetCustomAttributes(typeof(RestrictedImportAttribute), true).Length != 0; var names = type.GetMethods(ImportBindingFlags).Select(x => x.Name).Distinct().ToArray(); foreach (var name in names) { var methods = type.GetMember(name, ImportBindingFlags) .Where(x => x is MethodInfo) .Select(x => (MethodInfo)ResolveGenericMethod((MethodInfo)x)) .Where(x => ExtendsType(x, extendedType)) .ToList(); if (methods.Count == 0) { continue; } var importable = !restrictedImport || methods[0].GetCustomAttributes(typeof(LispAttribute), true).Length != 0; if (!importable) { continue; } var sym = package.FindOrCreate(name.LispName(), useMissing: true, excludeUseList: true, export: true); var builtin = sym.Value as ImportedFunction; if (builtin == null) { sym.FunctionValue = builtin = new ImportedFunction(name, type); } if (isbuiltin) { // Designed to go before other methods. methods.AddRange(builtin.BuiltinExtensionMembers); builtin.BuiltinExtensionMembers = methods.Distinct().ToArray(); } else { // todo: change order // Goes after other methods as in c#. methods.AddRange(builtin.ExternalExtensionMembers); builtin.ExternalExtensionMembers = methods.Distinct().ToArray(); } } }
public static void ImportIntoPackage(Package package, Type type) { AddPackageByType(type, package); var restrictedImport = type.GetCustomAttributes(typeof(RestrictedImportAttribute), true).Length != 0; package.ImportedType = type; package.RestrictedImport = restrictedImport; var sym = package.FindOrCreate("T", excludeUseList: true, export: true); sym.ConstantValue = type; sym.Documentation = string.Format("The .NET type <{0}> imported in this package.", type); if (!ToBool(Symbols.LazyImport.Value)) { VerifyNoMissingSymbols(package); } }
public static bool ImportMissingSymbol2(string name, Package package) { var members = package.ImportedType.GetMember(name, ImportBindingFlags).Select(x => ResolveGenericMethod(x)).ToArray(); if (members.Length == 0) { return(false); } var importable = !package.RestrictedImport || members[0].GetCustomAttributes(typeof(LispAttribute), true).Length != 0; if (!importable) { return(false); } var field = members[0] as FieldInfo; var ucName = members[0].Name.LispName().ToUpper(); var lcName = members[0].Name.LispName(); if (field != null) { if (field.IsLiteral || (field.IsStatic && field.IsInitOnly)) { var sym = package.FindOrCreate(ucName, excludeUseList: true, export: true); sym.ConstantValue = field.GetValue(null); } return(true); } if (members[0] is EventInfo) { var sym = package.FindOrCreate(lcName, excludeUseList: true, export: true); sym.ConstantValue = members[0]; return(true); } if (members[0] is ConstructorInfo) { var builtin = new ImportedConstructor(members.Cast <ConstructorInfo>().ToArray()); var sym = package.FindOrCreate("new", excludeUseList: true, export: true); sym.FunctionValue = builtin; package.ImportedConstructor = builtin; return(true); } if (members[0] is MethodInfo) { //if ( !name.StartsWith( "get_" ) && !name.StartsWith( "set_" ) ) { var sym = package.FindOrCreate(lcName, excludeUseList: true, export: true); var builtin = new ImportedFunction(members[0].Name, members[0].DeclaringType, members.Cast <MethodInfo>().ToArray(), false); sym.FunctionValue = builtin; } return(true); } if (members[0] is PropertyInfo) { var properties = members.Cast <PropertyInfo>().ToArray(); var getters = properties.Select(x => x.GetGetMethod()).Where(x => x != null).ToArray(); var setters = properties.Select(x => x.GetSetMethod()).Where(x => x != null).ToArray(); if (getters.Length != 0) { var sym = package.FindOrCreate(lcName, excludeUseList: true, export: true); var builtin = new ImportedFunction(members[0].Name, members[0].DeclaringType, getters, false); sym.FunctionValue = builtin; } if (setters.Length != 0) { // create getter symbol for setf/setq package.FindOrCreate(lcName, excludeUseList: true, export: true); // use set-xxx var sym = package.FindOrCreate("set-" + lcName, excludeUseList: true, export: true); var builtin = new ImportedFunction(members[0].Name, members[0].DeclaringType, setters, false); sym.FunctionValue = builtin; } return(true); } return(false); }
public static bool ImportMissingSymbol2(string name, Package package) { var members = package.ImportedType.GetMember(name, ImportBindingFlags).Select(x => ResolveGenericMethod(x)).ToArray(); if (members.Length == 0) { return false; } var importable = !package.RestrictedImport || members[0].GetCustomAttributes(typeof(LispAttribute), true).Length != 0; if (!importable) { return false; } var field = members[0] as FieldInfo; var ucName = members[0].Name.LispName().ToUpper(); var lcName = members[0].Name.LispName(); if (field != null) { if (field.IsLiteral || (field.IsStatic && field.IsInitOnly)) { var sym = package.FindOrCreate(ucName, excludeUseList: true, export: true); sym.ConstantValue = field.GetValue(null); } return true; } if (members[0] is EventInfo) { var sym = package.FindOrCreate(lcName, excludeUseList: true, export: true); sym.ConstantValue = members[0]; return true; } if (members[0] is ConstructorInfo) { var builtin = new ImportedConstructor(members.Cast<ConstructorInfo>().ToArray()); var sym = package.FindOrCreate("new", excludeUseList: true, export: true); sym.FunctionValue = builtin; package.ImportedConstructor = builtin; return true; } if (members[0] is MethodInfo) { //if ( !name.StartsWith( "get_" ) && !name.StartsWith( "set_" ) ) { var sym = package.FindOrCreate(lcName, excludeUseList: true, export: true); var builtin = new ImportedFunction(members[0].Name, members[0].DeclaringType, members.Cast<MethodInfo>().ToArray(), false); sym.FunctionValue = builtin; } return true; } if (members[0] is PropertyInfo) { var properties = members.Cast<PropertyInfo>().ToArray(); var getters = properties.Select(x => x.GetGetMethod()).Where(x => x != null).ToArray(); var setters = properties.Select(x => x.GetSetMethod()).Where(x => x != null).ToArray(); if (getters.Length != 0) { var sym = package.FindOrCreate(lcName, excludeUseList: true, export: true); var builtin = new ImportedFunction(members[0].Name, members[0].DeclaringType, getters, false); sym.FunctionValue = builtin; } if (setters.Length != 0) { // create getter symbol for setf/setq package.FindOrCreate(lcName, excludeUseList: true, export: true); // use set-xxx var sym = package.FindOrCreate("set-" + lcName, excludeUseList: true, export: true); var builtin = new ImportedFunction(members[0].Name, members[0].DeclaringType, setters, false); sym.FunctionValue = builtin; } return true; } return false; }