public IType AsIType(KnownTypesCache knownTypesCache, IPsiModule module) { // TODO: It would be nice to also cache the closed generic and array types // We would need a different key for that IType type = knownTypesCache.GetByClrTypeName(ClrTypeName, module); if (TypeParameters.Length > 0) { var typeParameters = new IType[TypeParameters.Length]; for (int i = 0; i < TypeParameters.Length; i++) { typeParameters[i] = knownTypesCache.GetByClrTypeName(TypeParameters[i], module); } var typeElement = type.GetTypeElement().NotNull("typeElement != null"); type = TypeFactory.CreateType(typeElement, typeParameters); } if (IsArray) { type = TypeFactory.CreateArrayType(type, 1); } return(type); }
public ITypeElement GetTypeElement(KnownTypesCache knownTypesCache, [NotNull] IPsiModule module) { using (CompilationContextCookie.GetExplicitUniversalContextIfNotSet()) { var type = knownTypesCache.GetByClrTypeName(myTypeName, module); return(type.GetTypeElement()); } }
private static ITypeElement GetTypeElement(IClrTypeName typeName, KnownTypesCache knownTypesCache, IPsiModule module) { using (CompilationContextCookie.GetExplicitUniversalContextIfNotSet()) { var type = knownTypesCache.GetByClrTypeName(typeName, module); return(type.GetTypeElement()); } }
public UnityApi(UnityVersion unityVersion, KnownTypesCache knownTypesCache) { myUnityVersion = unityVersion; myKnownTypesCache = knownTypesCache; myTypes = Lazy.Of(() => { var apiXml = new ApiXml(); return(apiXml.LoadTypes()); }, true); }
private static object GetTypeObject(UnityTypeSpec typeSpec, KnownTypesCache knownTypesCache, IPsiModule module) { if (typeSpec.TypeParameters.Length == 0) { var keyword = CSharpTypeFactory.GetTypeKeyword(typeSpec.ClrTypeName); if (keyword != null) { return(keyword); } } return(typeSpec.AsIType(knownTypesCache, module)); }
public static MethodSignature AsMethodSignature(this UnityEventFunction eventFunction, KnownTypesCache knownTypesCache, IPsiModule module) { var returnType = eventFunction.ReturnType.AsIType(knownTypesCache, module); if (eventFunction.Parameters.Length == 0) { return(new MethodSignature(returnType, eventFunction.IsStatic)); } var parameterTypes = new IType[eventFunction.Parameters.Length]; var parameterNames = new string[eventFunction.Parameters.Length]; for (var i = 0; i < eventFunction.Parameters.Length; i++) { var parameter = eventFunction.Parameters[i]; var paramType = parameter.TypeSpec.AsIType(knownTypesCache, module); parameterTypes[i] = paramType; parameterNames[i] = parameter.Name; } return(new MethodSignature(returnType, eventFunction.IsStatic, parameterTypes, parameterNames)); }
public IMethodDeclaration CreateDeclaration([NotNull] CSharpElementFactory factory, [NotNull] KnownTypesCache knownTypesCache, [NotNull] IClassLikeDeclaration classDeclaration, AccessRights accessRights, bool makeVirtual = false, bool makeCoroutine = false) { var builder = new StringBuilder(128); var args = new object[1 + Parameters.Length]; object arg; var argIndex = 0; var module = classDeclaration.GetPsiModule(); if (accessRights != AccessRights.NONE) { builder.Append(CSharpDeclaredElementPresenter.Instance.Format(accessRights)); builder.Append(" "); } if (IsStatic) { builder.Append("static "); } // Consider this declaration a template, and the final generated code implements (or overrides) this API if (makeVirtual) { builder.Append("virtual "); } if (makeCoroutine && CanBeCoroutine) { builder.Append(PredefinedType.IENUMERATOR_FQN.FullName); } else { arg = GetTypeObject(ReturnType, knownTypesCache, module); if (arg is string) { builder.Append(arg); if (ReturnType.IsArray) { builder.Append("[]"); } } else { builder.Append("$0"); args[argIndex++] = arg; } } builder.Append(" "); builder.Append(Name); builder.Append("("); for (var i = 0; i < Parameters.Length; i++) { if (i > 0) { builder.Append(","); } var parameter = Parameters[i]; // TODO: `out` or `ref`? // From reflection point of view, it's a "ByRef" Type, and that's all we know... // The only place it's currently being used is an out parameter if (parameter.IsByRef) { builder.Append("out "); } arg = GetTypeObject(parameter.TypeSpec, knownTypesCache, module); if (arg is string) { builder.Append(arg); if (parameter.TypeSpec.IsArray) { builder.Append("[]"); } } else { builder.Append($"${argIndex}"); args[argIndex++] = arg; } builder.Append(' '); builder.Append(parameter.Name); } builder.Append(");"); var declaration = (IMethodDeclaration)factory.CreateTypeMemberDeclaration(builder.ToString(), args); declaration.SetResolveContextForSandBox(classDeclaration, SandBoxContextType.Child); return(declaration); }