ppuout.pp 36 KB

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