PageRenderTime 1423ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/edk2/BaseTools/Source/Python/UPT/Object/Parser/InfGuidObject.py

https://gitlab.com/envieidoc/Clover
Python | 353 lines | 326 code | 4 blank | 23 comment | 0 complexity | 9de6017b3669020f9c036319a2831fd2 MD5 | raw file
  1. ## @file
  2. # This file is used to define class objects of INF file [Guids] section.
  3. # It will consumed by InfParser.
  4. #
  5. # Copyright (c) 2011 - 2014, Intel Corporation. All rights reserved.<BR>
  6. #
  7. # This program and the accompanying materials are licensed and made available
  8. # under the terms and conditions of the BSD License which accompanies this
  9. # distribution. The full text of the license may be found at
  10. # http://opensource.org/licenses/bsd-license.php
  11. #
  12. # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
  14. '''
  15. InfGuidObject
  16. '''
  17. from Library.ParserValidate import IsValidCVariableName
  18. from Library.CommentParsing import ParseComment
  19. from Library.ExpressionValidate import IsValidFeatureFlagExp
  20. from Library.Misc import Sdict
  21. from Library import DataType as DT
  22. import Logger.Log as Logger
  23. from Logger import ToolError
  24. from Logger import StringTable as ST
  25. class InfGuidItemCommentContent():
  26. def __init__(self):
  27. #
  28. # ## SOMETIMES_CONSUMES ## Variable:L"MemoryTypeInformation"
  29. # TailString.
  30. #
  31. #
  32. # SOMETIMES_CONSUMES
  33. #
  34. self.UsageItem = ''
  35. #
  36. # Variable
  37. #
  38. self.GuidTypeItem = ''
  39. #
  40. # MemoryTypeInformation
  41. #
  42. self.VariableNameItem = ''
  43. #
  44. # TailString
  45. #
  46. self.HelpStringItem = ''
  47. def SetUsageItem(self, UsageItem):
  48. self.UsageItem = UsageItem
  49. def GetUsageItem(self):
  50. return self.UsageItem
  51. def SetGuidTypeItem(self, GuidTypeItem):
  52. self.GuidTypeItem = GuidTypeItem
  53. def GetGuidTypeItem(self):
  54. return self.GuidTypeItem
  55. def SetVariableNameItem(self, VariableNameItem):
  56. self.VariableNameItem = VariableNameItem
  57. def GetVariableNameItem(self):
  58. return self.VariableNameItem
  59. def SetHelpStringItem(self, HelpStringItem):
  60. self.HelpStringItem = HelpStringItem
  61. def GetHelpStringItem(self):
  62. return self.HelpStringItem
  63. class InfGuidItem():
  64. def __init__(self):
  65. self.Name = ''
  66. self.FeatureFlagExp = ''
  67. #
  68. # A list contain instance of InfGuidItemCommentContent
  69. #
  70. self.CommentList = []
  71. self.SupArchList = []
  72. def SetName(self, Name):
  73. self.Name = Name
  74. def GetName(self):
  75. return self.Name
  76. def SetFeatureFlagExp(self, FeatureFlagExp):
  77. self.FeatureFlagExp = FeatureFlagExp
  78. def GetFeatureFlagExp(self):
  79. return self.FeatureFlagExp
  80. def SetCommentList(self, CommentList):
  81. self.CommentList = CommentList
  82. def GetCommentList(self):
  83. return self.CommentList
  84. def SetSupArchList(self, SupArchList):
  85. self.SupArchList = SupArchList
  86. def GetSupArchList(self):
  87. return self.SupArchList
  88. ## ParseComment
  89. #
  90. # ParseComment
  91. #
  92. def ParseGuidComment(CommentsList, InfGuidItemObj):
  93. #
  94. # Get/Set Usage and HelpString
  95. #
  96. if CommentsList != None and len(CommentsList) != 0 :
  97. CommentInsList = []
  98. PreUsage = None
  99. PreGuidType = None
  100. PreHelpText = ''
  101. BlockFlag = -1
  102. Count = 0
  103. for CommentItem in CommentsList:
  104. Count = Count + 1
  105. CommentItemUsage, \
  106. CommentItemGuidType, \
  107. CommentItemVarString, \
  108. CommentItemHelpText = \
  109. ParseComment(CommentItem,
  110. DT.ALL_USAGE_TOKENS,
  111. DT.GUID_TYPE_TOKENS,
  112. [],
  113. True)
  114. if CommentItemHelpText == None:
  115. CommentItemHelpText = ''
  116. if Count == len(CommentsList) and CommentItemUsage == CommentItemGuidType == DT.ITEM_UNDEFINED:
  117. CommentItemHelpText = DT.END_OF_LINE
  118. if Count == len(CommentsList):
  119. if BlockFlag == 1 or BlockFlag == 2:
  120. if CommentItemUsage == CommentItemGuidType == DT.ITEM_UNDEFINED:
  121. BlockFlag = 4
  122. else:
  123. BlockFlag = 3
  124. if BlockFlag == -1:
  125. BlockFlag = 4
  126. if BlockFlag == -1 or BlockFlag == 1 or BlockFlag == 2:
  127. if CommentItemUsage == CommentItemGuidType == DT.ITEM_UNDEFINED:
  128. if BlockFlag == -1:
  129. BlockFlag = 1
  130. elif BlockFlag == 1:
  131. BlockFlag = 2
  132. else:
  133. if BlockFlag == 1 or BlockFlag == 2:
  134. BlockFlag = 3
  135. elif BlockFlag == -1:
  136. BlockFlag = 4
  137. #
  138. # Combine two comment line if they are generic comment
  139. #
  140. if CommentItemUsage == CommentItemGuidType == PreUsage == PreGuidType == DT.ITEM_UNDEFINED:
  141. CommentItemHelpText = PreHelpText + DT.END_OF_LINE + CommentItemHelpText
  142. PreHelpText = CommentItemHelpText
  143. if BlockFlag == 4:
  144. CommentItemIns = InfGuidItemCommentContent()
  145. CommentItemIns.SetUsageItem(CommentItemUsage)
  146. CommentItemIns.SetGuidTypeItem(CommentItemGuidType)
  147. CommentItemIns.SetVariableNameItem(CommentItemVarString)
  148. if CommentItemHelpText == '' or CommentItemHelpText.endswith(DT.END_OF_LINE):
  149. CommentItemHelpText = CommentItemHelpText.strip(DT.END_OF_LINE)
  150. CommentItemIns.SetHelpStringItem(CommentItemHelpText)
  151. CommentInsList.append(CommentItemIns)
  152. BlockFlag = -1
  153. PreUsage = None
  154. PreGuidType = None
  155. PreHelpText = ''
  156. elif BlockFlag == 3:
  157. #
  158. # Add previous help string
  159. #
  160. CommentItemIns = InfGuidItemCommentContent()
  161. CommentItemIns.SetUsageItem(DT.ITEM_UNDEFINED)
  162. CommentItemIns.SetGuidTypeItem(DT.ITEM_UNDEFINED)
  163. if PreHelpText == '' or PreHelpText.endswith(DT.END_OF_LINE):
  164. PreHelpText = PreHelpText.strip(DT.END_OF_LINE)
  165. CommentItemIns.SetHelpStringItem(PreHelpText)
  166. CommentInsList.append(CommentItemIns)
  167. #
  168. # Add Current help string
  169. #
  170. CommentItemIns = InfGuidItemCommentContent()
  171. CommentItemIns.SetUsageItem(CommentItemUsage)
  172. CommentItemIns.SetGuidTypeItem(CommentItemGuidType)
  173. CommentItemIns.SetVariableNameItem(CommentItemVarString)
  174. if CommentItemHelpText == '' or CommentItemHelpText.endswith(DT.END_OF_LINE):
  175. CommentItemHelpText = CommentItemHelpText.strip(DT.END_OF_LINE)
  176. CommentItemIns.SetHelpStringItem(CommentItemHelpText)
  177. CommentInsList.append(CommentItemIns)
  178. BlockFlag = -1
  179. PreUsage = None
  180. PreGuidType = None
  181. PreHelpText = ''
  182. else:
  183. PreUsage = CommentItemUsage
  184. PreGuidType = CommentItemGuidType
  185. PreHelpText = CommentItemHelpText
  186. InfGuidItemObj.SetCommentList(CommentInsList)
  187. else:
  188. #
  189. # Still need to set the USAGE/GUIDTYPE to undefined.
  190. #
  191. CommentItemIns = InfGuidItemCommentContent()
  192. CommentItemIns.SetUsageItem(DT.ITEM_UNDEFINED)
  193. CommentItemIns.SetGuidTypeItem(DT.ITEM_UNDEFINED)
  194. InfGuidItemObj.SetCommentList([CommentItemIns])
  195. return InfGuidItemObj
  196. ## InfGuidObject
  197. #
  198. # InfGuidObject
  199. #
  200. class InfGuidObject():
  201. def __init__(self):
  202. self.Guids = Sdict()
  203. #
  204. # Macro defined in this section should be only used in this section.
  205. #
  206. self.Macros = {}
  207. def SetGuid(self, GuidList, Arch = None):
  208. __SupportArchList = []
  209. for ArchItem in Arch:
  210. #
  211. # Validate Arch
  212. #
  213. if (ArchItem == '' or ArchItem == None):
  214. ArchItem = 'COMMON'
  215. __SupportArchList.append(ArchItem)
  216. for Item in GuidList:
  217. #
  218. # Get Comment content of this protocol
  219. #
  220. CommentsList = None
  221. if len(Item) == 3:
  222. CommentsList = Item[1]
  223. CurrentLineOfItem = Item[2]
  224. Item = Item[0]
  225. InfGuidItemObj = InfGuidItem()
  226. if len(Item) >= 1 and len(Item) <= 2:
  227. #
  228. # Only GuildName contained
  229. #
  230. if not IsValidCVariableName(Item[0]):
  231. Logger.Error("InfParser",
  232. ToolError.FORMAT_INVALID,
  233. ST.ERR_INF_PARSER_INVALID_CNAME%(Item[0]),
  234. File=CurrentLineOfItem[2],
  235. Line=CurrentLineOfItem[1],
  236. ExtraData=CurrentLineOfItem[0])
  237. if (Item[0] != ''):
  238. InfGuidItemObj.SetName(Item[0])
  239. else:
  240. Logger.Error("InfParser",
  241. ToolError.FORMAT_INVALID,
  242. ST.ERR_INF_PARSER_CNAME_MISSING,
  243. File=CurrentLineOfItem[2],
  244. Line=CurrentLineOfItem[1],
  245. ExtraData=CurrentLineOfItem[0])
  246. if len(Item) == 2:
  247. #
  248. # Contained CName and Feature Flag Express
  249. # <statements> ::= <CName> ["|" <FeatureFlagExpress>]
  250. # For GUID entry.
  251. #
  252. if Item[1].strip() == '':
  253. Logger.Error("InfParser",
  254. ToolError.FORMAT_INVALID,
  255. ST.ERR_INF_PARSER_FEATURE_FLAG_EXP_MISSING,
  256. File=CurrentLineOfItem[2],
  257. Line=CurrentLineOfItem[1],
  258. ExtraData=CurrentLineOfItem[0])
  259. #
  260. # Validate Feature Flag Express
  261. #
  262. FeatureFlagRtv = IsValidFeatureFlagExp(Item[1].strip())
  263. if not FeatureFlagRtv[0]:
  264. Logger.Error("InfParser",
  265. ToolError.FORMAT_INVALID,
  266. ST.ERR_INF_PARSER_FEATURE_FLAG_EXP_SYNTAX_INVLID%(FeatureFlagRtv[1]),
  267. File=CurrentLineOfItem[2],
  268. Line=CurrentLineOfItem[1],
  269. ExtraData=CurrentLineOfItem[0])
  270. InfGuidItemObj.SetFeatureFlagExp(Item[1])
  271. if len(Item) != 1 and len(Item) != 2:
  272. #
  273. # Invalid format of GUID statement
  274. #
  275. Logger.Error("InfParser",
  276. ToolError.FORMAT_INVALID,
  277. ST.ERR_INF_PARSER_GUID_PPI_PROTOCOL_SECTION_CONTENT_ERROR,
  278. File=CurrentLineOfItem[2],
  279. Line=CurrentLineOfItem[1],
  280. ExtraData=CurrentLineOfItem[0])
  281. InfGuidItemObj = ParseGuidComment(CommentsList, InfGuidItemObj)
  282. InfGuidItemObj.SetSupArchList(__SupportArchList)
  283. #
  284. # Determine GUID name duplicate. Follow below rule:
  285. #
  286. # A GUID must not be duplicated within a [Guids] section.
  287. # A GUID may appear in multiple architectural [Guids]
  288. # sections. A GUID listed in an architectural [Guids]
  289. # section must not be listed in the common architectural
  290. # [Guids] section.
  291. #
  292. # NOTE: This check will not report error now.
  293. #
  294. for Item in self.Guids:
  295. if Item.GetName() == InfGuidItemObj.GetName():
  296. ItemSupArchList = Item.GetSupArchList()
  297. for ItemArch in ItemSupArchList:
  298. for GuidItemObjArch in __SupportArchList:
  299. if ItemArch == GuidItemObjArch:
  300. #
  301. # ST.ERR_INF_PARSER_ITEM_DUPLICATE
  302. #
  303. pass
  304. if ItemArch.upper() == 'COMMON' or GuidItemObjArch.upper() == 'COMMON':
  305. #
  306. # ST.ERR_INF_PARSER_ITEM_DUPLICATE_COMMON
  307. #
  308. pass
  309. if self.Guids.has_key((InfGuidItemObj)):
  310. GuidList = self.Guids[InfGuidItemObj]
  311. GuidList.append(InfGuidItemObj)
  312. self.Guids[InfGuidItemObj] = GuidList
  313. else:
  314. GuidList = []
  315. GuidList.append(InfGuidItemObj)
  316. self.Guids[InfGuidItemObj] = GuidList
  317. return True
  318. def GetGuid(self):
  319. return self.Guids