Beispiel #1
0
        /// <summary>
        /// Process all of the reachable children and return the list of found items
        /// </summary>
        /// <param name="originalToProcess"></param>
        /// <remarks></remarks>
        private List <NativeSymbolRelationship> FindAllReachableChildrenImpl(IEnumerable <NativeSymbol> originalToProcess)
        {
            List <NativeSymbolRelationship> found   = new List <NativeSymbolRelationship>();
            Dictionary <NativeSymbol, bool> map     = new Dictionary <NativeSymbol, bool>();
            Queue <NativeSymbol>            toVisit = new Queue <NativeSymbol>(originalToProcess);

            // First add in all of the original symbols with no parents
            foreach (NativeSymbol orig in originalToProcess)
            {
                found.Add(new NativeSymbolRelationship(null, orig));
            }

            while (toVisit.Count > 0)
            {
                NativeSymbol cur = toVisit.Dequeue();
                if (map.ContainsKey(cur))
                {
                    continue;
                }

                map[cur] = true;
                foreach (NativeSymbol child in cur.GetChildren())
                {
                    found.Add(new NativeSymbolRelationship(cur, child));
                    toVisit.Enqueue(child);
                }
            }

            return(found);
        }
Beispiel #2
0
        public List <NativeSymbolRelationship> FindAllNativeSymbolRelationships(NativeSymbol ns)
        {
            List <NativeSymbol> list = new List <NativeSymbol>();

            list.Add(ns);
            return(FindAllNativeSymbolRelationships(list));
        }
 public void CollapseTypedefs(NativeSymbol ns)
 {
     foreach (NativeSymbolRelationship rel in _it.FindAllNativeSymbolRelationships(ns))
     {
         CollapseTypedefsImpl(rel.Parent, rel.Symbol);
     }
 }
 public NativeGlobalSymbol(NativeName name, NativeSymbol symbol)
 {
     Contract.Requires(name.SymbolKind == symbol.Kind);
     Contract.Requires(name.Name == symbol.Name);
     Contract.Requires(name == NativeNameUtil.GetName(symbol));
     Name   = name;
     Symbol = symbol;
 }
Beispiel #5
0
        private void TestRoundTrip(NativeSymbol symbol)
        {
            var storage      = new BasicSymbolStorage();
            var name         = NativeNameUtil.GetName(symbol);
            var globalSymbol = new NativeGlobalSymbol(name, symbol);

            storage.Add(globalSymbol);
            TestRoundTrip(storage);
        }
 /// <summary>
 /// Renames matching defined types and named types to the new name
 /// </summary>
 /// <param name="ns"></param>
 /// <param name="oldName"></param>
 /// <param name="newName"></param>
 /// <remarks></remarks>
 public void RenameTypeSymbol(NativeSymbol ns, string oldName, string newName)
 {
     foreach (NativeSymbol sym in _it.FindAllNativeSymbols(ns))
     {
         if ((sym.Category == NativeSymbolCategory.Defined || sym.Kind == NativeSymbolKind.NamedType) && 0 == string.CompareOrdinal(sym.Name, oldName))
         {
             sym.Name = newName;
         }
     }
 }
Beispiel #7
0
        public static NativeName GetName(NativeSymbol symbol)
        {
            NativeName name;

            if (!TryGetName(symbol, out name))
            {
                throw new Exception($"Unable to create name for {symbol.Name} {symbol.Kind}");
            }

            return(name);
        }
Beispiel #8
0
        public static string Convert(NativeSymbol sym)
        {
            string str = sym.Name;

            foreach (NativeSymbol child in sym.GetChildren())
            {
                str += "(" + Convert(child) + ")";
            }

            return(str);
        }
Beispiel #9
0
        public List <NativeSymbol> FindAllNativeSymbols(NativeSymbol ns)
        {
            if (ns == null)
            {
                throw new ArgumentNullException("ns");
            }

            List <NativeSymbol> list = new List <NativeSymbol>();

            list.Add(ns);
            return(FindAllNativeSymbols(list));
        }
        private void EnsureIsParent(NativeSymbol sym, List <NativeSymbolRelationship> list)
        {
            foreach (NativeSymbolRelationship rel in list)
            {
                if (object.ReferenceEquals(sym, rel.Parent))
                {
                    return;
                }
            }

            throw new Exception("Could Not find the symbol");
        }
Beispiel #11
0
        public static bool TryGetName(NativeSymbol symbol, out NativeName name)
        {
            NativeNameKind kind;

            if (!TryGetNativeNameKind(symbol.Kind, out kind))
            {
                name = NativeName.Nil;
                return(false);
            }

            name = new NativeName(symbol.Name, kind);
            return(true);
        }
Beispiel #12
0
        private bool IsResolved(NativeSymbol ns, Dictionary <NativeSymbol, bool?> map)
        {
            ThrowIfNull(ns);
            ThrowIfNull(map);

            // See if this has already been calculated
            bool?ret = false;

            if (map.TryGetValue(ns, out ret))
            {
                if (ret.HasValue)
                {
                    return(ret.Value);
                }
                else
                {
                    // We're in a recursive call to the same type.  Return true here because if another type is
                    // not resolved then this will fall out
                    return(true);
                }
            }

            // If there are no immediate children then the type is most definately resolved
            NativeSymbolIterator it       = new NativeSymbolIterator();
            List <NativeSymbol>  children = new List <NativeSymbol>(ns.GetChildren());

            if (children.Count == 0)
            {
                return(true);
            }

            // Add an entry into the map to indicate that we are exploring this type
            map.Add(ns, null);

            ret = true;
            foreach (NativeSymbol child in children)
            {
                if (!child.IsImmediateResolved || !IsResolved(child, map))
                {
                    ret = false;
                    break;
                }
            }

            // Save the success
            map[ns] = ret;
            return(ret.Value);
        }
Beispiel #13
0
        private string Print(NativeSymbol ns)
        {
            if (ns == null)
            {
                return("<Nothing>");
            }

            string str = ns.Name;

            foreach (NativeSymbol child in ns.GetChildren())
            {
                str += "(" + Print(child) + ")";
            }

            return(str);
        }
Beispiel #14
0
        private bool TryGetValueCore(string name, out NativeSymbol symbol, out bool loadedFromNextLookup)
        {
            if (_storage.TryGetValue(name, out symbol))
            {
                loadedFromNextLookup = false;
                return(true);
            }

            if (_nextSymbolLookup.TryGetValue(name, out symbol))
            {
                loadedFromNextLookup = true;
                return(true);
            }

            loadedFromNextLookup = false;
            return(false);
        }
        private void CollapseTypedefsImpl(NativeSymbol ns, NativeSymbol child)
        {
            if (ns == null)
            {
                return;
            }
            ThrowIfNull(child);

            if (child.Kind == NativeSymbolKind.TypeDefType)
            {
                NativeTypeDef typedef = (NativeTypeDef)child;
                if (typedef.RealType != null)
                {
                    ns.ReplaceChild(child, typedef.RealType);
                }
            }
        }
        private void CollapseNamedTypesImpl(NativeSymbol ns, NativeSymbol child)
        {
            if (ns == null)
            {
                return;
            }
            ThrowIfNull(child);

            if (child.Kind == NativeSymbolKind.NamedType)
            {
                NativeNamedType namedNt = (NativeNamedType)child;
                if (namedNt.RealType != null)
                {
                    ns.ReplaceChild(child, namedNt.RealType);
                }
            }
        }
Beispiel #17
0
        /// <summary>
        /// Try and resolve the unresolved values in the system.
        /// </summary>
        private ResolveResult ResolveCoreValues(ErrorProvider ep)
        {
            var succeeded    = true;
            var needMoreWork = false;

            foreach (NativeValue nValue in this.FindUnresolvedNativeValues())
            {
                var loadedFromNextLookup = false;
                switch (nValue.ValueKind)
                {
                case NativeValueKind.SymbolValue:
                {
                    NativeSymbol symbol = null;
                    if (TryGetValueCore(nValue.Name, out symbol, out loadedFromNextLookup))
                    {
                        nValue.Value = symbol;
                    }
                }
                break;

                case NativeValueKind.SymbolType:
                {
                    NativeType type = null;
                    if (TryFindTypeCore(nValue.Name, out type, out loadedFromNextLookup))
                    {
                        nValue.Value = type;
                    }
                }
                break;
                }

                if (!nValue.IsImmediateResolved)
                {
                    ep.AddError($"Failed to resolve value '{nValue.Name}'");
                    succeeded = false;
                }

                if (loadedFromNextLookup)
                {
                    needMoreWork = true;
                }
            }

            return(new ResolveResult(succeeded, needMoreWork));
        }
Beispiel #18
0
        /// <summary>
        /// Convert a NativeValueExpression into managed code and make it the initialization expression of
        /// the passed in member.
        ///
        /// If the code is unable to generate a valid expression for the member it will make the expression
        /// a stringized version of the original native expression.  It will add information in the comments
        /// about why it could not properly generate the expression.  Lastly it will generate incompatible types
        /// to force a compile error
        /// </summary>
        /// <param name="member"></param>
        /// <param name="ntExpr"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        public bool GenerateInitExpression(CodeMemberField member, NativeSymbol target, NativeValueExpression ntExpr)
        {
            if (ntExpr == null)
            {
                member.Comments.Add(new CodeCommentStatement("Error: No value expression", true));
                member.InitExpression = new CodePrimitiveExpression(string.Empty);
                member.Type           = new CodeTypeReference(typeof(int));
                return(false);
            }

            member.Comments.Add(new CodeCommentStatement(string.Format("{0} -> {1}", member.Name, ntExpr.Expression), true));

            // It's not legal for a symbol to be used as part of it's initialization expression in most languages.
            // There for we need to mark it as the initialization member so the generated will output NULL in it's place
            _symbolValueMap.Add(target.Name, target);
            try
            {
                Exception         ex = null;
                CodeExpression    expr;
                CodeTypeReference typeRef;
                if (TryGenerateValueExpression(ntExpr, out expr, out typeRef, out ex))
                {
                    member.InitExpression = expr;
                    member.Type           = typeRef;
                    return(true);
                }
                else
                {
                    member.Comments.Add(new CodeCommentStatement(string.Format("Error generating expression: {0}", ex.Message), true));
                    member.InitExpression = new CodePrimitiveExpression(ntExpr.Expression);
                    member.Type           = new CodeTypeReference(typeof(string));
                    return(false);
                }
            }
            finally
            {
                _symbolValueMap.Remove(target.Name);
            }
        }
Beispiel #19
0
        private CodeExpression GenerateValueExpressionLeaf(ExpressionNode node, ref CodeTypeReference leafType)
        {
            ThrowIfNull(node);

            var ntVal = NativeValue.TryCreateForLeaf(node, _bag);

            if (ntVal == null)
            {
                throw new InvalidOperationException("Expected a NativeValue");
            }

            if (!ntVal.IsValueResolved)
            {
                throw new InvalidOperationException(string.Format("Value {0} is not resolved", ntVal.Name));
            }

            switch (ntVal.ValueKind)
            {
            case NativeValueKind.Number:
                leafType = new CodeTypeReference(ntVal.Value.GetType());
                return(new CodePrimitiveExpression(ntVal.Value));

            case NativeValueKind.Boolean:
                leafType = new CodeTypeReference(typeof(bool));
                return(new CodePrimitiveExpression(ntVal.Value));

            case NativeValueKind.String:
                leafType = new CodeTypeReference(typeof(string));
                return(new CodePrimitiveExpression(ntVal.Value));

            case NativeValueKind.Character:
                leafType = new CodeTypeReference(typeof(char));
                return(new CodePrimitiveExpression(ntVal.Value));

            case NativeValueKind.SymbolValue:
                NativeSymbol ns = ntVal.SymbolValue;

                // Prevent the generation of a circular reference
                if (_symbolValueMap.ContainsKey(ns.Name))
                {
                    leafType = new CodeTypeReference(typeof(object));
                    return(new CodePrimitiveExpression(null));
                }

                switch (ns.Kind)
                {
                case NativeSymbolKind.Constant:
                    leafType = CalculateConstantType((NativeConstant)ns);
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(TransformConstants.NativeConstantsName), ns.Name));

                case NativeSymbolKind.EnumType:
                    leafType = this.GenerateTypeReference((NativeEnum)ns);
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(ns.Name), ntVal.Name));

                default:
                    throw new InvalidOperationException(string.Format("Generation of {0} not supported as a value", ns.Kind));
                }

            case NativeValueKind.SymbolType:
                throw new InvalidOperationException("Types are not supported as leaf nodes");

            default:
                ThrowInvalidEnumValue(ntVal.ValueKind);
                return(null);
            }
        }
Beispiel #20
0
 public NativeSymbolRelationship(NativeSymbol parent, NativeSymbol symbol)
 {
     Parent = parent;
     Symbol = symbol;
 }
Beispiel #21
0
 public static bool TryGetValue(this INativeSymbolLookup lookup, string name, out NativeSymbol symbol) =>
 lookup.TryGetGlobalSymbol(new NativeName(name, NativeNameKind.Constant), out symbol) ||
 lookup.TryGetGlobalSymbol(new NativeName(name, NativeNameKind.EnumValue), out symbol);
Beispiel #22
0
        private void VerifyTree(NativeSymbol ns, string str)
        {
            string realStr = Print(ns);

            Assert.Equal(str, realStr);
        }