-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpaswrite.pp
1455 lines (1306 loc) · 38.6 KB
/
paswrite.pp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
This file is part of the Free Component Library
Pascal tree source file writer
Copyright (c) 2003 by
Areca Systems GmbH / Sebastian Guenther, [email protected]
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************************}
{$mode objfpc}
{$h+}
{$inline on}
unit PasWrite;
interface
uses StrUtils, SysUtils, Classes, PasTree;
type
EPasWriter = Class(Exception);
{ TPasWriter }
TPasWriterOption = (woNoImplementation, // Do not create implementation code.
woNoExternalClass, // Do not create classes as external
woNoExternalVar, // Do not declare external variables as external.
woNoExternalFunc, // Do not declare external functions as external.
woAddLineNumber, // Prefix line with generated line numbers in comment
woAddSourceLineNumber, // Prefix line with original source line numbers (when available) in comment
woForwardClasses, // Add forward definitions for all classes
woForceOverload // Force 'overload;' on overloads that are not marked as such.
);
TPasWriterOptions = Set of TPasWriterOption;
TPasWriter = class
private
FCurrentLineNumber : Integer;
FCurrentLine : String;
FExtraUnits: String;
FForwardClasses: TStrings;
FLineEnding: String;
FLineNumberWidth: Integer;
FOPtions: TPasWriterOptions;
FStream: TStream;
FIndentSize : Integer;
IsStartOfLine: Boolean;
FLineElement : TPasElement;
FIndentStep,
Indent,
CurDeclSection: string;
DeclSectionStack: TList;
FInImplementation : Boolean;
procedure SetForwardClasses(AValue: TStrings);
procedure SetIndentSize(AValue: Integer);
protected
procedure PrepareDeclSectionInStruct(const ADeclSection: string);
procedure MaybeSetLineElement(AElement: TPasElement);
function GetExpr(E: TPasExpr): String; virtual;
Function HasOption(aOption : TPasWriterOption) : Boolean; inline;
Function NotOption(aOption : TPasWriterOption) : Boolean; inline;
Function PostProcessLine(S : String) : String; virtual;
Function GetLineNumberComment : String; virtual;
Procedure ResetIndent;
procedure IncIndent;
procedure DecIndent;
procedure IncDeclSectionLevel;
procedure DecDeclSectionLevel;
procedure PrepareDeclSection(const ADeclSection: string);
procedure Add(const s: string);
procedure Add(const Fmt: string; Args : Array of const);
procedure AddLn(const s: string);overload;
procedure AddLn(const Fmt: string; Args : Array of const);overload;
procedure AddLn;overload;
procedure AddProcArgs(aList: TfpList); virtual;
public
constructor Create(AStream: TStream); virtual;
destructor Destroy; override;
procedure AddForwardClasses(aSection: TPasSection); virtual;
procedure WriteEnumType(AType: TPasEnumType); virtual;
procedure WriteElement(AElement: TPasElement);virtual;
procedure WriteType(AType: TPasType; Full : Boolean = True);virtual;
procedure WriteProgram(aModule : TPasProgram); virtual;
Procedure WriteLibrary(aModule : TPasLibrary); virtual;
Procedure WriteUnit(aModule : TPasModule); virtual;
procedure WriteModule(AModule: TPasModule); virtual;
procedure WriteSection(ASection: TPasSection); virtual;
procedure WriteUsesList(ASection: TPasSection); virtual;
procedure WriteClass(AClass: TPasClassType); virtual;
procedure WriteConst(AConst: TPasConst); virtual;
procedure WriteVariable(AVar: TPasVariable); virtual;
procedure WriteArgument(aArg: TPasArgument); virtual;
procedure WriteDummyExternalFunctions(aSection: TPasSection); virtual;
procedure WriteOverloadedProc(aProc : TPasOverloadedProc; ForceBody: Boolean = False; NamePrefix : String = ''); virtual;
Procedure WriteAliasType(AType : TPasAliasType); virtual;
Procedure WriteRecordType(AType : TPasRecordType); virtual;
Procedure WriteArrayType(AType : TPasArrayType); virtual;
procedure WriteProcType(AProc: TPasProcedureType); virtual;
procedure WriteProcDecl(AProc: TPasProcedure; ForceBody: Boolean = False; NamePrefix : String = ''); virtual;
procedure WriteProcImpl(AProc: TProcedureBody; IsAsm : Boolean = false); virtual;
procedure WriteProcImpl(AProc: TPasProcedureImpl); virtual;
procedure WriteProperty(AProp: TPasProperty); virtual;
procedure WriteImplBlock(ABlock: TPasImplBlock); virtual;
procedure WriteImplElement(AElement: TPasImplElement; AAutoInsertBeginEnd: Boolean); virtual;
procedure WriteImplCommand(ACommand: TPasImplCommand);virtual;
procedure WriteImplCommands(ACommands: TPasImplCommands); virtual;
procedure WriteImplIfElse(AIfElse: TPasImplIfElse); virtual;
procedure WriteImplForLoop(AForLoop: TPasImplForLoop); virtual;
procedure WriteImplWhileDo(aWhileDo : TPasImplWhileDo); virtual;
procedure WriteImplRepeatUntil(aRepeatUntil : TPasImplRepeatUntil); virtual;
procedure WriteImplTryFinallyExcept(aTry: TPasImplTry); virtual;
Procedure WriteImplRaise(aRaise : TPasImplRaise); virtual;
Procedure WriteImplAssign(aAssign : TPasImplAssign); virtual;
Procedure WriteImplSimple(aSimple: TPasImplSimple); virtual;
Procedure WriteImplExceptOn(aOn : TPasImplExceptOn); virtual;
//
procedure wrt(const s: string); deprecated ;
procedure wrtln(const s: string);overload; deprecated ;
procedure wrtln;overload; deprecated ;
property Stream: TStream read FStream;
Published
Property Options : TPasWriterOptions Read FOPtions Write FOptions;
Property IndentSize : Integer Read FIndentSize Write SetIndentSize;
Property LineEnding : String Read FLineEnding Write FLineEnding;
Property ExtraUnits : String Read FExtraUnits Write FExtraUnits;
Property ForwardClasses : TStrings Read FForwardClasses Write SetForwardClasses;
Property LineNumberWidth : Integer Read FLineNumberWidth Write FLineNumberWidth;
end;
procedure WritePasFile(AElement: TPasElement; const AFilename: string);overload;
procedure WritePasFile(AElement: TPasElement; AStream: TStream);overload;
implementation
type
PDeclSectionStackElement = ^TDeclSectionStackElement;
TDeclSectionStackElement = record
LastDeclSection, LastIndent: string;
end;
constructor TPasWriter.Create(AStream: TStream);
begin
FStream := AStream;
IndentSize:=2;
IsStartOfLine := True;
DeclSectionStack := TList.Create;
FForwardClasses:=TStringList.Create;
FLineEnding:=sLineBreak;
FLineNumberWidth:=4;
end;
destructor TPasWriter.Destroy;
var
i: Integer;
El: PDeclSectionStackElement;
begin
for i := 0 to DeclSectionStack.Count - 1 do
begin
El := PDeclSectionStackElement(DeclSectionStack[i]);
Dispose(El);
end;
DeclSectionStack.Free;
FForwardClasses.Free;
inherited Destroy;
end;
procedure TPasWriter.Add(const s: string);
begin
if IsStartOfLine then // We cannot check for empty, Indent may be empty
begin
Inc(FCurrentLineNumber);
IsStartOfLine := False;
end;
if (FCurrentLine='') and (S<>'') and (Length(Indent)>0) then
FCurrentLine:=FCurrentLine+Indent;
FCurrentLine:=FCurrentLine+S;
end;
procedure TPasWriter.Add(const Fmt: string; Args: array of const);
begin
Add(Format(Fmt,Args));
end;
procedure TPasWriter.AddLn(const s: string);
Var
L : String;
begin
Add(s);
L:=PostProcessLine(FCurrentLine);
Stream.Write(L[1],Length(L));
Stream.Write(FLineEnding[1],Length(FLineEnding));
IsStartOfLine:=True;
FCurrentLine:='';
FLineElement:=Nil;
end;
procedure TPasWriter.AddLn(const Fmt: string; Args: array of const);
begin
AddLn(Format(Fmt,Args));
end;
procedure TPasWriter.AddLn;
begin
AddLn('');
end;
procedure TPasWriter.MaybeSetLineElement(AElement : TPasElement);
begin
If FLineElement=Nil then
FLineElement:=AElement;
end;
procedure TPasWriter.WriteElement(AElement: TPasElement);
begin
MaybeSetLineElement(AElement);
if AElement.InheritsFrom(TPasModule) then
WriteModule(TPasModule(AElement))
else if AElement.InheritsFrom(TPasSection) then
WriteSection(TPasSection(AElement))
else if AElement.ClassType.InheritsFrom(TPasProperty) then
WriteProperty(TPasProperty(AElement))
else if AElement.InheritsFrom(TPasConst) then
WriteConst(TPasConst(AElement)) // Must be before variable
else if AElement.InheritsFrom(TPasVariable) then
WriteVariable(TPasVariable(AElement))
else if AElement.InheritsFrom(TPasArgument) then
WriteArgument(TPasArgument(AElement))
else if AElement.InheritsFrom(TPasType) then
WriteType(TPasType(AElement))
else if AElement.InheritsFrom(TPasOverloadedProc) then
WriteOverloadedProc(TPasOverloadedProc(AElement))
else if AElement.InheritsFrom(TPasProcedureImpl) then // This one must come before TProcedureBody/TPasProcedure
WriteProcImpl(TPasProcedureImpl(AElement))
else if AElement.InheritsFrom(TPasProcedure) then
WriteProcDecl(TPasProcedure(AElement))
else if AElement.InheritsFrom(TProcedureBody) then
WriteProcImpl(TProcedureBody(AElement))
else if AElement.InheritsFrom(TPasImplCommand) or AElement.InheritsFrom(TPasImplCommands) then
WriteImplElement(TPasImplElement(AElement),false)
else
raise EPasWriter.CreateFmt('Writing not implemented for %s nodes',[AElement.ElementTypeName]);
end;
procedure TPasWriter.WriteEnumType(AType: TPasEnumType);
begin
Add(Atype.GetDeclaration(true));
end;
procedure TPasWriter.WriteType(AType: TPasType; Full : Boolean = True);
begin
MaybeSetLineElement(AType);
if Full and (AType.Parent is TPasSection) then
PrepareDeclSection('type');
if AType.ClassType = TPasUnresolvedTypeRef then
Add(AType.Name)
else if AType.ClassType.InheritsFrom(TPasClassType) then
WriteClass(TPasClassType(AType))
else if AType.ClassType = TPasEnumType then
WriteEnumType(TPasEnumType(AType))
else if AType is TPasProcedureType then
WriteProcType(TPasProcedureType(AType))
else if AType is TPasArrayType then
WriteArrayType(TPasArrayType(AType))
else if AType is TPasRecordType then
WriteRecordType(TPasRecordType(AType))
else if AType is TPasAliasType then
WriteAliasType(TPasAliasType(AType))
else if AType is TPasPointerType then
Add(AType.GetDeclaration(true))
else
raise EPasWriter.Create('Writing not implemented for ' +
AType.ElementTypeName + ' nodes');
if Full then
AddLn(';');
end;
procedure TPasWriter.WriteProgram(aModule: TPasProgram);
Var
S : String;
begin
S:='';
if aModule.Name<>'' then
S:=Format('program %s',[aModule.Name]);
if (S<>'') then
begin
If AModule.InputFile<>'' then
begin
S:=S+'('+aModule.InputFile;
if aModule.OutPutFile<>'' then
S:=S+','+aModule.OutPutFile;
S:=S+')';
end;
AddLn(S+';');
AddLn;
end;
if HasOption(woNoImplementation) then
begin
Addln('{$HINTS OFF}');
Addln('{$WARNINGS OFF}');
Addln('{$NOTES OFF}');
end;
if Assigned(aModule.ProgramSection) then
WriteSection(aModule.ProgramSection);
if Assigned(AModule.InitializationSection) then
begin
PrepareDeclSection('');
AddLn;
AddLn('begin');
IncIndent;
if NotOption(woNoImplementation) then
WriteImplBlock(AModule.InitializationSection);
DecIndent;
end;
Addln('end.');
end;
procedure TPasWriter.WriteLibrary(aModule: TPasLibrary);
Var
S : String;
begin
S:='';
if aModule.Name<>'' then
S:=Format('library %s',[aModule.Name]);
if (S<>'') then
begin
If AModule.InputFile<>'' then
begin
S:=S+'('+aModule.InputFile;
if aModule.OutPutFile<>'' then
S:=S+','+aModule.OutPutFile;
S:=S+')';
end;
AddLn(S+';');
AddLn;
end;
if HasOption(woNoImplementation) then
begin
Addln('{$HINTS OFF}');
Addln('{$WARNINGS OFF}');
Addln('{$NOTES OFF}');
end;
if Assigned(AModule.InitializationSection) then
begin
PrepareDeclSection('');
AddLn;
AddLn('begin');
IncIndent;
if NotOption(woNoImplementation) then
WriteImplBlock(AModule.InitializationSection);
DecIndent;
end;
Addln('end.');
end;
procedure TPasWriter.WriteDummyExternalFunctions(aSection : TPasSection);
Function IsExt(P : TPasProcedure; AllowConstructor : Boolean) : Boolean;
begin
Result:=Assigned(P.LibrarySymbolName) or Assigned(P.LibraryExpr);
if (Not Result) Then
Result:=(AllowConstructor and (P is TPasConstructor));
end;
Procedure DoCheckElement(E : TPasElement; Force : Boolean; Prefix: String);
Var
P : TPasProcedure;
PP : TPasOverloadedProc;
I : Integer;
begin
if (E is TPasProcedure) then
begin
P:=E as TPasProcedure;
if Force or IsExt(P,False) then
WriteProcDecl(P,True,Prefix)
end
else if (E is TPasOverloadedProc) then
begin
PP:=(E as TPasOverloadedProc);
For I:=0 to PP.Overloads.Count-1 do
begin
P:=TPasProcedure(PP.Overloads[I]);
if Force or IsExt(P,False) then
WriteProcDecl(P,True,Prefix)
end
end;
end;
Var
I,J : Integer;
E,M : TPasElement;
C : TPasClassType;
begin
Addln;
Addln('// Dummy implementations for externals');
Addln;
For I:=0 to aSection.Declarations.Count-1 do
begin
E:=TPasElement(aSection.Declarations[i]);
DoCheckElement(E,False,'');
if (E is TPasClassType) then
begin
C:=E as TPasClassType;
if (C.ExternalName<>'') then
For J:=0 to C.Members.Count-1 do
begin
M:=TPasElement(C.members[J]);
DoCheckElement(M,True,C.Name+'.');
end;
end;
end;
Addln;
Addln('// end of dummy implementations');
Addln;
end;
procedure TPasWriter.AddForwardClasses(aSection : TPasSection);
Var
I : Integer;
CN : String;
begin
if Not Assigned(aSection.Classes) or (aSection.Classes.Count=0) then
exit;
PrepareDeclSection('type');
For I:=0 to aSection.Classes.Count-1 do
begin
CN:=TPasElement(aSection.Classes[i]).Name;
if (FForwardClasses.Count=0) or (ForwardClasses.IndexOf(CN)<>-1) then
Addln('%s = class;',[CN]);
end;
end;
procedure TPasWriter.WriteUnit(aModule: TPasModule);
begin
AddLn('unit ' + AModule.Name + ';');
if Assigned(AModule.GlobalDirectivesSection) then
begin
AddLn;
WriteImplElement(AModule.GlobalDirectivesSection,false);
end;
AddLn;
AddLn('interface');
AddLn;
WriteSection(AModule.InterfaceSection);
ResetIndent;
AddLn;
AddLn;
AddLn('implementation');
FInImplementation:=True;
if HasOption(woNoImplementation) then
begin
Addln('{$HINTS OFF}');
Addln('{$WARNINGS OFF}');
Addln('{$NOTES OFF}');
end;
if hasOption(woNoExternalFunc) then
WriteDummyExternalFunctions(AModule.InterfaceSection);
if Assigned(AModule.ImplementationSection) then
begin
AddLn;
WriteSection(AModule.ImplementationSection);
end;
AddLn;
if NotOption(woNoImplementation) then
begin
PrepareDeclSection('');
if Assigned(AModule.InitializationSection) then
begin
AddLn('initialization');
IncIndent;
WriteImplBlock(AModule.InitializationSection);
DecIndent;
end;
if Assigned(AModule.FinalizationSection) then
begin
AddLn('finalization');
IncIndent;
WriteImplBlock(AModule.FinalizationSection);
DecIndent;
end;
end;
AddLn('end.');
end;
procedure TPasWriter.WriteModule(AModule: TPasModule);
begin
FInImplementation:=False;;
if aModule is TPasProgram then
WriteProgram(TPasProgram(aModule))
else if aModule is TPasLibrary then
WriteLibrary(TPasLibrary(aModule))
else
WriteUnit(aModule)
end;
procedure TPasWriter.WriteUsesList(ASection: TPasSection);
Const
UnitSeps = [',',';',' '];
Var
C : Integer;
function AllowUnit(S : String) : Boolean;
begin
Result:=Not SameText(S,'System');
end;
Procedure AddUnit(Const aName : String; AUnitFile : TPasExpr);
begin
if c > 0 then
Add(', ')
else
Add('uses ');
Add(AName);
if (AUnitFile<>Nil) then
Add(' in '+GetExpr(AUnitFile));
Inc(c);
end;
Var
I : integer;
u : string;
begin
C:=0;
if ASection.UsesList.Count>0 then
begin
For I:=1 to WordCount(ExtraUnits,UnitSeps) do
begin
u:=Trim(ExtractWord(1,ExtraUnits,UnitSeps));
if (U<>'') then
AddUnit(U,Nil);
end;
if length(ASection.UsesClause)=ASection.UsesList.Count then
begin
for i := 0 to length(ASection.UsesClause)-1 do
if AllowUnit(ASection.UsesClause[i].Name) then
AddUnit(ASection.UsesClause[i].Name,ASection.UsesClause[i].InFilename);
end
else
for i := 0 to ASection.UsesList.Count - 1 do
if AllowUnit(TPasElement(ASection.UsesList[i]).Name) then
AddUnit(TPasElement(ASection.UsesList[i]).Name,Nil);
if C>0 then
begin
AddLn(';');
AddLn;
end;
end;
end;
procedure TPasWriter.WriteSection(ASection: TPasSection);
var
i: Integer;
begin
WriteUsesList(aSection);
CurDeclSection := '';
if HasOption(woForwardClasses) then
begin
AddForwardClasses(ASection);
AddLn;
end;
for i := 0 to ASection.Declarations.Count - 1 do
WriteElement(TPasElement(ASection.Declarations[i]));
end;
procedure TPasWriter.WriteClass(AClass: TPasClassType);
var
i: Integer;
Member, LastMember: TPasElement;
InterfacesListPrefix: string;
LastVisibility, CurVisibility: TPasMemberVisibility;
function ForceVisibility: boolean;
begin
Result := (LastMember <> nil) and
// variables can't be declared directly after methods nor properties
// (visibility section or var keyword is required)
((Member is TPasVariable) and not (Member is TPasProperty)) and not (LastMember is TPasVariable);
end;
begin
PrepareDeclSection('type');
Addln;
MaybeSetLineElement(AClass);
Add(AClass.Name + ' = ');
if AClass.IsPacked then
Add('packed '); // 12/04/04 - Dave - Added
case AClass.ObjKind of
okObject: Add('object');
okClass: Add('class');
okInterface: Add('interface');
okRecordHelper: Add('record helper');
okClassHelper: Add('class helper');
end;
if AClass.IsForward then
exit;
if (AClass.ObjKind=okClass) and (ACLass.ExternalName<>'') and NotOption(woNoExternalClass) then
Add(' external name ''%s'' ',[AClass.ExternalName]);
if Assigned(AClass.AncestorType) then
Add('(' + AClass.AncestorType.Name);
if AClass.Interfaces.Count > 0 then
begin
if Assigned(AClass.AncestorType) then
InterfacesListPrefix:=', '
else
InterfacesListPrefix:='(';
Add(InterfacesListPrefix + TPasType(AClass.Interfaces[0]).Name);
for i := 1 to AClass.Interfaces.Count - 1 do
Add(', ' + TPasType(AClass.Interfaces[i]).Name);
end;
if Assigned(AClass.AncestorType) or (AClass.Interfaces.Count > 0) then
AddLn(')')
else
AddLn;
if AClass.ObjKind = okInterface then
if Assigned(AClass.GUIDExpr) then
AddLn('['+AClass.InterfaceGUID+']');
IncIndent;
IncDeclSectionLevel;
LastVisibility := visDefault;
LastMember := nil;
for i := 0 to AClass.Members.Count - 1 do
begin
Member := TPasElement(AClass.Members[i]);
CurVisibility := Member.Visibility;
if (CurVisibility <> LastVisibility) or ForceVisibility then
begin
DecIndent;
case CurVisibility of
visPrivate: AddLn('private');
visProtected: AddLn('protected');
visPublic: AddLn('public');
visPublished: AddLn('published');
visAutomated: AddLn('automated');
end;
IncIndent;
LastVisibility := CurVisibility;
CurDeclSection := '';
end;
WriteElement(Member);
LastMember := Member;
end;
DecDeclSectionLevel;
DecIndent;
Add('end');
end;
procedure TPasWriter.WriteConst(AConst: TPasConst);
begin
PrepareDeclSection('const');
AddLn(AConst.GetDeclaration(True)+';');
end;
procedure TPasWriter.WriteVariable(AVar: TPasVariable);
var
LParentIsClassOrRecord: boolean;
begin
LParentIsClassOrRecord:= (AVar.Parent.ClassType = TPasClassType) or
(AVar.Parent.ClassType = TPasRecordType);
if not LParentIsClassOrRecord then
PrepareDeclSection('var')
// handle variables in classes/records
else if vmClass in AVar.VarModifiers then
PrepareDeclSectionInStruct('class var')
else if CurDeclSection<>'' then
PrepareDeclSectionInStruct('var');
Add(AVar.Name + ': ');
if Not Assigned(AVar.VarType) then
Raise EWriteError.CreateFmt('No type for variable %s',[AVar.Name]);
WriteType(AVar.VarType,False);
if (AVar.AbsoluteLocation<>'') then
Add(' absolute %s',[AVar.AbsoluteLocation])
else if (aVar.LibraryName<>Nil) or Assigned (aVar.ExportName) then
begin
if LParentIsClassOrRecord then
begin
if NotOption(woNoExternalClass) then
Add('; external name ''%s''',[aVar.ExportName.GetDeclaration(true)]);
end
else if NotOption(woNoExternalVar) then
begin
Add('; external ');
if (AVar.LibraryName<>Nil) then
Add('%s ',[AVar.LibraryName.GetDeclaration(true)]);
Add('name %s',[aVar.ExportName.GetDeclaration(true)]);
end;
end;
if Not LParentIsClassOrRecord then
if Assigned(aVar.Expr) then
Add(' = '+aVar.Expr.GetDeclaration(true));
AddLn(';');
end;
procedure TPasWriter.WriteArgument(aArg: TPasArgument);
begin
if (aArg.Access<>argDefault) then
Add(AccessNames[aArg.Access]+' ');
Add(aArg.Name+' : ');
WriteType(aArg.ArgType,False);
end;
procedure TPasWriter.WriteOverloadedProc(aProc: TPasOverloadedProc; ForceBody: Boolean = False; NamePrefix : String = '');
Var
I : integer;
begin
For I:=0 to aProc.Overloads.Count-1 do
begin
if HasOption(woForceOverload) then
TPasProcedure(aProc.Overloads[i]).AddModifier(pmOverload);
WriteProcDecl(TPasElement(aProc.Overloads[i]) as TPasProcedure,ForceBody,NamePrefix);
end;
end;
procedure TPasWriter.WriteAliasType(AType: TPasAliasType);
begin
If AType.Parent is TPasSection then
Add(AType.GetDeclaration(true))
else
Add(AType.Name)
end;
procedure TPasWriter.WriteRecordType(AType: TPasRecordType);
Var
S : TStrings;
I : Integer;
begin
S:=TStringList.Create;
try
S.Text:=AType.GetDeclaration(true);
For I:=0 to S.Count-2 do
AddLn(S[i]);
Add(S[S.Count-1]);
finally
S.Free;
end;
end;
procedure TPasWriter.WriteArrayType(AType: TPasArrayType);
begin
Add(AType.GetDeclaration(true));
end;
procedure TPasWriter.WriteProcType(AProc: TPasProcedureType);
begin
Add(TPasProcedureType(AProc).GetDeclaration(true));
if TPasProcedureType(AProc).CallingConvention<>ccDefault then
Add('; '+cCallingConventions[TPasProcedureType(AProc).CallingConvention]);
end;
procedure TPasWriter.WriteProcDecl(AProc: TPasProcedure; ForceBody : Boolean = False; NamePrefix : String = '');
Var
AddExternal : boolean;
IsImpl : Boolean;
begin
IsImpl:=AProc.Parent is TPasSection;
if IsImpl then
PrepareDeclSection('');
if Not IsImpl then
IsImpl:=FInImplementation;
Add(AProc.TypeName + ' ' + NamePrefix+AProc.Name);
if Assigned(AProc.ProcType) and (AProc.ProcType.Args.Count > 0) then
AddProcArgs(AProc.ProcType.Args) ;
if Assigned(AProc.ProcType) and
(AProc.ProcType.ClassType = TPasFunctionType) then
begin
Add(': ');
WriteType(TPasFunctionType(AProc.ProcType).ResultEl.ResultType,False);
end;
Add(';');
if not IsImpl then
begin
if AProc.IsVirtual then
Add(' virtual;');
if AProc.IsDynamic then
Add(' dynamic;');
if AProc.IsAbstract then
Add(' abstract;');
if AProc.IsOverride then
Add(' override;');
if AProc.IsReintroduced then
Add(' reintroduce;');
if AProc.IsStatic then
Add(' static;');
end;
if pmAssembler in AProc.Modifiers then
Add(' assembler;');
if AProc.IsOverload then
Add(' overload;');
if AProc.CallingConvention<>ccDefault then
Add(' '+cCallingConventions[AProc.CallingConvention]+';');
If Assigned(AProc.LibraryExpr) or Assigned(AProc.LibrarySymbolName) then
begin
if AProc.Parent is TPasClassType then
AddExternal:=NotOption(woNoExternalClass)
else
AddExternal:=NotOption(woNoExternalFunc);
if AddExternal then
begin
add('external');
if Assigned(AProc.LibraryExpr) then
Add(' '+GetExpr(AProc.LibraryExpr));
if Assigned(AProc.LibrarySymbolName) then
Add(' name '+GetExpr(AProc.LibrarySymbolName));
Add(';');
end;
end;
AddLn;
if Assigned(AProc.Body) then
WriteProcImpl(AProc.Body,pmAssembler in AProc.Modifiers)
else if ForceBody then
begin
Addln('');
Addln('begin');
AddLn('end;');
Addln('');
end;
end;
procedure TPasWriter.AddProcArgs(aList : TfpList);
Var
I : Integer;
A : TPasArgument;
begin
Add('(');
If Assigned(aList) then
for i := 0 to Alist.Count - 1 do
begin
A:= TPasArgument(AList[i]);
if i > 0 then
Add('; ');
Add(AccessNames[A.Access]+A.Name);
if Assigned(A.ArgType) then
begin
Add(': ');
WriteType(A.ArgType,False);
end;
if A.Value <> '' then
Add(' = ' + A.Value);
end;
Add(')');
end;
// For backwards compatibility
procedure TPasWriter.WriteProcImpl(AProc: TPasProcedureImpl);
var
i: Integer;
E,PE :TPasElement;
begin
PrepareDeclSection('');
if AProc.IsClassMethod then
Add('class ');
Add(AProc.TypeName + ' ');
if AProc.Parent.ClassType = TPasClassType then
Add(AProc.Parent.Name + '.');
Add(AProc.Name);
if Assigned(AProc.ProcType) and (AProc.ProcType.Args.Count > 0) then
AddProcArgs(AProc.ProcType.Args);
if Assigned(AProc.ProcType) and
(AProc.ProcType.ClassType = TPasFunctionType) then
begin
Add(': ');
WriteType(TPasFunctionType(AProc.ProcType).ResultEl.ResultType,False);
end;
AddLn(';');
IncDeclSectionLevel;
for i := 0 to AProc.Locals.Count - 1 do
begin
E:=TPasElement(AProc.Locals[i]);
if E.InheritsFrom(TPasProcedureImpl) then
begin
IncIndent;
if (i = 0) or not PE.InheritsFrom(TPasProcedureImpl) then
Addln;
end;
WriteElement(E);
if E.InheritsFrom(TPasProcedureImpl) then
DecIndent;
PE:=E;
end;
DecDeclSectionLevel;
AddLn('begin');
IncIndent;
if Assigned(AProc.Body) then
WriteImplBlock(AProc.Body);
DecIndent;
AddLn('end;');
AddLn;
end;
procedure TPasWriter.WriteProcImpl(AProc: TProcedureBody; IsAsm : Boolean = false);
var
i: Integer;
El,PEl : TPasElement;
begin
PrepareDeclSection('');
If NotOption(woNoImplementation) then
begin
IncDeclSectionLevel;
PEl:=Nil;
for i := 0 to aProc.Declarations.Count - 1 do
begin
El:=TPasElement(aProc.Declarations[i]);
if El.InheritsFrom(TPasProcedureImpl) then
begin
IncIndent;
if (PEL=Nil) or not PEL.InheritsFrom(TPasProcedureImpl) then
AddLn;
end;
WriteElement(El);
if El.InheritsFrom(TPasProcedureImpl) then
DecIndent;
Pel:=El;
end;
DecDeclSectionLevel;
end;
if IsAsm then
AddLn('asm')
else
AddLn('begin');
If NotOption(woNoImplementation) then
begin
IncIndent;
if Assigned(AProc.Body) then
WriteImplBlock(AProc.Body);
DecIndent;
end;
AddLn('end;');
AddLn;
end;
procedure TPasWriter.WriteProperty(AProp: TPasProperty);
var
i: Integer;
begin
if AProp.IsClass then
Add('class ');
Add('property ' + AProp.Name);
if AProp.Args.Count > 0 then
begin
Add('[');
for i := 0 to AProp.Args.Count - 1 do
begin
if I>0 then Add(',');
WriteArgument(TPasArgument(AProp.Args[i]));
end;
// !!!: Create WriteArgument method and call it here
Add(']');
end;
if Assigned(AProp.VarType) then
begin