/// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public object Clone()
        {
            AttributeRegistry <TObject, TAttribute> that = new AttributeRegistry <TObject, TAttribute>(IsIdentityEquality);

            foreach (KeyValuePair <TObject, Set <TAttribute> > entry in this.registry)
            {
                foreach (TAttribute attr in entry.Value)
                {
                    that.Add(entry.Key, attr);
                }
            }
            return(that);
        }
 /// <summary>
 /// Copies object attributes associated with source object
 /// to target object in a target registry
 /// </summary>
 /// <param name="sourceObject">Source object</param>
 /// <param name="targetRegistry">Target registry</param>
 /// <param name="targetObject">Target object</param>
 public void CopyObjectAttributesTo(
     TObject sourceObject,
     AttributeRegistry <TObject, TAttribute> targetRegistry,
     TObject targetObject)
 {
     if (ReferenceEquals(sourceObject, targetObject))
     {
         return;
     }
     if (!registry.ContainsKey(sourceObject))
     {
         return;
     }
     foreach (TAttribute attr in registry[sourceObject])
     {
         targetRegistry.Add(targetObject, attr);
     }
 }
Esempio n. 3
0
        /// <summary>
        /// Get the abstract syntax tree for the generated code.
        /// </summary>
        /// <param name="itd"></param>
        /// <param name="method"></param>
        /// <param name="inputAttributes"></param>
        /// <returns></returns>
        internal List <ITypeDeclaration> GetTransformedDeclaration(ITypeDeclaration itd, MethodBase method, AttributeRegistry <object, ICompilerAttribute> inputAttributes)
        {
            TransformerChain        tc = ConstructTransformChain(method);
            List <ITypeDeclaration> output;

            try
            {
                Compiling?.Invoke(this, new CompileEventArgs());
                bool trackTransform = (BrowserMode != BrowserMode.Never);
                List <TransformError> warnings;
                output = tc.TransformToDeclaration(itd, inputAttributes, trackTransform, ShowProgress, out warnings, CatchExceptions, TreatWarningsAsErrors);
                OnCompiled(new CompileEventArgs()
                {
                    Warnings = warnings
                });
                if (BrowserMode == BrowserMode.Always)
                {
                    ShowBrowser(tc, GeneratedSourceFolder, output[0].Name);
                }
                else if (BrowserMode == BrowserMode.WriteFiles)
                {
                    tc.WriteAllOutputs(Path.Combine(GeneratedSourceFolder, output[0].Name + " Transforms"));
                }
                if (ShowSchedule && InferenceEngine.Visualizer?.TaskGraphVisualizer != null)
                {
                    foreach (CodeTransformer ct in tc.transformers)
                    {
                        if (ct.Transform is DeadCodeTransform bst)
                        {
                            foreach (ITypeDeclaration itd2 in ct.transformMap.Values)
                            {
                                InferenceEngine.Visualizer.TaskGraphVisualizer.VisualizeTaskGraph(itd2, (BasicTransformContext)bst.Context);
                            }
                        }
                    }
                }
            }
            catch (TransformFailedException ex)
            {
                OnCompiled(new CompileEventArgs()
                {
                    Exception = ex
                });
                if (BrowserMode != BrowserMode.Never)
                {
                    ShowBrowser(tc, GeneratedSourceFolder, itd.Name);
                }
                throw new CompilationFailedException(ex.Results, ex.Message);
            }
            return(output);
        }
Esempio n. 4
0
        internal IGeneratedAlgorithm CompileWithoutParams(ITypeDeclaration itd, MethodBase method, AttributeRegistry <object, ICompilerAttribute> inputAttributes)
        {
            AttributeRegistry <object, ICompilerAttribute> outputAttributes = (AttributeRegistry <object, ICompilerAttribute>)inputAttributes.Clone();
            List <ITypeDeclaration> output = GetTransformedDeclaration(itd, method, outputAttributes);

            return(CompileWithoutParams <IGeneratedAlgorithm>(output));
        }
Esempio n. 5
0
        public List <ITypeDeclaration> TransformToDeclaration(ITypeDeclaration itd, AttributeRegistry <object, ICompilerAttribute> inputAttributes, bool trackTransform, bool showProgress,
                                                              out List <TransformError> warnings, bool catchExceptions = false, bool treatWarningsAsErrors = false)
        {
            List <ITypeDeclaration> res = new List <ITypeDeclaration>();

            res.Add(itd);
            AttributeRegistry <object, ICompilerAttribute> attributes = inputAttributes;

            warnings = new List <TransformError>();
            foreach (CodeTransformer ct in transformers)
            {
                string    name  = ct.Transform.Name;
                Stopwatch watch = null;
                if (showProgress)
                {
                    Console.Write(name + " ");
                    watch = Stopwatch.StartNew();
                }
                ct.Transform.Context.InputAttributes = attributes;
                ct.TrackTransform = trackTransform;
                if (catchExceptions)
                {
                    try
                    {
                        res = ct.TransformToDeclaration(res[0]);
                    }
                    catch (Exception ex)
                    {
                        ((BasicTransformContext)ct.Transform.Context).Error("Uncaught exception in transform", ex);
                    }
                }
                else
                {
                    res = ct.TransformToDeclaration(res[0]);
                }
                if (showProgress)
                {
                    watch.Stop();
                    Console.WriteLine("({0}ms)", watch.ElapsedMilliseconds);
                }
#if TestLanguageWriter
                try
                {
                    ILanguageWriter lw = new CSharpWriter() as ILanguageWriter;
                    lw.GenerateSource(res[0]);
                }
                catch
                {
                    Console.WriteLine("Language writer error for " + res[0].Name);
                }
#endif
                TransformResults tr = ct.Transform.Context.Results;
                tr.ThrowIfErrors(name + " failed", treatWarningsAsErrors);
                if (tr.WarningCount > 0)
                {
                    foreach (TransformError te in tr.errorsAndWarnings)
                    {
                        if (te.IsWarning)
                        {
                            warnings.Add(te);
                        }
                    }
                }
                attributes = ct.Transform.Context.OutputAttributes;
            }
            return(res);
        }