Пример #1
0
        private bool BindBreakpoint(ITypeMirror type, IBreakpoint bp)
        {
            foreach (var method in type.Methods)
            {
                var bestLocation = BestLocationIn(method, bp);
                if (bestLocation == null)
                {
                    continue;
                }

                var b = Factory.CreateBreakpoint(bestLocation);
                breakpoints[bp] = b;
                if (bp.Enabled)
                {
                    b.Enable();
                }

                if (BreakpointBound != null)
                {
                    BreakpointBound(bp, b, b.Location);
                }
                return(true);
            }
            return(false);
        }
        private string GetSerializedATN(ITypeMirror recognizerClass)
        {
            IList <IElement> elements = processingEnv.GetElementUtils().GetAllMembers((ITypeElement
                                                                                       )processingEnv.GetTypeUtils().AsElement(recognizerClass));

            foreach (IElement element in elements)
            {
                if (element.GetKind() != ElementKind.Field)
                {
                    continue;
                }
                IVariableElement field         = (IVariableElement)element;
                bool             isStatic      = element.GetModifiers().Contains(Modifier.Static);
                object           constantValue = field.GetConstantValue();
                bool             isString      = constantValue is string;
                string           name          = field.GetSimpleName().ToString();
                if (isStatic && isString && name.Equals("_serializedATN"))
                {
                    return((string)constantValue);
                }
            }
            processingEnv.GetMessager().PrintMessage(Diagnostic.Kind.Error, "Could not retrieve serialized ATN from grammar."
                                                     );
            return(null);
        }
Пример #3
0
        private RuleDependencyProcessor.RuleRelations ExtractRuleRelations(ITypeMirror recognizer)
        {
            string serializedATN = GetSerializedATN(recognizer);

            if (serializedATN == null)
            {
                return(null);
            }
            ATN atn = new ATNDeserializer().Deserialize(serializedATN.ToCharArray());

            RuleDependencyProcessor.RuleRelations relations = new RuleDependencyProcessor.RuleRelations(atn.ruleToStartState.Length);
            foreach (ATNState state in atn.states)
            {
                if (!state.epsilonOnlyTransitions)
                {
                    continue;
                }
                foreach (Transition transition in state.Transitions)
                {
                    if (transition.TransitionType != TransitionType.Rule)
                    {
                        continue;
                    }
                    RuleTransition ruleTransition = (RuleTransition)transition;
                    relations.AddRuleInvocation(state.ruleIndex, ruleTransition.target.ruleIndex);
                }
            }
            return(relations);
        }
Пример #4
0
        public override bool Process <_T0>(HashSet <_T0> annotations, IRoundEnvironment roundEnv)
        {
            if (!CheckClassNameConstants())
            {
                return(true);
            }
            IList <Tuple <RuleDependency, IElement> > dependencies = GetDependencies(roundEnv);
            IDictionary <ITypeMirror, IList <Tuple <RuleDependency, IElement> > > recognizerDependencies = new Dictionary <ITypeMirror, IList <Tuple <RuleDependency, IElement> > >();

            foreach (Tuple <RuleDependency, IElement> dependency in dependencies)
            {
                ITypeMirror recognizerType = GetRecognizerType(dependency.Item1);
                IList <Tuple <RuleDependency, IElement> > list = recognizerDependencies.Get(recognizerType);
                if (list == null)
                {
                    list = new List <Tuple <RuleDependency, IElement> >();
                    recognizerDependencies.Put(recognizerType, list);
                }
                list.Add(dependency);
            }
            foreach (KeyValuePair <ITypeMirror, IList <Tuple <RuleDependency, IElement> > > entry in recognizerDependencies.EntrySet())
            {
                processingEnv.GetMessager().PrintMessage(Diagnostic.Kind.Note, string.Format("ANTLR 4: Validating {0} dependencies on rules in {1}.", entry.Value.Count, entry.Key.ToString()));
                CheckDependencies(entry.Value, entry.Key);
            }
            return(true);
        }
Пример #5
0
        private int[] GetRuleVersions(ITypeMirror recognizerClass, string[] ruleNames)
        {
            int[]            versions = new int[ruleNames.Length];
            IList <IElement> elements = processingEnv.GetElementUtils().GetAllMembers((ITypeElement)processingEnv.GetTypeUtils().AsElement(recognizerClass));

            foreach (IElement element in elements)
            {
                if (element.GetKind() != ElementKind.Field)
                {
                    continue;
                }
                IVariableElement field         = (IVariableElement)element;
                bool             isStatic      = element.GetModifiers().Contains(Modifier.Static);
                object           constantValue = field.GetConstantValue();
                bool             isInteger     = constantValue is int;
                string           name          = field.GetSimpleName().ToString();
                if (isStatic && isInteger && name.StartsWith("RULE_"))
                {
                    try
                    {
                        name = Sharpen.Runtime.Substring(name, "RULE_".Length);
                        if (name.IsEmpty() || !System.Char.IsLower(name[0]))
                        {
                            continue;
                        }
                        int index = (int)constantValue;
                        if (index < 0 || index >= versions.Length)
                        {
                            string message = string.Format("Rule index {0} for rule '{1}' out of bounds for recognizer {2}.", index, name, recognizerClass.ToString());
                            processingEnv.GetMessager().PrintMessage(Diagnostic.Kind.Error, message, element);
                            continue;
                        }
                        if (name.IndexOf(ATNSimulator.RuleVariantDelimiter) >= 0)
                        {
                            // ignore left-factored pseudo-rules
                            continue;
                        }
                        IExecutableElement ruleMethod = GetRuleMethod(recognizerClass, name);
                        if (ruleMethod == null)
                        {
                            string message = string.Format("Could not find rule method for rule '{0}' in recognizer {1}.", name, recognizerClass.ToString());
                            processingEnv.GetMessager().PrintMessage(Diagnostic.Kind.Error, message, element);
                            continue;
                        }
                        RuleVersion ruleVersion = ruleMethod.GetAnnotation <RuleVersion>();
                        int         version     = ruleVersion != null?ruleVersion.Value() : 0;

                        versions[index] = version;
                    }
                    catch (ArgumentException)
                    {
                        processingEnv.GetMessager().PrintMessage(Diagnostic.Kind.Error, "Exception occurred while validating rule dependencies.", element);
                    }
                }
            }
            return(versions);
        }
Пример #6
0
        private void OnTypeUnloaded(ITypeMirror typeMirror)
        {
            var bps = breakpoints.Where(x => typeMirror.SourceFiles.Contains(x.Value.Location.SourceFile)).ToArray();

            foreach (var bp in bps)
            {
                breakpoints.Remove(bp.Key);
            }
        }
Пример #7
0
 public MethodMirror(string fullName, string name, ITypeMirror type, int token, MethodDefinition def, ILocation[] locations)
 {
     this.FullName = fullName;
     this.Name = name;
     this.DeclaringType = type;
     this.MetadataToken = token;
     this.metadata = def;
     this.locations = locations;
 }
Пример #8
0
 public MethodMirror(string fullName, string name, ITypeMirror type, ITypeMirror returnType, int token, MethodDefinition def, ILocation[] locations)
 {
     this.FullName      = fullName;
     this.Name          = name;
     this.DeclaringType = type;
     this.ReturnType    = returnType;
     this.MetadataToken = token;
     this.metadata      = def;
     this.locations     = locations;
 }
Пример #9
0
        public void TypeLoaded_PublishesEvent()
        {
            vm.Reset();
            ITypeMirror loadedType = null;

            typeProvider.TypeLoaded += mirror => loadedType = mirror;
            vm.LoadAssembly(typeof(type1).Assembly.Location);
            typeProvider.TypeLoaded += null;
            Assert.IsNotNull(loadedType);
        }
Пример #10
0
        private void OnTypeLoaded(ITypeMirror type)
        {
            var sourcefiles         = type.SourceFiles;
            var relevantBreakpoints = Breakpoints.Where(bp => sourcefiles.Contains(bp.Location.SourceFile));

            foreach (var bp in relevantBreakpoints)
            {
                BindBreakpoint(type, bp);
            }
        }
Пример #11
0
        public void AssemblyUnload_Publishes_TypeUnloadEvents()
        {
            vm.Reset();
            vm.LoadAssembly(typeof(type1).Assembly.Location);
            ITypeMirror unloadedType = null;

            typeProvider.TypeUnloaded += mirror => unloadedType = mirror;
            vm.UnloadAssembly(typeof(type1).Assembly.FullName);
            typeProvider.TypeLoaded += null;
            Assert.IsNotNull(unloadedType);
        }
Пример #12
0
        private void OnTypeUnloaded(ITypeMirror typeMirror)
        {
            var bps = breakpoints.Where(x => typeMirror.SourceFiles.Contains(x.Value.Location.SourceFile)).ToArray();

            foreach (var bp in bps)
            {
                if (BreakpointUnbound != null)
                {
                    BreakpointUnbound(bp.Key, bp.Value, bp.Value.Location);
                }
                breakpoints[bp.Key] = null;
            }
        }
Пример #13
0
        private string[] GetRuleNames(ITypeMirror recognizerClass)
        {
            IList <string>   result   = new List <string>();
            IList <IElement> elements = processingEnv.GetElementUtils().GetAllMembers((ITypeElement
                                                                                       )processingEnv.GetTypeUtils().AsElement(recognizerClass));

            foreach (IElement element in elements)
            {
                if (element.GetKind() != ElementKind.Field)
                {
                    continue;
                }
                IVariableElement field         = (IVariableElement)element;
                bool             isStatic      = element.GetModifiers().Contains(Modifier.Static);
                object           constantValue = field.GetConstantValue();
                bool             isInteger     = constantValue is int;
                string           name          = field.GetSimpleName().ToString();
                if (isStatic && isInteger && name.StartsWith("RULE_"))
                {
                    try
                    {
                        name = Sharpen.Runtime.Substring(name, "RULE_".Length);
                        if (name.IsEmpty() || !System.Char.IsLower(name[0]))
                        {
                            continue;
                        }
                        int index = (int)constantValue;
                        if (index < 0)
                        {
                            continue;
                        }
                        while (result.Count <= index)
                        {
                            result.AddItem(string.Empty);
                        }
                        result.Set(index, name);
                    }
                    catch (ArgumentException)
                    {
                        processingEnv.GetMessager().PrintMessage(Diagnostic.Kind.Error, "Exception occurred while validating rule dependencies."
                                                                 , element);
                    }
                }
            }
            return(Sharpen.Collections.ToArray(result, new string[result.Count]));
        }
Пример #14
0
        private IExecutableElement GetRuleMethod(ITypeMirror recognizerClass, string name)
        {
            IList <IElement> elements = processingEnv.GetElementUtils().GetAllMembers((ITypeElement)processingEnv.GetTypeUtils().AsElement(recognizerClass));

            foreach (IElement element in elements)
            {
                if (element.GetKind() != ElementKind.Method)
                {
                    continue;
                }
                IExecutableElement method = (IExecutableElement)element;
                if (method.GetSimpleName().ContentEquals(name) && HasRuleVersionAnnotation(method))
                {
                    return(method);
                }
            }
            return(null);
        }
Пример #15
0
        private void OnTypeLoaded(ITypeMirror type)
        {
            var sourcefiles = type.SourceFiles;
            var relevantBreakpoints = breakpointProvider.Breakpoints.Where (bp => sourcefiles.Contains (bp.Location.SourceFile));

            foreach (var bp in relevantBreakpoints)
            {
                foreach (var method in type.Methods)
                {
                    var bestLocation = BestLocationIn (method, bp);
                    if (bestLocation == null)
                        continue;

                    var b = Factory.CreateBreakpoint (bestLocation);
                    breakpoints.Add (bp, b);
                    b.Enable ();
                    break;
                }
            }
        }
Пример #16
0
        private void OnTypeLoaded(ITypeMirror type)
        {
            var sourcefiles         = type.SourceFiles;
            var relevantBreakpoints = breakpoints.Where(bp => sourcefiles.Contains(bp.Key.Location.SourceFile));

            foreach (var bp in relevantBreakpoints)
            {
                foreach (var method in type.Methods)
                {
                    var bestLocation = BestLocationIn(method, bp.Key);
                    if (bestLocation == null)
                    {
                        continue;
                    }

                    var b = Factory.CreateBreakpoint(bestLocation);
                    breakpoints[bp.Key] = b;
                    b.Enable();
                    break;
                }
            }
        }
Пример #17
0
        private bool BindBreakpoint(ITypeMirror type, IBreakpoint bp)
        {
            foreach (var method in type.Methods)
            {
                var bestLocation = BestLocationIn (method, bp);
                if (bestLocation == null)
                    continue;

                var b = Factory.CreateBreakpoint (bestLocation);
                breakpoints[bp] = b;
                if (bp.Enabled) b.Enable ();

                if (BreakpointBound != null)
                        BreakpointBound (bp, b, b.Location);
                return true;
            }
            return false;
        }
Пример #18
0
        private IAnnotationValue FindRuleDependencyProperty(Tuple <RuleDependency, IElement> dependency, IAnnotationMirror annotationMirror, RuleDependencyProcessor.RuleDependencyProperty property)
        {
            IAnnotationValue recognizerValue = null;
            IAnnotationValue ruleValue       = null;
            IAnnotationValue versionValue    = null;
            IAnnotationValue dependentsValue = null;
            IDictionary <IExecutableElement, IAnnotationValue> values = annotationMirror.GetElementValues();

            foreach (KeyValuePair <IExecutableElement, IAnnotationValue> value in values.EntrySet())
            {
                IAnnotationValue annotationValue = value.Value;
                if ("rule()".Equals(value.Key.ToString()))
                {
                    ruleValue = annotationValue;
                    if (!(annotationValue.GetValue() is int))
                    {
                        processingEnv.GetMessager().PrintMessage(Diagnostic.Kind.Warning, "Expected int constant for annotation property 'rule()'.", dependency.Item2, annotationMirror, annotationValue);
                        return(null);
                    }
                    if ((int)annotationValue.GetValue() != dependency.Item1.Rule())
                    {
                        // this is a valid dependency annotation, but not the one we're looking for
                        return(null);
                    }
                }
                else
                {
                    if ("recognizer()".Equals(value.Key.ToString()))
                    {
                        recognizerValue = annotationValue;
                        if (!(annotationValue.GetValue() is ITypeMirror))
                        {
                            processingEnv.GetMessager().PrintMessage(Diagnostic.Kind.Warning, "Expected Class constant for annotation property 'recognizer()'.", dependency.Item2, annotationMirror, annotationValue);
                            return(null);
                        }
                        ITypeMirror annotationRecognizer = (ITypeMirror)annotationValue.GetValue();
                        ITypeMirror expectedRecognizer   = GetRecognizerType(dependency.Item1);
                        if (!processingEnv.GetTypeUtils().IsSameType(expectedRecognizer, annotationRecognizer))
                        {
                            // this is a valid dependency annotation, but not the one we're looking for
                            return(null);
                        }
                    }
                    else
                    {
                        if ("version()".Equals(value.Key.ToString()))
                        {
                            versionValue = annotationValue;
                            if (!(annotationValue.GetValue() is int))
                            {
                                processingEnv.GetMessager().PrintMessage(Diagnostic.Kind.Warning, "Expected int constant for annotation property 'version()'.", dependency.Item2, annotationMirror, annotationValue);
                                return(null);
                            }
                            if ((int)annotationValue.GetValue() != dependency.Item1.Version())
                            {
                                // this is a valid dependency annotation, but not the one we're looking for
                                return(null);
                            }
                        }
                    }
                }
            }
            if (recognizerValue != null)
            {
                if (property == RuleDependencyProcessor.RuleDependencyProperty.Recognizer)
                {
                    return(recognizerValue);
                }
                else
                {
                    if (ruleValue != null)
                    {
                        if (property == RuleDependencyProcessor.RuleDependencyProperty.Rule)
                        {
                            return(ruleValue);
                        }
                        else
                        {
                            if (versionValue != null)
                            {
                                if (property == RuleDependencyProcessor.RuleDependencyProperty.Version)
                                {
                                    return(versionValue);
                                }
                                else
                                {
                                    if (property == RuleDependencyProcessor.RuleDependencyProperty.Dependents)
                                    {
                                        return(dependentsValue);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (recognizerValue == null)
            {
                processingEnv.GetMessager().PrintMessage(Diagnostic.Kind.Warning, "Could not find 'recognizer()' element in annotation.", dependency.Item2, annotationMirror);
            }
            if (property == RuleDependencyProcessor.RuleDependencyProperty.Recognizer)
            {
                return(null);
            }
            if (ruleValue == null)
            {
                processingEnv.GetMessager().PrintMessage(Diagnostic.Kind.Warning, "Could not find 'rule()' element in annotation.", dependency.Item2, annotationMirror);
            }
            if (property == RuleDependencyProcessor.RuleDependencyProperty.Rule)
            {
                return(null);
            }
            if (versionValue == null)
            {
                processingEnv.GetMessager().PrintMessage(Diagnostic.Kind.Warning, "Could not find 'version()' element in annotation.", dependency.Item2, annotationMirror);
            }
            return(null);
        }
Пример #19
0
 public TypeEvent(ITypeMirror type)
 {
     Type = type;
 }
Пример #20
0
 public TypeEvent(ITypeMirror type)
 {
     Type = type;
 }
Пример #21
0
        private void OnTypeLoaded(ITypeMirror type)
        {
            var sourcefiles = type.SourceFiles;
            var relevantBreakpoints = Breakpoints.Where (bp => sourcefiles.Contains (bp.Location.SourceFile));

            foreach (var bp in relevantBreakpoints)
                BindBreakpoint (type, bp);
        }
Пример #22
0
 private void CheckDependencies(IList <Tuple <RuleDependency, IElement> > dependencies, ITypeMirror recognizerType)
 {
     string[] ruleNames    = GetRuleNames(recognizerType);
     int[]    ruleVersions = GetRuleVersions(recognizerType, ruleNames);
     RuleDependencyProcessor.RuleRelations relations = ExtractRuleRelations(recognizerType);
     foreach (Tuple <RuleDependency, IElement> dependency in dependencies)
     {
         try
         {
             if (!processingEnv.GetTypeUtils().IsAssignable(GetRecognizerType(dependency.Item1), recognizerType))
             {
                 continue;
             }
             // this is the rule in the dependency set with the highest version number
             int effectiveRule = dependency.Item1.Rule();
             if (effectiveRule < 0 || effectiveRule >= ruleVersions.Length)
             {
                 Tuple <IAnnotationMirror, IAnnotationValue> ruleReferenceElement = FindRuleDependencyProperty(dependency, RuleDependencyProcessor.RuleDependencyProperty.Rule);
                 string message = string.Format("Rule dependency on unknown rule {0}@{1} in {2}", dependency.Item1.Rule(), dependency.Item1.Version(), GetRecognizerType(dependency.Item1).ToString());
                 if (ruleReferenceElement != null)
                 {
                     processingEnv.GetMessager().PrintMessage(Diagnostic.Kind.Error, message, dependency.Item2, ruleReferenceElement.Item1, ruleReferenceElement.Item2);
                 }
                 else
                 {
                     processingEnv.GetMessager().PrintMessage(Diagnostic.Kind.Error, message, dependency.Item2);
                 }
                 continue;
             }
             EnumSet <Dependents> dependents = EnumSet.Of(Dependents.Self, dependency.Item1.Dependents());
             ReportUnimplementedDependents(dependency, dependents);
             BitSet @checked = new BitSet();
             int    highestRequiredDependency = CheckDependencyVersion(dependency, ruleNames, ruleVersions, effectiveRule, null);
             if (dependents.Contains(Dependents.Parents))
             {
                 BitSet parents = relations.parents[dependency.Item1.Rule()];
                 for (int parent = parents.NextSetBit(0); parent >= 0; parent = parents.NextSetBit(parent + 1))
                 {
                     if (parent < 0 || parent >= ruleVersions.Length || @checked.Get(parent))
                     {
                         continue;
                     }
                     @checked.Set(parent);
                     int required = CheckDependencyVersion(dependency, ruleNames, ruleVersions, parent, "parent");
                     highestRequiredDependency = Math.Max(highestRequiredDependency, required);
                 }
             }
             if (dependents.Contains(Dependents.Children))
             {
                 BitSet children = relations.children[dependency.Item1.Rule()];
                 for (int child = children.NextSetBit(0); child >= 0; child = children.NextSetBit(child + 1))
                 {
                     if (child < 0 || child >= ruleVersions.Length || @checked.Get(child))
                     {
                         continue;
                     }
                     @checked.Set(child);
                     int required = CheckDependencyVersion(dependency, ruleNames, ruleVersions, child, "child");
                     highestRequiredDependency = Math.Max(highestRequiredDependency, required);
                 }
             }
             if (dependents.Contains(Dependents.Ancestors))
             {
                 BitSet ancestors = relations.GetAncestors(dependency.Item1.Rule());
                 for (int ancestor = ancestors.NextSetBit(0); ancestor >= 0; ancestor = ancestors.NextSetBit(ancestor + 1))
                 {
                     if (ancestor < 0 || ancestor >= ruleVersions.Length || @checked.Get(ancestor))
                     {
                         continue;
                     }
                     @checked.Set(ancestor);
                     int required = CheckDependencyVersion(dependency, ruleNames, ruleVersions, ancestor, "ancestor");
                     highestRequiredDependency = Math.Max(highestRequiredDependency, required);
                 }
             }
             if (dependents.Contains(Dependents.Descendants))
             {
                 BitSet descendants = relations.GetDescendants(dependency.Item1.Rule());
                 for (int descendant = descendants.NextSetBit(0); descendant >= 0; descendant = descendants.NextSetBit(descendant + 1))
                 {
                     if (descendant < 0 || descendant >= ruleVersions.Length || @checked.Get(descendant))
                     {
                         continue;
                     }
                     @checked.Set(descendant);
                     int required = CheckDependencyVersion(dependency, ruleNames, ruleVersions, descendant, "descendant");
                     highestRequiredDependency = Math.Max(highestRequiredDependency, required);
                 }
             }
             int declaredVersion = dependency.Item1.Version();
             if (declaredVersion > highestRequiredDependency)
             {
                 Tuple <IAnnotationMirror, IAnnotationValue> versionElement = FindRuleDependencyProperty(dependency, RuleDependencyProcessor.RuleDependencyProperty.Version);
                 string message = string.Format("Rule dependency version mismatch: {0} has maximum dependency version {1} (expected {2}) in {3}", ruleNames[dependency.Item1.Rule()], highestRequiredDependency, declaredVersion, GetRecognizerType(dependency.Item1).ToString());
                 if (versionElement != null)
                 {
                     processingEnv.GetMessager().PrintMessage(Diagnostic.Kind.Error, message, dependency.Item2, versionElement.Item1, versionElement.Item2);
                 }
                 else
                 {
                     processingEnv.GetMessager().PrintMessage(Diagnostic.Kind.Error, message, dependency.Item2);
                 }
             }
         }
         catch (AnnotationTypeMismatchException)
         {
             processingEnv.GetMessager().PrintMessage(Diagnostic.Kind.Warning, string.Format("Could not validate rule dependencies for element {0}", dependency.Item2.ToString()), dependency.Item2);
         }
     }
 }
Пример #23
0
 private void OnTypeUnloaded(ITypeMirror typeMirror)
 {
     var bps = breakpoints.Where (x => typeMirror.SourceFiles.Contains(x.Value.Location.SourceFile)).ToArray ();
     foreach (var bp in bps)
         breakpoints.Remove (bp.Key);
 }
Пример #24
0
 private void OnTypeUnloaded(ITypeMirror typeMirror)
 {
     var bps = breakpoints.Where (x => typeMirror.SourceFiles.Contains(x.Value.Location.SourceFile)).ToArray ();
     foreach (var bp in bps) {
         if (BreakpointUnbound != null)
             BreakpointUnbound (bp.Key, bp.Value, bp.Value.Location);
         breakpoints[bp.Key] = null;
     }
 }