ppuout.pp 34 KB

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