ppuout.pp 36 KB

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