ppuout.pp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471
  1. {
  2. Copyright (c) 2013 by Yury Sidorov and the FPC Development Team
  3. Base classes for a custom output of a PPU File
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************}
  16. unit ppuout;
  17. {$mode objfpc}{$H+}
  18. {$I+}
  19. interface
  20. uses SysUtils, cclasses, Classes;
  21. type
  22. TPpuDefType = (dtNone, dtUnit, dtObject, dtRecord, dtProc, dtField, dtProp, dtParam, dtVar,
  23. dtTypeRef, dtConst, dtProcType, dtEnum, dtSet, dtClassRef, dtArray, dtPointer,
  24. dtOrd, dtFloat, dtString, dtFile, dtVariant, dtUndefined, dtFormal);
  25. TPpuDef = class;
  26. TPpuContainerDef = class;
  27. TPpuUnitDef = class;
  28. { TPpuOutput }
  29. TPpuOutput = class
  30. private
  31. FOutFile: ^Text;
  32. FIndent: integer;
  33. FIndentSize: integer;
  34. FIndStr: string;
  35. FNoIndent: boolean;
  36. procedure SetIndent(AValue: integer);
  37. procedure SetIndentSize(AValue: integer);
  38. protected
  39. procedure WriteObjectStart(const AName: string; Def: TPpuDef = nil); virtual;
  40. procedure WriteObjectEnd(const AName: string; Def: TPpuDef = nil); virtual;
  41. procedure WriteArrayStart(const AName: string); virtual;
  42. procedure WriteArrayEnd(const AName: string); virtual;
  43. procedure WriteStr(const AName, AValue: string); virtual;
  44. procedure WriteInt(const AName: string; AValue: Int64; Signed: boolean = True); virtual;
  45. procedure WriteFloat(const AName: string; AValue: extended); virtual;
  46. procedure WriteBool(const AName: string; AValue: boolean); virtual;
  47. procedure WriteNull(const AName: string); virtual;
  48. public
  49. constructor Create(var OutFile: Text); virtual;
  50. destructor Destroy; override;
  51. procedure Write(const s: string);
  52. procedure WriteLn(const s: string = '');
  53. procedure IncI; virtual;
  54. procedure DecI; virtual;
  55. procedure Init; virtual;
  56. procedure Done; virtual;
  57. property Indent: integer read FIndent write SetIndent;
  58. property IndentSize: integer read FIndentSize write SetIndentSize;
  59. end;
  60. { TPpuRef }
  61. TPpuRef = class
  62. private
  63. FId: cardinal;
  64. function GetId: cardinal;
  65. function GetIsSymId: boolean;
  66. procedure SetId(AValue: cardinal);
  67. procedure SetIsSymId(AValue: boolean);
  68. public
  69. UnitIndex: word;
  70. constructor Create;
  71. procedure Write(Output: TPpuOutput; const RefName: string);
  72. property Id: cardinal read GetId write SetId;
  73. property IsSymId: boolean read GetIsSymId write SetIsSymId;
  74. function IsCurUnit: boolean; inline;
  75. function IsNull: boolean; inline;
  76. end;
  77. TPpuFilePos = record
  78. FileIndex: dword;
  79. Line, Col: integer;
  80. end;
  81. TPpuDefVisibility = (dvPublic, dvPublished, dvProtected, dvPrivate, dvHidden);
  82. { TPpuDef }
  83. TPpuDef = class
  84. private
  85. FId: cardinal;
  86. FParent: TPpuContainerDef;
  87. FParentUnit: TPpuUnitDef;
  88. function GetDefTypeName: string;
  89. function GetId: cardinal;
  90. function GetParentUnit: TPpuUnitDef;
  91. procedure SetId(AValue: cardinal);
  92. procedure SetParent(AValue: TPpuContainerDef);
  93. protected
  94. procedure WriteDef(Output: TPpuOutput); virtual;
  95. public
  96. DefType: TPpuDefType;
  97. Name: string;
  98. FilePos: TPpuFilePos;
  99. // Symbol/definition reference
  100. Ref: TPpuRef;
  101. Visibility: TPpuDefVisibility;
  102. constructor Create(AParent: TPpuContainerDef); virtual; reintroduce;
  103. destructor Destroy; override;
  104. procedure Write(Output: TPpuOutput; const AttrName: string = '');
  105. function CanWrite: boolean; virtual;
  106. procedure SetSymId(AId: integer);
  107. property Parent: TPpuContainerDef read FParent write SetParent;
  108. property ParentUnit: TPpuUnitDef read GetParentUnit;
  109. property Id: cardinal read GetId write SetId;
  110. property DefTypeName: string read GetDefTypeName;
  111. end;
  112. { TPpuContainerDef }
  113. TPpuContainerDef = class(TPpuDef)
  114. private
  115. FItems: TList;
  116. function GetCount: integer;
  117. function GetItem(Index: Integer): TPpuDef;
  118. procedure SetItem(Index: Integer; AValue: TPpuDef);
  119. protected
  120. procedure WriteDef(Output: TPpuOutput); override;
  121. procedure BeforeWriteItems(Output: TPpuOutput); virtual;
  122. public
  123. ItemsName: string;
  124. constructor Create(AParent: TPpuContainerDef); override;
  125. destructor Destroy; override;
  126. function Add(Def: TPpuDef): integer;
  127. property Items[Index: Integer]: TPpuDef read GetItem write SetItem; default;
  128. property Count: integer read GetCount;
  129. end;
  130. { TPpuTypeRef }
  131. TPpuTypeRef = class(TPpuDef)
  132. protected
  133. procedure WriteDef(Output: TPpuOutput); override;
  134. public
  135. constructor Create(AParent: TPpuContainerDef); override;
  136. end;
  137. { TPpuUnitDef }
  138. TPpuUnitDef = class(TPpuContainerDef)
  139. private
  140. FIndexById: THashSet;
  141. protected
  142. procedure WriteDef(Output: TPpuOutput); override;
  143. public
  144. Version: cardinal;
  145. Crc, IntfCrc: cardinal;
  146. TargetOS, TargetCPU: string;
  147. UsedUnits: TPpuContainerDef;
  148. RefUnits: array of string;
  149. SourceFiles: TPpuContainerDef;
  150. constructor Create(AParent: TPpuContainerDef); override;
  151. destructor Destroy; override;
  152. function FindById(AId: integer; FindSym: boolean = False): TPpuDef;
  153. end;
  154. { TPpuSrcFile }
  155. TPpuSrcFile = class(TPpuDef)
  156. protected
  157. procedure WriteDef(Output: TPpuOutput); override;
  158. public
  159. FileTime: TDateTime;
  160. constructor Create(AParent: TPpuContainerDef); override;
  161. end;
  162. TPpuProcOption = (poProcedure, poFunction, poConstructor, poDestructor, poOperator,
  163. poClassMethod, poVirtual, poAbstract, poOverriding, poOverload, poInline);
  164. TPpuProcOptions = set of TPpuProcOption;
  165. { TPpuProcDef }
  166. TPpuProcDef = class(TPpuContainerDef)
  167. protected
  168. procedure BeforeWriteItems(Output: TPpuOutput); override;
  169. public
  170. ReturnType: TPpuRef;
  171. Options: TPpuProcOptions;
  172. constructor Create(AParent: TPpuContainerDef); override;
  173. destructor Destroy; override;
  174. end;
  175. { TPpuProcTypeDef }
  176. TPpuProcTypeDef = class(TPpuProcDef)
  177. protected
  178. procedure BeforeWriteItems(Output: TPpuOutput); override;
  179. public
  180. MethodPtr: boolean;
  181. constructor Create(AParent: TPpuContainerDef); override;
  182. end;
  183. TPpuConstType = (ctUnknown, ctInt, ctFloat, ctStr, ctSet, ctPtr);
  184. { TPpuConstDef }
  185. TPpuConstDef = class(TPpuDef)
  186. protected
  187. procedure WriteDef(Output: TPpuOutput); override;
  188. public
  189. ConstType: TPpuConstType;
  190. TypeRef: TPpuRef;
  191. VInt: Int64;
  192. VFloat: extended;
  193. VStr: string;
  194. VSet: array[0..31] of byte;
  195. constructor Create(AParent: TPpuContainerDef); override;
  196. destructor Destroy; override;
  197. function CanWrite: boolean; override;
  198. end;
  199. { TPpuVarDef }
  200. TPpuVarDef = class(TPpuDef)
  201. protected
  202. procedure WriteDef(Output: TPpuOutput); override;
  203. public
  204. VarType: TPpuRef;
  205. constructor Create(AParent: TPpuContainerDef); override;
  206. destructor Destroy; override;
  207. end;
  208. TPpuParamSpez = (psValue, psVar, psOut, psConst, psConstRef, psHidden);
  209. { TPpuParamDef }
  210. TPpuParamDef = class(TPpuVarDef)
  211. protected
  212. procedure WriteDef(Output: TPpuOutput); override;
  213. public
  214. Spez: TPpuParamSpez;
  215. DefaultValue: TPpuRef;
  216. constructor Create(AParent: TPpuContainerDef); override;
  217. destructor Destroy; override;
  218. function CanWrite: boolean; override;
  219. end;
  220. TPpuObjType = (otUnknown, otClass, otObject, otInterface, otHelper);
  221. TPpuObjOption = (ooIsAbstract, ooCopied);
  222. TPpuObjOptions = set of TPpuObjOption;
  223. { TPpuObjectDef }
  224. TPpuObjectDef = class(TPpuContainerDef)
  225. protected
  226. procedure BeforeWriteItems(Output: TPpuOutput); override;
  227. public
  228. ObjType: TPpuObjType;
  229. Ancestor: TPpuRef;
  230. Options: TPpuObjOptions;
  231. IID: string;
  232. HelperParent: TPpuRef;
  233. Size: integer;
  234. constructor Create(AParent: TPpuContainerDef); override;
  235. destructor Destroy; override;
  236. function CanWrite: boolean; override;
  237. end;
  238. { TPpuFieldDef }
  239. TPpuFieldDef = class(TPpuVarDef)
  240. public
  241. constructor Create(AParent: TPpuContainerDef); override;
  242. end;
  243. { TPpuPropDef }
  244. TPpuPropDef = class(TPpuContainerDef)
  245. protected
  246. procedure BeforeWriteItems(Output: TPpuOutput); override;
  247. public
  248. PropType: TPpuRef;
  249. Getter, Setter: TPpuRef;
  250. constructor Create(AParent: TPpuContainerDef); override;
  251. destructor Destroy; override;
  252. end;
  253. { TPpuRecordDef }
  254. TPpuRecordDef = class(TPpuObjectDef)
  255. protected
  256. procedure BeforeWriteItems(Output: TPpuOutput); override;
  257. public
  258. constructor Create(AParent: TPpuContainerDef); override;
  259. function CanWrite: boolean; override;
  260. end;
  261. { TPpuClassRefDef }
  262. TPpuClassRefDef = class(TPpuDef)
  263. protected
  264. procedure WriteDef(Output: TPpuOutput); override;
  265. public
  266. ClassRef: TPpuRef;
  267. constructor Create(AParent: TPpuContainerDef); override;
  268. destructor Destroy; override;
  269. end;
  270. TPpuArrayOption = (aoDynamic);
  271. TPpuArrayOptions = set of TPpuArrayOption;
  272. { TPpuArrayDef }
  273. TPpuArrayDef = class(TPpuDef)
  274. protected
  275. procedure WriteDef(Output: TPpuOutput); override;
  276. public
  277. ElType: TPpuRef;
  278. RangeType: TPpuRef;
  279. RangeLow, RangeHigh: Int64;
  280. Options: TPpuArrayOptions;
  281. constructor Create(AParent: TPpuContainerDef); override;
  282. destructor Destroy; override;
  283. function CanWrite: boolean; override;
  284. end;
  285. { TPpuEnumDef }
  286. TPpuEnumDef = class(TPpuContainerDef)
  287. protected
  288. procedure BeforeWriteItems(Output: TPpuOutput); override;
  289. public
  290. ElLow, ElHigh: integer;
  291. Size: byte;
  292. CopyFrom: TPpuRef;
  293. constructor Create(AParent: TPpuContainerDef); override;
  294. destructor Destroy; override;
  295. end;
  296. { TPpuSetDef }
  297. TPpuSetDef = class(TPpuDef)
  298. protected
  299. procedure WriteDef(Output: TPpuOutput); override;
  300. public
  301. ElType: TPpuRef;
  302. SetBase, SetMax: integer;
  303. Size: byte;
  304. constructor Create(AParent: TPpuContainerDef); override;
  305. destructor Destroy; override;
  306. end;
  307. { TPpuPointerDef }
  308. TPpuPointerDef = class(TPpuDef)
  309. protected
  310. procedure WriteDef(Output: TPpuOutput); override;
  311. public
  312. Ptr: TPpuRef;
  313. constructor Create(AParent: TPpuContainerDef); override;
  314. destructor Destroy; override;
  315. end;
  316. TPpuOrdType = (otVoid, otUInt, otSInt, otPasBool, otBool, otChar, otCurrency);
  317. { TPpuOrdDef }
  318. TPpuOrdDef = class(TPpuDef)
  319. protected
  320. procedure WriteDef(Output: TPpuOutput); override;
  321. public
  322. OrdType: TPpuOrdType;
  323. Size: byte;
  324. RangeLow, RangeHigh: Int64;
  325. constructor Create(AParent: TPpuContainerDef); override;
  326. end;
  327. TPpuFloatType = (pftSingle, pftDouble, pftExtended, pftComp, pftCurrency, pftFloat128);
  328. { TPpuFloatDef }
  329. TPpuFloatDef = class(TPpuDef)
  330. protected
  331. procedure WriteDef(Output: TPpuOutput); override;
  332. public
  333. FloatType: TPpuFloatType;
  334. constructor Create(AParent: TPpuContainerDef); override;
  335. end;
  336. TPpuStrType = (stShort, stAnsi, stWide, stUnicode, stLong);
  337. { TPpuStringDef }
  338. TPpuStringDef = class(TPpuDef)
  339. protected
  340. procedure WriteDef(Output: TPpuOutput); override;
  341. public
  342. StrType: TPpuStrType;
  343. Len: integer;
  344. constructor Create(AParent: TPpuContainerDef); override;
  345. end;
  346. TPpuFileType = (ftText, ftTyped, ftUntyped);
  347. { TPpuFileDef }
  348. TPpuFileDef = class(TPpuDef)
  349. protected
  350. procedure WriteDef(Output: TPpuOutput); override;
  351. public
  352. FileType: TPpuFileType;
  353. TypeRef: TPpuRef;
  354. constructor Create(AParent: TPpuContainerDef); override;
  355. destructor Destroy; override;
  356. end;
  357. { TPpuVariantDef }
  358. TPpuVariantDef = class(TPpuDef)
  359. protected
  360. procedure WriteDef(Output: TPpuOutput); override;
  361. public
  362. IsOLE: boolean;
  363. constructor Create(AParent: TPpuContainerDef); override;
  364. end;
  365. { TPpuUndefinedDef }
  366. TPpuUndefinedDef = class(TPpuDef)
  367. public
  368. constructor Create(AParent: TPpuContainerDef); override;
  369. end;
  370. { TPpuFormalDef }
  371. TPpuFormalDef = class(TPpuDef)
  372. protected
  373. procedure WriteDef(Output: TPpuOutput); override;
  374. public
  375. IsTyped: boolean;
  376. constructor Create(AParent: TPpuContainerDef); override;
  377. end;
  378. implementation
  379. const
  380. DefTypeNames: array[TPpuDefType] of string =
  381. ('', 'unit', 'obj', 'rec', 'proc', 'field', 'prop', 'param', 'var',
  382. 'type', 'const', 'proctype', 'enum', 'set', 'classref', 'array', 'ptr',
  383. 'ord', 'float', 'string', 'file', 'variant', 'undefined', 'formal');
  384. ProcOptionNames: array[TPpuProcOption] of string =
  385. ('procedure', 'function', 'constructor', 'destructor', 'operator',
  386. 'classmethod', 'virtual', 'abstract', 'overriding', 'overload', 'inline');
  387. DefVisibilityNames: array[TPpuDefVisibility] of string =
  388. ('public', 'published', 'protected', 'private', '');
  389. ParamSpezNames: array[TPpuParamSpez] of string =
  390. ('value', 'var', 'out', 'const', 'constref', '');
  391. ObjTypeNames: array[TPpuObjType] of string =
  392. ('', 'class', 'object', 'interface', 'helper');
  393. ObjOptionNames: array[TPpuObjOption] of string =
  394. ('abstract','copied');
  395. ArrayOptionNames: array[TPpuArrayOption] of string =
  396. ('dynamic');
  397. ConstTypeNames: array[TPpuConstType] of string =
  398. ('', 'int', 'float', 'string', 'set', 'pointer');
  399. OrdTypeNames: array[TPpuOrdType] of string =
  400. ('void', 'uint', 'sint', 'pasbool', 'bool', 'char', 'currency');
  401. FloatTypeNames: array[TPpuFloatType] of string =
  402. ('single', 'double', 'extended', 'comp', 'currency', 'float128');
  403. StrTypeNames: array[TPpuStrType] of string =
  404. ('short', 'ansi', 'wide', 'unicode', 'long');
  405. FileTypeNames: array[TPpuFileType] of string =
  406. ('text', 'typed', 'untyped');
  407. SymIdBit = $80000000;
  408. InvalidId = cardinal(-1);
  409. InvalidUnit = word(-1);
  410. function IsSymId(Id: cardinal): boolean; inline;
  411. begin
  412. Result:=Id and SymIdBit <> 0;
  413. end;
  414. { TPpuUndefinedDef }
  415. constructor TPpuUndefinedDef.Create(AParent: TPpuContainerDef);
  416. begin
  417. inherited Create(AParent);
  418. DefType:=dtUndefined;
  419. end;
  420. { TPpuFormalDef }
  421. procedure TPpuFormalDef.WriteDef(Output: TPpuOutput);
  422. begin
  423. inherited WriteDef(Output);
  424. Output.WriteBool('IsTyped', IsTyped);
  425. end;
  426. constructor TPpuFormalDef.Create(AParent: TPpuContainerDef);
  427. begin
  428. inherited Create(AParent);
  429. DefType:=dtFormal;
  430. end;
  431. { TPpuVariantDef }
  432. procedure TPpuVariantDef.WriteDef(Output: TPpuOutput);
  433. begin
  434. inherited WriteDef(Output);
  435. if IsOLE then
  436. Output.WriteBool('OleVariant', True);
  437. end;
  438. constructor TPpuVariantDef.Create(AParent: TPpuContainerDef);
  439. begin
  440. inherited Create(AParent);
  441. DefType:=dtVariant;
  442. end;
  443. { TPpuFileDef }
  444. procedure TPpuFileDef.WriteDef(Output: TPpuOutput);
  445. begin
  446. inherited WriteDef(Output);
  447. Output.WriteStr('FileType', FileTypeNames[FileType]);
  448. if FileType = ftTyped then
  449. TypeRef.Write(Output, 'TypeRef');
  450. end;
  451. constructor TPpuFileDef.Create(AParent: TPpuContainerDef);
  452. begin
  453. inherited Create(AParent);
  454. DefType:=dtFile;
  455. TypeRef:=TPpuRef.Create;
  456. end;
  457. destructor TPpuFileDef.Destroy;
  458. begin
  459. TypeRef.Free;
  460. inherited Destroy;
  461. end;
  462. { TPpuStringDef }
  463. procedure TPpuStringDef.WriteDef(Output: TPpuOutput);
  464. begin
  465. inherited WriteDef(Output);
  466. Output.WriteStr('StrType', StrTypeNames[StrType]);
  467. if Len >= 0 then
  468. Output.WriteInt('Len', Len);
  469. end;
  470. constructor TPpuStringDef.Create(AParent: TPpuContainerDef);
  471. begin
  472. inherited Create(AParent);
  473. DefType:=dtString;
  474. end;
  475. { TPpuFloatDef }
  476. procedure TPpuFloatDef.WriteDef(Output: TPpuOutput);
  477. begin
  478. inherited WriteDef(Output);
  479. Output.WriteStr('FloatType', FloatTypeNames[FloatType]);
  480. end;
  481. constructor TPpuFloatDef.Create(AParent: TPpuContainerDef);
  482. begin
  483. inherited Create(AParent);
  484. DefType:=dtFloat;
  485. end;
  486. { TPpuOrdDef }
  487. procedure TPpuOrdDef.WriteDef(Output: TPpuOutput);
  488. var
  489. Signed: boolean;
  490. begin
  491. inherited WriteDef(Output);
  492. with Output do begin
  493. WriteStr('OrdType', OrdTypeNames[OrdType]);
  494. WriteInt('Size', Size);
  495. Signed:=OrdType in [otSInt, otCurrency, otBool];
  496. WriteInt('Low', RangeLow, Signed);
  497. WriteInt('High', RangeHigh, Signed);
  498. end;
  499. end;
  500. constructor TPpuOrdDef.Create(AParent: TPpuContainerDef);
  501. begin
  502. inherited Create(AParent);
  503. DefType:=dtOrd;
  504. end;
  505. { TPpuPointerDef }
  506. procedure TPpuPointerDef.WriteDef(Output: TPpuOutput);
  507. begin
  508. inherited WriteDef(Output);
  509. Ptr.Write(Output, 'Ptr');
  510. end;
  511. constructor TPpuPointerDef.Create(AParent: TPpuContainerDef);
  512. begin
  513. inherited Create(AParent);
  514. DefType:=dtPointer;
  515. Ptr:=TPpuRef.Create;
  516. end;
  517. destructor TPpuPointerDef.Destroy;
  518. begin
  519. Ptr.Free;
  520. inherited Destroy;
  521. end;
  522. { TPpuSetDef }
  523. procedure TPpuSetDef.WriteDef(Output: TPpuOutput);
  524. begin
  525. inherited WriteDef(Output);
  526. with Output do begin
  527. WriteInt('Size', Size);
  528. WriteInt('Base', SetBase);
  529. WriteInt('Max', SetMax);
  530. end;
  531. ElType.Write(Output, 'ElType');
  532. end;
  533. constructor TPpuSetDef.Create(AParent: TPpuContainerDef);
  534. begin
  535. inherited Create(AParent);
  536. DefType:=dtSet;
  537. ElType:=TPpuRef.Create;
  538. end;
  539. destructor TPpuSetDef.Destroy;
  540. begin
  541. ElType.Free;
  542. inherited Destroy;
  543. end;
  544. { TPpuEnumDef }
  545. procedure TPpuEnumDef.BeforeWriteItems(Output: TPpuOutput);
  546. begin
  547. inherited BeforeWriteItems(Output);
  548. with Output do begin
  549. WriteInt('Low', ElLow);
  550. WriteInt('High', ElHigh);
  551. WriteInt('Size', Size);
  552. end;
  553. if not CopyFrom.IsNull then
  554. CopyFrom.Write(Output, 'CopyFrom');
  555. end;
  556. constructor TPpuEnumDef.Create(AParent: TPpuContainerDef);
  557. begin
  558. inherited Create(AParent);
  559. DefType:=dtEnum;
  560. ItemsName:='Elements';
  561. CopyFrom:=TPpuRef.Create;
  562. end;
  563. destructor TPpuEnumDef.Destroy;
  564. begin
  565. CopyFrom.Free;
  566. inherited Destroy;
  567. end;
  568. { TPpuConstDef }
  569. procedure TPpuConstDef.WriteDef(Output: TPpuOutput);
  570. var
  571. s, ss: string;
  572. i: integer;
  573. begin
  574. inherited WriteDef(Output);
  575. with Output do begin
  576. WriteStr('ValType', ConstTypeNames[ConstType]);
  577. s:='Value';
  578. case ConstType of
  579. ctInt:
  580. WriteInt(s, VInt);
  581. ctFloat:
  582. WriteFloat(s, VFloat);
  583. ctStr:
  584. WriteStr(s, VStr);
  585. ctPtr:
  586. if VInt = 0 then
  587. WriteNull(s)
  588. else
  589. if QWord(VInt) > $FFFFFFFF then
  590. WriteStr(s, hexStr(QWord(VInt), 8))
  591. else
  592. WriteStr(s, hexStr(QWord(VInt), 16));
  593. ctSet:
  594. begin
  595. ss:='';
  596. for i:=Low(VSet) to High(VSet) do
  597. ss:=ss + hexStr(VSet[i], 2);
  598. WriteStr(s, ss);
  599. end;
  600. end;
  601. end;
  602. if not TypeRef.IsNull then
  603. TypeRef.Write(Output, 'TypeRef');
  604. end;
  605. constructor TPpuConstDef.Create(AParent: TPpuContainerDef);
  606. begin
  607. inherited Create(AParent);
  608. DefType:=dtConst;
  609. TypeRef:=TPpuRef.Create;
  610. ConstType:=ctUnknown;
  611. end;
  612. destructor TPpuConstDef.Destroy;
  613. begin
  614. TypeRef.Free;
  615. inherited Destroy;
  616. end;
  617. function TPpuConstDef.CanWrite: boolean;
  618. begin
  619. Result:=inherited CanWrite and (ConstType <> ctUnknown);
  620. end;
  621. { TPpuArrayDef }
  622. procedure TPpuArrayDef.WriteDef(Output: TPpuOutput);
  623. var
  624. opt: TPpuArrayOption;
  625. begin
  626. inherited WriteDef(Output);
  627. if Options <> [] then begin
  628. Output.WriteArrayStart('Options');
  629. for opt:=Low(opt) to High(opt) do
  630. if opt in Options then
  631. Output.WriteStr('', ArrayOptionNames[opt]);
  632. Output.WriteArrayEnd('Options');
  633. end;
  634. ElType.Write(Output, 'ElType');
  635. RangeType.Write(Output, 'RangeType');;
  636. Output.WriteInt('Low', RangeLow);
  637. Output.WriteInt('High', RangeHigh);
  638. end;
  639. constructor TPpuArrayDef.Create(AParent: TPpuContainerDef);
  640. begin
  641. inherited Create(AParent);
  642. DefType:=dtArray;
  643. ElType:=TPpuRef.Create;
  644. RangeType:=TPpuRef.Create;
  645. end;
  646. destructor TPpuArrayDef.Destroy;
  647. begin
  648. ElType.Free;
  649. RangeType.Free;
  650. inherited Destroy;
  651. end;
  652. function TPpuArrayDef.CanWrite: boolean;
  653. begin
  654. Result:=inherited CanWrite and (Name <> '');
  655. end;
  656. { TPpuClassRefDef }
  657. procedure TPpuClassRefDef.WriteDef(Output: TPpuOutput);
  658. begin
  659. inherited WriteDef(Output);
  660. ClassRef.Write(Output, 'Ref');
  661. end;
  662. constructor TPpuClassRefDef.Create(AParent: TPpuContainerDef);
  663. begin
  664. inherited Create(AParent);
  665. DefType:=dtClassRef;
  666. ClassRef:=TPpuRef.Create;
  667. end;
  668. destructor TPpuClassRefDef.Destroy;
  669. begin
  670. ClassRef.Free;
  671. inherited Destroy;
  672. end;
  673. { TPpuRecordDef }
  674. procedure TPpuRecordDef.BeforeWriteItems(Output: TPpuOutput);
  675. begin
  676. inherited BeforeWriteItems(Output);
  677. if ooCopied in Options then
  678. Ancestor.Write(Output, 'CopyFrom');
  679. end;
  680. constructor TPpuRecordDef.Create(AParent: TPpuContainerDef);
  681. begin
  682. inherited Create(AParent);
  683. DefType:=dtRecord;
  684. end;
  685. function TPpuRecordDef.CanWrite: boolean;
  686. begin
  687. Result:=True;
  688. end;
  689. { TPpuPropDef }
  690. procedure TPpuPropDef.BeforeWriteItems(Output: TPpuOutput);
  691. begin
  692. inherited BeforeWriteItems(Output);
  693. PropType.Write(Output, 'PropType');
  694. Getter.Write(Output, 'Getter');
  695. Setter.Write(Output, 'Setter');
  696. end;
  697. constructor TPpuPropDef.Create(AParent: TPpuContainerDef);
  698. begin
  699. inherited Create(AParent);
  700. DefType:=dtProp;
  701. ItemsName:='Params';
  702. PropType:=TPpuRef.Create;
  703. Getter:=TPpuRef.Create;
  704. Setter:=TPpuRef.Create;
  705. end;
  706. destructor TPpuPropDef.Destroy;
  707. begin
  708. Getter.Free;
  709. Setter.Free;
  710. PropType.Free;
  711. inherited Destroy;
  712. end;
  713. { TPpuTypeRef }
  714. procedure TPpuTypeRef.WriteDef(Output: TPpuOutput);
  715. begin
  716. inherited WriteDef(Output);
  717. Ref.Write(Output, 'Ref');
  718. end;
  719. constructor TPpuTypeRef.Create(AParent: TPpuContainerDef);
  720. begin
  721. inherited Create(AParent);
  722. DefType:=dtTypeRef;
  723. end;
  724. { TPpuFieldDef }
  725. constructor TPpuFieldDef.Create(AParent: TPpuContainerDef);
  726. begin
  727. inherited Create(AParent);
  728. DefType:=dtField;
  729. end;
  730. { TPpuParamDef }
  731. procedure TPpuParamDef.WriteDef(Output: TPpuOutput);
  732. var
  733. i, j: integer;
  734. d: TPpuDef;
  735. begin
  736. inherited WriteDef(Output);
  737. if Spez <> psValue then
  738. Output.WriteStr('Spez', ParamSpezNames[Spez]);
  739. if not DefaultValue.IsNull then begin
  740. j:=DefaultValue.Id;
  741. for i:=0 to Parent.Count - 1 do begin
  742. d:=Parent[i];
  743. if (d.DefType = dtConst) and (d.Id = j) then begin
  744. d.Visibility:=dvPublic;
  745. d.Name:='';
  746. d.Write(Output, 'Default');
  747. d.Visibility:=dvHidden;
  748. break;
  749. end;
  750. end;
  751. end;
  752. end;
  753. constructor TPpuParamDef.Create(AParent: TPpuContainerDef);
  754. begin
  755. inherited Create(AParent);
  756. DefType:=dtParam;
  757. Spez:=psValue;
  758. DefaultValue:=TPpuRef.Create;
  759. end;
  760. destructor TPpuParamDef.Destroy;
  761. begin
  762. DefaultValue.Free;
  763. inherited Destroy;
  764. end;
  765. function TPpuParamDef.CanWrite: boolean;
  766. begin
  767. Result:=inherited CanWrite and (Spez <> psHidden);
  768. end;
  769. { TPpuVarDef }
  770. procedure TPpuVarDef.WriteDef(Output: TPpuOutput);
  771. begin
  772. inherited WriteDef(Output);
  773. VarType.Write(Output, 'VarType');
  774. end;
  775. constructor TPpuVarDef.Create(AParent: TPpuContainerDef);
  776. begin
  777. inherited Create(AParent);
  778. DefType:=dtVar;
  779. VarType:=TPpuRef.Create;
  780. end;
  781. destructor TPpuVarDef.Destroy;
  782. begin
  783. VarType.Free;
  784. inherited Destroy;
  785. end;
  786. { TPpuObjectDef }
  787. procedure TPpuObjectDef.BeforeWriteItems(Output: TPpuOutput);
  788. var
  789. opt: TPpuObjOption;
  790. begin
  791. inherited BeforeWriteItems(Output);
  792. if ObjType <> otUnknown then begin
  793. Output.WriteStr('ObjType', ObjTypeNames[ObjType]);
  794. Ancestor.Write(Output, 'Ancestor');
  795. end;
  796. if Options <> [] then begin
  797. Output.WriteArrayStart('Options');
  798. for opt:=Low(opt) to High(opt) do
  799. if opt in Options then
  800. Output.WriteStr('', ObjOptionNames[opt]);
  801. Output.WriteArrayEnd('Options');
  802. end;
  803. Output.WriteInt('Size', Size);
  804. if IID <> '' then
  805. Output.WriteStr('IID', IID);
  806. if not HelperParent.IsNull then
  807. HelperParent.Write(Output, 'HelperParent');
  808. end;
  809. constructor TPpuObjectDef.Create(AParent: TPpuContainerDef);
  810. begin
  811. inherited Create(AParent);
  812. DefType:=dtObject;
  813. ItemsName:='Fields';
  814. ObjType:=otUnknown;
  815. Ancestor:=TPpuRef.Create;
  816. HelperParent:=TPpuRef.Create;
  817. end;
  818. destructor TPpuObjectDef.Destroy;
  819. begin
  820. Ancestor.Free;
  821. HelperParent.Free;
  822. inherited Destroy;
  823. end;
  824. function TPpuObjectDef.CanWrite: boolean;
  825. begin
  826. Result:=inherited CanWrite and (ObjType <> otUnknown);
  827. end;
  828. { TPpuRef }
  829. function TPpuRef.GetId: cardinal;
  830. begin
  831. if FId = InvalidId then
  832. Result:=InvalidId
  833. else
  834. Result:=FId and not SymIdBit;
  835. end;
  836. function TPpuRef.GetIsSymId: boolean;
  837. begin
  838. Result:=FId and SymIdBit <> 0;
  839. end;
  840. procedure TPpuRef.SetId(AValue: cardinal);
  841. begin
  842. if (FId = InvalidId) or (AValue = InvalidId) then
  843. FId:=AValue
  844. else
  845. FId:=AValue or (FId and SymIdBit);
  846. end;
  847. procedure TPpuRef.SetIsSymId(AValue: boolean);
  848. begin
  849. if AValue then
  850. FId:=FId or SymIdBit
  851. else
  852. FId:=FId and not SymIdBit;
  853. end;
  854. constructor TPpuRef.Create;
  855. begin
  856. UnitIndex:=InvalidUnit;
  857. FId:=InvalidId;
  858. end;
  859. procedure TPpuRef.Write(Output: TPpuOutput; const RefName: string);
  860. begin
  861. with Output do
  862. if IsNull then
  863. WriteNull(RefName)
  864. else begin
  865. WriteObjectStart(RefName);
  866. if not IsCurUnit then
  867. WriteInt('Unit', UnitIndex);
  868. if IsSymId then
  869. WriteInt('SymId', Id)
  870. else
  871. WriteInt('Id', Id);
  872. WriteObjectEnd(RefName);
  873. end;
  874. end;
  875. function TPpuRef.IsCurUnit: boolean;
  876. begin
  877. Result:=UnitIndex = InvalidUnit;
  878. end;
  879. function TPpuRef.IsNull: boolean;
  880. begin
  881. Result:=Id = InvalidId;
  882. end;
  883. { TPpuProcTypeDef }
  884. procedure TPpuProcTypeDef.BeforeWriteItems(Output: TPpuOutput);
  885. begin
  886. inherited BeforeWriteItems(Output);
  887. if MethodPtr then
  888. Output.WriteBool('MethodPtr', MethodPtr);
  889. end;
  890. constructor TPpuProcTypeDef.Create(AParent: TPpuContainerDef);
  891. begin
  892. inherited Create(AParent);
  893. DefType:=dtProcType;
  894. end;
  895. { TPpuProcDef }
  896. procedure TPpuProcDef.BeforeWriteItems(Output: TPpuOutput);
  897. var
  898. opt: TPpuProcOption;
  899. begin
  900. inherited BeforeWriteItems(Output);
  901. if Options <> [] then begin
  902. Output.WriteArrayStart('Options');
  903. for opt:=Low(opt) to High(opt) do
  904. if opt in Options then
  905. Output.WriteStr('', ProcOptionNames[opt]);
  906. Output.WriteArrayEnd('Options');
  907. end;
  908. ReturnType.Write(Output, 'RetType');
  909. end;
  910. constructor TPpuProcDef.Create(AParent: TPpuContainerDef);
  911. begin
  912. inherited Create(AParent);
  913. DefType:=dtProc;
  914. ItemsName:='Params';
  915. ReturnType:=TPpuRef.Create;
  916. end;
  917. destructor TPpuProcDef.Destroy;
  918. begin
  919. ReturnType.Free;
  920. inherited Destroy;
  921. end;
  922. { TPpuSrcFile }
  923. procedure TPpuSrcFile.WriteDef(Output: TPpuOutput);
  924. begin
  925. inherited WriteDef(Output);
  926. Output.WriteStr('Time', FormatDateTime('yyyy"-"mm"-"dd hh":"nn":"ss', FileTime));
  927. end;
  928. constructor TPpuSrcFile.Create(AParent: TPpuContainerDef);
  929. begin
  930. inherited Create(AParent);
  931. DefType:=dtFile;
  932. end;
  933. { TPpuOutput }
  934. procedure TPpuOutput.SetIndent(AValue: integer);
  935. begin
  936. if FIndent=AValue then Exit;
  937. FIndent:=AValue;
  938. if FIndent < 0 then
  939. FIndent:=0;
  940. SetLength(FIndStr, FIndent*IndentSize);
  941. if FIndent > 0 then
  942. FillChar(FIndStr[1], FIndent*IndentSize, ' ');
  943. end;
  944. procedure TPpuOutput.SetIndentSize(AValue: integer);
  945. begin
  946. if FIndentSize=AValue then Exit;
  947. FIndentSize:=AValue;
  948. end;
  949. procedure TPpuOutput.WriteStr(const AName, AValue: string);
  950. begin
  951. end;
  952. procedure TPpuOutput.WriteInt(const AName: string; AValue: Int64; Signed: boolean);
  953. begin
  954. if Signed then
  955. WriteStr(AName, IntToStr(AValue))
  956. else
  957. WriteStr(AName, IntToStr(QWord(AValue)));
  958. end;
  959. procedure TPpuOutput.WriteFloat(const AName: string; AValue: extended);
  960. var
  961. s: string;
  962. begin
  963. Str(AValue, s);
  964. WriteStr(AName, s);
  965. end;
  966. procedure TPpuOutput.WriteBool(const AName: string; AValue: boolean);
  967. begin
  968. if AValue then
  969. WriteStr(AName, '1')
  970. else
  971. WriteStr(AName, '0');
  972. end;
  973. procedure TPpuOutput.WriteNull(const AName: string);
  974. begin
  975. WriteStr(AName, '');
  976. end;
  977. procedure TPpuOutput.WriteArrayStart(const AName: string);
  978. begin
  979. IncI;
  980. end;
  981. procedure TPpuOutput.WriteArrayEnd(const AName: string);
  982. begin
  983. DecI;
  984. end;
  985. procedure TPpuOutput.WriteObjectStart(const AName: string; Def: TPpuDef);
  986. begin
  987. IncI;
  988. if Def = nil then
  989. exit;
  990. if Def.DefType <> dtNone then
  991. WriteStr('Type', Def.DefTypeName);
  992. if Def.Name <> '' then
  993. WriteStr('Name', Def.Name);
  994. end;
  995. procedure TPpuOutput.WriteObjectEnd(const AName: string; Def: TPpuDef);
  996. begin
  997. DecI;
  998. end;
  999. constructor TPpuOutput.Create(var OutFile: Text);
  1000. begin
  1001. FOutFile:=@OutFile;
  1002. FIndentSize:=2;
  1003. end;
  1004. destructor TPpuOutput.Destroy;
  1005. begin
  1006. inherited Destroy;
  1007. end;
  1008. procedure TPpuOutput.Write(const s: string);
  1009. begin
  1010. if not FNoIndent then
  1011. System.Write(FOutFile^, FIndStr);
  1012. System.Write(FOutFile^, s);
  1013. FNoIndent:=True;
  1014. end;
  1015. procedure TPpuOutput.WriteLn(const s: string);
  1016. begin
  1017. Self.Write(s + LineEnding);
  1018. FNoIndent:=False;
  1019. end;
  1020. procedure TPpuOutput.IncI;
  1021. begin
  1022. Indent:=Indent + 1;
  1023. end;
  1024. procedure TPpuOutput.DecI;
  1025. begin
  1026. Indent:=Indent - 1;
  1027. end;
  1028. procedure TPpuOutput.Init;
  1029. begin
  1030. end;
  1031. procedure TPpuOutput.Done;
  1032. begin
  1033. end;
  1034. { TPpuUnitDef }
  1035. procedure TPpuUnitDef.WriteDef(Output: TPpuOutput);
  1036. var
  1037. i: integer;
  1038. begin
  1039. with Output do begin
  1040. if Version <> 0 then
  1041. WriteInt('Version', Version);
  1042. if TargetCPU <> '' then
  1043. WriteStr('TargetCPU', TargetCPU);
  1044. if TargetOS <> '' then
  1045. WriteStr('TargetOS', TargetOS);
  1046. if Crc <> 0 then
  1047. WriteStr('CRC', hexStr(Crc, 8));
  1048. if IntfCrc <> 0 then
  1049. WriteStr('InterfaceCRC', hexStr(IntfCrc, 8));
  1050. UsedUnits.WriteDef(Output);
  1051. if Length(RefUnits) > 0 then begin
  1052. WriteArrayStart('Units');
  1053. for i:=0 to High(RefUnits) do
  1054. WriteStr('', RefUnits[i]);
  1055. WriteArrayEnd('Units');
  1056. end;
  1057. SourceFiles.WriteDef(Output);
  1058. end;
  1059. inherited WriteDef(Output);
  1060. end;
  1061. constructor TPpuUnitDef.Create(AParent: TPpuContainerDef);
  1062. begin
  1063. inherited Create(AParent);
  1064. DefType:=dtUnit;
  1065. ItemsName:='Interface';
  1066. UsedUnits:=TPpuContainerDef.Create(nil);
  1067. UsedUnits.FParent:=Self;
  1068. UsedUnits.ItemsName:='Uses';
  1069. SourceFiles:=TPpuContainerDef.Create(nil);
  1070. SourceFiles.FParent:=Self;
  1071. SourceFiles.ItemsName:='Files';
  1072. FIndexById:=THashSet.Create(64, True, False);
  1073. end;
  1074. destructor TPpuUnitDef.Destroy;
  1075. begin
  1076. UsedUnits.Free;
  1077. SourceFiles.Free;
  1078. FIndexById.Free;
  1079. inherited Destroy;
  1080. end;
  1081. function TPpuUnitDef.FindById(AId: integer; FindSym: boolean): TPpuDef;
  1082. var
  1083. h: PHashSetItem;
  1084. i: cardinal;
  1085. begin
  1086. Result:=nil;
  1087. if AId = -1 then
  1088. exit;
  1089. i:=AId;
  1090. if FindSym then
  1091. i:=i or SymIdBit;
  1092. h:=FIndexById.Find(@i, SizeOf(i));
  1093. if h <> nil then
  1094. Result:=TPpuDef(h^.Data)
  1095. else
  1096. Result:=nil;
  1097. end;
  1098. { TPpuContainerDef }
  1099. function TPpuContainerDef.GetCount: integer;
  1100. begin
  1101. Result:=FItems.Count;
  1102. end;
  1103. function TPpuContainerDef.GetItem(Index: Integer): TPpuDef;
  1104. begin
  1105. Result:=TPpuDef(FItems[Index]);
  1106. end;
  1107. procedure TPpuContainerDef.SetItem(Index: Integer; AValue: TPpuDef);
  1108. begin
  1109. FItems[Index]:=AValue;
  1110. end;
  1111. procedure TPpuContainerDef.WriteDef(Output: TPpuOutput);
  1112. var
  1113. i: integer;
  1114. begin
  1115. inherited WriteDef(Output);
  1116. BeforeWriteItems(Output);
  1117. if Count = 0 then
  1118. exit;
  1119. Output.WriteArrayStart(ItemsName);
  1120. for i:=0 to Count - 1 do
  1121. Items[i].Write(Output);
  1122. Output.WriteArrayEnd(ItemsName);
  1123. end;
  1124. procedure TPpuContainerDef.BeforeWriteItems(Output: TPpuOutput);
  1125. begin
  1126. end;
  1127. constructor TPpuContainerDef.Create(AParent: TPpuContainerDef);
  1128. begin
  1129. inherited Create(AParent);
  1130. FItems:=TList.Create;
  1131. ItemsName:='Contents';
  1132. end;
  1133. destructor TPpuContainerDef.Destroy;
  1134. var
  1135. i: integer;
  1136. begin
  1137. for i:=0 to FItems.Count - 1 do
  1138. TObject(FItems[i]).Free;
  1139. FItems.Free;
  1140. inherited Destroy;
  1141. end;
  1142. function TPpuContainerDef.Add(Def: TPpuDef): integer;
  1143. begin
  1144. Result:=FItems.Add(Def);
  1145. Def.FParent:=Self;
  1146. end;
  1147. { TPpuDef }
  1148. function TPpuDef.GetDefTypeName: string;
  1149. begin
  1150. Result:=DefTypeNames[DefType];
  1151. end;
  1152. function TPpuDef.GetId: cardinal;
  1153. begin
  1154. if FId = InvalidId then
  1155. Result:=InvalidId
  1156. else
  1157. Result:=FId and not SymIdBit;
  1158. end;
  1159. function TPpuDef.GetParentUnit: TPpuUnitDef;
  1160. var
  1161. d: TPpuContainerDef;
  1162. begin
  1163. if FParentUnit = nil then begin
  1164. d:=Parent;
  1165. while (d <> nil) and (d.DefType <> dtUnit) do
  1166. d:=d.Parent;
  1167. FParentUnit:=TPpuUnitDef(d);
  1168. end;
  1169. Result:=FParentUnit;
  1170. end;
  1171. procedure TPpuDef.SetId(AValue: cardinal);
  1172. var
  1173. h: PHashSetItem;
  1174. u: TPpuUnitDef;
  1175. begin
  1176. if FId = AValue then Exit;
  1177. u:=ParentUnit;
  1178. if (FId <> InvalidId) and (u <> nil) then begin
  1179. h:=u.FIndexById.Find(@FId, SizeOf(FId));
  1180. if h <> nil then
  1181. u.FIndexById.Remove(h);
  1182. end;
  1183. FId:=AValue;
  1184. if (FId <> InvalidId) and (u <> nil) then begin;
  1185. h:=u.FIndexById.FindOrAdd(@FId, SizeOf(FId));
  1186. h^.Data:=Self;
  1187. end;
  1188. end;
  1189. procedure TPpuDef.SetParent(AValue: TPpuContainerDef);
  1190. var
  1191. i: cardinal;
  1192. begin
  1193. if FParent=AValue then Exit;
  1194. if FParent <> nil then
  1195. raise Exception.Create('Parent can not be modified.');
  1196. AValue.Add(Self);
  1197. if FId <> InvalidId then begin
  1198. i:=FId;
  1199. FId:=InvalidId;
  1200. SetId(i);
  1201. end;
  1202. end;
  1203. procedure TPpuDef.SetSymId(AId: integer);
  1204. begin
  1205. Id:=cardinal(AId) or SymIdBit;
  1206. end;
  1207. procedure TPpuDef.WriteDef(Output: TPpuOutput);
  1208. begin
  1209. with Output do begin
  1210. if FId <> InvalidId then
  1211. if IsSymId(FId) then
  1212. WriteInt('SymId', Id)
  1213. else begin
  1214. WriteInt('Id', Id);
  1215. if not Ref.IsNull then
  1216. WriteInt('SymId', Ref.Id);
  1217. end;
  1218. if FilePos.Line > 0 then begin
  1219. WriteObjectStart('Pos');
  1220. if FilePos.FileIndex > 0 then
  1221. WriteInt('File', FilePos.FileIndex);
  1222. WriteInt('Line', FilePos.Line);
  1223. WriteInt('Col', FilePos.Col);
  1224. WriteObjectEnd('Pos');
  1225. end;
  1226. if Visibility <> dvPublic then
  1227. WriteStr('Visibility', DefVisibilityNames[Visibility]);
  1228. end;
  1229. end;
  1230. constructor TPpuDef.Create(AParent: TPpuContainerDef);
  1231. begin
  1232. FId:=InvalidId;
  1233. Ref:=TPpuRef.Create;
  1234. Visibility:=dvPublic;
  1235. if AParent <> nil then
  1236. AParent.Add(Self);
  1237. end;
  1238. destructor TPpuDef.Destroy;
  1239. begin
  1240. Ref.Free;
  1241. inherited Destroy;
  1242. end;
  1243. procedure TPpuDef.Write(Output: TPpuOutput; const AttrName: string);
  1244. begin
  1245. if not CanWrite then
  1246. exit;
  1247. if Parent <> nil then
  1248. Output.WriteObjectStart(AttrName, Self);
  1249. WriteDef(Output);
  1250. if Parent <> nil then
  1251. Output.WriteObjectEnd(AttrName, Self);
  1252. end;
  1253. function TPpuDef.CanWrite: boolean;
  1254. begin
  1255. Result:=Visibility <> dvHidden;
  1256. end;
  1257. end.