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