ppuout.pp 36 KB

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