Example #1
0
 public void Apply()
 {
     foreach (var nodeHelper in _script.Nodes.ToArray())
     {
         if (NodeTypes.IsIfDefType(nodeHelper.Type))
         {
             if (Defines.Contains(nodeHelper.Value))
             {
                 var oldLinks = nodeHelper.InputPins[0].Links.ToList();
                 foreach (var linkHelper in nodeHelper.OutputPins.First().Links)
                 {
                     foreach (var oldLink in oldLinks)
                     {
                         _script.Link(oldLink.From, linkHelper.To);
                     }
                 }
                 _script.Nodes.Remove(nodeHelper);
             }
             else if (Undefines.Contains(nodeHelper.Value))
             {
                 var oldLinks = nodeHelper.InputPins[1].Links.ToList();
                 foreach (var linkHelper in nodeHelper.OutputPins.First().Links)
                 {
                     foreach (var oldLink in oldLinks)
                     {
                         _script.Link(oldLink.From, linkHelper.To);
                     }
                 }
                 _script.Nodes.Remove(nodeHelper);
             }
         }
     }
 }
Example #2
0
 public void Undefine(string define)
 {
     if (Defines.Contains(define))
     {
         Defines.Remove(define);
     }
     Log("Undefined '{0}'", define);
 }
Example #3
0
 public void Define(string define)
 {
     if (!Defines.Contains(define))
     {
         Defines.Add(define);
     }
     Log("Defined '{0}'", define);
 }
Example #4
0
        /// <summary>
        /// Extracts meta comments from all C# files of this compilation, including project files and files added through meta option 'c'.
        /// Returns false if there are errors, except with flag ForCodeInfo. Then use <see cref="Errors"/>.
        /// </summary>
        /// <param name="f">Main C# file. If projFolder not null, must be the main file of the project.</param>
        /// <param name="projFolder">Project folder of the main file, or null if it is not in a project.</param>
        /// <param name="flags"></param>
        public bool Parse(FileNode f, FileNode projFolder, EMPFlags flags)
        {
            Debug.Assert(Errors == null);             //cannot be called multiple times
            Errors = new ErrBuilder();
            _flags = flags;

            _ParseFile(f, true);

            if (projFolder != null)
            {
                foreach (var ff in projFolder.EnumProjectClassFiles(f))
                {
                    _ParseFile(ff, false);
                }
            }

            if (_filesC != null)
            {
                foreach (var ff in _filesC)
                {
                    if (CodeFiles.Exists(o => o.f == ff))
                    {
                        continue;
                    }
                    _ParseFile(ff, false);
                }
            }

            if (!Optimize)
            {
                if (!Defines.Contains("DEBUG"))
                {
                    Defines.Add("DEBUG");
                }
                if (!Defines.Contains("TRACE"))
                {
                    Defines.Add("TRACE");
                }
            }
            //if(Role == ERole.exeProgram && !Defines.Contains("EXE")) Defines.Add("EXE"); //rejected

            _FinalCheckOptions();

            if (Errors.ErrorCount > 0)
            {
                if (flags.Has(EMPFlags.PrintErrors))
                {
                    Errors.PrintAll();
                }
                return(false);
            }
            return(true);
        }
Example #5
0
        public void Merge(ShaderCompilerContext cctx)
        {
            if (cctx == null)
            {
                throw new ArgumentNullException("cctx");
            }

            foreach (string symbol in cctx.Defines)
            {
                if (Defines.Contains(symbol) == false)
                {
                    Defines.Add(symbol);
                }
            }
            Defines.Sort();
        }
        private void AddRegistrationCode(List <TypeMemo> memos)
        {
            var autoClassName = $"__UnmanagedPostProcessorOutput__{(uint)AssemblyDefinition.FullName.GetHashCode()}";
            var mod           = AssemblyDefinition.MainModule;

            var classDef = new TypeDefinition("", autoClassName, TypeAttributes.Class, AssemblyDefinition.MainModule.ImportReference(typeof(object)));

            classDef.IsBeforeFieldInit = false;
            mod.Types.Add(classDef);

            var funcDef = new MethodDefinition(".cctor", MethodAttributes.Static | MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName, AssemblyDefinition.MainModule.ImportReference(typeof(void)));

            funcDef.Body.InitLocals = false;

#if !UNITY_DOTSPLAYER // This will need a different solution
            if (!Defines.Contains("UNITY_DOTSPLAYER"))
            {
                // Needs to run automatically in the player.
                var attributeCtor = AssemblyDefinition.MainModule.ImportReference(typeof(UnityEngine.RuntimeInitializeOnLoadMethodAttribute).GetConstructor(Type.EmptyTypes));
                funcDef.CustomAttributes.Add(new CustomAttribute(attributeCtor));
            }

            if (Defines.Contains("UNITY_EDITOR"))
            {
                // Needs to run automatically in the editor.
                var attributeCtor2 = AssemblyDefinition.MainModule.ImportReference(typeof(UnityEditor.InitializeOnLoadMethodAttribute).GetConstructor(Type.EmptyTypes));
                funcDef.CustomAttributes.Add(new CustomAttribute(attributeCtor2));
            }
#endif

            classDef.Methods.Add(funcDef);

            var processor = funcDef.Body.GetILProcessor();

            var registryType      = mod.ImportReference(typeof(SystemBaseRegistry)).Resolve();
            var addMethod         = mod.ImportReference(registryType.Methods.FirstOrDefault((x) => x.Name == nameof(SystemBaseRegistry.AddUnmanagedSystemType)));
            var delegateCtor      = mod.ImportReference(registryType.NestedTypes.FirstOrDefault((x) => x.Name == nameof(SystemBaseRegistry.ForwardingFunc)).GetConstructors().FirstOrDefault((x) => x.Parameters.Count == 2));
            var genericHashFunc   = mod.ImportReference(typeof(BurstRuntime)).Resolve().Methods.FirstOrDefault((x) => x.Name == nameof(BurstRuntime.GetHashCode64) && x.HasGenericParameters);
            var typeType          = mod.ImportReference(typeof(Type)).Resolve();
            var getTypeFromHandle = mod.ImportReference(typeType.Methods.FirstOrDefault((x) => x.Name == "GetTypeFromHandle"));

            foreach (var memo in memos)
            {
                // This craziness is equivalent to typeof(n)
                processor.Emit(OpCodes.Ldtoken, memo.m_SystemType);
                processor.Emit(OpCodes.Call, getTypeFromHandle);

                processor.Emit(OpCodes.Call, mod.ImportReference(genericHashFunc.MakeGenericInstanceMethod(memo.m_SystemType)));

                for (int i = 0; i < memo.m_Wrappers.Length; ++i)
                {
                    processor.Emit(OpCodes.Ldnull);
                    processor.Emit(OpCodes.Ldftn, memo.m_Wrappers[i]);
                    processor.Emit(OpCodes.Newobj, delegateCtor);
                }

                processor.Emit(OpCodes.Ldstr, memo.m_SystemType.Name);
                processor.Emit(OpCodes.Ldc_I4, memo.m_BurstCompileBits);
                processor.Emit(OpCodes.Call, addMethod);
            }

            processor.Emit(OpCodes.Ret);
        }
        public CompileSettingsFormViewModel(IProject project) : base("Compiler", project)
        {
            settings = project.GetToolchainSettings <GccToolchainSettings>().CompileSettings;

            defines      = new ObservableCollection <string>(settings.Defines);
            includePaths = new ObservableCollection <string>(settings.Includes);

            miscOptions = settings.CustomFlags;

            optimizationLevelSelectedIndex   = (int)settings.Optimization;
            cppLanguageStandardSelectedIndex = (int)settings.CppLanguageStandard;
            clanguageStandardSelectedIndex   = (int)settings.CLanguageStandard;
            fpuSelectedIndex = (int)settings.Fpu;
            debugSymbols     = settings.DebugInformation;
            rtti             = settings.Rtti;
            exceptions       = settings.Exceptions;

            AddDefineCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.DefineText, define => !string.IsNullOrEmpty(define) && !Defines.Contains(define)));
            AddDefineCommand.Subscribe(AddDefine);

            RemoveDefineCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.SelectedDefine, selected => !string.IsNullOrEmpty(selected)));
            RemoveDefineCommand.Subscribe(RemoveDefine);

            AddIncludePathCommand = ReactiveCommand.Create();
            AddIncludePathCommand.Subscribe(AddIncludePath);

            RemoveIncludePathCommand = ReactiveCommand.Create();
            RemoveIncludePathCommand.Subscribe(RemoveIncludePath);

            UpdateCompileString();
        }
        protected override bool PostProcessImpl(TypeDefinition[] componentSystemTypes)
        {
            var assemblyDefinition = AssemblyDefinition;

            var earlyInitHelpers = assemblyDefinition.MainModule.ImportReference(typeof(EarlyInitHelpers)).CheckedResolve();

            var autoClassName = $"__JobReflectionRegistrationOutput__{(uint) assemblyDefinition.FullName.GetHashCode()}";

            var classDef = new TypeDefinition("", autoClassName, TypeAttributes.Class, assemblyDefinition.MainModule.ImportReference(typeof(object)));

            classDef.IsBeforeFieldInit = false;

            classDef.CustomAttributes.Add(new CustomAttribute(AttributeConstructorReferenceFor(typeof(DOTSCompilerGeneratedAttribute), assemblyDefinition.MainModule)));

            var funcDef = new MethodDefinition("CreateJobReflectionData", MethodAttributes.Static | MethodAttributes.Public | MethodAttributes.HideBySig, assemblyDefinition.MainModule.ImportReference(typeof(void)));

            funcDef.Body.InitLocals = false;

            classDef.Methods.Add(funcDef);

            var body      = funcDef.Body;
            var processor = body.GetILProcessor();

            bool anythingChanged = false;

            var declaredGenerics = new HashSet <string>();
            var genericJobs      = new List <TypeReference>();

            foreach (var attr in assemblyDefinition.CustomAttributes)
            {
                if (attr.AttributeType.FullName != RegisterGenericJobTypeAttributeName)
                {
                    continue;
                }

                var openTypeRef = (TypeReference)attr.ConstructorArguments[0].Value;
                var openType    = assemblyDefinition.MainModule.ImportReference(openTypeRef).Resolve();

                if (!openTypeRef.IsGenericInstance || !openType.IsValueType)
                {
                    AddDiagnostic(UserError.DC3001(openType));
                    continue;
                }

                TypeReference result = new GenericInstanceType(assemblyDefinition.MainModule.ImportReference(new TypeReference(openType.Namespace, openType.Name, assemblyDefinition.MainModule, openTypeRef.Scope, true)));

                foreach (var ga in ((GenericInstanceType)openTypeRef).GenericArguments)
                {
                    ((GenericInstanceType)result).GenericArguments.Add(LaunderTypeRef(ga, assemblyDefinition.MainModule));
                }

                genericJobs.Add(result);


                var fn = openType.FullName;
                if (!declaredGenerics.Contains(fn))
                {
                    declaredGenerics.Add(fn);
                }
            }

            foreach (var t in assemblyDefinition.MainModule.Types)
            {
                anythingChanged |= VisitJobStructs(t, processor, body, declaredGenerics);
            }

            foreach (var t in genericJobs)
            {
                anythingChanged |= VisitJobStructs(t, processor, body, declaredGenerics);
            }

            processor.Emit(OpCodes.Ret);

            if (anythingChanged)
            {
                var ctorFuncDef = new MethodDefinition("EarlyInit", MethodAttributes.Static | MethodAttributes.Public | MethodAttributes.HideBySig, assemblyDefinition.MainModule.ImportReference(typeof(void)));

#if !UNITY_DOTSRUNTIME
                if (!Defines.Contains("UNITY_DOTSPLAYER") && !Defines.Contains("UNITY_EDITOR"))
                {
                    // Needs to run automatically in the player, but we need to
                    // exclude this attribute when building for the editor, or
                    // it will re-run the registration for every enter play mode.
                    var loadTypeEnumType = assemblyDefinition.MainModule.ImportReference(typeof(UnityEngine.RuntimeInitializeLoadType));
                    var attributeCtor    = assemblyDefinition.MainModule.ImportReference(typeof(UnityEngine.RuntimeInitializeOnLoadMethodAttribute).GetConstructor(new[] { typeof(UnityEngine.RuntimeInitializeLoadType) }));
                    var attribute        = new CustomAttribute(attributeCtor);
                    attribute.ConstructorArguments.Add(new CustomAttributeArgument(loadTypeEnumType, UnityEngine.RuntimeInitializeLoadType.AfterAssembliesLoaded));
                    ctorFuncDef.CustomAttributes.Add(attribute);
                }

                if (Defines.Contains("UNITY_EDITOR"))
                {
                    // Needs to run automatically in the editor.
                    var attributeCtor2 = assemblyDefinition.MainModule.ImportReference(typeof(UnityEditor.InitializeOnLoadMethodAttribute).GetConstructor(Type.EmptyTypes));
                    ctorFuncDef.CustomAttributes.Add(new CustomAttribute(attributeCtor2));
                }
#endif

                ctorFuncDef.Body.InitLocals = false;

                var p = ctorFuncDef.Body.GetILProcessor();

                p.Emit(OpCodes.Ldnull);
                p.Emit(OpCodes.Ldftn, funcDef);

                var delegateType = assemblyDefinition.MainModule.ImportReference(earlyInitHelpers.NestedTypes.First(x => x.Name == nameof(EarlyInitHelpers.EarlyInitFunction)));
                var delegateCtor = assemblyDefinition.MainModule.ImportReference(delegateType.CheckedResolve().GetConstructors().FirstOrDefault((x) => x.Parameters.Count == 2));
                p.Emit(OpCodes.Newobj, delegateCtor);

                p.Emit(OpCodes.Call, assemblyDefinition.MainModule.ImportReference(earlyInitHelpers.Methods.First(x => x.Name == nameof(EarlyInitHelpers.AddEarlyInitFunction))));

                p.Emit(OpCodes.Ret);

                classDef.Methods.Add(ctorFuncDef);

                assemblyDefinition.MainModule.Types.Add(classDef);
            }

            return(anythingChanged);
        }