pastree.pp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396
  1. {
  2. $Id$
  3. This file is part of the Free Component Library
  4. Pascal parse tree classes
  5. Copyright (c) 2000-2003 by
  6. Areca Systems GmbH / Sebastian Guenther, [email protected]
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. unit PasTree;
  14. interface
  15. uses Classes;
  16. resourcestring
  17. // Parse tree node type names
  18. SPasTreeElement = 'generic element';
  19. SPasTreeSection = 'unit section';
  20. SPasTreeModule = 'module';
  21. SPasTreePackage = 'package';
  22. SPasTreeResString = 'resource string';
  23. SPasTreeType = 'generic type';
  24. SPasTreePointerType = 'pointer type';
  25. SPasTreeAliasType = 'alias type';
  26. SPasTreeTypeAliasType = '"type" alias type';
  27. SPasTreeClassOfType = '"class of" type';
  28. SPasTreeRangeType = 'range type';
  29. SPasTreeArrayType = 'array type';
  30. SPasTreeEnumValue = 'enumeration value';
  31. SPasTreeEnumType = 'enumeration type';
  32. SPasTreeSetType = 'set type';
  33. SPasTreeRecordType = 'record type';
  34. SPasTreeObjectType = 'object';
  35. SPasTreeClassType = 'class';
  36. SPasTreeInterfaceType = 'interface';
  37. SPasTreeArgument = 'argument';
  38. SPasTreeProcedureType = 'procedure type';
  39. SPasTreeResultElement = 'function result';
  40. SPasTreeFunctionType = 'function type';
  41. SPasTreeUnresolvedTypeRef = 'unresolved type reference';
  42. SPasTreeVariable = 'variable';
  43. SPasTreeConst = 'constant';
  44. SPasTreeProperty = 'property';
  45. SPasTreeOverloadedProcedure = 'overloaded procedure';
  46. SPasTreeProcedure = 'procedure';
  47. SPasTreeFunction = 'function';
  48. SPasTreeConstructor = 'constructor';
  49. SPasTreeDestructor = 'destructor';
  50. SPasTreeProcedureImpl = 'procedure/function implementation';
  51. SPasTreeConstructorImpl = 'constructor implementation';
  52. SPasTreeDestructorImpl = 'destructor implementation';
  53. type
  54. TPasModule = class;
  55. TPasMemberVisibility = (visDefault, visPrivate, visProtected, visPublic,
  56. visPublished, visAutomated);
  57. TPasMemberVisibilities = set of TPasMemberVisibility;
  58. TPTreeElement = class of TPasElement;
  59. TPasElement = class
  60. private
  61. FRefCount: LongWord;
  62. FName: String;
  63. FParent: TPasElement;
  64. public
  65. constructor Create(const AName: String; AParent: TPasElement); virtual;
  66. procedure AddRef;
  67. procedure Release;
  68. function FullName: String; // Name including parent's names
  69. function PathName: String; // = Module.Name + FullName
  70. function GetModule: TPasModule;
  71. function ElementTypeName: String; virtual;
  72. function GetDeclaration(full : Boolean) : String; virtual;
  73. Visibility: TPasMemberVisibility;
  74. property RefCount: LongWord read FRefCount;
  75. property Name: String read FName;
  76. property Parent: TPasElement read FParent;
  77. end;
  78. TPasSection = class(TPasElement)
  79. public
  80. constructor Create(const AName: String; AParent: TPasElement); override;
  81. destructor Destroy; override;
  82. function ElementTypeName: String; override;
  83. procedure AddUnitToUsesList(const AUnitName: String);
  84. UsesList: TList; // TPasUnresolvedTypeRef or TPasModule elements
  85. Declarations, ResStrings, Types, Consts, Classes,
  86. Functions, Variables: TList;
  87. end;
  88. TPasModule = class(TPasElement)
  89. public
  90. destructor Destroy; override;
  91. function ElementTypeName: String; override;
  92. function GetDeclaration(full : boolean) : String; override;
  93. InterfaceSection, ImplementationSection: TPasSection;
  94. PackageName: String;
  95. end;
  96. TPasPackage = class(TPasElement)
  97. public
  98. constructor Create(const AName: String; AParent: TPasElement); override;
  99. destructor Destroy; override;
  100. function ElementTypeName: String; override;
  101. Modules: TList; // List of TPasModule objects
  102. end;
  103. TPasResString = class(TPasElement)
  104. public
  105. function ElementTypeName: String; override;
  106. function GetDeclaration(full : Boolean) : String; Override;
  107. Value: String;
  108. end;
  109. TPasType = class(TPasElement)
  110. public
  111. function ElementTypeName: String; override;
  112. end;
  113. TPasPointerType = class(TPasType)
  114. public
  115. destructor Destroy; override;
  116. function ElementTypeName: String; override;
  117. function GetDeclaration(full : Boolean): String; override;
  118. DestType: TPasType;
  119. end;
  120. TPasAliasType = class(TPasType)
  121. public
  122. destructor Destroy; override;
  123. function ElementTypeName: String; override;
  124. function GetDeclaration(full : Boolean): String; override;
  125. DestType: TPasType;
  126. end;
  127. TPasTypeAliasType = class(TPasAliasType)
  128. public
  129. function ElementTypeName: String; override;
  130. end;
  131. TPasClassOfType = class(TPasAliasType)
  132. public
  133. function ElementTypeName: String; override;
  134. function GetDeclaration(full: boolean) : String; override;
  135. end;
  136. TPasRangeType = class(TPasType)
  137. public
  138. function ElementTypeName: String; override;
  139. function GetDeclaration(full : boolean) : String; override;
  140. RangeStart, RangeEnd: String;
  141. end;
  142. TPasArrayType = class(TPasType)
  143. public
  144. destructor Destroy; override;
  145. function ElementTypeName: String; override;
  146. function GetDeclaration(full : boolean) : String; override;
  147. IndexRange : String;
  148. ElType: TPasType;
  149. end;
  150. TPasEnumValue = class(TPasElement)
  151. public
  152. function ElementTypeName: String; override;
  153. IsValueUsed: Boolean;
  154. Value: Integer;
  155. end;
  156. TPasEnumType = class(TPasType)
  157. public
  158. constructor Create(const AName: String; AParent: TPasElement); override;
  159. destructor Destroy; override;
  160. function ElementTypeName: String; override;
  161. function GetDeclaration(full : boolean) : String; override;
  162. Procedure GetEnumNames(Names : TStrings);
  163. Values: TList; // List of TPasEnumValue objects
  164. end;
  165. TPasSetType = class(TPasType)
  166. public
  167. destructor Destroy; override;
  168. function ElementTypeName: String; override;
  169. function GetDeclaration(full : boolean) : String; override;
  170. EnumType: TPasType;
  171. end;
  172. TPasRecordType = class(TPasType)
  173. public
  174. constructor Create(const AName: String; AParent: TPasElement); override;
  175. destructor Destroy; override;
  176. function ElementTypeName: String; override;
  177. function GetDeclaration(full : boolean) : String; override;
  178. IsPacked: Boolean;
  179. Members: TList; // array of TPasVariable elements
  180. end;
  181. TPasObjKind = (okObject, okClass, okInterface);
  182. TPasClassType = class(TPasType)
  183. public
  184. constructor Create(const AName: String; AParent: TPasElement); override;
  185. destructor Destroy; override;
  186. function ElementTypeName: String; override;
  187. ObjKind: TPasObjKind;
  188. AncestorType: TPasType; // TPasClassType or TPasUnresolvedTypeRef
  189. Members: TList; // array of TPasElement objects
  190. end;
  191. TArgumentAccess = (argDefault, argConst, argVar, argOut);
  192. TPasArgument = class(TPasElement)
  193. public
  194. destructor Destroy; override;
  195. function ElementTypeName: String; override;
  196. function GetDeclaration(full : boolean) : String; override;
  197. Access: TArgumentAccess;
  198. ArgType: TPasType;
  199. Value: String;
  200. end;
  201. TPasProcedureType = class(TPasType)
  202. public
  203. constructor Create(const AName: String; AParent: TPasElement); override;
  204. destructor Destroy; override;
  205. class function TypeName: String; virtual;
  206. function ElementTypeName: String; override;
  207. IsOfObject: Boolean;
  208. function GetDeclaration(full : boolean) : String; override;
  209. procedure GetArguments(List : TStrings);
  210. function CreateArgument(const AName, AUnresolvedTypeName: String):
  211. TPasArgument;
  212. Args: TList; // List of TPasArgument objects
  213. end;
  214. TPasResultElement = class(TPasElement)
  215. public
  216. destructor Destroy; override;
  217. function ElementTypeName : String; override;
  218. ResultType: TPasType;
  219. end;
  220. TPasFunctionType = class(TPasProcedureType)
  221. public
  222. destructor Destroy; override;
  223. class function TypeName: String; override;
  224. function ElementTypeName: String; override;
  225. function GetDeclaration(Full : boolean) : String; override;
  226. ResultEl: TPasResultElement;
  227. end;
  228. TPasUnresolvedTypeRef = class(TPasType)
  229. public
  230. // Typerefs cannot be parented! -> AParent _must_ be NIL
  231. constructor Create(const AName: String; AParent: TPasElement); override;
  232. function ElementTypeName: String; override;
  233. end;
  234. TPasTypeRef = class(TPasUnresolvedTypeRef)
  235. public
  236. // function GetDeclaration(full : Boolean): String; override;
  237. RefType: TPasType;
  238. end;
  239. TPasVariable = class(TPasElement)
  240. public
  241. destructor Destroy; override;
  242. function ElementTypeName: String; override;
  243. function GetDeclaration(full : boolean) : String; override;
  244. VarType: TPasType;
  245. Value: String;
  246. Modifiers : string;
  247. end;
  248. TPasConst = class(TPasVariable)
  249. public
  250. function ElementTypeName: String; override;
  251. end;
  252. TPasProperty = class(TPasVariable)
  253. public
  254. constructor Create(const AName: String; AParent: TPasElement); override;
  255. destructor Destroy; override;
  256. function ElementTypeName: String; override;
  257. function GetDeclaration(full : boolean) : String; override;
  258. Args: TList; // List of TPasArgument objects
  259. IndexValue, ReadAccessorName, WriteAccessorName,
  260. StoredAccessorName, DefaultValue: String;
  261. IsDefault, IsNodefault: Boolean;
  262. end;
  263. TPasProcedureBase = class(TPasElement)
  264. public
  265. function TypeName: String; virtual; abstract;
  266. end;
  267. TPasOverloadedProc = class(TPasProcedureBase)
  268. public
  269. constructor Create(const AName: String; AParent: TPasElement); override;
  270. destructor Destroy; override;
  271. function ElementTypeName: String; override;
  272. function TypeName: String; override;
  273. Overloads: TList; // List of TPasProcedure nodes
  274. end;
  275. TPasProcedure = class(TPasProcedureBase)
  276. public
  277. destructor Destroy; override;
  278. function ElementTypeName: String; override;
  279. function TypeName: String; override;
  280. ProcType: TPasProcedureType;
  281. function GetDeclaration(full: Boolean): String; override;
  282. procedure GetModifiers(List: TStrings);
  283. IsVirtual, IsDynamic, IsAbstract, IsOverride,
  284. IsOverload, IsMessage: Boolean;
  285. end;
  286. TPasFunction = class(TPasProcedure)
  287. public
  288. function ElementTypeName: String; override;
  289. function GetDeclaration (full : boolean) : String; override;
  290. end;
  291. TPasConstructor = class(TPasProcedure)
  292. public
  293. function ElementTypeName: String; override;
  294. function TypeName: String; override;
  295. end;
  296. TPasDestructor = class(TPasProcedure)
  297. public
  298. function ElementTypeName: String; override;
  299. function TypeName: String; override;
  300. end;
  301. TPasImplBlock = class;
  302. TPasProcedureImpl = class(TPasElement)
  303. public
  304. constructor Create(const AName: String; AParent: TPasElement); override;
  305. destructor Destroy; override;
  306. function ElementTypeName: String; override;
  307. function TypeName: String; virtual;
  308. ProcType: TPasProcedureType;
  309. Locals: TList;
  310. Body: TPasImplBlock;
  311. end;
  312. TPasConstructorImpl = class(TPasProcedureImpl)
  313. public
  314. function ElementTypeName: String; override;
  315. function TypeName: String; override;
  316. end;
  317. TPasDestructorImpl = class(TPasProcedureImpl)
  318. public
  319. function ElementTypeName: String; override;
  320. function TypeName: String; override;
  321. end;
  322. TPasImplElement = class(TPasElement)
  323. end;
  324. TPasImplCommand = class(TPasImplElement)
  325. public
  326. Command: String;
  327. end;
  328. TPasImplCommands = class(TPasImplElement)
  329. public
  330. constructor Create(const AName: String; AParent: TPasElement); override;
  331. destructor Destroy; override;
  332. Commands: TStrings;
  333. end;
  334. TPasImplIfElse = class(TPasImplElement)
  335. public
  336. destructor Destroy; override;
  337. Condition: String;
  338. IfBranch, ElseBranch: TPasImplElement;
  339. end;
  340. TPasImplForLoop = class(TPasImplElement)
  341. public
  342. destructor Destroy; override;
  343. Variable: TPasVariable;
  344. StartValue, EndValue: String;
  345. Body: TPasImplElement;
  346. end;
  347. TPasImplBlock = class(TPasImplElement)
  348. public
  349. constructor Create(const AName: String; AParent: TPasElement); override;
  350. destructor Destroy; override;
  351. function AddCommand(const ACommand: String): TPasImplCommand;
  352. function AddCommands: TPasImplCommands;
  353. function AddIfElse(const ACondition: String): TPasImplIfElse;
  354. function AddForLoop(AVar: TPasVariable;
  355. const AStartValue, AEndValue: String): TPasImplForLoop;
  356. Elements: TList; // TPasImplElement objects
  357. end;
  358. const
  359. AccessNames: array[TArgumentAccess] of String[6] = ('', 'const ', 'var ', 'out ');
  360. AllVisibilities: TPasMemberVisibilities =
  361. [visDefault, visPrivate, visProtected, visPublic,
  362. visPublished, visAutomated];
  363. VisibilityNames: array[TPasMemberVisibility] of String = (
  364. 'default', 'private', 'protected', 'public', 'published', 'automated');
  365. ObjKindNames: array[TPasObjKind] of String = (
  366. 'object', 'class', 'interface');
  367. implementation
  368. uses SysUtils;
  369. { Parse tree element type name functions }
  370. function TPasElement.ElementTypeName: String; begin Result := SPasTreeElement end;
  371. function TPasSection.ElementTypeName: String; begin Result := SPasTreeSection end;
  372. function TPasModule.ElementTypeName: String; begin Result := SPasTreeModule end;
  373. function TPasPackage.ElementTypeName: String; begin Result := SPasTreePackage end;
  374. function TPasResString.ElementTypeName: String; begin Result := SPasTreeResString end;
  375. function TPasType.ElementTypeName: String; begin Result := SPasTreeType end;
  376. function TPasPointerType.ElementTypeName: String; begin Result := SPasTreePointerType end;
  377. function TPasAliasType.ElementTypeName: String; begin Result := SPasTreeAliasType end;
  378. function TPasTypeAliasType.ElementTypeName: String; begin Result := SPasTreeTypeAliasType end;
  379. function TPasClassOfType.ElementTypeName: String; begin Result := SPasTreeClassOfType end;
  380. function TPasRangeType.ElementTypeName: String; begin Result := SPasTreeRangeType end;
  381. function TPasArrayType.ElementTypeName: String; begin Result := SPasTreeArrayType end;
  382. function TPasEnumValue.ElementTypeName: String; begin Result := SPasTreeEnumValue end;
  383. function TPasEnumType.ElementTypeName: String; begin Result := SPasTreeEnumType end;
  384. function TPasSetType.ElementTypeName: String; begin Result := SPasTreeSetType end;
  385. function TPasRecordType.ElementTypeName: String; begin Result := SPasTreeRecordType end;
  386. function TPasArgument.ElementTypeName: String; begin Result := SPasTreeArgument end;
  387. function TPasProcedureType.ElementTypeName: String; begin Result := SPasTreeProcedureType end;
  388. function TPasResultElement.ElementTypeName: String; begin Result := SPasTreeResultElement end;
  389. function TPasFunctionType.ElementTypeName: String; begin Result := SPasTreeFunctionType end;
  390. function TPasUnresolvedTypeRef.ElementTypeName: String; begin Result := SPasTreeUnresolvedTypeRef end;
  391. function TPasVariable.ElementTypeName: String; begin Result := SPasTreeVariable end;
  392. function TPasConst.ElementTypeName: String; begin Result := SPasTreeConst end;
  393. function TPasProperty.ElementTypeName: String; begin Result := SPasTreeProperty end;
  394. function TPasOverloadedProc.ElementTypeName: String; begin Result := SPasTreeOverloadedProcedure end;
  395. function TPasProcedure.ElementTypeName: String; begin Result := SPasTreeProcedure end;
  396. function TPasFunction.ElementTypeName: String; begin Result := SPasTreeFunction end;
  397. function TPasConstructor.ElementTypeName: String; begin Result := SPasTreeConstructor end;
  398. function TPasDestructor.ElementTypeName: String; begin Result := SPasTreeDestructor end;
  399. function TPasProcedureImpl.ElementTypeName: String; begin Result := SPasTreeProcedureImpl end;
  400. function TPasConstructorImpl.ElementTypeName: String; begin Result := SPasTreeConstructorImpl end;
  401. function TPasDestructorImpl.ElementTypeName: String; begin Result := SPasTreeDestructorImpl end;
  402. function TPasClassType.ElementTypeName: String;
  403. begin
  404. case ObjKind of
  405. okObject: Result := SPasTreeObjectType;
  406. okClass: Result := SPasTreeClassType;
  407. okInterface: Result := SPasTreeInterfaceType;
  408. end;
  409. end;
  410. { All other stuff: }
  411. constructor TPasElement.Create(const AName: String; AParent: TPasElement);
  412. begin
  413. inherited Create;
  414. FName := AName;
  415. FParent := AParent;
  416. end;
  417. procedure TPasElement.AddRef;
  418. begin
  419. Inc(FRefCount);
  420. end;
  421. procedure TPasElement.Release;
  422. begin
  423. if FRefCount = 0 then
  424. Free
  425. else
  426. Dec(FRefCount);
  427. end;
  428. function TPasElement.FullName: String;
  429. var
  430. p: TPasElement;
  431. begin
  432. Result := Name;
  433. p := Parent;
  434. while Assigned(p) and not p.InheritsFrom(TPasSection) do
  435. begin
  436. if (p.ClassType <> TPasOverloadedProc) and (Length(p.Name) > 0) then
  437. if Length(Result) > 0 then
  438. Result := p.Name + '.' + Result
  439. else
  440. Result := p.Name;
  441. p := p.Parent;
  442. end;
  443. end;
  444. function TPasElement.PathName: String;
  445. var
  446. p: TPasElement;
  447. begin
  448. Result := Name;
  449. p := Parent;
  450. while Assigned(p) do
  451. begin
  452. if (p.ClassType <> TPasOverloadedProc) and (Length(p.Name) > 0) then
  453. if Length(Result) > 0 then
  454. Result := p.Name + '.' + Result
  455. else
  456. Result := p.Name;
  457. p := p.Parent;
  458. end;
  459. end;
  460. function TPasElement.GetModule: TPasModule;
  461. begin
  462. if ClassType = TPasPackage then
  463. Result := nil
  464. else
  465. begin
  466. Result := TPasModule(Self);
  467. while Assigned(Result) and not (Result.ClassType = TPasModule) do
  468. Result := TPasModule(Result.Parent);
  469. end;
  470. end;
  471. function TPasElement.GetDeclaration (full : boolean): String;
  472. begin
  473. if Full then
  474. Result := Name
  475. else
  476. Result := '';
  477. end;
  478. constructor TPasSection.Create(const AName: String; AParent: TPasElement);
  479. begin
  480. inherited Create(AName, AParent);
  481. UsesList := TList.Create;
  482. Declarations := TList.Create;
  483. ResStrings := TList.Create;
  484. Types := TList.Create;
  485. Consts := TList.Create;
  486. Classes := TList.Create;
  487. Functions := TList.Create;
  488. Variables := TList.Create;
  489. end;
  490. destructor TPasSection.Destroy;
  491. var
  492. i: Integer;
  493. begin
  494. Variables.Free;
  495. Functions.Free;
  496. Classes.Free;
  497. Consts.Free;
  498. Types.Free;
  499. ResStrings.Free;
  500. for i := 0 to Declarations.Count - 1 do
  501. TPasElement(Declarations[i]).Release;
  502. Declarations.Free;
  503. for i := 0 to UsesList.Count - 1 do
  504. TPasType(UsesList[i]).Release;
  505. UsesList.Free;
  506. inherited Destroy;
  507. end;
  508. procedure TPasSection.AddUnitToUsesList(const AUnitName: String);
  509. begin
  510. UsesList.Add(TPasUnresolvedTypeRef.Create(AUnitName, Self));
  511. end;
  512. destructor TPasModule.Destroy;
  513. begin
  514. if Assigned(InterfaceSection) then
  515. InterfaceSection.Release;
  516. if Assigned(ImplementationSection) then
  517. ImplementationSection.Release;
  518. inherited Destroy;
  519. end;
  520. constructor TPasPackage.Create(const AName: String; AParent: TPasElement);
  521. begin
  522. if (Length(AName) > 0) and (AName[1] <> '#') then
  523. inherited Create('#' + AName, AParent)
  524. else
  525. inherited Create(AName, AParent);
  526. Modules := TList.Create;
  527. end;
  528. destructor TPasPackage.Destroy;
  529. var
  530. i: Integer;
  531. begin
  532. for i := 0 to Modules.Count - 1 do
  533. TPasModule(Modules[i]).Release;
  534. Modules.Free;
  535. inherited Destroy;
  536. end;
  537. destructor TPasPointerType.Destroy;
  538. begin
  539. if Assigned(DestType) then
  540. DestType.Release;
  541. inherited Destroy;
  542. end;
  543. destructor TPasAliasType.Destroy;
  544. begin
  545. if Assigned(DestType) then
  546. DestType.Release;
  547. inherited Destroy;
  548. end;
  549. destructor TPasArrayType.Destroy;
  550. begin
  551. if Assigned(ElType) then
  552. ElType.Release;
  553. inherited Destroy;
  554. end;
  555. constructor TPasEnumType.Create(const AName: String; AParent: TPasElement);
  556. begin
  557. inherited Create(AName, AParent);
  558. Values := TList.Create;
  559. end;
  560. destructor TPasEnumType.Destroy;
  561. var
  562. i: Integer;
  563. begin
  564. for i := 0 to Values.Count - 1 do
  565. TPasEnumValue(Values[i]).Release;
  566. Values.Free;
  567. inherited Destroy;
  568. end;
  569. procedure TPasEnumType.GetEnumNames(Names: TStrings);
  570. var
  571. i: Integer;
  572. begin
  573. with Values do
  574. begin
  575. for i := 0 to Count - 2 do
  576. Names.Add(TPasEnumValue(Items[i]).Name + ',');
  577. if Count > 0 then
  578. Names.Add(TPasEnumValue(Items[Count - 1]).Name);
  579. end;
  580. end;
  581. destructor TPasSetType.Destroy;
  582. begin
  583. if Assigned(EnumType) then
  584. EnumType.Release;
  585. inherited Destroy;
  586. end;
  587. constructor TPasRecordType.Create(const AName: String; AParent: TPasElement);
  588. begin
  589. inherited Create(AName, AParent);
  590. Members := TList.Create;
  591. end;
  592. destructor TPasRecordType.Destroy;
  593. var
  594. i: Integer;
  595. begin
  596. for i := 0 to Members.Count - 1 do
  597. TPasVariable(Members[i]).Release;
  598. Members.Free;
  599. inherited Destroy;
  600. end;
  601. constructor TPasClassType.Create(const AName: String; AParent: TPasElement);
  602. begin
  603. inherited Create(AName, AParent);
  604. Members := TList.Create;
  605. end;
  606. destructor TPasClassType.Destroy;
  607. var
  608. i: Integer;
  609. begin
  610. for i := 0 to Members.Count - 1 do
  611. TPasElement(Members[i]).Release;
  612. Members.Free;
  613. if Assigned(AncestorType) then
  614. AncestorType.Release;
  615. inherited Destroy;
  616. end;
  617. destructor TPasArgument.Destroy;
  618. begin
  619. if Assigned(ArgType) then
  620. ArgType.Release;
  621. inherited Destroy;
  622. end;
  623. constructor TPasProcedureType.Create(const AName: String; AParent: TPasElement);
  624. begin
  625. inherited Create(AName, AParent);
  626. Args := TList.Create;
  627. end;
  628. destructor TPasProcedureType.Destroy;
  629. var
  630. i: Integer;
  631. begin
  632. for i := 0 to Args.Count - 1 do
  633. TPasArgument(Args[i]).Release;
  634. Args.Free;
  635. inherited Destroy;
  636. end;
  637. function TPasProcedureType.TypeName: String;
  638. begin
  639. Result := 'procedure';
  640. end;
  641. function TPasProcedureType.CreateArgument(const AName,
  642. AUnresolvedTypeName: String): TPasArgument;
  643. begin
  644. Result := TPasArgument.Create(AName, Self);
  645. Args.Add(Result);
  646. Result.ArgType := TPasUnresolvedTypeRef.Create(AUnresolvedTypeName, Result);
  647. end;
  648. destructor TPasResultElement.Destroy;
  649. begin
  650. if Assigned(ResultType) then
  651. ResultType.Release;
  652. inherited Destroy;
  653. end;
  654. destructor TPasFunctionType.Destroy;
  655. begin
  656. if Assigned(ResultEl) then
  657. ResultEl.Release;
  658. inherited Destroy;
  659. end;
  660. function TPasFunctionType.TypeName: String;
  661. begin
  662. Result := 'function';
  663. end;
  664. constructor TPasUnresolvedTypeRef.Create(const AName: String; AParent: TPasElement);
  665. begin
  666. inherited Create(AName, nil);
  667. end;
  668. destructor TPasVariable.Destroy;
  669. begin
  670. { Attention, in derived classes, VarType isn't necessarily set!
  671. (e.g. in Constants) }
  672. if Assigned(VarType) then
  673. VarType.Release;
  674. inherited Destroy;
  675. end;
  676. constructor TPasProperty.Create(const AName: String; AParent: TPasElement);
  677. begin
  678. inherited Create(AName, AParent);
  679. Args := TList.Create;
  680. end;
  681. destructor TPasProperty.Destroy;
  682. var
  683. i: Integer;
  684. begin
  685. for i := 0 to Args.Count - 1 do
  686. TPasArgument(Args[i]).Release;
  687. Args.Free;
  688. inherited Destroy;
  689. end;
  690. constructor TPasOverloadedProc.Create(const AName: String; AParent: TPasElement);
  691. begin
  692. inherited Create(AName, AParent);
  693. Overloads := TList.Create;
  694. end;
  695. destructor TPasOverloadedProc.Destroy;
  696. var
  697. i: Integer;
  698. begin
  699. for i := 0 to Overloads.Count - 1 do
  700. TPasProcedure(Overloads[i]).Release;
  701. Overloads.Free;
  702. inherited Destroy;
  703. end;
  704. function TPasOverloadedProc.TypeName: String;
  705. begin
  706. if Assigned(TPasProcedure(Overloads[0]).ProcType) then
  707. Result := TPasProcedure(Overloads[0]).ProcType.TypeName
  708. else
  709. SetLength(Result, 0);
  710. end;
  711. destructor TPasProcedure.Destroy;
  712. begin
  713. if Assigned(ProcType) then
  714. ProcType.Release;
  715. inherited Destroy;
  716. end;
  717. function TPasProcedure.TypeName: String;
  718. begin
  719. Result := ProcType.TypeName;
  720. end;
  721. function TPasConstructor.TypeName: String;
  722. begin
  723. Result := 'constructor';
  724. end;
  725. function TPasDestructor.TypeName: String;
  726. begin
  727. Result := 'destructor';
  728. end;
  729. constructor TPasProcedureImpl.Create(const AName: String; AParent: TPasElement);
  730. begin
  731. inherited Create(AName, AParent);
  732. Locals := TList.Create;
  733. end;
  734. destructor TPasProcedureImpl.Destroy;
  735. var
  736. i: Integer;
  737. begin
  738. if Assigned(Body) then
  739. Body.Release;
  740. for i := 0 to Locals.Count - 1 do
  741. TPasElement(Locals[i]).Release;
  742. Locals.Free;
  743. if Assigned(ProcType) then
  744. ProcType.Release;
  745. inherited Destroy;
  746. end;
  747. function TPasProcedureImpl.TypeName: String;
  748. begin
  749. Result := ProcType.TypeName;
  750. end;
  751. function TPasConstructorImpl.TypeName: String;
  752. begin
  753. Result := 'constructor';
  754. end;
  755. function TPasDestructorImpl.TypeName: String;
  756. begin
  757. Result := 'destructor';
  758. end;
  759. constructor TPasImplCommands.Create(const AName: String; AParent: TPasElement);
  760. begin
  761. inherited Create(AName, AParent);
  762. Commands := TStringList.Create;
  763. end;
  764. destructor TPasImplCommands.Destroy;
  765. begin
  766. Commands.Free;
  767. inherited Destroy;
  768. end;
  769. destructor TPasImplIfElse.Destroy;
  770. begin
  771. if Assigned(IfBranch) then
  772. IfBranch.Release;
  773. if Assigned(ElseBranch) then
  774. ElseBranch.Release;
  775. inherited Destroy;
  776. end;
  777. destructor TPasImplForLoop.Destroy;
  778. begin
  779. if Assigned(Variable) then
  780. Variable.Release;
  781. if Assigned(Body) then
  782. Body.Release;
  783. inherited Destroy;
  784. end;
  785. constructor TPasImplBlock.Create(const AName: String; AParent: TPasElement);
  786. begin
  787. inherited Create(AName, AParent);
  788. Elements := TList.Create;
  789. end;
  790. destructor TPasImplBlock.Destroy;
  791. var
  792. i: Integer;
  793. begin
  794. for i := 0 to Elements.Count - 1 do
  795. TPasImplElement(Elements[i]).Release;
  796. Elements.Free;
  797. inherited Destroy;
  798. end;
  799. function TPasImplBlock.AddCommand(const ACommand: String): TPasImplCommand;
  800. begin
  801. Result := TPasImplCommand.Create('', Self);
  802. Elements.Add(Result);
  803. Result.Command := ACommand;
  804. end;
  805. function TPasImplBlock.AddCommands: TPasImplCommands;
  806. begin
  807. Result := TPasImplCommands.Create('', Self);
  808. Elements.Add(Result);
  809. end;
  810. function TPasImplBlock.AddIfElse(const ACondition: String): TPasImplIfElse;
  811. begin
  812. Result := TPasImplIfElse.Create('', Self);
  813. Elements.Add(Result);
  814. Result.Condition := ACondition;
  815. end;
  816. function TPasImplBlock.AddForLoop(AVar: TPasVariable; const AStartValue,
  817. AEndValue: String): TPasImplForLoop;
  818. begin
  819. Result := TPasImplForLoop.Create('', Self);
  820. Elements.Add(Result);
  821. Result.Variable := AVar;
  822. Result.StartValue := AStartValue;
  823. Result.EndValue := AEndValue;
  824. end;
  825. { ---------------------------------------------------------------------
  826. ---------------------------------------------------------------------}
  827. function TPasModule.GetDeclaration(full : boolean): String;
  828. begin
  829. Result := 'Unit ' + Name;
  830. end;
  831. {
  832. function TPas.GetDeclaration : string;
  833. begin
  834. Result:=Name;
  835. end;
  836. }
  837. function TPasResString.GetDeclaration (full : boolean) : string;
  838. begin
  839. Result:=Value;
  840. If Full Then
  841. Result:=Name+' = '+Result;
  842. end;
  843. function TPasPointerType.GetDeclaration (full : boolean) : string;
  844. begin
  845. Result:='^'+DestType.Name;
  846. If Full then
  847. Result:=Name+' = '+Result;
  848. end;
  849. function TPasAliasType.GetDeclaration (full : boolean) : string;
  850. begin
  851. Result:=DestType.Name;
  852. If Full then
  853. Result:=Name+' = '+Result;
  854. end;
  855. function TPasClassOfType.GetDeclaration (full : boolean) : string;
  856. begin
  857. Result:='Class of '+DestType.Name;
  858. If Full then
  859. Result:=Name+' = '+Result;
  860. end;
  861. function TPasRangeType.GetDeclaration (full : boolean) : string;
  862. begin
  863. Result:=RangeStart+'..'+RangeEnd;
  864. If Full then
  865. Result:=Name+' = '+Result;
  866. end;
  867. function TPasArrayType.GetDeclaration (full : boolean) : string;
  868. begin
  869. Result:='Array['+IndexRange+'] of ';
  870. If Assigned(Eltype) then
  871. Result:=Result+ElType.Name
  872. else
  873. Result:=Result+'const';
  874. If Assigned(ELtype) then
  875. If Full Then
  876. Result:=Name+' = '+Result;
  877. end;
  878. Function IndentStrings(S : TStrings; indent : Integer) : String;
  879. Var
  880. I,CurrLen,CurrPos : Integer;
  881. begin
  882. Result:='';
  883. CurrLen:=0;
  884. CurrPos:=0;
  885. For I:=0 to S.Count-1 do
  886. begin
  887. CurrLen:=Length(S[i]);
  888. If (CurrLen+CurrPos)>72 then
  889. begin
  890. Result:=Result+LineEnding+StringOfChar(' ',Indent);
  891. CurrPos:=Indent;
  892. end;
  893. Result:=Result+S[i];
  894. CurrPos:=CurrPos+CurrLen;
  895. end;
  896. end;
  897. function TPasEnumType.GetDeclaration (full : boolean) : string;
  898. Var
  899. S : TStringList;
  900. i : integer;
  901. begin
  902. S:=TStringList.Create;
  903. Try
  904. If Full then
  905. S.Add(Name+' = (')
  906. else
  907. S.Add('(');
  908. GetEnumNames(S);
  909. S[S.Count-1]:=S[S.Count-1]+')';
  910. If Full then
  911. Result:=IndentStrings(S,Length(Name)+4)
  912. else
  913. Result:=IndentStrings(S,1);
  914. finally
  915. S.Free;
  916. end;
  917. end;
  918. function TPasSetType.GetDeclaration (full : boolean) : string;
  919. Var
  920. S : TStringList;
  921. i : Integer;
  922. begin
  923. If EnumType is TPasEnumType then
  924. begin
  925. S:=TStringList.Create;
  926. Try
  927. If Full then
  928. S.Add(Name+'= Set of (')
  929. else
  930. S.Add('Set of (');
  931. TPasEnumType(EnumType).GetEnumNames(S);
  932. S[S.Count-1]:=S[S.Count-1]+')';
  933. I:=Pos('(',S[0]);
  934. Result:=IndentStrings(S,i);
  935. finally
  936. S.Free;
  937. end;
  938. end
  939. else
  940. begin
  941. Result:='Set of '+EnumType.Name;
  942. If Full then
  943. Result:=Name+' = '+Result;
  944. end;
  945. end;
  946. function TPasRecordType.GetDeclaration (full : boolean) : string;
  947. Var
  948. S,T : TStringList;
  949. temp : String;
  950. I : integer;
  951. begin
  952. S:=TStringList.Create;
  953. T:=TstringList.Create;
  954. Try
  955. Temp:='record';
  956. If IsPacked then
  957. Temp:='packed '+Temp;
  958. If Full then
  959. Temp:=Name+' = '+Temp;
  960. S.Add(Temp);
  961. For I:=0 to Members.Count-1 do
  962. begin
  963. Temp:=TPasVariable(Members[i]).GetDeclaration(True);
  964. If Pos(LineEnding,Temp)>0 then
  965. begin
  966. T.Text:=Temp;
  967. For I:=0 to T.Count-1 do
  968. S.Add(' '+T[i]+';');
  969. end
  970. else
  971. S.Add(' '+Temp+';');
  972. end;
  973. S.Add('end');
  974. Result:=S.Text;
  975. finally
  976. S.free;
  977. T.free;
  978. end;
  979. end;
  980. procedure TPasProcedureType.GetArguments(List : TStrings);
  981. Var
  982. T : String;
  983. I : Integer;
  984. begin
  985. For I:=0 to Args.Count-1 do
  986. begin
  987. T:=AccessNames[TPasArgument(Args[i]).Access];
  988. T:=T+TPasArgument(Args[i]).GetDeclaration(True);
  989. If I=0 then
  990. T:='('+T;
  991. If I<Args.Count-1 then
  992. List.Add(T+';')
  993. else
  994. List.Add(T+')');
  995. end;
  996. end;
  997. function TPasProcedureType.GetDeclaration (full : boolean) : string;
  998. Var
  999. S : TStringList;
  1000. begin
  1001. S:=TStringList.Create;
  1002. Try
  1003. If Full then
  1004. S.Add(Format('%s = ',[Name]));
  1005. S.Add(TypeName);
  1006. GetArguments(S);
  1007. If IsOfObject then
  1008. S.Add(' of object');
  1009. If Full then
  1010. Result:=IndentStrings(S,Length(S[0])+Length(S[1])+1)
  1011. else
  1012. Result:=IndentStrings(S,Length(S[0])+1);
  1013. finally
  1014. S.Free;
  1015. end;
  1016. end;
  1017. function TPasFunctionType.GetDeclaration (full : boolean) : string;
  1018. Var
  1019. S : TStringList;
  1020. T : String;
  1021. begin
  1022. S:=TStringList.Create;
  1023. Try
  1024. If Full then
  1025. S.Add(Format('%s = ',[Name]));
  1026. S.Add(TypeName);
  1027. GetArguments(S);
  1028. If Assigned(ResultEl) then
  1029. begin
  1030. T:=' : ';
  1031. If (ResultEl.ResultType.Name<>'') then
  1032. T:=T+ResultEl.ResultType.Name
  1033. else
  1034. T:=T+ResultEl.ResultType.GetDeclaration(False);
  1035. S.Add(T);
  1036. end;
  1037. If IsOfObject then
  1038. S.Add(' of object');
  1039. If Full then
  1040. Result:=IndentStrings(S,Length(S[0])+Length(S[1])+1)
  1041. else
  1042. Result:=IndentStrings(S,Length(S[0])+1);
  1043. finally
  1044. S.Free;
  1045. end;
  1046. end;
  1047. function TPasVariable.GetDeclaration (full : boolean) : string;
  1048. Const
  1049. Seps : Array[Boolean] of String = (' = ',' : ');
  1050. begin
  1051. If Assigned(VarType) then
  1052. begin
  1053. If VarType.Name='' then
  1054. Result:=VarType.GetDeclaration(False)
  1055. else
  1056. Result:=VarType.Name;
  1057. Result:=Result+Modifiers;
  1058. end
  1059. else
  1060. Result:=Value;
  1061. If Full then
  1062. Result:=Name+Seps[Assigned(VarType)]+Result;
  1063. end;
  1064. function TPasProperty.GetDeclaration (full : boolean) : string;
  1065. Var
  1066. S : String;
  1067. I : Integer;
  1068. begin
  1069. If Assigned(VarType) then
  1070. begin
  1071. If VarType.Name='' then
  1072. Result:=VarType.GetDeclaration(False)
  1073. else
  1074. Result:=VarType.Name;
  1075. end
  1076. else
  1077. Result:=Value;
  1078. S:='';
  1079. If Assigned(Args) and (Args.Count>0) then
  1080. begin
  1081. For I:=0 to Args.Count-1 do
  1082. begin
  1083. If (S<>'') then
  1084. S:=S+';';
  1085. S:=S+TPasElement(Args[i]).GetDeclaration(true);
  1086. end;
  1087. end;
  1088. If S<>'' then
  1089. S:='['+S+']'
  1090. else
  1091. S:=' ';
  1092. If Full then
  1093. Result:=Name+S+': '+Result;
  1094. If IsDefault then
  1095. Result:=Result+'; default'
  1096. end;
  1097. Procedure TPasProcedure.GetModifiers(List : TStrings);
  1098. Procedure DoAdd(B : Boolean; S : String);
  1099. begin
  1100. if B then
  1101. List.add('; '+S);
  1102. end;
  1103. begin
  1104. Doadd(IsVirtual,' Virtual');
  1105. DoAdd(IsDynamic,' Dynamic');
  1106. DoAdd(IsOverride,' Override');
  1107. DoAdd(IsAbstract,' Abstract');
  1108. DoAdd(IsOverload,' Overload');
  1109. DoAdd(IsMessage,' Message');
  1110. end;
  1111. function TPasProcedure.GetDeclaration (full : boolean) : string;
  1112. Var
  1113. S : TStringList;
  1114. Index : integer;
  1115. begin
  1116. S:=TStringList.Create;
  1117. try
  1118. If Full then
  1119. S.Add(TypeName+' '+Name);
  1120. ProcType.GetArguments(S);
  1121. GetModifiers(S);
  1122. Result:=IndentStrings(S,Length(S[0]));
  1123. finally
  1124. S.Free;
  1125. end;
  1126. end;
  1127. function TPasFunction.GetDeclaration (full : boolean) : string;
  1128. Var
  1129. S : TStringList;
  1130. T : String;
  1131. begin
  1132. S:=TStringList.Create;
  1133. try
  1134. If Full then
  1135. S.Add(TypeName+' '+Name);
  1136. ProcType.GetArguments(S);
  1137. If Assigned((Proctype as TPasFunctionType).ResultEl) then
  1138. With TPasFunctionType(ProcType).ResultEl.ResultType do
  1139. begin
  1140. T:=' : ';
  1141. If (Name<>'') then
  1142. T:=T+Name
  1143. else
  1144. T:=T+GetDeclaration(False);
  1145. S.Add(T);
  1146. end;
  1147. GetModifiers(S);
  1148. Result:=IndentStrings(S,Length(S[0]));
  1149. finally
  1150. S.Free;
  1151. end;
  1152. end;
  1153. function TPasArgument.GetDeclaration (full : boolean) : string;
  1154. begin
  1155. If Assigned(ArgType) then
  1156. begin
  1157. If ArgType.Name<>'' then
  1158. Result:=ArgType.Name
  1159. else
  1160. Result:=ArgType.GetDeclaration(False);
  1161. If Full then
  1162. Result:=Name+': '+Result;
  1163. end
  1164. else If Full then
  1165. Result:=Name
  1166. else
  1167. Result:='';
  1168. end;
  1169. end.
  1170. {
  1171. $Log$
  1172. Revision 1.1 2003-03-13 21:47:42 sg
  1173. * First version as part of FCL
  1174. }