ppuout.pp 34 KB

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