public bool Remove(TpmNamedConstant member) { if (!TpmTypes.RemoveConstant(member.SpecName)) { return(false); } return(Members.Remove(member)); }
internal static bool AddConstant(TpmNamedConstant nc) { if (ConstantIndex.ContainsKey(nc.SpecName) || (nc.SpecName.ToLower().Contains("reserved") && nc.EnclosingEnum.SpecName != "TPM_RC")) { return(false); } ConstantIndex.Add(nc.SpecName, nc); return(true); }
public TpmNamedConstant Add(string name, string value, string comment) { var nc = new TpmNamedConstant(this, name, value, comment); if (!TpmTypes.AddConstant(nc)) { return(null); } Members.Add(nc); return(nc); }
static void AddBitfieldElt(List <TpmNamedConstant> elements, string name, int val, string comment = null, string oldStyleName = null, bool custom = false) { // Comment is null only for custom enumerators "XXX_BIT_OFFSET" and "XXX_BIT_LENGTH" var nc = new TpmNamedConstant(null, custom ? null : name, null, comment); nc.Name = name; nc.Value = comment == null?val.ToString() : ToHex(val); nc.OldStyleName = oldStyleName ?? name; elements.Add(nc); }
void WriteAutoGeneratedSourceHeader() { TpmNamedConstant ver = TpmTypes.LookupConstant("TPM_SPEC_VERSION"); Write("\"\"\"\r\n" + " * Copyright(c) Microsoft Corporation. All rights reserved. \r\n" + " * Licensed under the MIT License. \r\n" + " * See the LICENSE file in the project root for full license information. \r\n" + "\"\"\"\r\n" + "\r\n" + "\"\"\"\r\n" + " * This file is automatically generated from the TPM 2.0 rev. " + (ver.NumericValue / 100.0).ToString("0.00") + " specification documents.\r\n" + " * Do not edit it directly.\r\n" + "\"\"\"\r\n"); }
public UnionMember(string typeName, string fieldName, string selectorName, string comment) : base(typeName, fieldName, comment) { Match m; if ((m = Regex.Match(fieldName, @"^(?<name>\w+)\s*\[?(?<val>[^\]]*)\)?]?")).Success) { Name = m.Groups["name"].ToString(); Debug.Assert(Name.Length > 0); string arraySize = m.Groups["val"].ToString(); if (arraySize != "") { ArraySize = arraySize; } } SelectorValue = TpmTypes.LookupConstant(selectorName); }
/// <summary> Makes constant values specified as arithmetic expressions comply with the current /// langauge syntax (adds enum type name qualifiers and type casts when necessary, eliminates /// sizeof() operators when unsupported, etc.) </summary> public static string TranslateConstExpr(TpmConstExpr ce, TpmEnum enclosingEnum = null) { var nameDelimiters = new char[] { ' ', ',', '{', '}', '(', ')', '+', '-', '<', '*', '/', '`' }; string suffix = TargetLang.Java ? ".toInt()" : ""; string expr = ce.Expr; string[] tokens = expr.Split(nameDelimiters); bool sizeofOccurred = false, commentNeeded = false; foreach (string token in tokens) { if (token.Length == 0) { continue; } if (token == "sizeof") { Debug.Assert(!sizeofOccurred); sizeofOccurred = true; continue; } if (Expression.IsNumber(token)) { if (token == "00") { Debug.Assert(expr == token); expr = "0"; } Debug.Assert(!sizeofOccurred); continue; } TpmNamedConstant nc = TpmTypes.LookupConstant(token); if (enclosingEnum != null && nc?.EnclosingEnum == enclosingEnum) { // Members of the enum being processed do not need to be qualified Debug.Assert(token == nc.SpecName); expr = expr.Replace(token, nc.Name + suffix); continue; } if (!TpmTypes.ContainsConstant(token)) { // This must be a type name operand of sizeof() Debug.Assert(sizeofOccurred); sizeofOccurred = false; TpmType t = TpmTypes.Lookup(token); if (t is TpmStruct || TargetLang.IsOneOf(Lang.Java, Lang.JS, Lang.Py)) { string sizeofExpr = "sizeof(" + token + ")"; Debug.Assert(expr.Contains(sizeofExpr)); commentNeeded = TargetLang.Py; string origExpr = TargetLang.Py ? "" : $"/*{sizeofExpr}*/"; expr = expr.Replace(sizeofExpr, $"0x{t.GetSize():X}{origExpr}"); } else if (TargetLang.DotNet) { expr = expr.Replace(token, t.StripTypedefs().Name); } } else { nc = TpmTypes.LookupConstant(token); var translated = nc.QualifiedName + suffix; if (!TargetLang.IsGenerated(nc.EnclosingEnum)) { translated = nc.NumericValue.ToString(); if (!TargetLang.Py) { translated += "/*" + token + "*/"; } else { commentNeeded = true; } } expr = expr.Replace(token, translated); } } // foreach token if (TargetLang.DotNet && expr.Contains("<<")) { // Shift operator in .Net requires right operand of type 'int' and unsigned left one. int curPos = 0; expr = expr.Replace(" << ", "<<"); do { int pos = expr.IndexOf("<<", curPos); if (pos == -1) { break; } curPos = pos + 2 + 6; // Add sizes of "<<" and "(uint)" while (char.IsLetterOrDigit(expr[--pos]) || expr[pos] == '.') { continue; } expr = expr.Insert(pos + 1, "(uint)"); } while (true); expr = expr.Replace("<<", " << (int)"); } if (commentNeeded) { expr += $" # {ce.Expr}"; } return(expr); } // TranslateConstExpr()
protected void WriteComment(TpmNamedConstant nc) { WriteComment(AsSummary(nc.Comment)); }