Пример #1
0
        /// <summary>
        ///     Finds a symbol in a sub declarator and adds it to retVal
        /// </summary>
        /// <param name="subDeclarator"></param>
        /// <param name="name"></param>
        /// <param name="retVal"></param>
        public static void Find(ISubDeclarator subDeclarator, string name, List <INamable> retVal)
        {
            if (subDeclarator != null)
            {
                CriticalSection.WaitOne();
                try
                {
                    // Ensure that the declared elements are initialized
                    if (subDeclarator.DeclaredElements == null)
                    {
                        subDeclarator.InitDeclaredElements();
                    }

                    if (name != null)
                    {
                        List <INamable> tmp;
                        if (subDeclarator.DeclaredElements.TryGetValue(name, out tmp))
                        {
                            retVal.AddRange(tmp);
                        }
                    }
                }
                finally
                {
                    CriticalSection.ReleaseMutex();
                }
            }
        }
Пример #2
0
        /// <summary>
        ///     Indicates whether the declarator contains the value provided as parameter
        /// </summary>
        /// <param name="subDeclarator"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool ContainsValue(ISubDeclarator subDeclarator, INamable value)
        {
            bool retVal = false;

            if (subDeclarator != null)
            {
                CriticalSection.WaitOne();
                try
                {
                    List <INamable> tmp;
                    if (subDeclarator.DeclaredElements.TryGetValue(value.Name, out tmp))
                    {
                        foreach (INamable namable in tmp)
                        {
                            if (namable == value)
                            {
                                retVal = true;
                                break;
                            }
                        }
                    }
                }
                finally
                {
                    CriticalSection.ReleaseMutex();
                }
            }

            return(retVal);
        }
Пример #3
0
        /// <summary>
        /// Provides all the values of type T along with their identification name
        /// </summary>
        /// <param name="root">The root element where search must be conducted</param>
        /// <param name="scope">The namespace where elements are declared</param>
        /// <param name="retVal">The result values</param>
        /// <param name="depth">The maximum depth in which the find must go</param>
        private void innerFindAllValues(object root, string scope, Dictionary <string, T> retVal, int depth)
        {
            if (root is ISubDeclarator)
            {
                ISubDeclarator subDeclarator = root as ISubDeclarator;
                if (subDeclarator != null)
                {
                    if (subDeclarator.DeclaredElements == null)
                    {
                        subDeclarator.InitDeclaredElements();
                    }
                    foreach (KeyValuePair <string, List <INamable> > element in subDeclarator.DeclaredElements)
                    {
                        string name = Utils.concat(scope, element.Key);

                        List <INamable> values = element.Value;
                        foreach (INamable value in values)
                        {
                            if (!retVal.ContainsKey(name) && value is T)
                            {
                                retVal[name] = value as T;
                            }
                            if (depth < MAX_DEPTH)
                            {
                                innerFindAllValues(value, name, retVal, depth + 1);
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Provides the reference for this subdeclarator
        /// </summary>
        /// <param name="subDeclarator"></param>
        /// <returns></returns>
        private INamable getReferenceBySubDeclarator(ISubDeclarator subDeclarator)
        {
            INamable retVal = null;

            List <INamable> tmp = new List <INamable>();

            subDeclarator.Find(Image, tmp);
            if (tmp.Count > 0)
            {
                // Remove duplicates
                List <INamable> tmp2 = new List <INamable>();
                foreach (INamable namable in tmp)
                {
                    bool found = false;
                    foreach (INamable other in tmp2)
                    {
                        if (namable == other)
                        {
                            found = true;
                            break;
                        }
                    }

                    if (!found)
                    {
                        tmp2.Add(namable);

                        // Consistency check.
                        Variables.IVariable subDeclVar = subDeclarator as Variables.Variable;
                        if (subDeclVar != null)
                        {
                            if (((IEnclosed)namable).Enclosing != subDeclVar.Value)
                            {
                                AddError("Consistency check failed : enclosed element's father relationship is inconsistent");
                            }
                        }
                        else
                        {
                            if (((IEnclosed)namable).Enclosing != subDeclarator)
                            {
                                AddError("Consistency check failed : enclosed element's father relationship is inconsistent");
                            }
                        }
                    }
                }

                // Provide the result, if it is unique
                if (tmp2.Count == 1)
                {
                    retVal = tmp2[0];
                }
            }

            return(retVal);
        }
Пример #5
0
 /// <summary>
 /// Appends a namable in a dictionary
 /// </summary>
 /// <param name="subDeclarator"></param>
 /// <param name="name"></param>
 /// <param name="namable"></param>
 public static void AppendNamable(ISubDeclarator subDeclarator, string name, INamable namable)
 {
     if (namable != null)
     {
         if (!subDeclarator.DeclaredElements.ContainsKey(name))
         {
             subDeclarator.DeclaredElements[name] = new List <INamable>();
         }
         subDeclarator.DeclaredElements[name].Add(namable);
     }
 }
Пример #6
0
        /// <summary>
        ///     Provides the enclosing sub declarator
        /// </summary>
        /// <param name="modelElement"></param>
        /// <returns></returns>
        private static ISubDeclarator EnclosingSubDeclarator(IModelElement modelElement)
        {
            ISubDeclarator retVal = null;

            while (modelElement != null && retVal == null)
            {
                retVal       = modelElement as ISubDeclarator;
                modelElement = modelElement.Enclosing as IModelElement;
            }

            return(retVal);
        }
Пример #7
0
        /// <summary>
        /// Finds a symbol in a sub declarator and adds it to retVal
        /// </summary>
        /// <param name="subDeclarator"></param>
        /// <param name="name"></param>
        /// <param name="retVal"></param>
        public static void Find(ISubDeclarator subDeclarator, string name, List <INamable> retVal)
        {
            // Ensure that the declared elements are initialized
            if (subDeclarator.DeclaredElements == null)
            {
                subDeclarator.InitDeclaredElements();
            }

            List <INamable> tmp;

            if (subDeclarator.DeclaredElements.TryGetValue(name, out tmp))
            {
                retVal.AddRange(tmp);
            }
        }
Пример #8
0
        /// <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);
        }
Пример #9
0
            /// <summary>
            ///     Cleans up the declared elements dictionaries
            /// </summary>
            /// <param name="obj"></param>
            /// <param name="visitSubNodes"></param>
            public override void visit(IXmlBBase obj, bool visitSubNodes)
            {
                ModelElement modelElement = obj as ModelElement;

                if (modelElement != null && ClearCaches)
                {
                    modelElement.ClearCache();
                }

                ISubDeclarator subDeclarator = obj as ISubDeclarator;

                if (subDeclarator != null)
                {
                    subDeclarator.InitDeclaredElements();
                }

                base.visit(obj, visitSubNodes);
            }
Пример #10
0
 /// <summary>
 ///     Appends a namable in a dictionary
 /// </summary>
 /// <param name="subDeclarator"></param>
 /// <param name="name"></param>
 /// <param name="namable"></param>
 public static void AppendNamable(ISubDeclarator subDeclarator, string name, INamable namable)
 {
     if (namable != null && subDeclarator != null && subDeclarator.DeclaredElements != null)
     {
         CriticalSection.WaitOne();
         try
         {
             if (!subDeclarator.DeclaredElements.ContainsKey(name))
             {
                 subDeclarator.DeclaredElements[name] = new List <INamable>();
             }
             subDeclarator.DeclaredElements[name].Add(namable);
         }
         finally
         {
             CriticalSection.ReleaseMutex();
         }
     }
 }
Пример #11
0
        /// <summary>
        /// Indicates whether the declarator contains the value provided as parameter
        /// </summary>
        /// <param name="subDeclarator"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool ContainsValue(ISubDeclarator subDeclarator, INamable value)
        {
            bool retVal = false;

            List <INamable> tmp;

            if (subDeclarator.DeclaredElements.TryGetValue(value.Name, out tmp))
            {
                foreach (INamable namable in tmp)
                {
                    if (namable == value)
                    {
                        retVal = true;
                        break;
                    }
                }
            }

            return(retVal);
        }
Пример #12
0
        /// <summary>
        /// Considers this sub declarator to create the list of choices
        /// </summary>
        /// <param name="prefix"></param>
        /// <param name="subDeclarator"></param>
        /// <param name="retVal"></param>
        private static void ConsiderSubDeclarator(string prefix, ISubDeclarator subDeclarator, ICollection <ObjectReference> retVal)
        {
            IVariable variable = subDeclarator as IVariable;

            if (variable != null)
            {
                subDeclarator = variable.Type as ISubDeclarator;
            }

            if (subDeclarator != null)
            {
                foreach (KeyValuePair <string, List <INamable> > pair in subDeclarator.DeclaredElements)
                {
                    if (pair.Key.StartsWith(prefix))
                    {
                        foreach (INamable namable in pair.Value)
                        {
                            retVal.Add(new ObjectReference(pair.Key, namable));
                        }
                    }
                }
            }
        }
Пример #13
0
        /// <summary>
        ///     Provides the reference for this subdeclarator
        /// </summary>
        /// <param name="subDeclarator"></param>
        /// <returns></returns>
        private INamable GetReferenceBySubDeclarator(ISubDeclarator subDeclarator)
        {
            INamable retVal = null;

            List<INamable> tmp = new List<INamable>();
            subDeclarator.Find(Image, tmp);
            if (tmp.Count > 0)
            {
                // Remove duplicates
                List<INamable> tmp2 = new List<INamable>();
                foreach (INamable namable in tmp)
                {
                    bool found = false;
                    foreach (INamable other in tmp2)
                    {
                        if (namable == other)
                        {
                            found = true;
                            break;
                        }
                    }

                    if (!found)
                    {
                        tmp2.Add(namable);

                        if (EfsSystem.Instance.CheckParentRelationship && !(namable is EmptyValue))
                        {
                            // Consistency check.
                            // Empty value should not be considered because we can dereference 'Empty'
                            IVariable subDeclVar = subDeclarator as IVariable;
                            object enclosed = ((IEnclosed) namable).Enclosing;
                            if (subDeclVar != null)
                            {
                                if (enclosed != subDeclVar.Value)
                                {
                                    AddError("Consistency check failed : enclosed element's father relationship is inconsistent", RuleChecksEnum.SemanticAnalysisError);
                                }
                            }
                            else
                            {
                                if (enclosed != subDeclarator)
                                {
                                    // There is still an exception : when the element is declared in the default namespace
                                    if (subDeclarator != EfsSystem.Instance ||
                                        enclosed != EfsSystem.Instance.FindByFullName("Default"))
                                    {
                                        AddError(
                                            "Consistency check failed : enclosed element's father relationship is inconsistent", RuleChecksEnum.SemanticAnalysisError);
                                    }
                                }
                            }
                        }
                    }
                }

                // Provide the result, if it is unique
                if (tmp2.Count == 1)
                {
                    retVal = tmp2[0];
                }
            }

            return retVal;
        }
Пример #14
0
        /// <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;
        }
Пример #15
0
        /// <summary>
        /// Finds an element in the model based on its decomposed name, where the index indicates which element have already been matched
        /// </summary>
        /// <param name="root">the root where search need be conducted</param>
        /// <param name="names">the decomposed name</param>
        /// <param name="index">the index where elements have already been matched</param>
        /// <param name="skipDefault">Indicates that the default namespace should not be considered as empty in the name</param>
        /// <returns></returns>
        private T findByName(object root, string[] names, int index, bool skipDefault)
        {
            T retVal = null;

            if (root != null)
            {
                if (root is ISubDeclarator && index < names.Length)
                {
                    ISubDeclarator declarator = root as ISubDeclarator;

                    // This is the last name to check and all names match.
                    // Provide the first one with the correct type
                    List <INamable> values = new List <INamable>();
                    declarator.Find(names[index], values);
                    foreach (INamable namable in values)
                    {
                        if (index == names.Length - 1)
                        {
                            retVal = namable as T;
                            if (retVal != null)
                            {
                                break;
                            }
                        }
                        else
                        {
                            retVal = findByName(namable, names, index + 1, skipDefault);
                            if (retVal != null)
                            {
                                break;
                            }
                        }
                    }

                    if (retVal == null && !skipDefault)
                    {
                        values.Clear();
                        declarator.Find("Default", values);

                        foreach (INamable namable in values)
                        {
                            retVal = findByName(namable, names, index, true);
                            if (retVal != null)
                            {
                                break;
                            }
                        }
                    }
                }

                // Nothing has been found under this node, try with the enclosing node
                if (retVal == null && index == 0)
                {
                    if (root is IModelElement)
                    {
                        IModelElement elt = root as IModelElement;
                        if (elt.Enclosing != null)
                        {
                            retVal = findByName(elt.Enclosing, names, 0, skipDefault);
                        }
                    }
                }
            }

            return(retVal);
        }
Пример #16
0
        /// <summary>
        /// Provides the reference for this subdeclarator
        /// </summary>
        /// <param name="subDeclarator"></param>
        /// <returns></returns>
        private INamable getReferenceBySubDeclarator(ISubDeclarator subDeclarator)
        {
            INamable retVal = null;

            List<INamable> tmp = new List<INamable>();
            subDeclarator.Find(Image, tmp);
            if (tmp.Count > 0)
            {
                // Remove duplicates
                List<INamable> tmp2 = new List<INamable>();
                foreach (INamable namable in tmp)
                {
                    bool found = false;
                    foreach (INamable other in tmp2)
                    {
                        if (namable == other)
                        {
                            found = true;
                            break;
                        }
                    }

                    if (!found)
                    {
                        tmp2.Add(namable);

                        // Consistency check.
                        Variables.IVariable subDeclVar = subDeclarator as Variables.Variable;
                        if (subDeclVar != null)
                        {
                            if (((IEnclosed)namable).Enclosing != subDeclVar.Value)
                            {
                                AddError("Consistency check failed : enclosed element's father relationship is inconsistent");
                            }
                        }
                        else
                        {
                            if (((IEnclosed)namable).Enclosing != subDeclarator)
                            {
                                AddError("Consistency check failed : enclosed element's father relationship is inconsistent");
                            }

                        }
                    }
                }

                // Provide the result, if it is unique
                if (tmp2.Count == 1)
                {
                    retVal = tmp2[0];
                }
            }

            return retVal;
        }
Пример #17
0
        /// <summary>
        ///     Provides the element referenced by this designator, given the enclosing element
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public INamable GetReference(InterpretationContext context)
        {
            INamable retVal = null;

            switch (Location)
            {
            case LocationEnum.Stack:
                retVal = context.LocalScope.GetVariable(Image);

                if (retVal == null)
                {
                    AddError(Image + " not found on the stack", RuleChecksEnum.ExecutionFailed);
                }
                break;

            case LocationEnum.Instance:
                INamable instance = context.Instance;

                while (instance != null)
                {
                    ISubDeclarator subDeclarator = instance as ISubDeclarator;
                    if (subDeclarator != null)
                    {
                        INamable tmp = GetReferenceBySubDeclarator(subDeclarator);
                        if (tmp != null)
                        {
                            if (retVal == null)
                            {
                                retVal   = tmp;
                                instance = null;
                            }
                        }
                    }

                    instance = EnclosingSubDeclarator(instance);
                }

                if (retVal == null)
                {
                    AddError(Image + " not found in the current instance " + context.Instance.Name, RuleChecksEnum.ExecutionFailed);
                }
                break;

            case LocationEnum.This:
                retVal = context.Instance;
                break;

            case LocationEnum.Enclosing:
                ITypedElement typedElement = context.Instance as ITypedElement;
                while (typedElement != null && !(typedElement.Type is Structure))
                {
                    typedElement = typedElement.Enclosing as ITypedElement;
                }
                retVal = typedElement;
                break;


            case LocationEnum.Model:
                retVal = Ref;

                if (retVal == null)
                {
                    AddError(Image + " not found in the enclosing model", RuleChecksEnum.ExecutionFailed);
                }
                break;

            case LocationEnum.NotDefined:
                AddError("Semantic analysis not performed on " + ToString(), RuleChecksEnum.ExecutionFailed);
                break;
            }

            return(retVal);
        }
Пример #18
0
        /// <summary>
        ///     Provides the reference for this subdeclarator
        /// </summary>
        /// <param name="subDeclarator"></param>
        /// <returns></returns>
        private INamable GetReferenceBySubDeclarator(ISubDeclarator subDeclarator)
        {
            INamable retVal = null;

            List <INamable> tmp = new List <INamable>();

            subDeclarator.Find(Image, tmp);
            if (tmp.Count > 0)
            {
                // Remove duplicates
                List <INamable> tmp2 = new List <INamable>();
                foreach (INamable namable in tmp)
                {
                    bool found = false;
                    foreach (INamable other in tmp2)
                    {
                        if (namable == other)
                        {
                            found = true;
                            break;
                        }
                    }

                    if (!found)
                    {
                        tmp2.Add(namable);

                        if (EfsSystem.Instance.CheckParentRelationship && !(namable is EmptyValue))
                        {
                            // Consistency check.
                            // Empty value should not be considered because we can dereference 'Empty'
                            IVariable subDeclVar = subDeclarator as IVariable;
                            object    enclosed   = ((IEnclosed)namable).Enclosing;
                            if (subDeclVar != null)
                            {
                                if (enclosed != subDeclVar.Value)
                                {
                                    AddError("Consistency check failed : enclosed element's father relationship is inconsistent", RuleChecksEnum.SemanticAnalysisError);
                                }
                            }
                            else
                            {
                                if (enclosed != subDeclarator)
                                {
                                    // There is still an exception : when the element is declared in the default namespace
                                    if (subDeclarator != EfsSystem.Instance ||
                                        enclosed != EfsSystem.Instance.FindByFullName("Default"))
                                    {
                                        AddError(
                                            "Consistency check failed : enclosed element's father relationship is inconsistent", RuleChecksEnum.SemanticAnalysisError);
                                    }
                                }
                            }
                        }
                    }
                }

                // Provide the result, if it is unique
                if (tmp2.Count == 1)
                {
                    retVal = tmp2[0];
                }
            }

            return(retVal);
        }
        /// <summary>
        /// Considers this sub declarator to create the list of choices
        /// </summary>
        /// <param name="prefix"></param>
        /// <param name="subDeclarator"></param>
        /// <param name="retVal"></param>
        private static void ConsiderSubDeclarator(string prefix, ISubDeclarator subDeclarator, ICollection<ObjectReference> retVal)
        {
            IVariable variable = subDeclarator as IVariable;
            if (variable != null)
            {
                subDeclarator = variable.Type as ISubDeclarator;
            }

            if (subDeclarator != null)
            {
                foreach (KeyValuePair<string, List<INamable>> pair in subDeclarator.DeclaredElements)
                {
                    if (pair.Key.StartsWith(prefix))
                    {
                        foreach (INamable namable in pair.Value)
                        {
                            retVal.Add(new ObjectReference(pair.Key, namable));
                        }
                    }
                }
            }
        }
Пример #20
0
        /// <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);
        }
Пример #21
0
        /// <summary>
        ///     Provides the list of model elements which correspond to the prefix given
        /// </summary>
        /// <param name="index">The location of the cursor in the text box</param>
        /// <param name="prefix">The prefix used to reduce the choices</param>
        /// <returns></returns>
        public SortedSet <ObjectReference> AllChoices(int index, string prefix)
        {
            SortedSet <ObjectReference> retVal = new SortedSet <ObjectReference>();

            double val;
            bool   isANumber = double.TryParse(CurrentPrefix(index - 1).Trim(), NumberStyles.Any, CultureInfo.InvariantCulture, out val);

            if (!isANumber)
            {
                ISubDeclarator subDeclarator = null;

                INamable namable = GetInstance(index);
                if (namable == null)
                {
                    if (Text[index] != '.')
                    {
                        // Out of context search, create the corresponding context according to the current instance
                        subDeclarator = EnclosingSubDeclarator(Instance as IModelElement);
                        while (!(subDeclarator is Dictionary))
                        {
                            ConsiderSubDeclarator(prefix, subDeclarator, retVal);
                            subDeclarator =
                                EnclosingSubDeclarator(((IModelElement)subDeclarator).Enclosing as IModelElement);
                        }

                        // Consider toplevel elements
                        ConsiderSubDeclarator(prefix, EfsSystem.Instance, retVal);
                        foreach (Dictionary dictionary in EfsSystem.Instance.Dictionaries)
                        {
                            ConsiderSubDeclarator(prefix, dictionary, retVal);
                        }

                        // Also add the templates
                        foreach (string template in Templates)
                        {
                            if (template.StartsWith(prefix))
                            {
                                retVal.Add(new ObjectReference(template, null));
                            }
                        }
                    }
                }
                else
                {
                    subDeclarator = namable as ISubDeclarator;
                    if (subDeclarator == null)
                    {
                        ITypedElement typedElement = namable as ITypedElement;
                        if (typedElement != null && typedElement.Type is Structure)
                        {
                            subDeclarator = typedElement.Type as ISubDeclarator;
                        }
                    }
                }

                if (subDeclarator != null)
                {
                    ConsiderSubDeclarator(prefix, subDeclarator, retVal);
                }
            }

            return(retVal);
        }
Пример #22
0
        /// <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);
        }
Пример #23
0
        /// <summary>
        /// Provides the element referenced by this designator, given the enclosing element
        /// </summary>
        /// <param name="enclosing"></param>
        /// <returns></returns>
        public INamable getReference(InterpretationContext context)
        {
            INamable retVal = null;

            switch (Location)
            {
            case LocationEnum.Stack:
                retVal = context.LocalScope.getVariable(Image);

                if (retVal == null)
                {
                    AddError(Image + " not found on the stack");
                }
                break;

            case LocationEnum.Instance:
                Utils.INamable instance = context.Instance;

                while (instance != null)
                {
                    ISubDeclarator subDeclarator = instance as ISubDeclarator;
                    if (subDeclarator != null)
                    {
                        INamable tmp = getReferenceBySubDeclarator(subDeclarator);
                        if (tmp != null)
                        {
                            if (retVal == null)
                            {
                                retVal   = tmp;
                                instance = null;
                            }
                        }
                    }

                    instance = enclosingSubDeclarator(instance);
                }

                if (retVal == null)
                {
                    AddError(Image + " not found in the current instance " + context.Instance.Name);
                }
                break;

            case LocationEnum.This:
                retVal = context.Instance;
                break;

            case LocationEnum.Model:
                retVal = Ref;

                if (retVal == null)
                {
                    AddError(Image + " not found in the enclosing model");
                }
                break;

            case LocationEnum.NotDefined:
                AddError("Semantic analysis not performed on " + ToString());
                break;
            }

            return(retVal);
        }