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
|
{
Copyright (C) 2017 - 2020 by Michael Van Canneyt michael@freepascal.org
pas2js Delphi stub generator - component
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.
}
unit stubcreator;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, strutils, inifiles, pscanner, pparser, pastree, iostream, paswrite;
type
{ We have to override abstract TPasTreeContainer methods }
TSimpleEngine = class(TPasTreeContainer)
public
function CreateElement(AClass: TPTreeElement; const AName: String;
AParent: TPasElement; AVisibility: TPasMemberVisibility;
const ASourceFilename: String; ASourceLinenumber: Integer): TPasElement;
override;
function FindElement(const AName: String): TPasElement; override;
end;
TWriteCallBack = Procedure (Data : Pointer; AFileData : PAnsiChar; AFileDataLen: Int32); stdcall;
TWriteEvent = Procedure(AFileData : String) of object;
TUnitAliasCallBack = Function (Data: Pointer; AUnitName: PAnsiChar;
var AUnitNameMaxLen: Int32): boolean; {$IFDEF UseCDecl}cdecl{$ELSE}stdcall{$ENDIF};
{ TStubCreator }
TStubCreator = Class(TComponent)
private
FConfigFile: String;
FHeaderStream: TStream;
FIncludePaths: TStrings;
FInputFile: String;
FOnUnitAliasData: Pointer;
FOnWrite: TWriteEvent;
FOnWriteCallBack: TWriteCallBack;
FOutputFile: String;
FDefines : TStrings;
FOptions: TPasWriterOptions;
FLineNumberWidth,
FIndentSize : Integer;
FExtraUnits : String;
FForwardClasses : String;
FHeaderFile : String;
FOutputStream: TStream;
FWriteStream : TStringStream;
FCallBackData : Pointer;
FLastErrorClass : String;
FLastError : String;
FOnUnitAlias : TUnitAliasCallBack;
procedure SetDefines(AValue: TStrings);
procedure SetIncludePaths(AValue: TStrings);
procedure SetOnWrite(AValue: TWriteEvent);
procedure SetWriteCallback(AValue: TWriteCallBack);
function CheckUnitAlias(const AUnitName: String): String;
Protected
procedure DoExecute;virtual;
Procedure DoWriteEvent; virtual;
procedure ReadConfig(const aFileName: String); virtual;
procedure ReadConfig(const aIni: TIniFile); virtual;
procedure WriteModule(M: TPasModule); virtual;
function GetModule: TPasModule; virtual;
Function MaybeGetFileStream(AStream : TStream; Const AFileName : String; aFileMode : Word) : TStream; virtual;
Public
Constructor Create(AOwner : TComponent); override;
Destructor Destroy; override;
Function Execute: Boolean;
Procedure GetLastError(Out AError,AErrorClass : String);
// Streams take precedence over filenames. They will be freed on destroy!
// OutputStream can be used combined with write callbacks.
Property OutputStream : TStream Read FOutputStream Write FOutputStream;
Property HeaderStream : TStream Read FHeaderStream Write FHeaderStream;
Property OnUnitAlias: TUnitAliasCallBack read FOnUnitAlias Write FOnUnitAlias;
Property OnUnitAliasData : Pointer Read FOnUnitAliasData Write FOnUnitAliasData;
Property OnWriteCallBack : TWriteCallBack Read FOnWriteCallBack Write SetWriteCallback;
Property CallbackData : Pointer Read FCallBackData Write FCallBackData;
Property ExtraUnits : String Read FExtraUnits write FExtraUnits;
Published
Property Defines : TStrings Read FDefines Write SetDefines;
Property ConfigFileName : String Read FConfigFile Write FConfigFile;
Property InputFileName : String Read FInputFile write FInputFile;
Property OutputFileName : String Read FOutputFile write FOutputFile;
Property HeaderFileName : String Read FHeaderFile write FHeaderFile;
Property ForwardClasses : String Read FForwardClasses write FForwardClasses;
Property IncludePaths : TStrings Read FIncludePaths Write SetIncludePaths;
Property OnWrite : TWriteEvent Read FOnWrite Write SetOnWrite;
end;
Implementation
uses Math;
ResourceString
SErrNoDestGiven = 'No destination file specified.';
SErrNoSourceParsed = 'Parsing produced no file.';
procedure TStubCreator.SetDefines(AValue: TStrings);
begin
if FDefines=AValue then Exit;
FDefines.Assign(AValue);
end;
procedure TStubCreator.SetIncludePaths(AValue: TStrings);
begin
if FIncludePaths=AValue then Exit;
FIncludePaths.Assign(AValue);
end;
procedure TStubCreator.SetOnWrite(AValue: TWriteEvent);
begin
if FOnWrite=AValue then Exit;
FOnWrite:=AValue;
FreeAndNil(FWriteStream);
if Assigned(AValue) then
FWriteStream:=TStringStream.Create('');
end;
procedure TStubCreator.SetWriteCallback(AValue: TWriteCallBack);
begin
if FOnWriteCallBack=AValue then Exit;
FOnWriteCallBack:=AValue;
FreeAndNil(FWriteStream);
if Assigned(AValue) then
FWriteStream:=TStringStream.Create('');
end;
function TStubCreator.CheckUnitAlias(const AUnitName: String): String;
const
MAX_UNIT_NAME_LENGTH = 255;
var
UnitMaxLenthName: Integer;
begin
Result := AUnitName;
UnitMaxLenthName := Max(MAX_UNIT_NAME_LENGTH, Result.Length);
SetLength(Result, UnitMaxLenthName);
if FOnUnitAlias(OnUnitAliasData, @Result[1], UnitMaxLenthName) then
Result := LeftStr(PChar(Result), UnitMaxLenthName);
end;
procedure TStubCreator.DoWriteEvent;
Var
S : String;
begin
If Assigned(FOnWrite) then
FOnWrite(FWriteStream.DataString);
if Assigned(FOnWriteCallBack) then
begin
S:=FWriteStream.DataString;
FOnWriteCallBack(FCallBackData,PChar(S),Length(S));
end;
end;
{ TStubCreator }
procedure TStubCreator.ReadConfig(const aFileName: String);
Var
ini : TMemIniFile;
begin
ini:=TMemIniFile.Create(AFileName);
try
ReadConfig(Ini);
finally
Ini.Free;
end;
end;
procedure TStubCreator.ReadConfig(const aIni: TIniFile);
Const
DelChars = [',',' '];
Var
O : TPaswriterOptions;
S : String;
I : Integer;
begin
O:=[];
With aIni do
begin
if ReadBool('Config','addlinenumber',False) then
Include(O,woAddLineNumber);
if ReadBool('Config','addsourcelinenumber',False) then
Include(O,woAddLineNumber);
FOptions:=FOptions+O;
InputFilename:=ReadString('config','input',InputFilename);
OutputFilename:=ReadString('config','output',OutputFilename);
HeaderFilename:=ReadString('config','header',HeaderFilename);
FIndentSize:=ReadInteger('config','indentsize',FIndentSize);
FLineNumberWidth:=ReadInteger('config','linenumberwidth',FLineNumberWidth);
FExtraUnits:=ReadString('config','extra',FExtraUnits);
FForwardClasses:=ReadString('config','forwardclasses',FForwardClasses);
S:=ReadString('config','defines','');
if (S<>'') then
For I:=1 to WordCount(S,DelChars) do
FDefines.Add(UpperCase(ExtractWord(I,S,DelChars)));
S:=ReadString('config','includepaths','');
if (S<>'') then
For I:=1 to WordCount(S,[',',';']) do
FIncludePaths.Add(ExtractWord(I,S,[',',';']));
end;
if (FForwardClasses<>'') or (FForwardClasses='all') then
Include(O,woForwardClasses);
end;
function TStubCreator.Execute: Boolean;
begin
FLastErrorClass:='';
FLastError:='';
Result := False;
if Defines.IndexOf('MakeStub')=-1 then
Try
DoExecute;
Result := True;
except
On E : Exception do
begin
FLastErrorClass:=E.Classname;
FLastError:=E.Message;
end;
end;
end;
procedure TStubCreator.GetLastError(out AError, AErrorClass: String);
begin
AError:=FLastError;
AErrorClass:=FLastErrorClass;
end;
procedure TStubCreator.DoExecute;
Var
M : TPasModule;
begin
If (ConfigFileName<>'') then
ReadConfig(ConfigFileName);
if InputFilename = '' then
raise Exception.Create(SErrNoSourceGiven);
if (OutputFilename = '') and (FoutputStream=Nil) and (FWriteStream=Nil) then
raise Exception.Create(SErrNoDestGiven);
if CompareText(ForwardClasses,'all')=0 then
begin
Include(Foptions,woForwardClasses);
ForwardClasses:='';
end
else if (ForwardClasses<>'') then
Include(Foptions,woForwardClasses);
Include(Foptions,woForceOverload);
M:=GetModule;
if M=Nil then
raise Exception.Create(SErrNoSourceParsed);
try
WriteModule(M);
finally
M.Free;
end;
end;
{ TSimpleEngine }
function TSimpleEngine.CreateElement(AClass: TPTreeElement; const AName: String;
AParent: TPasElement; AVisibility: TPasMemberVisibility;
const ASourceFilename: String; ASourceLinenumber: Integer): TPasElement;
begin
Result := AClass.Create(AName, AParent);
Result.Visibility := AVisibility;
Result.SourceFilename := ASourceFilename;
Result.SourceLinenumber := ASourceLinenumber;
end;
function TSimpleEngine.FindElement(const AName: String): TPasElement;
begin
{ dummy implementation, see TFPDocEngine.FindElement for a real example }
Result := nil;
if AName<>'' then ; // Keep compiler happy
end;
function TStubCreator.GetModule: TPasModule;
Var
SE : TSimpleEngine;
FileResolver: TFileResolver;
Parser: TPasParser;
Scanner: TPascalScanner;
var
s: String;
begin
Result := nil;
FileResolver := nil;
Scanner := nil;
Parser := nil;
SE:=TSimpleEngine.Create;
try
// File resolver
FileResolver := TFileResolver.Create;
FileResolver.UseStreams:=True;
FileResolver.AddIncludePath(ExtractFilePath(InputFileName));
For S in FIncludePaths do
FileResolver.AddIncludePath(S);
// Scanner
Scanner := TPascalScanner.Create(FileResolver);
Scanner.Options:=[po_AsmWhole,po_KeepClassForward,po_ExtConstWithoutExpr];
SCanner.LogEvents:=SE.ScannerLogEvents;
SCanner.OnLog:=SE.Onlog;
For S in FDefines do
Scanner.AddDefine(S);
if FDefines.IndexOf('MAKESTUB')=-1 then
Scanner.AddDefine('MAKESTUB');
Scanner.OpenFile(InputFilename);
// Parser
Parser:=TPasParser.Create(Scanner, FileResolver, SE);
Parser.LogEvents:=SE.ParserLogEvents;
Parser.OnLog:=SE.Onlog;
Parser.Options:=Parser.Options+[po_AsmWhole,po_delphi,po_KeepClassForward,po_ExtConstWithoutExpr,po_AsyncProcs];
Parser.ParseMain(Result);
finally
Parser.Free;
Scanner.Free;
FileResolver.Free;
SE.Free;
end;
end;
function TStubCreator.MaybeGetFileStream(AStream: TStream;
const AFileName: String; aFileMode: Word): TStream;
begin
If Assigned(AStream) then
Result:=AStream
else if (AFileName<>'') then
Result:=TFileStream.Create(AFileName,aFileMode)
else
Result:=Nil;
end;
constructor TStubCreator.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FDefines:=TStringList.Create;
FIncludePaths:=TStringList.Create;
FLineNumberWidth:=4;
FIndentSize:=2;
FExtraUnits:='';
FOptions:=[woNoImplementation,woNoExternalClass,woNoExternalVar,woNoExternalFunc,woNoAsm,woSkipPrivateExternals,woAlwaysRecordHelper,woSkipHints];
end;
destructor TStubCreator.Destroy;
begin
FreeAndNil(FWriteStream);
FreeAndNil(FOutputStream);
FreeAndNil(FHeaderStream);
FreeAndNil(FIncludePaths);
FreeAndNil(FDefines);
inherited Destroy;
end;
procedure TStubCreator.WriteModule(M: TPasModule);
Var
F,H : TStream;
W : TPasWriter;
begin
W:=Nil;
F:=MaybeGetFileStream(OutputStream,FOutputFile,fmCreate);
if (F=Nil) then
if FWriteStream<>nil then
F:=FWriteStream
else
F:=TIOStream.Create(iosOutPut);
try
H:=MaybeGetFileStream(HeaderStream,FHeaderFile,fmOpenRead or fmShareDenyWrite);
if Assigned(h) then
try
F.CopyFrom(H,H.Size);
finally
if H<>HeaderStream then
H.Free;
end;
W:=TPasWriter.Create(F);
W.Options:=FOptions;
W.ExtraUnits:=FExtraUnits;
if Assigned(FOnUnitAlias) then
W.OnUnitAlias:=@CheckUnitAlias;
if FIndentSize<>-1 then
W.IndentSize:=FIndentSize;
if FLineNumberWidth>0 then
W.LineNumberWidth:=FLineNumberWidth;
W.ForwardClasses.CommaText:=FForwardClasses;
W.WriteModule(M);
if Assigned(FWriteStream) then
DoWriteEvent;
finally
W.Free;
if (F<>OutputStream) and (F<>FWriteStream) then
F.Free;
end;
end;
end.
|