/// <summary> /// Adds a reference which satisfies the provided expectation in the result set /// </summary> /// <param name="namable"></param> /// <param name="expectation"></param> /// <param name="asType">Indicates that we had to move from instance to type to perform the deferencing</param> /// <param name="resultSet"></param> private int addReference(INamable namable, BaseFilter expectation, bool asType, ReturnValue resultSet) { int retVal = 0; if (namable != null) { if (expectation.AcceptableChoice(namable)) { if (asType) { if (!(namable is IValue) && !(namable is Type)) { resultSet.Add(namable); retVal += 1; } else if (namable is State) { // TODO : Refactor model to avoid this resultSet.Add(namable); retVal += 1; } } else { resultSet.Add(namable); retVal += 1; } } } return retVal; }
/// <summary> /// Fills the retVal result set according to the subDeclarator class provided as parameter /// </summary> /// <param name="subDeclarator">The subdeclarator used to get the image</param> /// <param name="expectation">The expectatino of the desired element</param> /// <param name="asType">Indicates that we had to go from the values to the types to perform dereferencing</param> /// <param name="values">The return value to update</param> /// <return>the number of elements added</return> private int FillBySubdeclarator(ISubDeclarator subDeclarator, BaseFilter expectation, bool asType, ReturnValue values) { int retVal = 0; if (subDeclarator != null) { // Go to the beginning of the update chain ISubDeclarator currentDeclarator = subDeclarator; ModelElement modelElement = subDeclarator as ModelElement; while (modelElement != null) { if (modelElement.Updates != null) { currentDeclarator = modelElement.Updates as ISubDeclarator; } modelElement = modelElement.Updates; } while (currentDeclarator != null) { // Adds the elements of the current declarator List<INamable> tmp = new List<INamable>(); currentDeclarator.Find(Image, tmp); foreach (INamable namable in tmp) { retVal += addReference(namable, expectation, asType, values); } // Follow the update chain modelElement = currentDeclarator as ModelElement; if (modelElement != null && modelElement.UpdatedBy.Count == 1) { currentDeclarator = modelElement.UpdatedBy[0] as ISubDeclarator; } else { currentDeclarator = null; } } } values.ApplyUpdates(); return retVal; }
/// <summary> /// Provides the possible references for this expression (only available during semantic analysis) /// </summary> /// <param name="instance">the instance on which this element should be found.</param> /// <param name="expectation">the expectation on the element found</param> /// <param name="last">indicates that this is the last element in a dereference chain</param> /// <returns></returns> public override ReturnValue getReferences(INamable instance, BaseFilter expectation, bool last) { ReturnValue retVal = Arguments[0].getReferences(instance, AllMatches.INSTANCE, false); if (retVal.IsEmpty) { retVal = Arguments[0].getReferenceTypes(instance, AllMatches.INSTANCE, false); } // When variables & parameters are found, only consider the first one // which is the one that is closer in the tree { ReturnValue tmp2 = retVal; retVal = new ReturnValue(); ReturnValueElement variable = null; foreach (ReturnValueElement elem in tmp2.Values) { if (elem.Value is Parameter || elem.Value is IVariable) { if (variable == null) { variable = elem; retVal.Values.Add(elem); } } else { retVal.Values.Add(elem); } } } if (!retVal.IsEmpty) { for (int i = 1; i < Arguments.Count; i++) { ReturnValue tmp2 = retVal; retVal = new ReturnValue(Arguments[i]); foreach (ReturnValueElement elem in tmp2.Values) { bool removed = false; ModelElement model = elem.Value as ModelElement; if (model != null) { removed = model.IsRemoved; } if (!removed) { retVal.Merge(elem, Arguments[i].getReferences(elem.Value, AllMatches.INSTANCE, i == (Arguments.Count - 1))); } } if (retVal.IsEmpty) { AddError("Cannot find " + Arguments[i].ToString() + " in " + Arguments[i - 1].ToString()); } } } else { AddError("Cannot evaluate " + Arguments[0].ToString()); } retVal.filter(expectation); return retVal; }
/// <summary> /// Provides the possible references for this designator (only available during semantic analysis) /// </summary> /// <param name="instance">the instance on which this element should be found.</param> /// <param name="expectation">the expectation on the element found</param> /// <param name="lastElement">Indicates that this element is the last one in a dereference chain</param> /// <returns></returns> public ReturnValue GetReferences(INamable instance, BaseFilter expectation, bool lastElement) { ReturnValue retVal = new ReturnValue(this); if (instance == null) { // Special handling for THIS or ENCLOSING if (Image == ThisKeyword || Image == EnclosingKeyword) { INamable currentElem = Root; while (currentElem != null) { Type type = currentElem as Type; if (type != null) { StateMachine stateMachine = type as StateMachine; while (stateMachine != null) { type = stateMachine; stateMachine = stateMachine.EnclosingStateMachine; } // Enclosing does not references state machines. if (!(Image == EnclosingKeyword && type is StateMachine)) { retVal.Add(type); return retVal; } } currentElem = enclosing(currentElem); } return retVal; } // No enclosing instance. Try to first name of a . separated list of names // . First in the enclosing expression InterpreterTreeNode current = this; while (current != null) { ISubDeclarator subDeclarator = current as ISubDeclarator; if (FillBySubdeclarator(subDeclarator, expectation, false, retVal) > 0) { // If this is the last element in the dereference chain, stop at first match if (lastElement) { return retVal; } current = null; } else { current = current.Enclosing; } } // . In the predefined elements addReference(EfsSystem.Instance.GetPredefinedItem(Image), expectation, false, retVal); if (lastElement && !retVal.IsEmpty) { return retVal; } // . In the enclosing items, except the enclosing dictionary since dictionaries are handled in a later step INamable currentNamable = Root; while (currentNamable != null) { ISubDeclarator subDeclarator = currentNamable as ISubDeclarator; if (subDeclarator != null && !(subDeclarator is Dictionary)) { if (FillBySubdeclarator(subDeclarator, expectation, false, retVal) > 0 && lastElement) { return retVal; } } currentNamable = EnclosingSubDeclarator(currentNamable); } // . In the dictionaries declared in the system foreach (Dictionary dictionary in EfsSystem.Instance.Dictionaries) { if (FillBySubdeclarator(dictionary, expectation, false, retVal) > 0 && lastElement) { return retVal; } NameSpace defaultNameSpace = dictionary.FindNameSpace("Default"); if (defaultNameSpace != null) { if (FillBySubdeclarator(defaultNameSpace, expectation, false, retVal) > 0 && lastElement) { return retVal; } } } } else { // The instance is provided, hence, this is not the first designator in the . separated list of designators bool asType = false; if (instance is ITypedElement && !(instance is State)) { // If the instance is a typed element, dereference it to its corresponding type ITypedElement element = instance as ITypedElement; if (element.Type != EfsSystem.Instance.NoType) { instance = element.Type; asType = true; } } // Find the element in all enclosing sub declarators of the instance while (instance != null) { ISubDeclarator subDeclarator = instance as ISubDeclarator; if (FillBySubdeclarator(subDeclarator, expectation, asType, retVal) > 0) { instance = null; } else { if (instance is Dictionary) { instance = EnclosingSubDeclarator(instance); } else { instance = null; } } } } return retVal; }
/// <summary> /// Provides the possible references types for this expression (used in semantic analysis) /// </summary> /// <param name="instance">the reference instance on which this element should analysed</param> /// <param name="expectation">Indicates the kind of element we are looking for</param> /// <param name="last">indicates that this is the last element in a dereference chain</param> /// <returns></returns> public virtual ReturnValue GetReferenceTypes(INamable instance, BaseFilter expectation, bool last) { ReturnValue retVal = new ReturnValue(this); SemanticAnalysis(instance, AllMatches.INSTANCE); const bool asType = true; retVal.Add(GetExpressionType(), null, asType); return retVal; }
/// <summary> /// Provides the possible references types for this expression (used in semantic analysis) /// </summary> /// <param name="instance">the reference instance on which this element should analysed</param> /// <param name="expectation">Indicates the kind of element we are looking for</param> /// <param name="last">indicates that this is the last element in a dereference chain</param> /// <returns></returns> public ReturnValue GetReferenceTypes(INamable instance, BaseFilter expectation, bool last) { ReturnValue retVal = null; if (Designator != null) { retVal = new ReturnValue(); foreach (ReturnValueElement element in Designator.GetReferences(instance, expectation, last).Values) { if (element.Value is Type) { const bool asType = true; retVal.Add(element.Value, null, asType); } } } else if (LiteralValue != null) { retVal = LiteralValue.GetReferenceTypes(instance, expectation, true); } return retVal; }
/// <summary> /// Merges the other return value with this one /// </summary> /// <param name="previous">The previous ReturnValueElement which lead to this ReturnValueElement</param> /// <param name="other">The other return value to merge with</param> public void Merge(ReturnValueElement previous, ReturnValue other) { foreach (ReturnValueElement elem in other.Values) { Add(elem.Value, previous); } }
/// <summary> /// Performs the semantic analysis of the expression /// </summary> /// <param name="instance">the reference instance on which this element should analysed</param> /// <paraparam name="expectation">Indicates the kind of element we are looking for</paraparam> /// <returns>True if semantic analysis should be continued</returns> public override bool SemanticAnalysis(Utils.INamable instance, Filter.AcceptableChoice expectation) { bool retVal = base.SemanticAnalysis(instance, expectation); if (retVal) { Ref = null; ReturnValue tmp = Arguments[0].getReferences(instance, Filter.AllMatches, false); if (tmp.IsEmpty) { tmp = Arguments[0].getReferenceTypes(instance, Filter.AllMatches, false); } if (!tmp.IsEmpty) { for (int i = 1; i < Arguments.Count; i++) { ReturnValue tmp2 = tmp; tmp = new ReturnValue(Arguments[i]); foreach (ReturnValueElement elem in tmp2.Values) { tmp.Merge(elem, Arguments[i].getReferences(elem.Value, Filter.AllMatches, i == (Arguments.Count - 1))); } if (tmp.IsEmpty) { AddError("Cannot find " + Arguments[i].ToString() + " in " + Arguments[i - 1].ToString()); } } } else { AddError("Cannot evaluate " + Arguments[0].ToString()); } tmp.filter(expectation); if (tmp.IsUnique) { // Unique element has been found. Reference it and perform the semantic analysis // on all dereferenced expression, now that the context is known for each expression Ref = tmp.Values[0].Value; ReturnValueElement current = tmp.Values[0]; for (int i = Arguments.Count - 1; i > 0; i--) { current = current.PreviousElement; Arguments[i].SemanticAnalysis(current.Value); } Arguments[0].SemanticAnalysis(); } else if (tmp.IsAmbiguous) { // Several possible interpretations for this deref expression, not allowed AddError("Expression " + ToString() + " may have several interpretations " + tmp.ToString() + ", please disambiguate"); } else { // No possible interpretation for this deref expression, not allowed AddError("Expression " + ToString() + " has no interpretation"); } } return retVal; }
/// <summary> /// Provides the possible references types for this expression (used in semantic analysis) /// </summary> /// <param name="instance">the reference instance on which this element should analysed</param> /// <paraparam name="expectation">Indicates the kind of element we are looking for</paraparam> /// <param name="last">indicates that this is the last element in a dereference chain</param> /// <returns></returns> public virtual ReturnValue getReferenceTypes(Utils.INamable instance, Filter.AcceptableChoice expectation, bool last) { ReturnValue retVal = new ReturnValue(this); SemanticAnalysis(instance, Filter.AllMatches); retVal.Add(GetExpressionType()); return retVal; }
/// <summary> /// Provides the possible references types for this expression (used in semantic analysis) /// </summary> /// <param name="instance">the reference instance on which this element should analysed</param> /// <paraparam name="expectation">Indicates the kind of element we are looking for</paraparam> /// <param name="last">indicates that this is the last element in a dereference chain</param> /// <returns></returns> public ReturnValue getReferenceTypes(Utils.INamable instance, Filter.AcceptableChoice expectation, bool last) { ReturnValue retVal = null; if (Designator != null) { retVal = new ReturnValue(); foreach (ReturnValueElement element in Designator.getReferences(instance, expectation, last).Values) { if (element.Value is Types.Type) { retVal.Add(element.Value); } } } else if (LiteralValue != null) { retVal = LiteralValue.getReferenceTypes(instance, expectation, true); } return retVal; }
/// <summary> /// Fills the retVal result set according to the subDeclarator class provided as parameter /// </summary> /// <param name="subDeclarator">The subdeclarator used to get the image</param> /// <param name="expectation">The expectatino of the desired element</param> /// <param name="location">The location of the element found</param> /// <param name="values">The return value to update</param> /// <return>the number of elements added</return> private int FillBySubdeclarator(Utils.ISubDeclarator subDeclarator, Filter.AcceptableChoice expectation, ReturnValue values) { int retVal = 0; if (subDeclarator != null) { List<Utils.INamable> tmp = new List<Utils.INamable>(); subDeclarator.Find(Image, tmp); foreach (Utils.INamable namable in tmp) { addReference(namable, expectation, values); retVal += 1; } } return retVal; }
/// <summary> /// Adds a reference which satisfies the provided expectation in the result set /// </summary> /// <param name="namable"></param> /// <param name="expectation"></param> /// <param name="resultSet"></param> private void addReference(INamable namable, Filter.AcceptableChoice expectation, ReturnValue resultSet) { if (namable != null) { if (expectation(namable)) { resultSet.Add(namable); } } }
/// <summary> /// Provides the possible references for this designator (only available during semantic analysis) /// </summary> /// <param name="instance">the instance on which this element should be found.</param> /// <param name="expectation">the expectation on the element found</param> /// <param name="lastElement">Indicates that this element is the last one in a dereference chain</param> /// <returns></returns> public ReturnValue getReferences(INamable instance, Filter.AcceptableChoice expectation, bool lastElement) { ReturnValue retVal = new ReturnValue(this); if (instance == null) { // Special handling for THIS if (Image.CompareTo("THIS") == 0) { INamable currentElem = Root; while (currentElem != null) { Types.Type type = currentElem as Types.Type; if (type != null) { Types.StateMachine stateMachine = type as Types.StateMachine; while (stateMachine != null) { type = stateMachine; stateMachine = stateMachine.EnclosingStateMachine; } retVal.Add(type); return retVal; } currentElem = enclosing(currentElem); } return retVal; } // No enclosing instance. Try to first name of a . separated list of names // . First in the enclosing expression InterpreterTreeNode current = this; while (current != null) { ISubDeclarator subDeclarator = current as ISubDeclarator; if (FillBySubdeclarator(subDeclarator, expectation, retVal) > 0) { // If this is the last element in the dereference chain, stop at first match if (lastElement) { return retVal; } current = null; } else { current = current.Enclosing; } } // . In the predefined elements addReference(EFSSystem.getPredefinedItem(Image), expectation, retVal); if (lastElement && !retVal.IsEmpty) { return retVal; } // . In the enclosing items, except the enclosing dictionary since dictionaries are handled in a later step INamable currentNamable = Root; while (currentNamable != null) { Utils.ISubDeclarator subDeclarator = currentNamable as Utils.ISubDeclarator; if (subDeclarator != null && !(subDeclarator is Dictionary)) { if (FillBySubdeclarator(subDeclarator, expectation, retVal) > 0 && lastElement) { return retVal; } } currentNamable = enclosingSubDeclarator(currentNamable); } // . In the dictionaries declared in the system foreach (Dictionary dictionary in EFSSystem.Dictionaries) { if (FillBySubdeclarator(dictionary, expectation, retVal) > 0 && lastElement) { return retVal; } Types.NameSpace defaultNameSpace = dictionary.findNameSpace("Default"); if (defaultNameSpace != null) { if (FillBySubdeclarator(defaultNameSpace, expectation, retVal) > 0 && lastElement) { return retVal; } } } } else { // The instance is provided, hence, this is not the first designator in the . separated list of designators if (instance is Types.ITypedElement && !(instance is Constants.State)) { // If the instance is a typed element, dereference it to its corresponding type Types.ITypedElement element = instance as Types.ITypedElement; if (element.Type != EFSSystem.NoType) { instance = element.Type; } } // Find the element in all enclosing sub declarators of the instance while (instance != null) { Utils.ISubDeclarator subDeclarator = instance as Utils.ISubDeclarator; if (FillBySubdeclarator(subDeclarator, expectation, retVal) > 0) { instance = null; } else { if (instance is Dictionary) { instance = enclosingSubDeclarator(instance); } else { instance = null; } } } } return retVal; }