From 980d731d322f559fad4cb26bd210d0f5e93d57fb Mon Sep 17 00:00:00 2001 From: velter Date: Wed, 7 May 2014 09:59:55 +0200 Subject: [PATCH 1/3] Proposal to prevent duplicate register Add a new property to control the behavior. By default, the behavior is not changed. Setting AllowDuplicateRegsiter to false will raise an exception each time a class/interface/function register a name already known. --- Source/uPSCompiler.pas | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/Source/uPSCompiler.pas b/Source/uPSCompiler.pas index 044f90d..892f005 100644 --- a/Source/uPSCompiler.pas +++ b/Source/uPSCompiler.pas @@ -923,6 +923,7 @@ type FAllowNoBegin: Boolean; FAllowNoEnd: Boolean; FAllowUnit: Boolean; + FAllowDuplicateRegister : Boolean; FBooleanShortCircuit: Boolean; FDebugOutput: tbtString; FOnExternalProc: TPSOnExternalProc; @@ -1177,6 +1178,7 @@ type property AllowNoEnd: Boolean read FAllowNoEnd write FAllowNoEnd; + property AllowDuplicateRegister : Boolean read FAllowDuplicateRegister write FAllowDuplicateRegister; property BooleanShortCircuit: Boolean read FBooleanShortCircuit write FBooleanShortCircuit; @@ -1805,6 +1807,9 @@ const RPS_UnknownWarning = 'Unknown warning'; + RPS_ClassAlreadyRegistered = 'Class %s already registered'; + RPS_InterfaceAlreadyRegistered = 'Interface %s already registered'; + RPS_FunctionAlreadyRegistered = 'Function %s already registered'; {$IFDEF DEBUG } RPS_UnableToRegister = 'Unable to register %s'; {$ENDIF} @@ -12271,6 +12276,7 @@ begin FParser.OnParserError := ParserError; FAutoFreeList := TPSList.Create; FOutput := ''; + FAllowDuplicateRegister := true; {$IFDEF PS_USESSUPPORT} FAllowUnit := true; {$ENDIF} @@ -13487,6 +13493,9 @@ begin if not ParseMethod(Self, '', Decl, DOrgName, pDecl, FT) then Raise EPSCompilerException.CreateFmt(RPS_UnableToRegisterFunction, [Decl]); + if (FindProc(DOrgName)<>InvalidVal) and not(FAllowDuplicateRegister) then + Raise EPSCompilerException.CreateFmt(RPS_FunctionAlreadyRegistered, [Decl]); + p := TPSRegProc.Create; P.Name := FastUppercase(DOrgName); p.OrgName := DOrgName; @@ -13520,6 +13529,9 @@ var begin if FProcs = nil then raise EPSCompilerException.Create(RPS_OnUseEventOnly); f := FindType(Name); + if (f<>nil) and not(FAllowDuplicateRegister) then + Raise EPSCompilerException.CreateFmt(RPS_InterfaceAlreadyRegistered, [Name]); + if (f <> nil) and (f is TPSInterfaceType) then begin result := TPSInterfaceType(f).Intf; @@ -13556,7 +13568,8 @@ var begin if FProcs = nil then raise EPSCompilerException.Create(RPS_OnUseEventOnly); Result := FindClass(tbtstring(aClass.ClassName)); - if Result <> nil then exit; + if (Result<>nil) and not(FAllowDuplicateRegister) then + Raise EPSCompilerException.CreateFmt(RPS_ClassAlreadyRegistered, [aClass.ClassName]); f := AddType(tbtstring(aClass.ClassName), btClass); Result := TPSCompileTimeClass.CreateC(aClass, Self, f); Result.FInheritsFrom := InheritsFrom; @@ -13571,6 +13584,8 @@ var begin if FProcs = nil then raise EPSCompilerException.Create(RPS_OnUseEventOnly); Result := FindClass(aClass); + if (Result<>nil) and not(FAllowDuplicateRegister) then + Raise EPSCompilerException.CreateFmt(RPS_ClassAlreadyRegistered, [aClass]); if Result <> nil then begin if InheritsFrom <> nil then From 98145c07a1b3e4f65f5637c3bbee59a8722da5c8 Mon Sep 17 00:00:00 2001 From: velter Date: Wed, 7 May 2014 11:36:31 +0200 Subject: [PATCH 2/3] Allow forward declaration --- Source/uPSCompiler.pas | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/uPSCompiler.pas b/Source/uPSCompiler.pas index 892f005..bc7608d 100644 --- a/Source/uPSCompiler.pas +++ b/Source/uPSCompiler.pas @@ -13584,7 +13584,7 @@ var begin if FProcs = nil then raise EPSCompilerException.Create(RPS_OnUseEventOnly); Result := FindClass(aClass); - if (Result<>nil) and not(FAllowDuplicateRegister) then + if (Result<>nil) and (Result.FInheritsFrom<>nil) and not(FAllowDuplicateRegister) then Raise EPSCompilerException.CreateFmt(RPS_ClassAlreadyRegistered, [aClass]); if Result <> nil then begin From 5967ab113b5f4b93b6d294d1f5755181e8417cf7 Mon Sep 17 00:00:00 2001 From: velter Date: Tue, 27 May 2014 16:05:00 +0200 Subject: [PATCH 3/3] Update Duplicate definition change --- Source/uPSCompiler.pas | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/Source/uPSCompiler.pas b/Source/uPSCompiler.pas index bc7608d..b0bdaf0 100644 --- a/Source/uPSCompiler.pas +++ b/Source/uPSCompiler.pas @@ -1806,10 +1806,6 @@ const RPS_AbstractClass = 'Abstract Class Construction'; RPS_UnknownWarning = 'Unknown warning'; - - RPS_ClassAlreadyRegistered = 'Class %s already registered'; - RPS_InterfaceAlreadyRegistered = 'Interface %s already registered'; - RPS_FunctionAlreadyRegistered = 'Function %s already registered'; {$IFDEF DEBUG } RPS_UnableToRegister = 'Unable to register %s'; {$ENDIF} @@ -2388,6 +2384,9 @@ begin raise EPSCompilerException.Create(RPS_OnUseEventOnly); end; + if not(AllowDuplicateRegister) and IsDuplicate(FastUpperCase(Name),[dcTypes, dcProcs, dcVars]) then + Raise EPSCompilerException.CreateFmt(RPS_DuplicateIdent, [Name]); + case BaseType of btProcPtr: Result := TPSProceduralType.Create; BtTypeCopy: Result := TPSTypeLink.Create; @@ -12425,6 +12424,10 @@ begin FType := GetTypeCopyLink(FType); if FType = nil then Raise EPSCompilerException.CreateFmt(RPS_UnableToRegisterConst, [name]); + + if not(AllowDuplicateRegister) and IsDuplicate(FastUpperCase(Name),[dcProcs, dcVars, dcConsts]) then + Raise EPSCompilerException.CreateFmt(RPS_DuplicateIdent, [Name]); + pc := TPSConstant.Create; pc.OrgName := name; pc.Name := FastUppercase(name); @@ -13386,6 +13389,10 @@ begin if FProcs = nil then raise EPSCompilerException.Create(RPS_OnUseEventOnly); Parser := TPSPascalParser.Create; Parser.SetText(Decl); + + if not(AllowDuplicateRegister) and (FindType(Name)<>nil) then + Raise EPSCompilerException.CreateFmt(RPS_DuplicateIdent, [Name]); + Result := ReadType(Name, Parser); if Result<>nil then begin @@ -13494,7 +13501,7 @@ begin Raise EPSCompilerException.CreateFmt(RPS_UnableToRegisterFunction, [Decl]); if (FindProc(DOrgName)<>InvalidVal) and not(FAllowDuplicateRegister) then - Raise EPSCompilerException.CreateFmt(RPS_FunctionAlreadyRegistered, [Decl]); + Raise EPSCompilerException.CreateFmt(RPS_DuplicateIdent, [Decl]); p := TPSRegProc.Create; P.Name := FastUppercase(DOrgName); @@ -13530,7 +13537,7 @@ begin if FProcs = nil then raise EPSCompilerException.Create(RPS_OnUseEventOnly); f := FindType(Name); if (f<>nil) and not(FAllowDuplicateRegister) then - Raise EPSCompilerException.CreateFmt(RPS_InterfaceAlreadyRegistered, [Name]); + Raise EPSCompilerException.CreateFmt(RPS_DuplicateIdent, [Name]); if (f <> nil) and (f is TPSInterfaceType) then begin @@ -13569,7 +13576,7 @@ begin if FProcs = nil then raise EPSCompilerException.Create(RPS_OnUseEventOnly); Result := FindClass(tbtstring(aClass.ClassName)); if (Result<>nil) and not(FAllowDuplicateRegister) then - Raise EPSCompilerException.CreateFmt(RPS_ClassAlreadyRegistered, [aClass.ClassName]); + Raise EPSCompilerException.CreateFmt(RPS_DuplicateIdent, [aClass.ClassName]); f := AddType(tbtstring(aClass.ClassName), btClass); Result := TPSCompileTimeClass.CreateC(aClass, Self, f); Result.FInheritsFrom := InheritsFrom; @@ -13585,7 +13592,7 @@ begin if FProcs = nil then raise EPSCompilerException.Create(RPS_OnUseEventOnly); Result := FindClass(aClass); if (Result<>nil) and (Result.FInheritsFrom<>nil) and not(FAllowDuplicateRegister) then - Raise EPSCompilerException.CreateFmt(RPS_ClassAlreadyRegistered, [aClass]); + Raise EPSCompilerException.CreateFmt(RPS_DuplicateIdent, [aClass]); if Result <> nil then begin if InheritsFrom <> nil then