* Merging revisions r46282 from trunk:

------------------------------------------------------------------------
    r46282 | michael | 2020-08-06 09:21:22 +0200 (Thu, 06 Aug 2020) | 1 line
    
    * Escape keywords when writing
    ------------------------------------------------------------------------

git-svn-id: branches/fixes_3_2@46608 -
This commit is contained in:
michael 2020-08-23 09:30:20 +00:00
parent d4f696a18d
commit 70cf4d95d7
2 changed files with 83 additions and 49 deletions

View File

@ -162,12 +162,15 @@ type
{$ENDIF} {$ENDIF}
constructor Create(const AName: string; AParent: TPasElement); virtual; constructor Create(const AName: string; AParent: TPasElement); virtual;
destructor Destroy; override; destructor Destroy; override;
Class Function IsKeyWord(Const S : String) : Boolean;
Class Function EscapeKeyWord(Const S : String) : String;
procedure AddRef{$IFDEF CheckPasTreeRefCount}(const aId: string){$ENDIF}; procedure AddRef{$IFDEF CheckPasTreeRefCount}(const aId: string){$ENDIF};
procedure Release{$IFDEF CheckPasTreeRefCount}(const aId: string){$ENDIF}; procedure Release{$IFDEF CheckPasTreeRefCount}(const aId: string){$ENDIF};
procedure ForEachCall(const aMethodCall: TOnForEachPasElement; procedure ForEachCall(const aMethodCall: TOnForEachPasElement;
const Arg: Pointer); virtual; const Arg: Pointer); virtual;
procedure ForEachChildCall(const aMethodCall: TOnForEachPasElement; procedure ForEachChildCall(const aMethodCall: TOnForEachPasElement;
const Arg: Pointer; Child: TPasElement; CheckParent: boolean); virtual; const Arg: Pointer; Child: TPasElement; CheckParent: boolean); virtual;
Function SafeName : String; // Name but with & prepended if name is a keyword.
function FullPath: string; // parent's names, until parent is not TPasDeclarations function FullPath: string; // parent's names, until parent is not TPasDeclarations
function ParentPath: string; // parent's names function ParentPath: string; // parent's names
function FullName: string; virtual; // FullPath + Name function FullName: string; virtual; // FullPath + Name
@ -2551,7 +2554,7 @@ function TPasType.FixTypeDecl(aDecl: String): String;
begin begin
Result:=aDecl; Result:=aDecl;
if (Name<>'') then if (Name<>'') then
Result:=Name+' = '+Result; Result:=SafeName+' = '+Result;
ProcessHints(false,Result); ProcessHints(false,Result);
end; end;
@ -2829,6 +2832,30 @@ begin
inherited Destroy; inherited Destroy;
end; end;
class function TPasElement.IsKeyWord(const S: String): Boolean;
Const
KW=';absolute;and;array;asm;begin;case;const;constructor;destructor;div;do;'+
'downto;else;end;file;for;function;goto;if;implementation;in;inherited;'+
'inline;interface;label;mod;nil;not;object;of;on;operator;or;packed;'+
'procedure;program;record;reintroduce;repeat;self;set;shl;shr;string;then;'+
'to;type;unit;until;uses;var;while;with;xor;dispose;exit;false;new;true;'+
'as;class;dispinterface;except;exports;finalization;finally;initialization;'+
'inline;is;library;on;out;packed;property;raise;resourcestring;threadvar;try;'+
'private;published;length;setlength;';
begin
Result:=Pos(';'+lowercase(S)+';',KW)<>0;
end;
class function TPasElement.EscapeKeyWord(const S: String): String;
begin
Result:=S;
If IsKeyWord(Result) then
Result:='&'+Result
end;
procedure TPasElement.AddRef{$IFDEF CheckPasTreeRefCount}(const aId: string){$ENDIF}; procedure TPasElement.AddRef{$IFDEF CheckPasTreeRefCount}(const aId: string){$ENDIF};
begin begin
{$ifdef EnablePasTreeGlobalRefCount} {$ifdef EnablePasTreeGlobalRefCount}
@ -2918,6 +2945,13 @@ begin
Child.ForEachCall(aMethodCall,Arg); Child.ForEachCall(aMethodCall,Arg);
end; end;
function TPasElement.SafeName: String;
begin
Result:=Name;
if IsKeyWord(Result) then
Result:='&'+Result;
end;
function TPasElement.FullPath: string; function TPasElement.FullPath: string;
var var
@ -2996,7 +3030,7 @@ function TPasElement.GetDeclaration(full: Boolean): string;
begin begin
if Full then if Full then
Result := Name Result := SafeName
else else
Result := ''; Result := '';
end; end;
@ -4016,7 +4050,7 @@ end;
function TPasModule.GetDeclaration(full : boolean): string; function TPasModule.GetDeclaration(full : boolean): string;
begin begin
Result := 'Unit ' + Name; Result := 'Unit ' + SafeName;
if full then ; if full then ;
end; end;
@ -4043,7 +4077,7 @@ begin
Result:=Expr.GetDeclaration(true); Result:=Expr.GetDeclaration(true);
If Full Then If Full Then
begin begin
Result:=Name+' = '+Result; Result:=SafeName+' = '+Result;
ProcessHints(False,Result); ProcessHints(False,Result);
end; end;
end; end;
@ -4063,10 +4097,10 @@ end;
function TPasPointerType.GetDeclaration(full: Boolean): string; function TPasPointerType.GetDeclaration(full: Boolean): string;
begin begin
Result:='^'+DestType.Name; Result:='^'+DestType.SafeName;
If Full then If Full then
begin begin
Result:=Name+' = '+Result; Result:=SafeName+' = '+Result;
ProcessHints(False,Result); ProcessHints(False,Result);
end; end;
end; end;
@ -4086,7 +4120,7 @@ end;
function TPasAliasType.GetDeclaration(full: Boolean): string; function TPasAliasType.GetDeclaration(full: Boolean): string;
begin begin
Result:=DestType.Name; Result:=DestType.SafeName;
If Full then If Full then
Result:=FixTypeDecl(Result); Result:=FixTypeDecl(Result);
end; end;
@ -4107,7 +4141,7 @@ end;
function TPasClassOfType.GetDeclaration (full : boolean) : string; function TPasClassOfType.GetDeclaration (full : boolean) : string;
begin begin
Result:='Class of '+DestType.Name; Result:='Class of '+DestType.SafeName;
If Full then If Full then
Result:=FixTypeDecl(Result); Result:=FixTypeDecl(Result);
end; end;
@ -4158,7 +4192,7 @@ begin
If IsPacked then If IsPacked then
Result := 'packed '+Result; // 12/04/04 Dave - Added Result := 'packed '+Result; // 12/04/04 Dave - Added
If Assigned(Eltype) then If Assigned(Eltype) then
Result:=Result+ElType.Name Result:=Result+ElType.SafeName
else else
Result:=Result+'const'; Result:=Result+'const';
end; end;
@ -4186,7 +4220,7 @@ function TPasFileType.GetDeclaration (full : boolean) : string;
begin begin
Result:='File'; Result:='File';
If Assigned(Eltype) then If Assigned(Eltype) then
Result:=Result+' of '+ElType.Name; Result:=Result+' of '+ElType.SafeName;
If Full Then If Full Then
Result:=FixTypeDecl(Result); Result:=FixTypeDecl(Result);
end; end;
@ -4207,13 +4241,13 @@ begin
S:=TStringList.Create; S:=TStringList.Create;
Try Try
If Full and (Name<>'') then If Full and (Name<>'') then
S.Add(Name+' = (') S.Add(SafeName+' = (')
else else
S.Add('('); S.Add('(');
GetEnumNames(S); GetEnumNames(S);
S[S.Count-1]:=S[S.Count-1]+')'; S[S.Count-1]:=S[S.Count-1]+')';
If Full then If Full then
Result:=IndentStrings(S,Length(Name)+4) Result:=IndentStrings(S,Length(SafeName)+4)
else else
Result:=IndentStrings(S,1); Result:=IndentStrings(S,1);
if Full then if Full then
@ -4241,7 +4275,7 @@ begin
S:=TStringList.Create; S:=TStringList.Create;
Try Try
If Full and (Name<>'') then If Full and (Name<>'') then
S.Add(Name+'= Set of (') S.Add(SafeName+'= Set of (')
else else
S.Add('Set of ('); S.Add('Set of (');
TPasEnumType(EnumType).GetEnumNames(S); TPasEnumType(EnumType).GetEnumNames(S);
@ -4254,9 +4288,9 @@ begin
end end
else else
begin begin
Result:='Set of '+EnumType.Name; Result:='Set of '+EnumType.SafeName;
If Full then If Full then
Result:=Name+' = '+Result; Result:=SafeName+' = '+Result;
end; end;
If Full then If Full then
ProcessHints(False,Result); ProcessHints(False,Result);
@ -4393,9 +4427,9 @@ begin
If Full and (Name<>'') then If Full and (Name<>'') then
begin begin
if GenericTemplateTypes.Count>0 then if GenericTemplateTypes.Count>0 then
Temp:=Name+GenericTemplateTypesAsString(GenericTemplateTypes)+' = '+Temp Temp:=SafeName+GenericTemplateTypesAsString(GenericTemplateTypes)+' = '+Temp
else else
Temp:=Name+' = '+Temp; Temp:=SafeName+' = '+Temp;
end; end;
S.Add(Temp); S.Add(Temp);
GetMembers(S); GetMembers(S);
@ -4467,7 +4501,7 @@ begin
S:=TStringList.Create; S:=TStringList.Create;
Try Try
If Full then If Full then
S.Add(Format('%s = ',[Name])); S.Add(Format('%s = ',[SafeName]));
S.Add(TypeName); S.Add(TypeName);
GetArguments(S); GetArguments(S);
If IsOfObject then If IsOfObject then
@ -4493,14 +4527,14 @@ begin
S:=TStringList.Create; S:=TStringList.Create;
Try Try
If Full then If Full then
S.Add(Format('%s = ',[Name])); S.Add(Format('%s = ',[SafeName]));
S.Add(TypeName); S.Add(TypeName);
GetArguments(S); GetArguments(S);
If Assigned(ResultEl) then If Assigned(ResultEl) then
begin begin
T:=' : '; T:=' : ';
If (ResultEl.ResultType.Name<>'') then If (ResultEl.ResultType.Name<>'') then
T:=T+ResultEl.ResultType.Name T:=T+ResultEl.ResultType.SafeName
else else
T:=T+ResultEl.ResultType.GetDeclaration(False); T:=T+ResultEl.ResultType.GetDeclaration(False);
S.Add(T); S.Add(T);
@ -4534,7 +4568,7 @@ begin
If VarType.Name='' then If VarType.Name='' then
Result:=VarType.GetDeclaration(False) Result:=VarType.GetDeclaration(False)
else else
Result:=VarType.Name; Result:=VarType.SafeName;
Result:=Result+Modifiers; Result:=Result+Modifiers;
if (Value<>'') then if (Value<>'') then
Result:=Result+' = '+Value; Result:=Result+' = '+Value;
@ -4543,7 +4577,7 @@ begin
Result:=Value; Result:=Value;
If Full then If Full then
begin begin
Result:=Name+' '+Seps[Assigned(VarType)]+' '+Result; Result:=SafeName+' '+Seps[Assigned(VarType)]+' '+Result;
Result:=Result+HintsString; Result:=Result+HintsString;
end; end;
end; end;
@ -4584,7 +4618,7 @@ begin
If VarType.Name='' then If VarType.Name='' then
Result:=VarType.GetDeclaration(False) Result:=VarType.GetDeclaration(False)
else else
Result:=VarType.Name; Result:=VarType.SafeName;
end end
else if Assigned(Expr) then else if Assigned(Expr) then
Result:=Expr.GetDeclaration(True); Result:=Expr.GetDeclaration(True);
@ -4604,9 +4638,9 @@ begin
S:=' '; S:=' ';
If Full then If Full then
begin begin
Result:=Name+S+': '+Result; Result:=SafeName+S+': '+Result;
If (ImplementsName<>'') then If (ImplementsName<>'') then
Result:=Result+' implements '+ImplementsName; Result:=Result+' implements '+EscapeKeyWord(ImplementsName);
end; end;
If IsDefault then If IsDefault then
Result:=Result+'; default'; Result:=Result+'; default';
@ -4836,7 +4870,7 @@ begin
end; end;
end end
else if Name<>'' then else if Name<>'' then
T:=T+' '+Name; T:=T+' '+SafeName;
S.Add(T); S.Add(T);
end; end;
ProcType.GetArguments(S); ProcType.GetArguments(S);
@ -4846,7 +4880,7 @@ begin
begin begin
T:=' : '; T:=' : ';
If (Name<>'') then If (Name<>'') then
T:=T+Name T:=T+SafeName
else else
T:=T+GetDeclaration(False); T:=T+GetDeclaration(False);
S.Add(T); S.Add(T);
@ -4902,7 +4936,7 @@ begin
begin begin
T:=' : '; T:=' : ';
If (Name<>'') then If (Name<>'') then
T:=T+Name T:=T+SafeName
else else
T:=T+GetDeclaration(False); T:=T+GetDeclaration(False);
S.Add(T); S.Add(T);
@ -4970,14 +5004,14 @@ begin
If Assigned(ArgType) then If Assigned(ArgType) then
begin begin
If ArgType.Name<>'' then If ArgType.Name<>'' then
Result:=ArgType.Name Result:=ArgType.SafeName
else else
Result:=ArgType.GetDeclaration(False); Result:=ArgType.GetDeclaration(False);
If Full and (Name<>'') then If Full and (Name<>'') then
Result:=Name+': '+Result; Result:=SafeName+': '+Result;
end end
else If Full then else If Full then
Result:=Name Result:=SafeName
else else
Result:=''; Result:='';
end; end;
@ -5637,7 +5671,7 @@ begin
begin begin
If Result<>'' then If Result<>'' then
Result:=Result+'; '; Result:=Result+'; ';
Result:=Result+Fields[I].Name+': '+Fields[i].ValueExp.getDeclaration(Full); Result:=Result+EscapeKeyWord(Fields[I].Name)+': '+Fields[i].ValueExp.getDeclaration(Full);
end; end;
Result:='('+Result+')'; Result:='('+Result+')';
end; end;

View File

@ -305,7 +305,7 @@ Var
begin begin
S:=''; S:='';
if aModule.Name<>'' then if aModule.Name<>'' then
S:=Format('program %s',[aModule.Name]); S:=Format('program %s',[aModule.SafeName]);
if (S<>'') then if (S<>'') then
begin begin
If AModule.InputFile<>'' then If AModule.InputFile<>'' then
@ -346,7 +346,7 @@ Var
begin begin
S:=''; S:='';
if aModule.Name<>'' then if aModule.Name<>'' then
S:=Format('library %s',[aModule.Name]); S:=Format('library %s',[aModule.SafeName]);
if (S<>'') then if (S<>'') then
begin begin
If AModule.InputFile<>'' then If AModule.InputFile<>'' then
@ -434,7 +434,7 @@ begin
For J:=0 to C.Members.Count-1 do For J:=0 to C.Members.Count-1 do
begin begin
M:=TPasElement(C.members[J]); M:=TPasElement(C.members[J]);
DoCheckElement(M,True,C.Name+'.'); DoCheckElement(M,True,C.SafeName+'.');
end; end;
end; end;
end; end;
@ -455,7 +455,7 @@ begin
PrepareDeclSection('type'); PrepareDeclSection('type');
For I:=0 to aSection.Classes.Count-1 do For I:=0 to aSection.Classes.Count-1 do
begin begin
CN:=TPasElement(aSection.Classes[i]).Name; CN:=TPasElement(aSection.Classes[i]).SafeName;
if (FForwardClasses.Count=0) or (ForwardClasses.IndexOf(CN)<>-1) then if (FForwardClasses.Count=0) or (ForwardClasses.IndexOf(CN)<>-1) then
Addln('%s = class;',[CN]); Addln('%s = class;',[CN]);
end; end;
@ -464,7 +464,7 @@ end;
procedure TPasWriter.WriteUnit(aModule: TPasModule); procedure TPasWriter.WriteUnit(aModule: TPasModule);
begin begin
AddLn('unit ' + AModule.Name + ';'); AddLn('unit ' + AModule.SafeName + ';');
if Assigned(AModule.GlobalDirectivesSection) then if Assigned(AModule.GlobalDirectivesSection) then
begin begin
AddLn; AddLn;
@ -621,7 +621,7 @@ begin
PrepareDeclSection('type'); PrepareDeclSection('type');
Addln; Addln;
MaybeSetLineElement(AClass); MaybeSetLineElement(AClass);
Add(AClass.Name + ' = '); Add(AClass.SafeName + ' = ');
if AClass.IsPacked then if AClass.IsPacked then
Add('packed '); // 12/04/04 - Dave - Added Add('packed '); // 12/04/04 - Dave - Added
case AClass.ObjKind of case AClass.ObjKind of
@ -636,16 +636,16 @@ begin
if (AClass.ObjKind=okClass) and (ACLass.ExternalName<>'') and NotOption(woNoExternalClass) then if (AClass.ObjKind=okClass) and (ACLass.ExternalName<>'') and NotOption(woNoExternalClass) then
Add(' external name ''%s'' ',[AClass.ExternalName]); Add(' external name ''%s'' ',[AClass.ExternalName]);
if Assigned(AClass.AncestorType) then if Assigned(AClass.AncestorType) then
Add('(' + AClass.AncestorType.Name); Add('(' + AClass.AncestorType.SafeName);
if AClass.Interfaces.Count > 0 then if AClass.Interfaces.Count > 0 then
begin begin
if Assigned(AClass.AncestorType) then if Assigned(AClass.AncestorType) then
InterfacesListPrefix:=', ' InterfacesListPrefix:=', '
else else
InterfacesListPrefix:='('; InterfacesListPrefix:='(';
Add(InterfacesListPrefix + TPasType(AClass.Interfaces[0]).Name); Add(InterfacesListPrefix + TPasType(AClass.Interfaces[0]).SafeName);
for i := 1 to AClass.Interfaces.Count - 1 do for i := 1 to AClass.Interfaces.Count - 1 do
Add(', ' + TPasType(AClass.Interfaces[i]).Name); Add(', ' + TPasType(AClass.Interfaces[i]).SafeName);
end; end;
if Assigned(AClass.AncestorType) or (AClass.Interfaces.Count > 0) then if Assigned(AClass.AncestorType) or (AClass.Interfaces.Count > 0) then
AddLn(')') AddLn(')')
@ -706,9 +706,9 @@ begin
PrepareDeclSectionInStruct('class var') PrepareDeclSectionInStruct('class var')
else if CurDeclSection<>'' then else if CurDeclSection<>'' then
PrepareDeclSectionInStruct('var'); PrepareDeclSectionInStruct('var');
Add(aVar.Name + ': '); Add(aVar.SafeName + ': ');
if Not Assigned(aVar.VarType) then if Not Assigned(aVar.VarType) then
Raise EWriteError.CreateFmt('No type for variable %s',[aVar.Name]); Raise EWriteError.CreateFmt('No type for variable %s',[aVar.SafeName]);
WriteType(aVar.VarType,False); WriteType(aVar.VarType,False);
if (aVar.AbsoluteExpr<>nil) then if (aVar.AbsoluteExpr<>nil) then
Add(' absolute %s',[aVar.AbsoluteExpr.ClassName]) Add(' absolute %s',[aVar.AbsoluteExpr.ClassName])
@ -738,7 +738,7 @@ procedure TPasWriter.WriteArgument(aArg: TPasArgument);
begin begin
if (aArg.Access<>argDefault) then if (aArg.Access<>argDefault) then
Add(AccessNames[aArg.Access]+' '); Add(AccessNames[aArg.Access]+' ');
Add(aArg.Name+' : '); Add(aArg.SafeName+' : ');
WriteType(aArg.ArgType,False); WriteType(aArg.ArgType,False);
end; end;
@ -809,7 +809,7 @@ begin
PrepareDeclSection(''); PrepareDeclSection('');
if Not IsImpl then if Not IsImpl then
IsImpl:=FInImplementation; IsImpl:=FInImplementation;
Add(AProc.TypeName + ' ' + NamePrefix+AProc.Name); Add(AProc.TypeName + ' ' + NamePrefix+AProc.SafeName);
if Assigned(AProc.ProcType) and (AProc.ProcType.Args.Count > 0) then if Assigned(AProc.ProcType) and (AProc.ProcType.Args.Count > 0) then
AddProcArgs(AProc.ProcType.Args) ; AddProcArgs(AProc.ProcType.Args) ;
if Assigned(AProc.ProcType) and if Assigned(AProc.ProcType) and
@ -912,8 +912,8 @@ begin
Add('class '); Add('class ');
Add(AProc.TypeName + ' '); Add(AProc.TypeName + ' ');
if AProc.Parent.ClassType = TPasClassType then if AProc.Parent.ClassType = TPasClassType then
Add(AProc.Parent.Name + '.'); Add(AProc.Parent.SafeName + '.');
Add(AProc.Name); Add(AProc.SafeName);
if Assigned(AProc.ProcType) and (AProc.ProcType.Args.Count > 0) then if Assigned(AProc.ProcType) and (AProc.ProcType.Args.Count > 0) then
AddProcArgs(AProc.ProcType.Args); AddProcArgs(AProc.ProcType.Args);
if Assigned(AProc.ProcType) and if Assigned(AProc.ProcType) and
@ -997,7 +997,7 @@ var
begin begin
if AProp.IsClass then if AProp.IsClass then
Add('class '); Add('class ');
Add('property ' + AProp.Name); Add('property ' + AProp.SafeName);
if AProp.Args.Count > 0 then if AProp.Args.Count > 0 then
begin begin
Add('['); Add('[');
@ -1238,7 +1238,7 @@ end;
procedure TPasWriter.WriteImplExceptOn(aOn: TPasImplExceptOn); procedure TPasWriter.WriteImplExceptOn(aOn: TPasImplExceptOn);
begin begin
Addln('On %s : %s do',[aOn.VarEl.Name,aOn.TypeEl.Name]); Addln('On %s : %s do',[aOn.VarEl.SafeName,aOn.TypeEl.SafeName]);
if Assigned(aOn.Body) then if Assigned(aOn.Body) then
WriteImplElement(aOn.Body,True); WriteImplElement(aOn.Body,True);
end; end;