Modify code according to Wang, jian's comments.
[mirror_edk2.git] / Tools / Source / PcdTools / org / tianocore / pcd / entity / UsageInstance.java
1 /** @file
2 UsageInstance class.
3
4 This class indicate an usage instance for a PCD token. This instance maybe a module
5 or platform setting. When a module produce or cosume a PCD token, then this module
6 is an usage instance for this PCD token.
7
8 Copyright (c) 2006, Intel Corporation
9 All rights reserved. This program and the accompanying materials
10 are licensed and made available under the terms and conditions of the BSD License
11 which accompanies this distribution. The full text of the license may be found at
12 http://opensource.org/licenses/bsd-license.php
13
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16
17 **/
18 package org.tianocore.pcd.entity;
19
20 import org.tianocore.pcd.entity.CommonDefinition;
21 import org.tianocore.pcd.entity.UsageIdentification;
22
23 /**
24 This class indicate an usage instance for a PCD token. This instance maybe a module
25 or platform setting. When a module produce or cosume a PCD token, then this module
26 is an usage instance for this PCD token.
27 **/
28 public class UsageInstance {
29 ///
30 /// This parent that this usage instance belongs to.
31 ///
32 public Token parentToken;
33
34 ///
35 /// ModuleIdentification for Usage Instance
36 ///
37 public UsageIdentification usageId;
38
39 ///
40 /// Arch also is a key for a UsageInstance
41 ///
42 public String arch;
43
44 ///
45 /// The PCD type defined for module
46 ///
47 public Token.PCD_TYPE modulePcdType;
48
49 ///
50 /// The value of the PCD in this usage instance.
51 ///
52 public String datum;
53
54 ///
55 /// The maxDatumSize could be different for same PCD in different module
56 /// But this case is allow for FeatureFlag, FixedAtBuild, PatchableInModule
57 /// type.
58 ///
59 public int maxDatumSize;
60
61 ///
62 /// Autogen string for header file.
63 ///
64 public String hAutogenStr;
65
66 ///
67 /// Auotgen string for C code file.
68 ///
69 public String cAutogenStr;
70
71 /**
72 Constructure function for UsageInstance
73
74 @param parentToken The token instance for this usgaInstance
75 @param usageId The identification for usage instance
76 @param modulePcdType The PCD type for this usage instance
77 @param value The value of this PCD in this usage instance
78 @param maxDatumSize The max datum size of this PCD in this usage
79 instance.
80 **/
81 public UsageInstance(Token parentToken,
82 UsageIdentification usageId,
83 Token.PCD_TYPE modulePcdType,
84 String value,
85 int maxDatumSize) {
86 this.parentToken = parentToken;
87 this.usageId = usageId;
88 this.modulePcdType = modulePcdType;
89 this.datum = value;
90 this.maxDatumSize = maxDatumSize;
91 }
92
93 /**
94 Get the primary key for usage instance array for every token.
95
96 @param usageId The identification of UsageInstance
97
98 @retval String The primary key for this usage instance
99 **/
100 public static String getPrimaryKey(UsageIdentification usageId) {
101 return usageId.toString();
102 }
103
104 /**
105 Get primary key string for this usage instance
106
107 @return String primary key string
108 **/
109 public String getPrimaryKey() {
110 return UsageInstance.getPrimaryKey(usageId);
111 }
112
113 /**
114 Judget whether current module is PEI driver
115
116 @return boolean whether current module is PEI driver
117 **/
118 public boolean isPeiPhaseComponent() {
119 int moduleType = CommonDefinition.getModuleType(usageId.moduleType);
120
121 if ((moduleType == CommonDefinition.ModuleTypePeiCore) ||
122 (moduleType == CommonDefinition.ModuleTypePeim)) {
123 return true;
124 }
125 return false;
126 }
127
128 /**
129 Judge whether current module is DXE driver.
130
131 @return boolean whether current module is DXE driver
132 **/
133 public boolean isDxePhaseComponent() {
134 int moduleType = CommonDefinition.getModuleType(usageId.moduleType);
135
136 if ((moduleType == CommonDefinition.ModuleTypeDxeDriver) ||
137 (moduleType == CommonDefinition.ModuleTypeDxeRuntimeDriver) ||
138 (moduleType == CommonDefinition.ModuleTypeDxeSalDriver) ||
139 (moduleType == CommonDefinition.ModuleTypeDxeSmmDriver) ||
140 (moduleType == CommonDefinition.ModuleTypeUefiDriver) ||
141 (moduleType == CommonDefinition.ModuleTypeUefiApplication)
142 ) {
143 return true;
144 }
145 return false;
146 }
147
148 /**
149 Generate autogen string for header file and C code file.
150
151 @param isBuildUsedLibrary whether the autogen is for library.
152 **/
153 public void generateAutoGen(boolean isBuildUsedLibrary) {
154 String guidStringCName = null;
155 boolean isByteArray = false;
156 String printDatum = null;
157 String tokenNumberString = null;
158
159 hAutogenStr = "";
160 cAutogenStr = "";
161
162 if (this.modulePcdType == Token.PCD_TYPE.DYNAMIC_EX) {
163 //
164 // For DYNAMIC_EX type PCD, use original token number in SPD or FPD to generate autogen
165 //
166 tokenNumberString = Long.toString(parentToken.dynamicExTokenNumber, 16);
167 } else {
168 //
169 // For Others type PCD, use autogenerated token number to generate autogen
170 //
171 tokenNumberString = Long.toString(parentToken.tokenNumber, 16);
172 }
173
174 hAutogenStr += String.format("#define _PCD_TOKEN_%s 0x%s\r\n", parentToken.cName, tokenNumberString);
175
176 //
177 // Judge the value of this PCD is byte array type
178 //
179 if (!isBuildUsedLibrary && !parentToken.isDynamicPCD) {
180 if (datum.trim().charAt(0) == '{') {
181 isByteArray = true;
182 }
183 }
184
185 //
186 // "ULL" should be added to value's tail for UINT64 value
187 //
188 if (parentToken.datumType == Token.DATUM_TYPE.UINT64) {
189 printDatum = this.datum + "ULL";
190 } else {
191 printDatum = this.datum;
192 }
193
194 switch (modulePcdType) {
195 case FEATURE_FLAG:
196 hAutogenStr += String.format("extern const BOOLEAN _gPcd_FixedAtBuild_%s;\r\n",
197 parentToken.cName);
198 hAutogenStr += String.format("#define _PCD_GET_MODE_%s_%s _gPcd_FixedAtBuild_%s\r\n",
199 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
200 parentToken.cName,
201 parentToken.cName);
202 hAutogenStr += String.format("//#define _PCD_SET_MODE_%s_%s ASSERT(FALSE) If is not allowed to set value for a FEATURE_FLAG PCD\r\n",
203 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
204 parentToken.cName);
205
206 if (!isBuildUsedLibrary) {
207 hAutogenStr += String.format("#define _PCD_VALUE_%s %s\r\n",
208 parentToken.cName,
209 printDatum);
210 cAutogenStr += String.format("GLOBAL_REMOVE_IF_UNREFERENCED const BOOLEAN _gPcd_FixedAtBuild_%s = _PCD_VALUE_%s;\r\n",
211 parentToken.cName,
212 parentToken.cName);
213 }
214 break;
215 case FIXED_AT_BUILD:
216 if (isByteArray) {
217 hAutogenStr += String.format("extern const UINT8 _gPcd_FixedAtBuild_%s[];\r\n",
218 parentToken.cName);
219 hAutogenStr += String.format("#define _PCD_GET_MODE_%s_%s (VOID*)_gPcd_FixedAtBuild_%s\r\n",
220 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
221 parentToken.cName,
222 parentToken.cName);
223 } else {
224 hAutogenStr += String.format("extern const %s _gPcd_FixedAtBuild_%s;\r\n",
225 Token.getAutogendatumTypeString(parentToken.datumType),
226 parentToken.cName);
227 hAutogenStr += String.format("#define _PCD_GET_MODE_%s_%s _gPcd_FixedAtBuild_%s\r\n",
228 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
229 parentToken.cName,
230 parentToken.cName);
231 }
232
233 hAutogenStr += String.format("//#define _PCD_SET_MODE_%s_%s ASSERT(FALSE) // It is not allowed to set value for a FIXED_AT_BUILD PCD\r\n",
234 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
235 parentToken.cName);
236 if (!isBuildUsedLibrary) {
237 if (parentToken.datumType == Token.DATUM_TYPE.POINTER) {
238 if (isByteArray) {
239 hAutogenStr += String.format("#define _PCD_VALUE_%s (VOID*)_gPcd_FixedAtBuild_%s\r\n",
240 parentToken.cName,
241 parentToken.cName);
242 cAutogenStr += String.format("GLOBAL_REMOVE_IF_UNREFERENCED const UINT8 _gPcd_FixedAtBuild_%s[] = %s;\r\n",
243 parentToken.cName,
244 printDatum);
245 } else {
246 hAutogenStr += String.format("#define _PCD_VALUE_%s %s\r\n",
247 parentToken.cName,
248 printDatum);
249 cAutogenStr += String.format("GLOBAL_REMOVE_IF_UNREFERENCED const %s _gPcd_FixedAtBuild_%s = _PCD_VALUE_%s;\r\n",
250 Token.getAutogendatumTypeString(parentToken.datumType),
251 parentToken.cName,
252 parentToken.cName);
253 }
254 } else {
255 hAutogenStr += String.format("#define _PCD_VALUE_%s %s\r\n",
256 parentToken.cName,
257 printDatum);
258 cAutogenStr += String.format("GLOBAL_REMOVE_IF_UNREFERENCED const %s _gPcd_FixedAtBuild_%s = _PCD_VALUE_%s;\r\n",
259 Token.getAutogendatumTypeString(parentToken.datumType),
260 parentToken.cName,
261 parentToken.cName);
262 }
263 }
264 break;
265 case PATCHABLE_IN_MODULE:
266 if (isByteArray) {
267 hAutogenStr += String.format("extern UINT8 _gPcd_BinaryPatch_%s[];\r\n",
268 parentToken.cName);
269 hAutogenStr += String.format("#define _PCD_GET_MODE_%s_%s (VOID*)_gPcd_BinaryPatch_%s\r\n",
270 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
271 parentToken.cName,
272 parentToken.cName);
273 } else {
274 hAutogenStr += String.format("extern %s _gPcd_BinaryPatch_%s;\r\n",
275 Token.getAutogendatumTypeString(parentToken.datumType),
276 parentToken.cName);
277 hAutogenStr += String.format("#define _PCD_GET_MODE_%s_%s _gPcd_BinaryPatch_%s\r\n",
278 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
279 parentToken.cName,
280 parentToken.cName);
281 }
282
283 //
284 // Generate _PCD_SET_MODE_xx macro for using set BinaryPatch value via PcdSet macro
285 //
286 if (parentToken.datumType == Token.DATUM_TYPE.POINTER) {
287 hAutogenStr += String.format("#define _PCD_SET_MODE_%s_%s(SizeOfBuffer, Buffer) CopyMem (_gPcd_BinaryPatch_%s, (Buffer), (SizeOfBuffer))\r\n",
288 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
289 parentToken.cName,
290 parentToken.cName);
291 } else {
292 hAutogenStr += String.format("#define _PCD_SET_MODE_%s_%s(Value) (_gPcd_BinaryPatch_%s = (Value))\r\n",
293 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
294 parentToken.cName,
295 parentToken.cName);
296 }
297
298 if (!isBuildUsedLibrary) {
299 hAutogenStr += String.format("#define _PCD_VALUE_%s %s\r\n",
300 parentToken.cName,
301 printDatum);
302 if (isByteArray) {
303 cAutogenStr += String.format("GLOBAL_REMOVE_IF_UNREFERENCED UINT8 _gPcd_BinaryPatch_%s[] = _PCD_VALUE_%s;\r\n",
304 parentToken.cName,
305 parentToken.cName);
306 } else {
307 cAutogenStr += String.format("GLOBAL_REMOVE_IF_UNREFERENCED %s _gPcd_BinaryPatch_%s = _PCD_VALUE_%s;\r\n",
308 Token.getAutogendatumTypeString(parentToken.datumType),
309 parentToken.cName,
310 parentToken.cName);
311 }
312 }
313
314 break;
315 case DYNAMIC:
316 hAutogenStr += String.format("#define _PCD_GET_MODE_%s_%s LibPcdGet%s(_PCD_TOKEN_%s)\r\n",
317 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
318 parentToken.cName,
319 Token.getAutogenLibrarydatumTypeString(parentToken.datumType),
320 parentToken.cName);
321 if (parentToken.datumType == Token.DATUM_TYPE.POINTER) {
322 hAutogenStr += String.format("#define _PCD_SET_MODE_%s_%s(SizeOfBuffer, Buffer) LibPcdSet%s(_PCD_TOKEN_%s, (SizeOfBuffer), (Buffer))\r\n",
323 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
324 parentToken.cName,
325 Token.getAutogenLibrarydatumTypeString(parentToken.datumType),
326 parentToken.cName);
327 } else {
328 hAutogenStr += String.format("#define _PCD_SET_MODE_%s_%s(Value) LibPcdSet%s(_PCD_TOKEN_%s, (Value))\r\n",
329 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
330 parentToken.cName,
331 Token.getAutogenLibrarydatumTypeString(parentToken.datumType),
332 parentToken.cName);
333 }
334 break;
335 case DYNAMIC_EX:
336 guidStringCName = "_gPcd_TokenSpaceGuid_" +
337 parentToken.tokenSpaceName.toString().replaceAll("-", "_");
338
339 hAutogenStr += String.format("#define _PCD_GET_MODE_%s_%s LibPcdGetEx%s(&%s, _PCD_TOKEN_%s)\r\n",
340 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
341 parentToken.cName,
342 Token.getAutogenLibrarydatumTypeString(parentToken.datumType),
343 guidStringCName,
344 parentToken.cName);
345
346 if (parentToken.datumType == Token.DATUM_TYPE.POINTER) {
347 hAutogenStr += String.format("#define _PCD_SET_MODE_%s_%s(SizeOfBuffer, Buffer) LibPcdSetEx%s(&%s, _PCD_TOKEN_%s, (SizeOfBuffer), (Buffer))\r\n",
348 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
349 parentToken.cName,
350 Token.getAutogenLibrarydatumTypeString(parentToken.datumType),
351 guidStringCName,
352 parentToken.cName);
353 } else {
354 hAutogenStr += String.format("#define _PCD_SET_MODE_%s_%s(Value) LibPcdSetEx%s(&%s, _PCD_TOKEN_%s, (Value))\r\n",
355 Token.GetAutogenDefinedatumTypeString(parentToken.datumType),
356 parentToken.cName,
357 Token.getAutogenLibrarydatumTypeString(parentToken.datumType),
358 guidStringCName,
359 parentToken.cName);
360
361 }
362 break;
363 }
364 }
365
366 /**
367 Get the autogen string for header file.
368
369 @return The string of header file.
370 **/
371 public String getHAutogenStr() {
372 return hAutogenStr;
373 }
374
375 /**
376 Get the autogen string for C code file.
377
378 @return The string of C Code file.
379 **/
380 public String getCAutogenStr() {
381 return cAutogenStr;
382 }
383 }
384