pastree.pp 34 KB

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