]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/Common/Expression.py
BaseTools: Expression - remove variable
[mirror_edk2.git] / BaseTools / Source / Python / Common / Expression.py
index 5a0ade9e7ecad6d1fa6cbeb2dec09f7d4ef26eae..4f0f377f37880a2eb9981ba52c778f4a9d93bc90 100644 (file)
@@ -15,7 +15,7 @@
 from Common.GlobalData import *\r
 from CommonDataClass.Exceptions import BadExpression\r
 from CommonDataClass.Exceptions import WrnExpression\r
-from Misc import GuidStringToGuidStructureString, ParseFieldValue\r
+from Misc import GuidStringToGuidStructureString, ParseFieldValue, IsFieldValueAnArray\r
 import Common.EdkLogger as EdkLogger\r
 import copy\r
 \r
@@ -40,20 +40,21 @@ ERR_ARRAY_ELE           = 'This must be HEX value for NList or Array: [%s].'
 ERR_EMPTY_EXPR          = 'Empty expression is not allowed.'\r
 ERR_IN_OPERAND          = 'Macro after IN operator can only be: $(FAMILY), $(ARCH), $(TOOL_CHAIN_TAG) and $(TARGET).'\r
 \r
+__ValidString = re.compile(r'[_a-zA-Z][_0-9a-zA-Z]*$')\r
+\r
 ## SplitString\r
 #  Split string to list according double quote\r
 #  For example: abc"de\"f"ghi"jkl"mn will be: ['abc', '"de\"f"', 'ghi', '"jkl"', 'mn']\r
 #\r
 def SplitString(String):\r
     # There might be escaped quote: "abc\"def\\\"ghi", 'abc\'def\\\'ghi'\r
-    Str = String\r
     RetList = []\r
     InSingleQuote = False\r
     InDoubleQuote = False\r
     Item = ''\r
-    for i, ch in enumerate(Str):\r
+    for i, ch in enumerate(String):\r
         if ch == '"' and not InSingleQuote:\r
-            if Str[i - 1] != '\\':\r
+            if String[i - 1] != '\\':\r
                 InDoubleQuote = not InDoubleQuote\r
             if not InDoubleQuote:\r
                 Item += String[i]\r
@@ -64,7 +65,7 @@ def SplitString(String):
                 RetList.append(Item)\r
                 Item = ''\r
         elif ch == "'" and not InDoubleQuote:\r
-            if Str[i - 1] != '\\':\r
+            if String[i - 1] != '\\':\r
                 InSingleQuote = not InSingleQuote\r
             if not InSingleQuote:\r
                 Item += String[i]\r
@@ -84,27 +85,26 @@ def SplitString(String):
 def SplitPcdValueString(String):\r
     # There might be escaped comma in GUID() or DEVICE_PATH() or " "\r
     # or ' ' or L' ' or L" "\r
-    Str = String\r
     RetList = []\r
     InParenthesis = 0\r
     InSingleQuote = False\r
     InDoubleQuote = False\r
     Item = ''\r
-    for i, ch in enumerate(Str):\r
+    for i, ch in enumerate(String):\r
         if ch == '(':\r
             InParenthesis += 1\r
-        if ch == ')':\r
+        elif ch == ')':\r
             if InParenthesis:\r
                 InParenthesis -= 1\r
             else:\r
                 raise BadExpression(ERR_STRING_TOKEN % Item)\r
-        if ch == '"' and not InSingleQuote:\r
+        elif ch == '"' and not InSingleQuote:\r
             if String[i-1] != '\\':\r
                 InDoubleQuote = not InDoubleQuote\r
-        if ch == "'" and not InDoubleQuote:\r
+        elif ch == "'" and not InDoubleQuote:\r
             if String[i-1] != '\\':\r
                 InSingleQuote = not InSingleQuote\r
-        if ch == ',':\r
+        elif ch == ',':\r
             if InParenthesis or InSingleQuote or InDoubleQuote:\r
                 Item += String[i]\r
                 continue\r
@@ -119,11 +119,24 @@ def SplitPcdValueString(String):
         RetList.append(Item)\r
     return RetList\r
 \r
-def IsValidCString(Str):\r
-    ValidString = re.compile(r'[_a-zA-Z][_0-9a-zA-Z]*$')\r
-    if not ValidString.match(Str):\r
-        return False\r
-    return True\r
+def IsValidCName(Str):\r
+    return True if __ValidString.match(Str) else False\r
+\r
+def BuildOptionValue(PcdValue, GuidDict):\r
+    if PcdValue.startswith('H'):\r
+        InputValue = PcdValue[1:]\r
+    elif PcdValue.startswith("L'") or PcdValue.startswith("'"):\r
+        InputValue = PcdValue\r
+    elif PcdValue.startswith('L'):\r
+        InputValue = 'L"' + PcdValue[1:] + '"'\r
+    else:\r
+        InputValue = PcdValue\r
+    if IsFieldValueAnArray(InputValue):\r
+        try:\r
+            PcdValue = ValueExpressionEx(InputValue, 'VOID*', GuidDict)(True)\r
+        except:\r
+            pass\r
+    return PcdValue\r
 \r
 ## ReplaceExprMacro\r
 #\r
@@ -890,11 +903,12 @@ class ValueExpressionEx(ValueExpression):
                         LabelOffset = 0\r
                         for Index, Item in enumerate(PcdValueList):\r
                             # compute byte offset of every LABEL\r
-                            Item = Item.strip()\r
                             LabelList = ReLabel.findall(Item)\r
+                            Item = ReLabel.sub('', Item)\r
+                            Item = Item.strip()\r
                             if LabelList:\r
                                 for Label in LabelList:\r
-                                    if not IsValidCString(Label):\r
+                                    if not IsValidCName(Label):\r
                                         raise BadExpression('%s is not a valid c variable name' % Label)\r
                                     if Label not in LabelDict.keys():\r
                                         LabelDict[Label] = str(LabelOffset)\r