ppuout.pp 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561
  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. ('unknown', '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. ctUnknown: ;
  590. ctInt:
  591. WriteInt(s, VInt);
  592. ctFloat:
  593. WriteFloat(s, VFloat);
  594. ctStr:
  595. WriteStr(s, VStr);
  596. ctPtr:
  597. if VInt = 0 then
  598. WriteNull(s)
  599. else
  600. if QWord(VInt) > $FFFFFFFF then
  601. WriteStr(s, hexStr(QWord(VInt), 8))
  602. else
  603. WriteStr(s, hexStr(QWord(VInt), 16));
  604. ctSet:
  605. begin
  606. ss:='';
  607. for i:=Low(VSet) to High(VSet) do
  608. ss:=ss + hexStr(VSet[i], 2);
  609. WriteStr(s, ss);
  610. end;
  611. end;
  612. end;
  613. if not TypeRef.IsNull then
  614. TypeRef.Write(Output, 'TypeRef');
  615. end;
  616. constructor TPpuConstDef.Create(AParent: TPpuContainerDef);
  617. begin
  618. inherited Create(AParent);
  619. DefType:=dtConst;
  620. TypeRef:=TPpuRef.Create;
  621. ConstType:=ctUnknown;
  622. end;
  623. destructor TPpuConstDef.Destroy;
  624. begin
  625. TypeRef.Free;
  626. inherited Destroy;
  627. end;
  628. function TPpuConstDef.CanWrite: boolean;
  629. begin
  630. Result:=inherited CanWrite and (ConstType <> ctUnknown);
  631. end;
  632. { TPpuArrayDef }
  633. procedure TPpuArrayDef.WriteDef(Output: TPpuOutput);
  634. var
  635. opt: TPpuArrayOption;
  636. begin
  637. inherited WriteDef(Output);
  638. if Options <> [] then begin
  639. Output.WriteArrayStart('Options');
  640. for opt:=Low(opt) to High(opt) do
  641. if opt in Options then
  642. Output.WriteStr('', ArrayOptionNames[opt]);
  643. Output.WriteArrayEnd('Options');
  644. end;
  645. ElType.Write(Output, 'ElType');
  646. RangeType.Write(Output, 'RangeType');;
  647. Output.WriteInt('Low', RangeLow);
  648. Output.WriteInt('High', RangeHigh);
  649. end;
  650. constructor TPpuArrayDef.Create(AParent: TPpuContainerDef);
  651. begin
  652. inherited Create(AParent);
  653. ItemsName:='Types';
  654. DefType:=dtArray;
  655. ElType:=TPpuRef.Create;
  656. RangeType:=TPpuRef.Create;
  657. end;
  658. destructor TPpuArrayDef.Destroy;
  659. begin
  660. ElType.Free;
  661. RangeType.Free;
  662. inherited Destroy;
  663. end;
  664. { TPpuClassRefDef }
  665. procedure TPpuClassRefDef.WriteDef(Output: TPpuOutput);
  666. begin
  667. inherited WriteDef(Output);
  668. ClassRef.Write(Output, 'Ref');
  669. end;
  670. constructor TPpuClassRefDef.Create(AParent: TPpuContainerDef);
  671. begin
  672. inherited Create(AParent);
  673. DefType:=dtClassRef;
  674. ClassRef:=TPpuRef.Create;
  675. end;
  676. destructor TPpuClassRefDef.Destroy;
  677. begin
  678. ClassRef.Free;
  679. inherited Destroy;
  680. end;
  681. { TPpuRecordDef }
  682. procedure TPpuRecordDef.BeforeWriteItems(Output: TPpuOutput);
  683. begin
  684. inherited BeforeWriteItems(Output);
  685. if ooCopied in Options then
  686. Ancestor.Write(Output, 'CopyFrom');
  687. end;
  688. constructor TPpuRecordDef.Create(AParent: TPpuContainerDef);
  689. begin
  690. inherited Create(AParent);
  691. DefType:=dtRecord;
  692. end;
  693. function TPpuRecordDef.CanWrite: boolean;
  694. begin
  695. Result:=True;
  696. end;
  697. { TPpuPropDef }
  698. procedure TPpuPropDef.BeforeWriteItems(Output: TPpuOutput);
  699. var
  700. opt: TPpuPropOption;
  701. begin
  702. inherited BeforeWriteItems(Output);
  703. PropType.Write(Output, 'PropType');
  704. Getter.Write(Output, 'Getter');
  705. Setter.Write(Output, 'Setter');
  706. if Options <> [] then begin
  707. Output.WriteArrayStart('Options');
  708. for opt:=Low(opt) to High(opt) do
  709. if opt in Options then
  710. Output.WriteStr('', PropOptionNames[opt]);
  711. Output.WriteArrayEnd('Options');
  712. end;
  713. end;
  714. constructor TPpuPropDef.Create(AParent: TPpuContainerDef);
  715. begin
  716. inherited Create(AParent);
  717. DefType:=dtProp;
  718. ItemsName:='Params';
  719. PropType:=TPpuRef.Create;
  720. Getter:=TPpuRef.Create;
  721. Setter:=TPpuRef.Create;
  722. end;
  723. destructor TPpuPropDef.Destroy;
  724. begin
  725. Getter.Free;
  726. Setter.Free;
  727. PropType.Free;
  728. inherited Destroy;
  729. end;
  730. { TPpuTypeRef }
  731. procedure TPpuTypeRef.WriteDef(Output: TPpuOutput);
  732. begin
  733. inherited WriteDef(Output);
  734. Ref.Write(Output, 'Ref');
  735. end;
  736. constructor TPpuTypeRef.Create(AParent: TPpuContainerDef);
  737. begin
  738. inherited Create(AParent);
  739. DefType:=dtTypeRef;
  740. end;
  741. { TPpuFieldDef }
  742. constructor TPpuFieldDef.Create(AParent: TPpuContainerDef);
  743. begin
  744. inherited Create(AParent);
  745. DefType:=dtField;
  746. end;
  747. { TPpuParamDef }
  748. procedure TPpuParamDef.WriteDef(Output: TPpuOutput);
  749. var
  750. i, j: integer;
  751. d: TPpuDef;
  752. begin
  753. inherited WriteDef(Output);
  754. if Spez <> psValue then
  755. Output.WriteStr('Spez', ParamSpezNames[Spez]);
  756. if not DefaultValue.IsNull then begin
  757. j:=DefaultValue.Id;
  758. for i:=0 to Parent.Count - 1 do begin
  759. d:=Parent[i];
  760. if (d.DefType = dtConst) and (d.Id = j) then begin
  761. d.Visibility:=dvPublic;
  762. d.Name:='';
  763. d.Write(Output, 'Default');
  764. d.Visibility:=dvHidden;
  765. break;
  766. end;
  767. end;
  768. end;
  769. end;
  770. constructor TPpuParamDef.Create(AParent: TPpuContainerDef);
  771. begin
  772. inherited Create(AParent);
  773. DefType:=dtParam;
  774. Spez:=psValue;
  775. DefaultValue:=TPpuRef.Create;
  776. end;
  777. destructor TPpuParamDef.Destroy;
  778. begin
  779. DefaultValue.Free;
  780. inherited Destroy;
  781. end;
  782. function TPpuParamDef.CanWrite: boolean;
  783. begin
  784. Result:=inherited CanWrite and (Spez <> psHidden);
  785. end;
  786. { TPpuVarDef }
  787. procedure TPpuVarDef.WriteDef(Output: TPpuOutput);
  788. begin
  789. inherited WriteDef(Output);
  790. VarType.Write(Output, 'VarType');
  791. end;
  792. constructor TPpuVarDef.Create(AParent: TPpuContainerDef);
  793. begin
  794. inherited Create(AParent);
  795. DefType:=dtVar;
  796. VarType:=TPpuRef.Create;
  797. end;
  798. destructor TPpuVarDef.Destroy;
  799. begin
  800. VarType.Free;
  801. inherited Destroy;
  802. end;
  803. { TPpuObjectDef }
  804. procedure TPpuObjectDef.BeforeWriteItems(Output: TPpuOutput);
  805. var
  806. opt: TPpuObjOption;
  807. begin
  808. inherited BeforeWriteItems(Output);
  809. if ObjType <> otUnknown then begin
  810. Output.WriteStr('ObjType', ObjTypeNames[ObjType]);
  811. Ancestor.Write(Output, 'Ancestor');
  812. end;
  813. if Options <> [] then begin
  814. Output.WriteArrayStart('Options');
  815. for opt:=Low(opt) to High(opt) do
  816. if opt in Options then
  817. Output.WriteStr('', ObjOptionNames[opt]);
  818. Output.WriteArrayEnd('Options');
  819. end;
  820. Output.WriteInt('Size', Size);
  821. if IID <> '' then
  822. Output.WriteStr('IID', IID);
  823. if not HelperParent.IsNull then
  824. HelperParent.Write(Output, 'HelperParent');
  825. end;
  826. constructor TPpuObjectDef.Create(AParent: TPpuContainerDef);
  827. begin
  828. inherited Create(AParent);
  829. DefType:=dtObject;
  830. ItemsName:='Fields';
  831. ObjType:=otUnknown;
  832. Ancestor:=TPpuRef.Create;
  833. HelperParent:=TPpuRef.Create;
  834. end;
  835. destructor TPpuObjectDef.Destroy;
  836. begin
  837. Ancestor.Free;
  838. HelperParent.Free;
  839. inherited Destroy;
  840. end;
  841. function TPpuObjectDef.CanWrite: boolean;
  842. begin
  843. Result:=inherited CanWrite and (ObjType <> otUnknown);
  844. end;
  845. { TPpuRef }
  846. function TPpuRef.GetId: cardinal;
  847. begin
  848. if FId = InvalidId then
  849. Result:=InvalidId
  850. else
  851. Result:=FId and not SymIdBit;
  852. end;
  853. function TPpuRef.GetIsSymId: boolean;
  854. begin
  855. Result:=FId and SymIdBit <> 0;
  856. end;
  857. procedure TPpuRef.SetId(AValue: cardinal);
  858. begin
  859. if (FId = InvalidId) or (AValue = InvalidId) then
  860. FId:=AValue
  861. else
  862. FId:=AValue or (FId and SymIdBit);
  863. end;
  864. procedure TPpuRef.SetIsSymId(AValue: boolean);
  865. begin
  866. if AValue then
  867. FId:=FId or SymIdBit
  868. else
  869. FId:=FId and not SymIdBit;
  870. end;
  871. constructor TPpuRef.Create;
  872. begin
  873. UnitIndex:=InvalidUnit;
  874. FId:=InvalidId;
  875. end;
  876. procedure TPpuRef.Write(Output: TPpuOutput; const RefName: string);
  877. begin
  878. with Output do
  879. if IsNull then
  880. WriteNull(RefName)
  881. else begin
  882. WriteObjectStart(RefName);
  883. if not IsCurUnit then
  884. WriteInt('Unit', UnitIndex);
  885. if IsSymId then
  886. WriteInt('SymId', Id)
  887. else
  888. WriteInt('Id', Id);
  889. WriteObjectEnd(RefName);
  890. end;
  891. end;
  892. function TPpuRef.IsCurUnit: boolean;
  893. begin
  894. Result:=UnitIndex = InvalidUnit;
  895. end;
  896. function TPpuRef.IsNull: boolean;
  897. begin
  898. Result:=Id = InvalidId;
  899. end;
  900. { TPpuProcTypeDef }
  901. procedure TPpuProcTypeDef.BeforeWriteItems(Output: TPpuOutput);
  902. begin
  903. inherited BeforeWriteItems(Output);
  904. if MethodPtr then
  905. Output.WriteBool('MethodPtr', MethodPtr);
  906. end;
  907. constructor TPpuProcTypeDef.Create(AParent: TPpuContainerDef);
  908. begin
  909. inherited Create(AParent);
  910. DefType:=dtProcType;
  911. end;
  912. { TPpuProcDef }
  913. procedure TPpuProcDef.BeforeWriteItems(Output: TPpuOutput);
  914. var
  915. opt: TPpuProcOption;
  916. begin
  917. inherited BeforeWriteItems(Output);
  918. if Options <> [] then begin
  919. Output.WriteArrayStart('Options');
  920. for opt:=Low(opt) to High(opt) do
  921. if opt in Options then
  922. Output.WriteStr('', ProcOptionNames[opt]);
  923. Output.WriteArrayEnd('Options');
  924. end;
  925. ReturnType.Write(Output, 'RetType');
  926. end;
  927. constructor TPpuProcDef.Create(AParent: TPpuContainerDef);
  928. begin
  929. inherited Create(AParent);
  930. DefType:=dtProc;
  931. ItemsName:='Params';
  932. ReturnType:=TPpuRef.Create;
  933. end;
  934. destructor TPpuProcDef.Destroy;
  935. begin
  936. ReturnType.Free;
  937. inherited Destroy;
  938. end;
  939. { TPpuSrcFile }
  940. procedure TPpuSrcFile.WriteDef(Output: TPpuOutput);
  941. begin
  942. inherited WriteDef(Output);
  943. Output.WriteStr('Time', FormatDateTime('yyyy"-"mm"-"dd hh":"nn":"ss', FileTime));
  944. end;
  945. constructor TPpuSrcFile.Create(AParent: TPpuContainerDef);
  946. begin
  947. inherited Create(AParent);
  948. DefType:=dtFile;
  949. end;
  950. { TPpuOutput }
  951. procedure TPpuOutput.SetIndent(AValue: integer);
  952. begin
  953. if FIndent=AValue then Exit;
  954. FIndent:=AValue;
  955. if FIndent < 0 then
  956. FIndent:=0;
  957. SetLength(FIndStr, FIndent*IndentSize);
  958. if FIndent > 0 then
  959. FillChar(FIndStr[1], FIndent*IndentSize, ' ');
  960. end;
  961. procedure TPpuOutput.SetIndentSize(AValue: integer);
  962. begin
  963. if FIndentSize=AValue then Exit;
  964. FIndentSize:=AValue;
  965. end;
  966. procedure TPpuOutput.WriteStr(const AName, AValue: string);
  967. begin
  968. end;
  969. procedure TPpuOutput.WriteInt(const AName: string; AValue: Int64; Signed: boolean);
  970. begin
  971. if Signed then
  972. WriteStr(AName, IntToStr(AValue))
  973. else
  974. WriteStr(AName, IntToStr(QWord(AValue)));
  975. end;
  976. procedure TPpuOutput.WriteFloat(const AName: string; AValue: extended);
  977. var
  978. s: string;
  979. begin
  980. Str(AValue, s);
  981. WriteStr(AName, s);
  982. end;
  983. procedure TPpuOutput.WriteBool(const AName: string; AValue: boolean);
  984. begin
  985. if AValue then
  986. WriteStr(AName, '1')
  987. else
  988. WriteStr(AName, '0');
  989. end;
  990. procedure TPpuOutput.WriteNull(const AName: string);
  991. begin
  992. WriteStr(AName, '');
  993. end;
  994. procedure TPpuOutput.WriteArrayStart(const AName: string);
  995. begin
  996. IncI;
  997. end;
  998. procedure TPpuOutput.WriteArrayEnd(const AName: string);
  999. begin
  1000. DecI;
  1001. end;
  1002. procedure TPpuOutput.WriteObjectStart(const AName: string; Def: TPpuDef);
  1003. begin
  1004. IncI;
  1005. if Def = nil then
  1006. exit;
  1007. if Def.DefType <> dtNone then
  1008. WriteStr('Type', Def.DefTypeName);
  1009. if Def.Name <> '' then
  1010. WriteStr('Name', Def.Name);
  1011. end;
  1012. procedure TPpuOutput.WriteObjectEnd(const AName: string; Def: TPpuDef);
  1013. begin
  1014. DecI;
  1015. end;
  1016. constructor TPpuOutput.Create(OutFileHandle: THandle);
  1017. begin
  1018. FOutFileHandle:=OutFileHandle;
  1019. FIndentSize:=2;
  1020. end;
  1021. destructor TPpuOutput.Destroy;
  1022. begin
  1023. Flush;
  1024. inherited Destroy;
  1025. end;
  1026. procedure TPpuOutput.Flush;
  1027. var
  1028. i, len: integer;
  1029. begin
  1030. i:=0;
  1031. while FOutBufPos > 0 do begin
  1032. len:=FileWrite(FOutFileHandle, FOutBuf[i], FOutBufPos);
  1033. if len < 0 then
  1034. raise Exception.CreateFmt('Error writing to file: %s', [ {$if declared(GetLastOSError) } SysErrorMessage(GetLastOSError) {$else} 'I/O error' {$endif} ]);
  1035. Inc(i, len);
  1036. Dec(FOutBufPos, len);
  1037. end;
  1038. end;
  1039. procedure TPpuOutput.Write(const s: string);
  1040. var
  1041. ss: string;
  1042. i, len, len2: integer;
  1043. begin
  1044. if not FNoIndent then
  1045. ss:=FIndStr + s
  1046. else
  1047. ss:=s;
  1048. i:=1;
  1049. len:=Length(ss);
  1050. while len > 0 do begin
  1051. len2:=Length(FOutBuf) - FOutBufPos;
  1052. if len2 > 0 then begin
  1053. if len < len2 then
  1054. len2:=len;
  1055. Move(ss[i], FOutBuf[FOutBufPos], len2);
  1056. Inc(FOutBufPos, len2);
  1057. end;
  1058. if FOutBufPos = Length(FOutBuf) then
  1059. Flush;
  1060. Inc(i, len2);
  1061. Dec(len, len2);
  1062. end;
  1063. FNoIndent:=True;
  1064. end;
  1065. procedure TPpuOutput.WriteLn(const s: string);
  1066. begin
  1067. Self.Write(s + LineEnding);
  1068. FNoIndent:=False;
  1069. end;
  1070. procedure TPpuOutput.IncI;
  1071. begin
  1072. Indent:=Indent + 1;
  1073. end;
  1074. procedure TPpuOutput.DecI;
  1075. begin
  1076. Indent:=Indent - 1;
  1077. end;
  1078. procedure TPpuOutput.Init;
  1079. begin
  1080. end;
  1081. procedure TPpuOutput.Done;
  1082. begin
  1083. Flush;
  1084. end;
  1085. { TPpuUnitDef }
  1086. procedure TPpuUnitDef.WriteDef(Output: TPpuOutput);
  1087. var
  1088. i: integer;
  1089. begin
  1090. Done;
  1091. with Output do begin
  1092. if Version <> 0 then
  1093. WriteInt('Version', Version);
  1094. if TargetCPU <> '' then
  1095. WriteStr('TargetCPU', TargetCPU);
  1096. if TargetOS <> '' then
  1097. WriteStr('TargetOS', TargetOS);
  1098. if Crc <> 0 then
  1099. WriteStr('CRC', hexStr(Crc, 8));
  1100. if IntfCrc <> 0 then
  1101. WriteStr('InterfaceCRC', hexStr(IntfCrc, 8));
  1102. UsedUnits.WriteDef(Output);
  1103. if Length(RefUnits) > 0 then begin
  1104. WriteArrayStart('Units');
  1105. for i:=0 to High(RefUnits) do
  1106. WriteStr('', RefUnits[i]);
  1107. WriteArrayEnd('Units');
  1108. end;
  1109. SourceFiles.WriteDef(Output);
  1110. end;
  1111. inherited WriteDef(Output);
  1112. end;
  1113. constructor TPpuUnitDef.Create(AParent: TPpuContainerDef);
  1114. begin
  1115. inherited Create(AParent);
  1116. DefType:=dtUnit;
  1117. ItemsName:='Interface';
  1118. UsedUnits:=TPpuContainerDef.Create(nil);
  1119. UsedUnits.FParent:=Self;
  1120. UsedUnits.ItemsName:='Uses';
  1121. SourceFiles:=TPpuContainerDef.Create(nil);
  1122. SourceFiles.FParent:=Self;
  1123. SourceFiles.ItemsName:='Files';
  1124. FIndexById:=THashSet.Create(64, True, False);
  1125. end;
  1126. destructor TPpuUnitDef.Destroy;
  1127. begin
  1128. UsedUnits.Free;
  1129. SourceFiles.Free;
  1130. FIndexById.Free;
  1131. inherited Destroy;
  1132. end;
  1133. function TPpuUnitDef.FindById(AId: integer; FindSym: boolean): TPpuDef;
  1134. var
  1135. h: PHashSetItem;
  1136. i: cardinal;
  1137. begin
  1138. Result:=nil;
  1139. if AId = -1 then
  1140. exit;
  1141. i:=AId;
  1142. if FindSym then
  1143. i:=i or SymIdBit;
  1144. h:=FIndexById.Find(@i, SizeOf(i));
  1145. if h <> nil then
  1146. Result:=TPpuDef(h^.Data)
  1147. else
  1148. Result:=nil;
  1149. end;
  1150. { TPpuContainerDef }
  1151. function TPpuContainerDef.GetCount: integer;
  1152. begin
  1153. Result:=FItems.Count;
  1154. end;
  1155. function TPpuContainerDef.GetItem(Index: Integer): TPpuDef;
  1156. begin
  1157. Result:=TPpuDef(FItems[Index]);
  1158. end;
  1159. procedure TPpuContainerDef.SetItem(Index: Integer; AValue: TPpuDef);
  1160. begin
  1161. FItems[Index]:=AValue;
  1162. end;
  1163. procedure TPpuContainerDef.WriteDef(Output: TPpuOutput);
  1164. var
  1165. i: integer;
  1166. begin
  1167. inherited WriteDef(Output);
  1168. BeforeWriteItems(Output);
  1169. if Count = 0 then
  1170. exit;
  1171. Output.WriteArrayStart(ItemsName);
  1172. for i:=0 to Count - 1 do
  1173. Items[i].Write(Output);
  1174. Output.WriteArrayEnd(ItemsName);
  1175. end;
  1176. procedure TPpuContainerDef.BeforeWriteItems(Output: TPpuOutput);
  1177. begin
  1178. end;
  1179. procedure TPpuContainerDef.Done;
  1180. var
  1181. i: integer;
  1182. d: TPpuDef;
  1183. begin
  1184. i:=0;
  1185. while i < Count do begin
  1186. d:=Items[i];
  1187. d.Done;
  1188. if d.Parent = Self then
  1189. Inc(i);
  1190. end;
  1191. inherited Done;
  1192. end;
  1193. constructor TPpuContainerDef.Create(AParent: TPpuContainerDef);
  1194. begin
  1195. inherited Create(AParent);
  1196. FItems:=TList.Create;
  1197. ItemsName:='Contents';
  1198. end;
  1199. destructor TPpuContainerDef.Destroy;
  1200. var
  1201. i: integer;
  1202. begin
  1203. for i:=0 to FItems.Count - 1 do
  1204. TObject(FItems[i]).Free;
  1205. FItems.Free;
  1206. inherited Destroy;
  1207. end;
  1208. function TPpuContainerDef.Add(Def: TPpuDef): integer;
  1209. begin
  1210. Result:=FItems.Add(Def);
  1211. Def.FParent:=Self;
  1212. end;
  1213. { TPpuDef }
  1214. function TPpuDef.GetDefTypeName: string;
  1215. begin
  1216. Result:=DefTypeNames[DefType];
  1217. end;
  1218. function TPpuDef.GetId: cardinal;
  1219. begin
  1220. if FId = InvalidId then
  1221. Result:=InvalidId
  1222. else
  1223. Result:=FId and not SymIdBit;
  1224. end;
  1225. function TPpuDef.GetParentUnit: TPpuUnitDef;
  1226. var
  1227. d: TPpuContainerDef;
  1228. begin
  1229. if FParentUnit = nil then begin
  1230. d:=Parent;
  1231. while (d <> nil) and (d.DefType <> dtUnit) do
  1232. d:=d.Parent;
  1233. FParentUnit:=TPpuUnitDef(d);
  1234. end;
  1235. Result:=FParentUnit;
  1236. end;
  1237. procedure TPpuDef.SetId(AValue: cardinal);
  1238. var
  1239. h: PHashSetItem;
  1240. u: TPpuUnitDef;
  1241. begin
  1242. if FId = AValue then Exit;
  1243. u:=ParentUnit;
  1244. if (FId <> InvalidId) and (u <> nil) then begin
  1245. h:=u.FIndexById.Find(@FId, SizeOf(FId));
  1246. if h <> nil then
  1247. u.FIndexById.Remove(h);
  1248. end;
  1249. FId:=AValue;
  1250. if (FId <> InvalidId) and (u <> nil) then begin;
  1251. h:=u.FIndexById.FindOrAdd(@FId, SizeOf(FId));
  1252. h^.Data:=Self;
  1253. end;
  1254. end;
  1255. procedure TPpuDef.SetParent(AValue: TPpuContainerDef);
  1256. var
  1257. i: cardinal;
  1258. begin
  1259. if FParent=AValue then Exit;
  1260. if FParent <> nil then
  1261. raise Exception.Create('Parent can not be modified.');
  1262. AValue.Add(Self);
  1263. if FId <> InvalidId then begin
  1264. i:=FId;
  1265. FId:=InvalidId;
  1266. SetId(i);
  1267. end;
  1268. end;
  1269. procedure TPpuDef.SetSymId(AId: integer);
  1270. begin
  1271. Id:=cardinal(AId) or SymIdBit;
  1272. end;
  1273. procedure TPpuDef.Done;
  1274. var
  1275. symdef: TPpuDef;
  1276. begin
  1277. if IsSymId(FId) then
  1278. exit;
  1279. if not Ref.IsNull and Ref.IsCurUnit and (Name = '') then begin
  1280. // If there is no definition name, but there is a symbol ref -
  1281. // get the name from the symbol and move the def to the symbol container
  1282. symdef:=ParentUnit.FindById(Ref.Id, True);
  1283. if symdef <> nil then begin
  1284. Name:=symdef.Name;
  1285. Visibility:=symdef.Visibility;
  1286. Parent.FItems.Remove(Self);
  1287. symdef.Parent.FItems.Add(Self);
  1288. // Hide the symbol, since it is not needed anymore
  1289. symdef.Visibility:=dvHidden;
  1290. end;
  1291. end;
  1292. end;
  1293. procedure TPpuDef.WriteDef(Output: TPpuOutput);
  1294. begin
  1295. with Output do begin
  1296. if FId <> InvalidId then
  1297. if IsSymId(FId) then
  1298. WriteInt('SymId', Id)
  1299. else begin
  1300. WriteInt('Id', Id);
  1301. if not Ref.IsNull then
  1302. WriteInt('SymId', Ref.Id);
  1303. end;
  1304. if FilePos.Line > 0 then begin
  1305. WriteObjectStart('Pos');
  1306. if FilePos.FileIndex > 0 then
  1307. WriteInt('File', FilePos.FileIndex);
  1308. WriteInt('Line', FilePos.Line);
  1309. WriteInt('Col', FilePos.Col);
  1310. WriteObjectEnd('Pos');
  1311. end;
  1312. if Visibility <> dvPublic then
  1313. WriteStr('Visibility', DefVisibilityNames[Visibility]);
  1314. end;
  1315. end;
  1316. constructor TPpuDef.Create(AParent: TPpuContainerDef);
  1317. begin
  1318. FId:=InvalidId;
  1319. Ref:=TPpuRef.Create;
  1320. Visibility:=dvPublic;
  1321. if AParent <> nil then
  1322. AParent.Add(Self);
  1323. end;
  1324. destructor TPpuDef.Destroy;
  1325. begin
  1326. Ref.Free;
  1327. inherited Destroy;
  1328. end;
  1329. procedure TPpuDef.Write(Output: TPpuOutput; const AttrName: string);
  1330. begin
  1331. if not CanWrite then
  1332. exit;
  1333. if Parent <> nil then
  1334. Output.WriteObjectStart(AttrName, Self);
  1335. WriteDef(Output);
  1336. if Parent <> nil then
  1337. Output.WriteObjectEnd(AttrName, Self);
  1338. end;
  1339. function TPpuDef.CanWrite: boolean;
  1340. begin
  1341. Result:=Visibility <> dvHidden;
  1342. end;
  1343. end.