]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/Common/Expression.py
BaseTools: cleanup LongFilePathSupport usage
[mirror_edk2.git] / BaseTools / Source / Python / Common / Expression.py
index e1a2c155b7f3d94afa62997413d8a1f6f6b43480..a21ab5daa73ee95a462e413844431e412c3dc384 100644 (file)
 ## Import Modules\r
 #\r
 from __future__ import print_function\r
+from __future__ import absolute_import\r
 from Common.GlobalData import *\r
 from CommonDataClass.Exceptions import BadExpression\r
 from CommonDataClass.Exceptions import WrnExpression\r
-from Misc import GuidStringToGuidStructureString, ParseFieldValue, IsFieldValueAnArray\r
+from .Misc import GuidStringToGuidStructureString, ParseFieldValue\r
 import Common.EdkLogger as EdkLogger\r
 import copy\r
 from Common.DataType import *\r
+import sys\r
+from random import sample\r
+import string\r
 \r
 ERR_STRING_EXPR         = 'This operator cannot be used in string expression: [%s].'\r
 ERR_SNYTAX              = 'Syntax error, the rest of expression cannot be evaluated: [%s].'\r
@@ -27,7 +31,7 @@ ERR_MATCH               = 'No matching right parenthesis.'
 ERR_STRING_TOKEN        = 'Bad string token: [%s].'\r
 ERR_MACRO_TOKEN         = 'Bad macro token: [%s].'\r
 ERR_EMPTY_TOKEN         = 'Empty token is not allowed.'\r
-ERR_PCD_RESOLVE         = 'PCD token cannot be resolved: [%s].'\r
+ERR_PCD_RESOLVE         = 'The PCD should be FeatureFlag type or FixedAtBuild type: [%s].'\r
 ERR_VALID_TOKEN         = 'No more valid token found from rest of string: [%s].'\r
 ERR_EXPR_TYPE           = 'Different types found in expression.'\r
 ERR_OPERATOR_UNSUPPORT  = 'Unsupported operator: [%s]'\r
@@ -53,6 +57,8 @@ PcdPattern = re.compile(r'[_a-zA-Z][0-9A-Za-z_]*\.[_a-zA-Z][0-9A-Za-z_]*$')
 #\r
 def SplitString(String):\r
     # There might be escaped quote: "abc\"def\\\"ghi", 'abc\'def\\\'ghi'\r
+    RanStr = ''.join(sample(string.ascii_letters + string.digits, 8))\r
+    String = String.replace('\\\\', RanStr).strip()\r
     RetList = []\r
     InSingleQuote = False\r
     InDoubleQuote = False\r
@@ -85,11 +91,16 @@ def SplitString(String):
         raise BadExpression(ERR_STRING_TOKEN % Item)\r
     if Item:\r
         RetList.append(Item)\r
+    for i, ch in enumerate(RetList):\r
+        if RanStr in ch:\r
+            RetList[i] = ch.replace(RanStr,'\\\\')\r
     return RetList\r
 \r
 def SplitPcdValueString(String):\r
     # There might be escaped comma in GUID() or DEVICE_PATH() or " "\r
     # or ' ' or L' ' or L" "\r
+    RanStr = ''.join(sample(string.ascii_letters + string.digits, 8))\r
+    String = String.replace('\\\\', RanStr).strip()\r
     RetList = []\r
     InParenthesis = 0\r
     InSingleQuote = False\r
@@ -122,6 +133,9 @@ def SplitPcdValueString(String):
         raise BadExpression(ERR_STRING_TOKEN % Item)\r
     if Item:\r
         RetList.append(Item)\r
+    for i, ch in enumerate(RetList):\r
+        if RanStr in ch:\r
+            RetList[i] = ch.replace(RanStr,'\\\\')\r
     return RetList\r
 \r
 def IsValidCName(Str):\r
@@ -136,11 +150,11 @@ def BuildOptionValue(PcdValue, GuidDict):
         InputValue = 'L"' + PcdValue[1:] + '"'\r
     else:\r
         InputValue = PcdValue\r
-    if IsFieldValueAnArray(InputValue):\r
-        try:\r
-            PcdValue = ValueExpressionEx(InputValue, TAB_VOID, GuidDict)(True)\r
-        except:\r
-            pass\r
+    try:\r
+        PcdValue = ValueExpressionEx(InputValue, TAB_VOID, GuidDict)(True)\r
+    except:\r
+        pass\r
+\r
     return PcdValue\r
 \r
 ## ReplaceExprMacro\r
@@ -254,7 +268,8 @@ class ValueExpression(BaseExpression):
                 Oprand2 = IntToStr(Oprand2)\r
         TypeDict = {\r
             type(0)  : 0,\r
-            type(0L) : 0,\r
+            # For python2 long type\r
+            type(sys.maxsize + 1) : 0,\r
             type('') : 1,\r
             type(True) : 2\r
         }\r
@@ -294,8 +309,8 @@ class ValueExpression(BaseExpression):
                 else:\r
                     raise BadExpression(ERR_EXPR_TYPE)\r
             if isinstance(Oprand1, type('')) and isinstance(Oprand2, type('')):\r
-                if (Oprand1.startswith('L"') and not Oprand2.startswith('L"')) or \\r
-                    (not Oprand1.startswith('L"') and Oprand2.startswith('L"')):\r
+                if ((Oprand1.startswith('L"') or Oprand1.startswith("L'")) and (not Oprand2.startswith('L"')) and (not Oprand2.startswith("L'"))) or \\r
+                        (((not Oprand1.startswith('L"')) and (not Oprand1.startswith("L'"))) and (Oprand2.startswith('L"') or Oprand2.startswith("L'"))):\r
                     raise BadExpression(ERR_STRING_CMP % (Oprand1, Operator, Oprand2))\r
             if 'in' in Operator and isinstance(Oprand2, type('')):\r
                 Oprand2 = Oprand2.split()\r
@@ -387,7 +402,7 @@ class ValueExpression(BaseExpression):
             elif not Val:\r
                 Val = False\r
                 RealVal = '""'\r
-            elif not Val.startswith('L"') and not Val.startswith('{') and not Val.startswith("L'"):\r
+            elif not Val.startswith('L"') and not Val.startswith('{') and not Val.startswith("L'") and not Val.startswith("'"):\r
                 Val = True\r
                 RealVal = '"' + RealVal + '"'\r
 \r
@@ -785,7 +800,7 @@ class ValueExpression(BaseExpression):
         OpToken = ''\r
         for Ch in Expr:\r
             if Ch in self.NonLetterOpLst:\r
-                if '!' == Ch and OpToken:\r
+                if Ch in ['!', '~'] and OpToken:\r
                     break\r
                 self._Idx += 1\r
                 OpToken += Ch\r
@@ -824,6 +839,8 @@ class ValueExpressionEx(ValueExpression):
                 PcdValue = PcdValue.strip()\r
                 if PcdValue.startswith('{') and PcdValue.endswith('}'):\r
                     PcdValue = SplitPcdValueString(PcdValue[1:-1])\r
+                if ERR_STRING_CMP.split(':')[0] in Value.message:\r
+                    raise BadExpression("Type: %s, Value: %s, %s" % (self.PcdType, PcdValue, Value))\r
                 if isinstance(PcdValue, type([])):\r
                     TmpValue = 0\r
                     Size = 0\r
@@ -892,7 +909,7 @@ class ValueExpressionEx(ValueExpression):
                     raise BadExpression('Type %s PCD Value Size is Larger than 8 byte' % self.PcdType)\r
             else:\r
                 try:\r
-                    TmpValue = long(PcdValue)\r
+                    TmpValue = int(PcdValue)\r
                     TmpList = []\r
                     if TmpValue.bit_length() == 0:\r
                         PcdValue = '{0x00}'\r