ppuout.pp 36 KB

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