Browse Source

* (reworked) Patch from m. spiller to implement visitor pattern

git-svn-id: trunk@13925 -
michael 16 years ago
parent
commit
a611bfc103
1 changed files with 110 additions and 1 deletions
  1. 110 1
      packages/fcl-passrc/src/pastree.pp

+ 110 - 1
packages/fcl-passrc/src/pastree.pp

@@ -63,6 +63,13 @@ resourcestring
   SPasTreeDestructorImpl = 'destructor implementation';
 
 type
+  // Visitor pattern.
+  TPassTreeVisitor = class;
+
+  TPasElementBase = class
+    procedure Accept(Visitor: TPassTreeVisitor); virtual; abstract;
+  end;
+
 
   TPasModule = class;
 
@@ -75,7 +82,9 @@ type
 
   TPTreeElement = class of TPasElement;
 
-  TPasElement = class
+  { TPasElement }
+
+  TPasElement = class(TPasElementBase)
   private
     FRefCount: LongWord;
     FName: string;
@@ -94,12 +103,15 @@ type
     function GetModule: TPasModule;
     function ElementTypeName: string; virtual;
     function GetDeclaration(full : Boolean) : string; virtual;
+    procedure Accept(Visitor: TPassTreeVisitor); override;
     property RefCount: LongWord read FRefCount;
     property Name: string read FName write FName;
     property Parent: TPasElement read FParent;
     Property Hints : TPasMemberHints Read FHints Write FHints;
   end;
 
+  { TPasSection }
+
   TPasSection = class(TPasElement)
   public
     constructor Create(const AName: string; AParent: TPasElement); override;
@@ -112,6 +124,8 @@ type
     Functions, Variables, Properties: TList;
   end;
 
+  { TPasModule }
+
   TPasModule = class(TPasElement)
   public
     destructor Destroy; override;
@@ -122,6 +136,8 @@ type
     PackageName: string;
   end;
 
+  { TPasPackage }
+
   TPasPackage = class(TPasElement)
   public
     constructor Create(const AName: string; AParent: TPasElement); override;
@@ -131,6 +147,8 @@ type
     Modules: TList;     // List of TPasModule objects
   end;
 
+  { TPasResString }
+
   TPasResString = class(TPasElement)
   public
     function ElementTypeName: string; override;
@@ -139,11 +157,15 @@ type
     Value: string;
   end;
 
+  { TPasType }
+
   TPasType = class(TPasElement)
   public
     function ElementTypeName: string; override;
   end;
 
+  { TPasPointerType }
+
   TPasPointerType = class(TPasType)
   public
     destructor Destroy; override;
@@ -153,6 +175,8 @@ type
     DestType: TPasType;
   end;
 
+  { TPasAliasType }
+
   TPasAliasType = class(TPasType)
   public
     destructor Destroy; override;
@@ -162,11 +186,15 @@ type
     DestType: TPasType;
   end;
 
+  { TPasTypeAliasType }
+
   TPasTypeAliasType = class(TPasAliasType)
   public
     function ElementTypeName: string; override;
   end;
 
+  { TPasClassOfType }
+
   TPasClassOfType = class(TPasAliasType)
   public
     function ElementTypeName: string; override;
@@ -174,6 +202,8 @@ type
   end;
 
 
+  { TPasRangeType }
+
   TPasRangeType = class(TPasType)
   public
     function ElementTypeName: string; override;
@@ -182,6 +212,8 @@ type
     RangeStart, RangeEnd: string;
   end;
 
+  { TPasArrayType }
+
   TPasArrayType = class(TPasType)
   public
     destructor Destroy; override;
@@ -193,6 +225,8 @@ type
     ElType: TPasType;
   end;
 
+  { TPasFileType }
+
   TPasFileType = class(TPasType)
   public
     destructor Destroy; override;
@@ -202,6 +236,8 @@ type
     ElType: TPasType;
   end;
 
+  { TPasEnumValue }
+
   TPasEnumValue = class(TPasElement)
   public
     function ElementTypeName: string; override;
@@ -211,6 +247,8 @@ type
     AssignedValue : string;
   end;
 
+  { TPasEnumType }
+
   TPasEnumType = class(TPasType)
   public
     constructor Create(const AName: string; AParent: TPasElement); override;
@@ -222,6 +260,8 @@ type
     Values: TList;      // List of TPasEnumValue objects
   end;
 
+  { TPasSetType }
+
   TPasSetType = class(TPasType)
   public
     destructor Destroy; override;
@@ -233,6 +273,8 @@ type
 
   TPasRecordType = class;
 
+  { TPasVariant }
+
   TPasVariant = class(TPasElement)
   public
     constructor Create(const AName: string; AParent: TPasElement); override;
@@ -242,6 +284,8 @@ type
     Members: TPasRecordType;
   end;
 
+  { TPasRecordType }
+
   TPasRecordType = class(TPasType)
   public
     constructor Create(const AName: string; AParent: TPasElement); override;
@@ -260,6 +304,8 @@ type
 
   TPasObjKind = (okObject, okClass, okInterface);
 
+  { TPasClassType }
+
   TPasClassType = class(TPasType)
   public
     constructor Create(const AName: string; AParent: TPasElement); override;
@@ -276,6 +322,8 @@ type
 
   TArgumentAccess = (argDefault, argConst, argVar, argOut);
 
+  { TPasArgument }
+
   TPasArgument = class(TPasElement)
   public
     destructor Destroy; override;
@@ -287,6 +335,8 @@ type
     Value: string;
   end;
 
+  { TPasProcedureType }
+
   TPasProcedureType = class(TPasType)
   public
     constructor Create(const AName: string; AParent: TPasElement); override;
@@ -301,6 +351,8 @@ type
     Args: TList;        // List of TPasArgument objects
   end;
 
+  { TPasResultElement }
+
   TPasResultElement = class(TPasElement)
   public
     destructor Destroy; override;
@@ -309,6 +361,8 @@ type
     ResultType: TPasType;
   end;
 
+  { TPasFunctionType }
+
   TPasFunctionType = class(TPasProcedureType)
   public
     destructor Destroy; override;
@@ -326,12 +380,17 @@ type
     function ElementTypeName: string; override;
   end;
 
+  { TPasTypeRef }
+
   TPasTypeRef = class(TPasUnresolvedTypeRef)
+  public
   public
     // function GetDeclaration(full : Boolean): string; override;
     RefType: TPasType;
   end;
 
+  { TPasVariable }
+
   TPasVariable = class(TPasElement)
   public
     destructor Destroy; override;
@@ -344,11 +403,16 @@ type
     AbsoluteLocation : String;
   end;
 
+  { TPasConst }
+
   TPasConst = class(TPasVariable)
+  public
   public
     function ElementTypeName: string; override;
   end;
 
+  { TPasProperty }
+
   TPasProperty = class(TPasVariable)
   public
     constructor Create(const AName: string; AParent: TPasElement); override;
@@ -362,11 +426,15 @@ type
     IsDefault, IsNodefault: Boolean;
   end;
 
+  { TPasProcedureBase }
+
   TPasProcedureBase = class(TPasElement)
   public
     function TypeName: string; virtual; abstract;
   end;
 
+  { TPasOverloadedProc }
+
   TPasOverloadedProc = class(TPasProcedureBase)
   public
     constructor Create(const AName: string; AParent: TPasElement); override;
@@ -396,18 +464,24 @@ type
     function GetDeclaration (full : boolean) : string; override;
   end;
 
+  { TPasOperator }
+
   TPasOperator = class(TPasProcedure)
   public
     function ElementTypeName: string; override;
     function GetDeclaration (full : boolean) : string; override;
   end;
 
+  { TPasConstructor }
+
   TPasConstructor = class(TPasProcedure)
   public
     function ElementTypeName: string; override;
     function TypeName: string; override;
   end;
 
+  { TPasDestructor }
+
   TPasDestructor = class(TPasProcedure)
   public
     function ElementTypeName: string; override;
@@ -417,6 +491,8 @@ type
 
   TPasImplBlock = class;
 
+  { TPasProcedureImpl }
+
   TPasProcedureImpl = class(TPasElement)
   public
     constructor Create(const AName: string; AParent: TPasElement); override;
@@ -429,12 +505,16 @@ type
     Body: TPasImplBlock;
   end;
 
+  { TPasConstructorImpl }
+
   TPasConstructorImpl = class(TPasProcedureImpl)
   public
     function ElementTypeName: string; override;
     function TypeName: string; override;
   end;
 
+  { TPasDestructorImpl }
+
   TPasDestructorImpl = class(TPasProcedureImpl)
   public
     function ElementTypeName: string; override;
@@ -444,11 +524,15 @@ type
   TPasImplElement = class(TPasElement)
   end;
 
+  { TPasImplCommand }
+
   TPasImplCommand = class(TPasImplElement)
   public
     Command: string;
   end;
 
+  { TPasImplCommands }
+
   TPasImplCommands = class(TPasImplElement)
   public
     constructor Create(const AName: string; AParent: TPasElement); override;
@@ -457,6 +541,8 @@ type
     Commands: TStrings;
   end;
 
+  { TPasImplIfElse }
+
   TPasImplIfElse = class(TPasImplElement)
   public
     destructor Destroy; override;
@@ -465,6 +551,8 @@ type
     IfBranch, ElseBranch: TPasImplElement;
   end;
 
+  { TPasImplForLoop }
+
   TPasImplForLoop = class(TPasImplElement)
   public
     destructor Destroy; override;
@@ -474,6 +562,8 @@ type
     Body: TPasImplElement;
   end;
 
+  { TPasImplBlock }
+
   TPasImplBlock = class(TPasImplElement)
   public
     constructor Create(const AName: string; AParent: TPasElement); override;
@@ -487,6 +577,11 @@ type
     Elements: TList;    // TPasImplElement objects
   end;
 
+  { TPassTreeVisitor }
+
+  TPassTreeVisitor = class
+    procedure Visit(obj: TPasElement); virtual;
+  end;
 
 const
   AccessNames: array[TArgumentAccess] of string[6] = ('', 'const ', 'var ', 'out ');
@@ -635,6 +730,11 @@ begin
     Result := '';
 end;
 
+procedure TPasElement.Accept(Visitor: TPassTreeVisitor);
+begin
+  Visitor.Visit(Self);
+end;
+
 constructor TPasSection.Create(const AName: string; AParent: TPasElement);
 begin
   inherited Create(AName, AParent);
@@ -1550,4 +1650,13 @@ begin
     Result:='';
 end;
 
+
+
+{ TPassTreeVisitor }
+
+procedure TPassTreeVisitor.Visit(obj: TPasElement);
+begin
+  // Needs to be implemented by descendents.
+end;
+
 end.