1## @file
2# This file is used to define class objects for DEC file. It will consumed by
3#DecParser
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'''
16DecObject
17'''
18
19## Import modules
20#
21import os.path
22
23from Library.Misc import Sdict
24from Library.DataType import TAB_GUIDS
25from Library.DataType import TAB_PPIS
26from Library.DataType import TAB_PROTOCOLS
27from Library.DataType import TAB_DEC_DEFINES
28from Library.DataType import TAB_INCLUDES
29from Library.DataType import TAB_LIBRARY_CLASSES
30from Library.DataType import TAB_USER_EXTENSIONS
31from Library.DataType import TAB_PCDS
32from Library.DataType import TAB_ARCH_COMMON
33
34## _DecComments
35#
36# Base class for all data objects which have head and tail comments
37#
38class _DecComments:
39
40    ##constructor
41    #
42    def __init__(self):
43        self._HeadComment = []
44        self._TailComment = []
45
46    ## GetComments
47    #
48    def GetComments(self):
49        return self._HeadComment, self._TailComment
50
51    ## GetHeadComment
52    #
53    def GetHeadComment(self):
54        return self._HeadComment
55
56    ## SetHeadComment
57    #
58    # @param Comment: comment content
59    #
60    def SetHeadComment(self, Comment):
61        self._HeadComment = Comment
62
63    ## GetTailComment
64    #
65    def GetTailComment(self):
66        return self._TailComment
67
68    ## SetTailComment
69    #
70    # @param Comment: comment content
71    #
72    def SetTailComment(self, Comment):
73        self._TailComment = Comment
74
75## _DecBaseObject
76#
77# Base class that hold common info
78#
79class _DecBaseObject(_DecComments):
80    def __init__(self, PkgFullName):
81        _DecComments.__init__(self)
82        #
83        # Key is combined with (Arch, SectionType)
84        # Default is common
85        #
86        self.ValueDict = Sdict()
87        self._PkgFullName = PkgFullName
88        self._PackagePath, self._FileName = os.path.split(PkgFullName)
89        self._SecName = ''
90
91    ## GetSectionName
92    #
93    def GetSectionName(self):
94        return self._SecName
95
96    ## GetPackagePath
97    #
98    def GetPackagePath(self):
99        return self._PackagePath
100
101    ## GetPackageFile
102    #
103    def GetPackageFile(self):
104        return self._FileName
105
106    ## GetPackageFullName
107    #
108    def GetPackageFullName(self):
109        return self._PkgFullName
110
111    ## AddItem
112    # Add sub-item to current object, sub-class should override it if needed
113    #
114    # @param Item: Sub-item to be added
115    # @param Scope: A list store section name and arch info
116    #
117    def AddItem(self, Item, Scope):
118        if not Scope:
119            return
120        if not Item:
121            return
122        ArchModule = []
123        for Ele in Scope:
124            if Ele[1] in self.ValueDict:
125                self.ValueDict[Ele[1]].append(Item)
126            else:
127                self.ValueDict[Ele[1]] = [Item]
128            ArchModule.append(Ele[1])
129        Item.ArchAndModuleType = ArchModule
130
131    ## _GetItemByArch
132    # Helper class used by sub-class
133    # @param Arch:  arch
134    #
135    def _GetItemByArch(self, Arch):
136        Arch = Arch.upper()
137        if Arch not in self.ValueDict:
138            return []
139        return self.ValueDict[Arch]
140
141    ## _GetAllItems
142    # Get all items, union all arches, items in returned list are unique
143    #
144    def _GetAllItems(self):
145        Retlst = []
146        for Arch in self.ValueDict:
147            for Item in self.ValueDict[Arch]:
148                if Item not in Retlst:
149                    Retlst.append(Item)
150        return Retlst
151
152## _DecItemBaseObject
153#
154# Module type and arch the item belongs to
155#
156class _DecItemBaseObject(_DecComments):
157    def __init__(self):
158        _DecComments.__init__(self)
159        #
160        # Item's arch, if PCD, also include PCD type
161        #
162        self.ArchAndModuleType = []
163
164    ## GetArchList
165    #
166    def GetArchList(self):
167        ArchSet = set()
168        for Arch in self.ArchAndModuleType:
169            ArchSet.add(Arch)
170        return list(ArchSet)
171
172## DecDefineObject
173#
174# Class to hold define section infomation
175#
176class DecDefineObject(_DecBaseObject):
177    def __init__(self, PkgFullName):
178        _DecBaseObject.__init__(self, PkgFullName)
179        self._SecName = TAB_DEC_DEFINES.upper()
180        self._DecSpec = ''
181        self._PkgName = ''
182        self._PkgGuid = ''
183        self._PkgVersion = ''
184        self._PkgUniFile = ''
185
186    ## GetPackageSpecification
187    #
188    def GetPackageSpecification(self):
189        return self._DecSpec
190
191    def SetPackageSpecification(self, DecSpec):
192        self._DecSpec = DecSpec
193
194    ## GetPackageName
195    #
196    def GetPackageName(self):
197        return self._PkgName
198
199    def SetPackageName(self, PkgName):
200        self._PkgName = PkgName
201
202    ## GetPackageGuid
203    #
204    def GetPackageGuid(self):
205        return self._PkgGuid
206
207    def SetPackageGuid(self, PkgGuid):
208        self._PkgGuid = PkgGuid
209
210    ## GetPackageVersion
211    #
212    def GetPackageVersion(self):
213        return self._PkgVersion
214
215    def SetPackageVersion(self, PkgVersion):
216        self._PkgVersion = PkgVersion
217
218    ## GetPackageUniFile
219    #
220    def GetPackageUniFile(self):
221        return self._PkgUniFile
222
223    def SetPackageUniFile(self, PkgUniFile):
224        self._PkgUniFile = PkgUniFile
225
226    ## GetDefines
227    #
228    def GetDefines(self):
229        return self._GetItemByArch(TAB_ARCH_COMMON)
230
231    ## GetAllDefines
232    #
233    def GetAllDefines(self):
234        return self._GetAllItems()
235
236## DecDefineItemObject
237#
238# Each item of define section
239#
240class DecDefineItemObject(_DecItemBaseObject):
241    def __init__(self):
242        _DecItemBaseObject.__init__(self)
243        self.Key = ''
244        self.Value = ''
245
246    ## __hash__
247    #
248    def __hash__(self):
249        return hash(self.Key + self.Value)
250
251    ## __eq__
252    #
253    def __eq__(self, Other):
254        return id(self) == id(Other)
255
256    ## __str__
257    #
258    def __str__(self):
259        return str(self.ArchAndModuleType) + '\n' + self.Key + \
260            ' = ' + self.Value
261
262## DecIncludeObject
263#
264# Class to hold include section info
265#
266class DecIncludeObject(_DecBaseObject):
267    def __init__(self, PkgFullName):
268        _DecBaseObject.__init__(self, PkgFullName)
269        self._SecName = TAB_INCLUDES.upper()
270
271    ## GetIncludes
272    #
273    def GetIncludes(self, Arch=TAB_ARCH_COMMON):
274        return self._GetItemByArch(Arch)
275
276    ## GetAllIncludes
277    #
278    def GetAllIncludes(self):
279        return self._GetAllItems()
280
281## DecIncludeItemObject
282#
283# Item of include section
284#
285class DecIncludeItemObject(_DecItemBaseObject):
286    def __init__(self, File, Root):
287        self.File = File
288        self.Root = Root
289        _DecItemBaseObject.__init__(self)
290
291    ## __hash__
292    #
293    def __hash__(self):
294        return hash(self.File)
295
296    ## __eq__
297    #
298    def __eq__(self, Other):
299        return id(self) == id(Other)
300
301    ## __str__
302    #
303    def __str__(self):
304        return self.File
305
306## DecLibraryclassObject
307#
308# Class to hold library class section info
309#
310class DecLibraryclassObject(_DecBaseObject):
311    def __init__(self, PkgFullName):
312        _DecBaseObject.__init__(self, PkgFullName)
313        self._PackagePath, self._FileName = os.path.split(PkgFullName)
314        self._SecName = TAB_LIBRARY_CLASSES.upper()
315
316    ## GetLibraryclasses
317    #
318    def GetLibraryclasses(self, Arch=TAB_ARCH_COMMON):
319        return self._GetItemByArch(Arch)
320
321    ## GetAllLibraryclasses
322    #
323    def GetAllLibraryclasses(self):
324        return self._GetAllItems()
325
326## DecLibraryclassItemObject
327# Item of library class section
328#
329class DecLibraryclassItemObject(_DecItemBaseObject):
330    def __init__(self, Libraryclass, File, Root):
331        _DecItemBaseObject.__init__(self)
332        self.File = File
333        self.Root = Root
334        self.Libraryclass = Libraryclass
335
336    ## __hash__
337    #
338    def __hash__(self):
339        return hash(self.Libraryclass + self.File)
340
341    ## __eq__
342    #
343    def __eq__(self, Other):
344        return id(self) == id(Other)
345
346    ## __str__
347    #
348    def __str__(self):
349        return self.Libraryclass + '|' + self.File
350
351## DecPcdObject
352# Class to hold PCD section
353#
354class DecPcdObject(_DecBaseObject):
355    def __init__(self, PkgFullName):
356        _DecBaseObject.__init__(self, PkgFullName)
357        self._SecName = TAB_PCDS.upper()
358
359    ## AddItem
360    #
361    # Diff from base class
362    #
363    # @param Item: Item
364    # @param Scope: Scope
365    #
366    def AddItem(self, Item, Scope):
367        if not Scope:
368            return
369        if not Item:
370            return
371        ArchModule = []
372        for Type, Arch in Scope:
373            if (Type, Arch) in self.ValueDict:
374                self.ValueDict[Type, Arch].append(Item)
375            else:
376                self.ValueDict[Type, Arch] = [Item]
377            ArchModule.append([Type, Arch])
378        Item.ArchAndModuleType = ArchModule
379
380    ## GetPcds
381    #
382    # @param PcdType: PcdType
383    # @param Arch: Arch
384    #
385    def GetPcds(self, PcdType, Arch=TAB_ARCH_COMMON):
386        PcdType = PcdType.upper()
387        Arch = Arch.upper()
388        if (PcdType, Arch) not in self.ValueDict:
389            return []
390        return self.ValueDict[PcdType, Arch]
391
392    ## GetPcdsByType
393    #
394    # @param PcdType: PcdType
395    #
396    def GetPcdsByType(self, PcdType):
397        PcdType = PcdType.upper()
398        Retlst = []
399        for TypeInDict, Arch in self.ValueDict:
400            if TypeInDict != PcdType:
401                continue
402            for Item in self.ValueDict[PcdType, Arch]:
403                if Item not in Retlst:
404                    Retlst.append(Item)
405        return Retlst
406
407## DecPcdItemObject
408#
409# Item of PCD section
410#
411# @param _DecItemBaseObject: _DecItemBaseObject object
412#
413class DecPcdItemObject(_DecItemBaseObject):
414    def __init__(self, Guid, Name, Value, DatumType,
415                 Token, MaxDatumSize=''):
416        _DecItemBaseObject.__init__(self)
417        self.TokenCName = Name
418        self.TokenSpaceGuidCName = Guid
419        self.DatumType = DatumType
420        self.DefaultValue = Value
421        self.TokenValue = Token
422        self.MaxDatumSize = MaxDatumSize
423
424    ## __hash__
425    #
426    def __hash__(self):
427        return hash(self.TokenSpaceGuidCName + self.TokenCName)
428
429    ## __eq__
430    #
431    def __eq__(self, Other):
432        return id(self) == id(Other)
433
434    ## GetArchListOfType
435    #
436    # @param PcdType: PcdType
437    #
438    def GetArchListOfType(self, PcdType):
439        ItemSet = set()
440        PcdType = PcdType.upper()
441        for Type, Arch in self.ArchAndModuleType:
442            if Type != PcdType:
443                continue
444            ItemSet.add(Arch)
445        return list(ItemSet)
446
447## DecGuidObjectBase
448#
449# Base class for PPI, Protocol, and GUID.
450# Hold same data but has different method for clarification in sub-class
451#
452# @param _DecBaseObject: Dec Base Object
453#
454class DecGuidObjectBase(_DecBaseObject):
455    def __init__(self, PkgFullName):
456        _DecBaseObject.__init__(self, PkgFullName)
457
458    ## GetGuidStyleItems
459    #
460    # @param Arch: Arch
461    #
462    def GetGuidStyleItems(self, Arch=TAB_ARCH_COMMON):
463        return self._GetItemByArch(Arch)
464
465    ## GetGuidStyleAllItems
466    #
467    def GetGuidStyleAllItems(self):
468        return self._GetAllItems()
469
470## DecGuidItemObject
471#
472# Item of GUID, PPI and Protocol section
473#
474# @param _DecItemBaseObject: Dec Item Base Object
475#
476class DecGuidItemObject(_DecItemBaseObject):
477    def __init__(self, CName, GuidCValue, GuidString):
478        _DecItemBaseObject.__init__(self)
479        self.GuidCName = CName
480        self.GuidCValue = GuidCValue
481        self.GuidString = GuidString
482
483    ## __hash__
484    #
485    def __hash__(self):
486        return hash(self.GuidCName)
487
488    ## __eq__
489    #
490    def __eq__(self, Other):
491        return id(self) == id(Other)
492
493    ## __str__
494    #
495    def __str__(self):
496        return self.GuidCName + ' = ' + self.GuidCValue
497
498## DecGuidObject
499#
500# Class for GUID section
501#
502# @param DecGuidObjectBase: Dec Guid Object Base
503#
504class DecGuidObject(DecGuidObjectBase):
505    def __init__(self, PkgFullName):
506        DecGuidObjectBase.__init__(self, PkgFullName)
507        self._SecName = TAB_GUIDS.upper()
508
509    ## GetGuids
510    #
511    # @param Arch: Arch
512    #
513    def GetGuids(self, Arch=TAB_ARCH_COMMON):
514        return self._GetItemByArch(Arch)
515
516    ## GetAllGuids
517    #
518    def GetAllGuids(self):
519        return self._GetAllItems()
520
521## DecPpiObject
522#
523# Class for PPI seciont
524#
525# @param DecGuidObjectBase: Dec Guid Object Base
526#
527class DecPpiObject(DecGuidObjectBase):
528    def __init__(self, PkgFullName):
529        DecGuidObjectBase.__init__(self, PkgFullName)
530        self._SecName = TAB_PPIS.upper()
531
532    ## GetPpis
533    #
534    # @param Arch: Arch
535    #
536    def GetPpis(self, Arch=TAB_ARCH_COMMON):
537        return self._GetItemByArch(Arch)
538
539    ## GetAllPpis
540    #
541    def GetAllPpis(self):
542        return self._GetAllItems()
543
544## DecProtocolObject
545#
546# Class for protocol section
547#
548# @param DecGuidObjectBase: Dec Guid Object Base
549#
550class DecProtocolObject(DecGuidObjectBase):
551    def __init__(self, PkgFullName):
552        DecGuidObjectBase.__init__(self, PkgFullName)
553        self._SecName = TAB_PROTOCOLS.upper()
554
555    ## GetProtocols
556    #
557    # @param Arch: Arch
558    #
559    def GetProtocols(self, Arch=TAB_ARCH_COMMON):
560        return self._GetItemByArch(Arch)
561
562    ## GetAllProtocols
563    #
564    def GetAllProtocols(self):
565        return self._GetAllItems()
566
567## DecUserExtensionObject
568#
569# Class for user extension section
570#
571# @param _DecBaseObject: Dec Guid Object Base
572#
573class DecUserExtensionObject(_DecBaseObject):
574    def __init__(self, PkgFullName):
575        _DecBaseObject.__init__(self, PkgFullName)
576        self._SecName = TAB_USER_EXTENSIONS.upper()
577        self.ItemList = []
578
579    ## GetProtocols
580    #
581    # @param Item: Item
582    # @param Scope: Scope
583    #
584    def AddItem(self, Item, Scope):
585        if not Scope:
586            pass
587        if not Item:
588            return
589        self.ItemList.append(Item)
590
591    ## GetAllUserExtensions
592    #
593    def GetAllUserExtensions(self):
594        return self.ItemList
595
596
597## DecUserExtensionItemObject
598# Item for user extension section
599#
600# @param _DecItemBaseObject: Dec Item Base Object
601#
602class DecUserExtensionItemObject(_DecItemBaseObject):
603    def __init__(self):
604        _DecItemBaseObject.__init__(self)
605        self.UserString = ''
606        self.UserId = ''
607        self.IdString = ''
608
609
610
611
612