]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/UPT/Object/Parser/InfGuidObject.py
Sync BaseTools Branch (version r2271) to EDKII main trunk.
[mirror_edk2.git] / BaseTools / Source / Python / UPT / Object / Parser / InfGuidObject.py
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, 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 '''
16 InfGuidObject
17 '''
18
19 from Library.ParserValidate import IsValidCVariableName
20 from Library.CommentParsing import ParseComment
21 from Library.ExpressionValidate import IsValidFeatureFlagExp
22
23 from Library.Misc import Sdict
24 from Library import DataType as DT
25 import Logger.Log as Logger
26 from Logger import ToolError
27 from Logger import StringTable as ST
28
29 class InfGuidItemCommentContent():
30 def __init__(self):
31 #
32 # ## SOMETIMES_CONSUMES ## Variable:L"MemoryTypeInformation"
33 # TailString.
34 #
35 #
36 # SOMETIMES_CONSUMES
37 #
38 self.UsageItem = ''
39 #
40 # Variable
41 #
42 self.GuidTypeItem = ''
43 #
44 # MemoryTypeInformation
45 #
46 self.VariableNameItem = ''
47 #
48 # TailString
49 #
50 self.HelpStringItem = ''
51
52 def SetUsageItem(self, UsageItem):
53 self.UsageItem = UsageItem
54 def GetUsageItem(self):
55 return self.UsageItem
56
57 def SetGuidTypeItem(self, GuidTypeItem):
58 self.GuidTypeItem = GuidTypeItem
59 def GetGuidTypeItem(self):
60 return self.GuidTypeItem
61
62 def SetVariableNameItem(self, VariableNameItem):
63 self.VariableNameItem = VariableNameItem
64 def GetVariableNameItem(self):
65 return self.VariableNameItem
66
67 def SetHelpStringItem(self, HelpStringItem):
68 self.HelpStringItem = HelpStringItem
69 def GetHelpStringItem(self):
70 return self.HelpStringItem
71
72 class InfGuidItem():
73 def __init__(self):
74 self.Name = ''
75 self.FeatureFlagExp = ''
76 #
77 # A list contain instance of InfGuidItemCommentContent
78 #
79 self.CommentList = []
80 self.SupArchList = []
81
82 def SetName(self, Name):
83 self.Name = Name
84 def GetName(self):
85 return self.Name
86
87 def SetFeatureFlagExp(self, FeatureFlagExp):
88 self.FeatureFlagExp = FeatureFlagExp
89 def GetFeatureFlagExp(self):
90 return self.FeatureFlagExp
91
92 def SetCommentList(self, CommentList):
93 self.CommentList = CommentList
94 def GetCommentList(self):
95 return self.CommentList
96
97 def SetSupArchList(self, SupArchList):
98 self.SupArchList = SupArchList
99 def GetSupArchList(self):
100 return self.SupArchList
101
102 ## ParseComment
103 #
104 # ParseComment
105 #
106 def ParseGuidComment(CommentsList, InfGuidItemObj):
107 #
108 # Get/Set Usage and HelpString
109 #
110 if CommentsList != None and len(CommentsList) != 0 :
111 CommentInsList = []
112 PreUsage = None
113 PreGuidType = None
114 PreHelpText = ''
115 BlockFlag = -1
116 Count = 0
117 for CommentItem in CommentsList:
118 Count = Count + 1
119 CommentItemUsage, \
120 CommentItemGuidType, \
121 CommentItemVarString, \
122 CommentItemHelpText = \
123 ParseComment(CommentItem,
124 DT.ALL_USAGE_TOKENS,
125 DT.GUID_TYPE_TOKENS,
126 [],
127 True)
128
129 if CommentItemHelpText == None:
130 CommentItemHelpText = ''
131 if Count == len(CommentsList) and CommentItemUsage == CommentItemGuidType == DT.ITEM_UNDEFINED:
132 CommentItemHelpText = DT.END_OF_LINE
133
134 if Count == len(CommentsList):
135 if BlockFlag == 1 or BlockFlag == 2:
136 if CommentItemUsage == CommentItemGuidType == DT.ITEM_UNDEFINED:
137 BlockFlag = 4
138 else:
139 BlockFlag = 3
140 if BlockFlag == -1:
141 BlockFlag = 4
142 if BlockFlag == -1 or BlockFlag == 1 or BlockFlag == 2:
143 if CommentItemUsage == CommentItemGuidType == DT.ITEM_UNDEFINED:
144 if BlockFlag == -1:
145 BlockFlag = 1
146 elif BlockFlag == 1:
147 BlockFlag = 2
148 else:
149 if BlockFlag == 1 or BlockFlag == 2:
150 BlockFlag = 3
151 elif BlockFlag == -1:
152 BlockFlag = 4
153
154 #
155 # Combine two comment line if they are generic comment
156 #
157 if CommentItemUsage == CommentItemGuidType == PreUsage == PreGuidType == DT.ITEM_UNDEFINED:
158 CommentItemHelpText = PreHelpText + DT.END_OF_LINE + CommentItemHelpText
159
160 PreHelpText = CommentItemHelpText
161
162 if BlockFlag == 4:
163 CommentItemIns = InfGuidItemCommentContent()
164 CommentItemIns.SetUsageItem(CommentItemUsage)
165 CommentItemIns.SetGuidTypeItem(CommentItemGuidType)
166 CommentItemIns.SetVariableNameItem(CommentItemVarString)
167 CommentItemIns.SetHelpStringItem(CommentItemHelpText)
168 CommentInsList.append(CommentItemIns)
169
170 BlockFlag = -1
171 PreUsage = None
172 PreGuidType = None
173 PreHelpText = ''
174
175 elif BlockFlag == 3:
176 #
177 # Add previous help string
178 #
179 CommentItemIns = InfGuidItemCommentContent()
180 CommentItemIns.SetUsageItem(DT.ITEM_UNDEFINED)
181 CommentItemIns.SetGuidTypeItem(DT.ITEM_UNDEFINED)
182 if PreHelpText == '' or PreHelpText.endswith(DT.END_OF_LINE):
183 PreHelpText += DT.END_OF_LINE
184 CommentItemIns.SetHelpStringItem(PreHelpText)
185 CommentInsList.append(CommentItemIns)
186 #
187 # Add Current help string
188 #
189 CommentItemIns = InfGuidItemCommentContent()
190 CommentItemIns.SetUsageItem(CommentItemUsage)
191 CommentItemIns.SetGuidTypeItem(CommentItemGuidType)
192 CommentItemIns.SetVariableNameItem(CommentItemVarString)
193 CommentItemIns.SetHelpStringItem(CommentItemHelpText)
194 CommentInsList.append(CommentItemIns)
195
196 BlockFlag = -1
197 PreUsage = None
198 PreGuidType = None
199 PreHelpText = ''
200
201 else:
202 PreUsage = CommentItemUsage
203 PreGuidType = CommentItemGuidType
204 PreHelpText = CommentItemHelpText
205
206 InfGuidItemObj.SetCommentList(CommentInsList)
207 else:
208 #
209 # Still need to set the USAGE/GUIDTYPE to undefined.
210 #
211 CommentItemIns = InfGuidItemCommentContent()
212 CommentItemIns.SetUsageItem(DT.ITEM_UNDEFINED)
213 CommentItemIns.SetGuidTypeItem(DT.ITEM_UNDEFINED)
214 InfGuidItemObj.SetCommentList([CommentItemIns])
215
216 return InfGuidItemObj
217
218 ## InfGuidObject
219 #
220 # InfGuidObject
221 #
222 class InfGuidObject():
223 def __init__(self):
224 self.Guids = Sdict()
225 #
226 # Macro defined in this section should be only used in this section.
227 #
228 self.Macros = {}
229
230 def SetGuid(self, GuidList, Arch = None):
231 __SupportArchList = []
232 for ArchItem in Arch:
233 #
234 # Validate Arch
235 #
236 if (ArchItem == '' or ArchItem == None):
237 ArchItem = 'COMMON'
238
239 __SupportArchList.append(ArchItem)
240
241 for Item in GuidList:
242 #
243 # Get Comment content of this protocol
244 #
245 CommentsList = None
246 if len(Item) == 3:
247 CommentsList = Item[1]
248 CurrentLineOfItem = Item[2]
249 Item = Item[0]
250 InfGuidItemObj = InfGuidItem()
251 if len(Item) >= 1 and len(Item) <= 2:
252 #
253 # Only GuildName contained
254 #
255 if not IsValidCVariableName(Item[0]):
256 Logger.Error("InfParser",
257 ToolError.FORMAT_INVALID,
258 ST.ERR_INF_PARSER_INVALID_CNAME%(Item[0]),
259 File=CurrentLineOfItem[2],
260 Line=CurrentLineOfItem[1],
261 ExtraData=CurrentLineOfItem[0])
262 if (Item[0] != ''):
263 InfGuidItemObj.SetName(Item[0])
264 else:
265 Logger.Error("InfParser",
266 ToolError.FORMAT_INVALID,
267 ST.ERR_INF_PARSER_CNAME_MISSING,
268 File=CurrentLineOfItem[2],
269 Line=CurrentLineOfItem[1],
270 ExtraData=CurrentLineOfItem[0])
271 if len(Item) == 2:
272 #
273 # Contained CName and Feature Flag Express
274 # <statements> ::= <CName> ["|" <FeatureFlagExpress>]
275 # For GUID entry.
276 #
277 if Item[1].strip() == '':
278 Logger.Error("InfParser",
279 ToolError.FORMAT_INVALID,
280 ST.ERR_INF_PARSER_FEATURE_FLAG_EXP_MISSING,
281 File=CurrentLineOfItem[2],
282 Line=CurrentLineOfItem[1],
283 ExtraData=CurrentLineOfItem[0])
284 #
285 # Validate Feature Flag Express
286 #
287 FeatureFlagRtv = IsValidFeatureFlagExp(Item[1].strip())
288 if not FeatureFlagRtv[0]:
289 Logger.Error("InfParser",
290 ToolError.FORMAT_INVALID,
291 ST.ERR_INF_PARSER_FEATURE_FLAG_EXP_SYNTAX_INVLID%(FeatureFlagRtv[1]),
292 File=CurrentLineOfItem[2],
293 Line=CurrentLineOfItem[1],
294 ExtraData=CurrentLineOfItem[0])
295 InfGuidItemObj.SetFeatureFlagExp(Item[1])
296 if len(Item) != 1 and len(Item) != 2:
297 #
298 # Invalid format of GUID statement
299 #
300 Logger.Error("InfParser",
301 ToolError.FORMAT_INVALID,
302 ST.ERR_INF_PARSER_GUID_PPI_PROTOCOL_SECTION_CONTENT_ERROR,
303 File=CurrentLineOfItem[2],
304 Line=CurrentLineOfItem[1],
305 ExtraData=CurrentLineOfItem[0])
306
307 InfGuidItemObj = ParseGuidComment(CommentsList, InfGuidItemObj)
308 InfGuidItemObj.SetSupArchList(__SupportArchList)
309
310 #
311 # Determine GUID name duplicate. Follow below rule:
312 #
313 # A GUID must not be duplicated within a [Guids] section.
314 # A GUID may appear in multiple architectural [Guids]
315 # sections. A GUID listed in an architectural [Guids]
316 # section must not be listed in the common architectural
317 # [Guids] section.
318 #
319 # NOTE: This check will not report error now.
320 #
321 for Item in self.Guids:
322 if Item.GetName() == InfGuidItemObj.GetName():
323 ItemSupArchList = Item.GetSupArchList()
324 for ItemArch in ItemSupArchList:
325 for GuidItemObjArch in __SupportArchList:
326 if ItemArch == GuidItemObjArch:
327 #
328 # ST.ERR_INF_PARSER_ITEM_DUPLICATE
329 #
330 pass
331
332 if ItemArch.upper() == 'COMMON' or GuidItemObjArch.upper() == 'COMMON':
333 #
334 # ST.ERR_INF_PARSER_ITEM_DUPLICATE_COMMON
335 #
336 pass
337
338 if self.Guids.has_key((InfGuidItemObj)):
339 GuidList = self.Guids[InfGuidItemObj]
340 GuidList.append(InfGuidItemObj)
341 self.Guids[InfGuidItemObj] = GuidList
342 else:
343 GuidList = []
344 GuidList.append(InfGuidItemObj)
345 self.Guids[InfGuidItemObj] = GuidList
346
347 return True
348
349 def GetGuid(self):
350 return self.Guids