ppuout.pp 33 KB

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