ppuout.pp 35 KB

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