Skip to content

Commit cf30716

Browse files
committed
Codetools: Use nullable unit
1 parent dd984c3 commit cf30716

File tree

3 files changed

+46
-124
lines changed

3 files changed

+46
-124
lines changed

Source/ide/codetools/simba.ide_codetools_base.pas

-39
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,6 @@ generic TKeywordDictionary<_T> = class(TObject)
3838
property Value[Key: PChar]: _T read getValue; default;
3939
end;
4040

41-
// not initialized, so only "safe" to use when used within a class
42-
generic TCache<_T> = record
43-
type
44-
TSelf = specialize TCache<_T>;
45-
private
46-
FValue: _T;
47-
FCached: Boolean;
48-
49-
function getEmpty: Boolean;
50-
procedure setEmpty(Value: Boolean);
51-
public
52-
property Empty: Boolean read getEmpty write setEmpty;
53-
54-
class operator := (AValue: _T): TSelf;
55-
class operator := (AValue: TSelf): _T;
56-
end;
57-
TStringCache = specialize TCache<String>;
58-
5941
{$IFDEF PARSER_LEAK_CHECKS}
6042
TLeakChecker = class(TObject)
6143
protected
@@ -126,27 +108,6 @@ procedure TKeywordDictionary.Add(Key: String; Value: _T);
126108
FBuckets[Bucket].Value := Value;
127109
end;
128110

129-
function TCache.getEmpty: Boolean;
130-
begin
131-
Result := not FCached;
132-
end;
133-
134-
procedure TCache.setEmpty(Value: Boolean);
135-
begin
136-
FCached := Value;
137-
end;
138-
139-
class operator TCache.:=(AValue: _T): TSelf;
140-
begin
141-
Result.FValue := AValue;
142-
Result.FCached := True;
143-
end;
144-
145-
class operator TCache.:=(AValue: TSelf): _T;
146-
begin
147-
Result := AValue.FValue;
148-
end;
149-
150111
{$IFDEF PARSER_LEAK_CHECKS}
151112
class constructor TLeakChecker.Create;
152113
begin

Source/ide/codetools/simba.ide_codetools_includes.pas

+1-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ TCodetoolsInclude = class(TCodeParser)
6161
function IsOutdated: Boolean;
6262
function IncRef: TCodetoolsInclude;
6363
function DecRef: TCodetoolsInclude;
64-
6564
end;
6665

6766
TCodetoolsPlugin = class(TCodetoolsInclude)
@@ -115,7 +114,7 @@ function TCodetoolsInclude.GetPlugins: TStringArray;
115114

116115
function TCodetoolsInclude.GetHash: String;
117116
begin
118-
if FHash.Empty then
117+
if FHash.IsNull then
119118
FHash := inherited + FInDefines.Defines + IntToStr(FInDefines.Stack) + FPlugins.Text;
120119

121120
Result := FHash;

Source/ide/codetools/simba.ide_codetools_parser.pas

+45-83
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
interface
1111

1212
uses
13-
SysUtils, Classes,
13+
SysUtils, Classes, nullable,
1414
simba.base,
1515
simba.containers,
1616
simba.ide_codetools_base,
@@ -25,7 +25,8 @@ TDeclarationClass = class of TDeclaration;
2525
TDeclarationArray = array of TDeclaration;
2626

2727
TDeclarationStack = specialize TSimbaStack<TDeclaration>;
28-
TDeclarationCache = specialize TCache<TDeclarationArray>;
28+
TDeclarationCache = specialize TNullable<TDeclarationArray>;
29+
TStringCache = specialize TNullable<String>;
2930

3031
TDeclarationList = class(TObject)
3132
protected
@@ -722,7 +723,7 @@ procedure TDeclarationList.Clear(const FreeDecls: Boolean);
722723

723724
function TDeclaration.GetText: String;
724725
begin
725-
if FText.Empty then
726+
if FText.IsNull then
726727
FText := FLexer.CopyDoc(FStartPos, FEndPos);
727728

728729
Result := FText;
@@ -750,7 +751,7 @@ function TDeclaration.GetTextNoComments: String;
750751
end;
751752

752753
begin
753-
if FTextNoComments.Empty then
754+
if FTextNoComments.IsNull then
754755
FTextNoComments := Filter(FLexer.CopyDoc(FStartPos, FEndPos));
755756

756757
Result := FTextNoComments;
@@ -786,18 +787,15 @@ function TDeclaration.GetTextNoCommentsSingleLine: String;
786787
end;
787788

788789
begin
789-
if FTextNoCommentsSingleLine.Empty then
790+
if FTextNoCommentsSingleLine.IsNull then
790791
FTextNoCommentsSingleLine := Filter(FLexer.CopyDoc(FStartPos, FEndPos));
791792

792793
Result := FTextNoCommentsSingleLine;
793794
end;
794795

795796
function TDeclaration.GetName: String;
796797
begin
797-
if FName.Empty then
798-
Result := ''
799-
else
800-
Result := FName;
798+
Result := FName.ValueOrDefault;
801799
end;
802800

803801
function TDeclaration.GetFullName: String;
@@ -1002,31 +1000,23 @@ constructor TDeclaration_Keyword.Create(Keyword: String);
10021000

10031001
function TDeclaration_Anchor.GetHeader: String;
10041002
begin
1005-
if FHeader.Empty then
1006-
begin
1007-
Result := 'Anchor "' + Name + '"';
1008-
1009-
FHeader := Result;
1010-
end;
1003+
if FHeader.IsNull then
1004+
FHeader := 'Anchor "' + Name + '"';
10111005

10121006
Result := FHeader;
10131007
end;
10141008

10151009
function TDeclaration_Type.GetHeader: String;
10161010
begin
1017-
if FHeader.Empty then
1018-
begin
1019-
Result := 'type ' + Name + ' = ' + TextNoCommentsSingleLine;
1020-
1021-
FHeader := Result;
1022-
end;
1011+
if FHeader.IsNull then
1012+
FHeader := 'type ' + Name + ' = ' + TextNoCommentsSingleLine;
10231013

10241014
Result := FHeader;
10251015
end;
10261016

10271017
function TDeclaration_EnumElement.GetName: string;
10281018
begin
1029-
if FName.Empty then
1019+
if FName.IsNull then
10301020
FName := Items.GetTextOfClass(TDeclaration_EnumElementName);
10311021

10321022
Result := inherited;
@@ -1042,13 +1032,10 @@ function TDeclaration_EnumElement.GetFullName: String;
10421032

10431033
function TDeclaration_EnumElement.GetHeader: String;
10441034
begin
1045-
if FHeader.Empty then
1046-
begin
1047-
if (FParent is TDeclaration_TypeEnumScoped) then
1048-
Result := FParent.Name + '.' + Name
1049-
else
1050-
Result := Name;
1051-
end;
1035+
if FHeader.IsNull then
1036+
FHeader := IfThen(FParent is TDeclaration_TypeEnumScoped, FParent.Name + '.' + Name, Name);
1037+
1038+
Result := FHeader;
10521039
end;
10531040

10541041
function TDeclaration_TypeAlias.Dump: String;
@@ -1098,36 +1085,24 @@ function TDeclaration_TypeArray.VarType: TDeclaration;
10981085

10991086
function TDeclaration_TypeRecord.GetFields: TDeclarationArray;
11001087
begin
1101-
if FFields.Empty then
1102-
begin
1103-
Result := Items.GetByClass(TDeclaration_Field);
1104-
1105-
FFields := Result;
1106-
end;
1088+
if FFields.IsNull then
1089+
FFields := Items.GetByClass(TDeclaration_Field, True);
11071090

11081091
Result := FFields;
11091092
end;
11101093

11111094
function TDeclaration_TypeRecord.GetConsts: TDeclarationArray;
11121095
begin
1113-
if FConsts.Empty then
1114-
begin
1115-
Result := Items.GetByClass(TDeclaration_Const);
1116-
1117-
FConsts := Result;
1118-
end;
1096+
if FConsts.IsNull then
1097+
FConsts := Items.GetByClass(TDeclaration_Const, True);
11191098

11201099
Result := FConsts;
11211100
end;
11221101

11231102
function TDeclaration_Var.GetHeader: String;
11241103
begin
1125-
if FHeader.Empty then
1126-
begin
1127-
Result := 'var ' + Name + VarTypeString + VarDefaultString;
1128-
1129-
FHeader := Result;
1130-
end;
1104+
if FHeader.IsNull then
1105+
FHeader := 'var ' + Name + VarTypeString + VarDefaultString;
11311106

11321107
Result := FHeader;
11331108
end;
@@ -1139,15 +1114,15 @@ function TDeclaration_Var.GetVarType: TDeclaration;
11391114

11401115
function TDeclaration_Var.GetVarTypeString: String;
11411116
begin
1142-
if FVarTypeString.Empty then
1117+
if FVarTypeString.IsNull then
11431118
FVarTypeString := Items.GetTextOfClassNoCommentsSingleLine(TDeclaration_VarType, ': ');
11441119

11451120
Result := FVarTypeString;
11461121
end;
11471122

11481123
function TDeclaration_Var.GetVarDefaultString: String;
11491124

1150-
function ReplaceunPrintable(const Str: String): String;
1125+
function ReplaceUnPrintable(const Str: String): String;
11511126
var
11521127
I: Integer = 1;
11531128
begin
@@ -1165,27 +1140,24 @@ function TDeclaration_Var.GetVarDefaultString: String;
11651140
end;
11661141

11671142
begin
1168-
if FVarDefaultString.Empty then
1143+
if FVarDefaultString.IsNull then
11691144
begin
11701145
case DefToken of
11711146
tokAssign: FVarDefaultString := Items.GetTextOfClassNoCommentsSingleLine(TDeclaration_VarDefault, ' := ');
11721147
tokEqual: FVarDefaultString := Items.GetTextOfClassNoCommentsSingleLine(TDeclaration_VarDefault, ' = ');
1148+
else
1149+
FVarDefaultString := '';
11731150
end;
1174-
1175-
FVarDefaultString := ReplaceunPrintable(FVarDefaultString);
1151+
FVarDefaultString := ReplaceUnPrintable(FVarDefaultString);
11761152
end;
11771153

11781154
Result := FVarDefaultString;
11791155
end;
11801156

11811157
function TDeclaration_Const.GetHeader: String;
11821158
begin
1183-
if FHeader.Empty then
1184-
begin
1185-
Result := 'const ' + Name + VarDefaultString;
1186-
1187-
FHeader := Result;
1188-
end;
1159+
if FHeader.IsNull then
1160+
FHeader := 'const ' + Name + VarDefaultString;
11891161

11901162
Result := FHeader;
11911163
end;
@@ -1199,32 +1171,24 @@ function TDeclaration_Field.Dump: String;
11991171

12001172
function TDeclaration_EnumElementName.GetName: string;
12011173
begin
1202-
if FName.Empty then
1174+
if FName.IsNull then
12031175
FName := Text;
12041176

12051177
Result := FName;
12061178
end;
12071179

12081180
function TDeclaration_TypeEnum.GetElements: TDeclarationArray;
12091181
begin
1210-
if FElements.Empty then
1211-
begin
1212-
Result := FItems.GetByClass(TDeclaration_EnumElement);
1213-
1214-
FElements := Result;
1215-
end;
1182+
if FElements.IsNull then
1183+
FElements := FItems.GetByClass(TDeclaration_EnumElement);
12161184

12171185
Result := FElements;
12181186
end;
12191187

12201188
function TDeclaration_TypeSet.GetEnumElements: TDeclarationArray;
12211189
begin
1222-
if FEnumElements.Empty then
1223-
begin
1224-
Result := FItems.GetByClass(TDeclaration_EnumElement);
1225-
1226-
FEnumElements := Result;
1227-
end;
1190+
if FEnumElements.IsNull then
1191+
FEnumElements := FItems.GetByClass(TDeclaration_EnumElement);
12281192

12291193
Result := FEnumElements;
12301194
end;
@@ -1301,15 +1265,15 @@ function TDeclaration_Method.GetParamVarType(Index: Integer): TDeclaration;
13011265

13021266
function TDeclaration_Method.GetParamString: String;
13031267
begin
1304-
if FParamString.Empty then
1268+
if FParamString.IsNull then
13051269
FParamString := FItems.GetTextOfClassNoCommentsSingleLine(TDeclaration_ParamList);
13061270

13071271
Result := FParamString;
13081272
end;
13091273

13101274
function TDeclaration_Method.GetResultString: String;
13111275
begin
1312-
if FResultString.Empty then
1276+
if FResultString.IsNull then
13131277
FResultString := FItems.GetTextOfClassNoCommentsSingleLine(TDeclaration_MethodResult, ': ');
13141278

13151279
Result := FResultString;
@@ -1319,7 +1283,7 @@ function TDeclaration_Method.GetHeader: String;
13191283
var
13201284
Builder: TSimbaStringBuilder;
13211285
begin
1322-
if FHeader.Empty then
1286+
if FHeader.IsNull then
13231287
begin
13241288
if isFunc then Builder.Append('function') else
13251289
if isProc then Builder.Append('procedure') else
@@ -1346,16 +1310,14 @@ function TDeclaration_Method.GetParams: TDeclarationArray;
13461310
var
13471311
Decl: TDeclaration;
13481312
begin
1349-
Result := [];
1350-
1351-
if FParams.Empty then
1313+
if FParams.IsNull then
13521314
begin
1315+
FParams := [];
1316+
13531317
Decl := Items.GetByClassFirst(TDeclaration_ParamList);
13541318
if (Decl <> nil) then
13551319
for Decl in Decl.Items.GetByClass(TDeclaration_ParamGroup) do
1356-
Result.Add(Decl.Items.GetByClass(TDeclaration_Parameter));
1357-
1358-
FParams := Result;
1320+
FParams.Value.Add(Decl.Items.GetByClass(TDeclaration_Parameter));
13591321
end;
13601322

13611323
Result := FParams;
@@ -1370,7 +1332,7 @@ function TDeclaration_MethodOfType.GetHeader: String;
13701332
var
13711333
Builder: TSimbaStringBuilder;
13721334
begin
1373-
if FHeader.Empty then
1335+
if FHeader.IsNull then
13741336
begin
13751337
if isFunc then Builder.Append('function') else
13761338
if isProc then Builder.Append('procedure') else
@@ -1488,7 +1450,7 @@ function TCodeParser.GetHash: String;
14881450
Builder: TSimbaStringBuilder;
14891451
I: Integer;
14901452
begin
1491-
if FHash.Empty then
1453+
if FHash.IsNull then
14921454
begin
14931455
with Lexer.SaveDefines() do
14941456
Builder.Append(Defines + IntToStr(Stack));
@@ -2037,7 +1999,7 @@ procedure TCodeParser.Reset;
20371999
begin
20382000
inherited Reset();
20392001

2040-
FHash.Empty := True;
2002+
FHash.Clear();
20412003
FManagedItems.Clear(True);
20422004

20432005
FRoot.Items.Clear();

0 commit comments

Comments
 (0)