]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/UPT/Object/Parser/InfProtocolObject.py
BaseTools: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / BaseTools / Source / Python / UPT / Object / Parser / InfProtocolObject.py
1 ## @file
2 # This file is used to define class objects of INF file [Protocols] section.
3 # It will consumed by InfParser.
4 #
5 # Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
6 #
7 # SPDX-License-Identifier: BSD-2-Clause-Patent
8
9 '''
10 InfProtocolObject
11 '''
12
13 from Library.ParserValidate import IsValidCVariableName
14 from Library.CommentParsing import ParseComment
15 from Library.ExpressionValidate import IsValidFeatureFlagExp
16
17 from Library.Misc import Sdict
18
19 from Object.Parser.InfMisc import ErrorInInf
20
21 from Library import DataType as DT
22 from Logger import StringTable as ST
23
24 def ParseProtocolComment(CommentsList, InfProtocolItemObj):
25 CommentInsList = []
26 PreUsage = None
27 PreNotify = None
28 PreHelpText = ''
29 BlockFlag = -1
30 Count = 0
31 for CommentItem in CommentsList:
32 Count = Count + 1
33 CommentItemUsage, \
34 CommentItemNotify, \
35 CommentItemString, \
36 CommentItemHelpText = \
37 ParseComment(CommentItem,
38 DT.PROTOCOL_USAGE_TOKENS,
39 DT.PROTOCOL_NOTIFY_TOKENS,
40 ['PROTOCOL'],
41 False)
42
43 if CommentItemString:
44 pass
45
46 if CommentItemHelpText is None:
47 CommentItemHelpText = ''
48 if Count == len(CommentsList) and CommentItemUsage == CommentItemNotify == DT.ITEM_UNDEFINED:
49 CommentItemHelpText = DT.END_OF_LINE
50
51 if Count == len(CommentsList):
52 if BlockFlag == 1 or BlockFlag == 2:
53 if CommentItemUsage == CommentItemNotify == DT.ITEM_UNDEFINED:
54 BlockFlag = 4
55 else:
56 BlockFlag = 3
57 elif BlockFlag == -1:
58 BlockFlag = 4
59
60 if BlockFlag == -1 or BlockFlag == 1 or BlockFlag == 2:
61 if CommentItemUsage == CommentItemNotify == DT.ITEM_UNDEFINED:
62 if BlockFlag == -1:
63 BlockFlag = 1
64 elif BlockFlag == 1:
65 BlockFlag = 2
66 else:
67 if BlockFlag == 1 or BlockFlag == 2:
68 BlockFlag = 3
69 elif BlockFlag == -1:
70 BlockFlag = 4
71
72 #
73 # Combine two comment line if they are generic comment
74 #
75 if CommentItemUsage == CommentItemNotify == PreUsage == PreNotify == DT.ITEM_UNDEFINED:
76 CommentItemHelpText = PreHelpText + DT.END_OF_LINE + CommentItemHelpText
77
78 PreHelpText = CommentItemHelpText
79
80 if BlockFlag == 4:
81 CommentItemIns = InfProtocolItemCommentContent()
82 CommentItemIns.SetUsageItem(CommentItemUsage)
83 CommentItemIns.SetNotify(CommentItemNotify)
84 CommentItemIns.SetHelpStringItem(CommentItemHelpText)
85 CommentInsList.append(CommentItemIns)
86
87 BlockFlag = -1
88 PreUsage = None
89 PreNotify = None
90 PreHelpText = ''
91
92 elif BlockFlag == 3:
93 #
94 # Add previous help string
95 #
96 CommentItemIns = InfProtocolItemCommentContent()
97 CommentItemIns.SetUsageItem(DT.ITEM_UNDEFINED)
98 CommentItemIns.SetNotify(DT.ITEM_UNDEFINED)
99 if PreHelpText == '' or PreHelpText.endswith(DT.END_OF_LINE):
100 PreHelpText += DT.END_OF_LINE
101 CommentItemIns.SetHelpStringItem(PreHelpText)
102 CommentInsList.append(CommentItemIns)
103 #
104 # Add Current help string
105 #
106 CommentItemIns = InfProtocolItemCommentContent()
107 CommentItemIns.SetUsageItem(CommentItemUsage)
108 CommentItemIns.SetNotify(CommentItemNotify)
109 CommentItemIns.SetHelpStringItem(CommentItemHelpText)
110 CommentInsList.append(CommentItemIns)
111
112 BlockFlag = -1
113 PreUsage = None
114 PreNotify = None
115 PreHelpText = ''
116
117 else:
118 PreUsage = CommentItemUsage
119 PreNotify = CommentItemNotify
120 PreHelpText = CommentItemHelpText
121
122 InfProtocolItemObj.SetCommentList(CommentInsList)
123
124 return InfProtocolItemObj
125
126 class InfProtocolItemCommentContent():
127 def __init__(self):
128 #
129 # ## SOMETIMES_CONSUMES ## HelpString
130 #
131 self.UsageItem = ''
132 #
133 # Help String
134 #
135 self.HelpStringItem = ''
136 self.Notify = ''
137 self.CommentList = []
138
139 def SetUsageItem(self, UsageItem):
140 self.UsageItem = UsageItem
141 def GetUsageItem(self):
142 return self.UsageItem
143
144 def SetNotify(self, Notify):
145 if Notify != DT.ITEM_UNDEFINED:
146 self.Notify = 'true'
147 def GetNotify(self):
148 return self.Notify
149
150 def SetHelpStringItem(self, HelpStringItem):
151 self.HelpStringItem = HelpStringItem
152 def GetHelpStringItem(self):
153 return self.HelpStringItem
154
155 class InfProtocolItem():
156 def __init__(self):
157 self.Name = ''
158 self.FeatureFlagExp = ''
159 self.SupArchList = []
160 self.CommentList = []
161
162 def SetName(self, Name):
163 self.Name = Name
164 def GetName(self):
165 return self.Name
166
167 def SetFeatureFlagExp(self, FeatureFlagExp):
168 self.FeatureFlagExp = FeatureFlagExp
169 def GetFeatureFlagExp(self):
170 return self.FeatureFlagExp
171
172 def SetSupArchList(self, SupArchList):
173 self.SupArchList = SupArchList
174 def GetSupArchList(self):
175 return self.SupArchList
176
177 def SetCommentList(self, CommentList):
178 self.CommentList = CommentList
179 def GetCommentList(self):
180 return self.CommentList
181
182 ##
183 #
184 #
185 #
186 class InfProtocolObject():
187 def __init__(self):
188 self.Protocols = Sdict()
189 #
190 # Macro defined in this section should be only used in this section.
191 #
192 self.Macros = {}
193
194 def SetProtocol(self, ProtocolContent, Arch = None,):
195 __SupArchList = []
196 for ArchItem in Arch:
197 #
198 # Validate Arch
199 #
200 if (ArchItem == '' or ArchItem is None):
201 ArchItem = 'COMMON'
202 __SupArchList.append(ArchItem)
203
204 for Item in ProtocolContent:
205 #
206 # Get Comment content of this protocol
207 #
208 CommentsList = None
209 if len(Item) == 3:
210 CommentsList = Item[1]
211 CurrentLineOfItem = Item[2]
212 LineInfo = (CurrentLineOfItem[2], CurrentLineOfItem[1], CurrentLineOfItem[0])
213 Item = Item[0]
214 InfProtocolItemObj = InfProtocolItem()
215 if len(Item) >= 1 and len(Item) <= 2:
216 #
217 # Only CName contained
218 #
219 if not IsValidCVariableName(Item[0]):
220 ErrorInInf(ST.ERR_INF_PARSER_INVALID_CNAME%(Item[0]),
221 LineInfo=LineInfo)
222 if (Item[0] != ''):
223 InfProtocolItemObj.SetName(Item[0])
224 else:
225 ErrorInInf(ST.ERR_INF_PARSER_CNAME_MISSING,
226 LineInfo=LineInfo)
227 if len(Item) == 2:
228 #
229 # Contained CName and Feature Flag Express
230 # <statements> ::= <CName> ["|"
231 # <FeatureFlagExpress>]
232 # For Protocol Object
233 #
234 if Item[1].strip() == '':
235 ErrorInInf(ST.ERR_INF_PARSER_FEATURE_FLAG_EXP_MISSING,
236 LineInfo=LineInfo)
237 #
238 # Validate Feature Flag Express for Item[1]
239 #
240 FeatureFlagRtv = IsValidFeatureFlagExp(Item[1].strip())
241 if not FeatureFlagRtv[0]:
242 ErrorInInf(ST.ERR_INF_PARSER_FEATURE_FLAG_EXP_SYNTAX_INVLID%(FeatureFlagRtv[1]),
243 LineInfo=LineInfo)
244 InfProtocolItemObj.SetFeatureFlagExp(Item[1])
245
246 if len(Item) < 1 or len(Item) > 2:
247 #
248 # Invalid format of Protocols statement
249 #
250 ErrorInInf(ST.ERR_INF_PARSER_GUID_PPI_PROTOCOL_SECTION_CONTENT_ERROR,
251 LineInfo=LineInfo)
252
253 #
254 # Get/Set Usage and HelpString for Protocol entry
255 #
256 if CommentsList is not None and len(CommentsList) != 0:
257 InfProtocolItemObj = ParseProtocolComment(CommentsList, InfProtocolItemObj)
258 else:
259 CommentItemIns = InfProtocolItemCommentContent()
260 CommentItemIns.SetUsageItem(DT.ITEM_UNDEFINED)
261 CommentItemIns.SetNotify(DT.ITEM_UNDEFINED)
262 InfProtocolItemObj.SetCommentList([CommentItemIns])
263
264 InfProtocolItemObj.SetSupArchList(__SupArchList)
265
266 #
267 # Determine protocol name duplicate. Follow below rule:
268 #
269 # A protocol must not be duplicated within a [Protocols] section.
270 # A protocol may appear in multiple architectural [Protocols]
271 # sections. A protocol listed in an architectural [Protocols]
272 # section must not be listed in the common architectural
273 # [Protocols] section.
274 #
275 # NOTE: This check will not report error now.
276 #
277 for Item in self.Protocols:
278 if Item.GetName() == InfProtocolItemObj.GetName():
279 ItemSupArchList = Item.GetSupArchList()
280 for ItemArch in ItemSupArchList:
281 for ProtocolItemObjArch in __SupArchList:
282 if ItemArch == ProtocolItemObjArch:
283 #
284 # ST.ERR_INF_PARSER_ITEM_DUPLICATE
285 #
286 pass
287 if ItemArch.upper() == 'COMMON' or ProtocolItemObjArch.upper() == 'COMMON':
288 #
289 # ST.ERR_INF_PARSER_ITEM_DUPLICATE_COMMON
290 #
291 pass
292
293 if (InfProtocolItemObj) in self.Protocols:
294 ProcotolList = self.Protocols[InfProtocolItemObj]
295 ProcotolList.append(InfProtocolItemObj)
296 self.Protocols[InfProtocolItemObj] = ProcotolList
297 else:
298 ProcotolList = []
299 ProcotolList.append(InfProtocolItemObj)
300 self.Protocols[InfProtocolItemObj] = ProcotolList
301
302 return True
303
304 def GetProtocol(self):
305 return self.Protocols