ISPP.IdentMan.pas 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  1. {
  2. Inno Setup Preprocessor
  3. Copyright (C) 2001-2002 Alex Yackimoff
  4. Inno Setup
  5. Copyright (C) 1997-2025 Jordan Russell
  6. Portions by Martijn Laan
  7. For conditions of distribution and use, see LICENSE.TXT.
  8. }
  9. unit ISPP.IdentMan;
  10. interface
  11. uses
  12. SysUtils, Classes, ISPP.Intf, ISPP.Base;
  13. type
  14. EIdentError = class(Exception);
  15. EMacroError = class(EIdentError);
  16. TIdentManager = class;
  17. PIdent = ^TIdent;
  18. TIdent = object
  19. Name: string;
  20. Hash: Integer;
  21. IdentType: TIdentType;
  22. end;
  23. PDefinable = ^TDefinable;
  24. TDefinable = object(TIdent)
  25. Scope: record
  26. LocalLevel: Integer; // 0 means public
  27. IsProtected: Boolean; // False means private, not used if Locality = 0
  28. end;
  29. end;
  30. PVariable = ^TVariable;
  31. TVariable = object(TDefinable)
  32. Dim: Integer;
  33. Value: array[0..0] of TIsppVariant;
  34. end;
  35. TExprPosition = record
  36. FileIndex, Line, Column: Integer;
  37. end;
  38. PMacro = ^TMacro;
  39. TMacro = object(TDefinable)
  40. Expression: string;
  41. DeclPos: TExprPosition;
  42. ParserOptions: TIsppParserOptions;
  43. ParamCount: Integer;
  44. Params: array[0..0] of TIsppMacroParam;
  45. end;
  46. PFunc = ^TFunc;
  47. TFunc = object(TIdent)
  48. Code: TIsppFunction;
  49. Ext: NativeInt;
  50. end;
  51. PActualParams = ^TActualParams;
  52. TActualParams = array of TVariable;
  53. IInternalFuncParams = interface(IIsppFuncParams)
  54. function Get(Index: NativeInt): PIsppVariant;
  55. function ResPtr: PIsppVariant;
  56. end;
  57. TDefineScope = (dsAny, dsPublic, dsProtected, dsPrivate); // order matters
  58. TIdentManager = class(TObject, IIdentManager)
  59. private
  60. FCustomIdents: IIdentManager;
  61. FFuncSender: NativeInt;
  62. FRefCount: Integer;
  63. FVarMan: TList;
  64. FLocalLevel: Integer;
  65. function FindIndex(const Name: string; AScope: TDefineScope): Integer;
  66. function Find(const Name: string; AScope: TDefineScope): PIdent;
  67. procedure FreeItem(Item: Pointer);
  68. function MacroIdents: IIdentManager;
  69. protected
  70. function QueryInterface(const IID: TGUID; out Obj): HRESULT; stdcall;
  71. public
  72. constructor Create(const CustomIdents: IIdentManager; FuncSender: NativeInt);
  73. destructor Destroy; override;
  74. function _AddRef: Integer; stdcall;
  75. function _Release: Integer; stdcall;
  76. procedure BeginLocal;
  77. procedure EndLocal;
  78. function Defined(const Name: string): Boolean;
  79. procedure DefineFunction(const Name: string; Handler: TIsppFunction;
  80. Ext: NativeInt);
  81. procedure DefineMacro(const Name, Expression: string; ExprPos: TExprPosition;
  82. const ParserOptions: TIsppParserOptions; Params: array of TIsppMacroParam;
  83. Scope: TDefineScope);
  84. procedure DefineVariable(const Name: string; Index: Integer;
  85. const Value: TIsppVariant; Scope: TDefineScope);
  86. procedure Delete(const Name: string; Scope: TDefineScope);
  87. procedure DimVariable(const Name: string; Length: Integer; Scope: TDefineScope; var ReDim: Boolean);
  88. function GetIdent(const Name: string; out CallContext: ICallContext): TIdentType;
  89. function TypeOf(const Name: string): Byte;
  90. function DimOf(const Name: String): Integer;
  91. end;
  92. const
  93. TYPE_ERROR = 0;
  94. TYPE_NULL = 1;
  95. TYPE_INTEGER = 2;
  96. TYPE_STRING = 3;
  97. TYPE_MACRO = 4;
  98. TYPE_FUNC = 5;
  99. TYPE_ARRAY = 6;
  100. implementation
  101. uses
  102. Windows, Types, ISPP.Preprocessor, ISPP.CTokenizer, ISPP.Parser,
  103. ISPP.VarUtils, ISPP.Consts, ISPP.Sessions;
  104. const
  105. MaxLocalArraySize = 16;
  106. GL: array[TDefineScope] of string = ('Public', 'Public', 'Protected', 'Private');
  107. function MakeHash(const S: string): Integer;
  108. begin
  109. Result := 0;
  110. for var I := 1 to Length(S) do
  111. Result := ((Result shl 7) or (Result shr 25)) + Ord(UpCase(S[I]));
  112. end;
  113. { TCallContext }
  114. type
  115. TCallContext = class(TInterfacedObject)
  116. private
  117. procedure ErrorDefined(const ArgName: string);
  118. procedure ErrorNotSpecified(const ArgName: string);
  119. procedure ErrorTooMany;
  120. procedure ErrorTooFew;
  121. procedure ErrorWrongType(const ArgName: string);
  122. protected
  123. function GroupingStyle: TArgGroupingStyle;
  124. end;
  125. procedure TCallContext.ErrorDefined(const ArgName: string);
  126. begin
  127. raise EIdentError.CreateFmt(SParamSpecifiedTwice, [ArgName])
  128. end;
  129. procedure TCallContext.ErrorNotSpecified(const ArgName: string);
  130. begin
  131. raise EIdentError.CreateFmt(SRequiredParamMissing, [ArgName])
  132. end;
  133. procedure TCallContext.ErrorTooMany;
  134. begin
  135. raise EIdentError.Create(STooManyActualParams);
  136. end;
  137. procedure TCallContext.ErrorTooFew;
  138. begin
  139. raise EIdentError.Create(SInsufficientParams)
  140. end;
  141. procedure TCallContext.ErrorWrongType(const ArgName: string);
  142. begin
  143. raise EIdentError.CreateFmt(SWrongParamType, [ArgName])
  144. end;
  145. function TCallContext.GroupingStyle: TArgGroupingStyle;
  146. begin
  147. Result := agsParenteses;
  148. end;
  149. { TVarCallContext }
  150. type
  151. TVarCallContext = class(TCallContext, ICallContext)
  152. private
  153. FVariable: PVariable;
  154. FIndex: Integer;
  155. protected
  156. constructor Create(Variable: PVariable);
  157. { ICallContext }
  158. procedure Add(const Name: string; const Value: TIsppVariant);
  159. function Call: TIsppVariant;
  160. function GroupingStyle: TArgGroupingStyle;
  161. procedure Clone(out NewContext: ICallContext);
  162. end;
  163. constructor TVarCallContext.Create(Variable: PVariable);
  164. begin
  165. FVariable := Variable;
  166. FIndex := -1;
  167. end;
  168. procedure TVarCallContext.Add(const Name: string;
  169. const Value: TIsppVariant);
  170. begin
  171. if FVariable.Dim <> 0 then
  172. begin
  173. if (Name = '') or (CompareText(Name, 'INDEX') = 0) then
  174. begin
  175. if FIndex <> -1 then ErrorDefined('Index');
  176. try
  177. FIndex := ToInt(Value).AsInteger;
  178. except on E: Exception do
  179. raise EIdentError(E.Message);
  180. end;
  181. end
  182. else
  183. raise EIdentError.CreateFmt(SUnknownParam, [Name]);
  184. if (FIndex < 0) or (FIndex >= FVariable.Dim) then
  185. raise EIdentError.CreateFmt(SIndexIsOutOfArraySize, [FIndex, FVariable.Name]);
  186. end
  187. else
  188. raise EIdentError.Create(SParameterlessVariable);
  189. end;
  190. function TVarCallContext.Call: TIsppVariant;
  191. begin
  192. if FIndex < 0 then
  193. if FVariable.Dim <> 0 then
  194. raise EIdentError.CreateFmt(SIndexNotSpecifiedForArray, [FVariable.Name])
  195. else
  196. FIndex := 0;
  197. Result.Typ := evLValue;
  198. Result.AsPtr := @(FVariable^.Value[FIndex]);
  199. SimplifyLValue(Result);
  200. end;
  201. function TVarCallContext.GroupingStyle: TArgGroupingStyle;
  202. begin
  203. if FVariable.Dim <> 0 then
  204. Result := agsBrackets
  205. else
  206. Result := agsNone
  207. end;
  208. { TMacroCallContext }
  209. var
  210. MacroStack: TStrings;
  211. procedure PushMacro(const Name: string);
  212. begin
  213. if MacroStack = nil then
  214. MacroStack := TStringList.Create
  215. else ;
  216. {if MacroStack.IndexOf(UpperCase(Name)) >= 0 then
  217. raise EMacroError.CreateFmt(SRecursiveMacroCall, [Name]);}
  218. MacroStack.Add(UpperCase(Name));
  219. end;
  220. procedure PopMacro;
  221. begin
  222. MacroStack.Delete(MacroStack.Count - 1);
  223. if MacroStack.Count = 0 then
  224. begin
  225. MacroStack.Free;
  226. MacroStack := nil
  227. end;
  228. end;
  229. type
  230. { TMacroLocalArrayCallContext }
  231. TMacroCallContext = class;
  232. TMacroLocalArrayCallContext = class(TCallContext, ICallContext)
  233. private
  234. FMacroContext: TMacroCallContext;
  235. FIndex: Integer;
  236. public
  237. constructor Create(MacroContext: TMacroCallContext);
  238. procedure Add(const Name: String; const Value: TIsppVariant);
  239. function Call: TIsppVariant;
  240. function GroupingStyle: TArgGroupingStyle;
  241. procedure Clone(out NewContext: ICallContext);
  242. end;
  243. { TMacroCallContext }
  244. TMacroArgument = record
  245. Value: TVariable;
  246. Defined: Boolean;
  247. end;
  248. PMacroArgArray = ^TMacroArgArray;
  249. TMacroArgArray = array[0..0] of TMacroArgument;
  250. TMacroCallContext = class(TCallContext, ICallContext, IIdentManager)
  251. private
  252. FIdentManager: IIdentManager;
  253. FMacro: PMacro;
  254. FList: PMacroArgArray;
  255. FCurrentParam: Integer;
  256. FLocalVars: TList;
  257. procedure AdjustLocalArray(Index: Integer);
  258. function FindFormalParam(const Name: string): Integer;
  259. protected
  260. constructor Create(const IdentManager: IIdentManager; Macro: PMacro);
  261. destructor Destroy; override;
  262. { IIdentManager}
  263. function GetIdent(const Name: string; out CallContext: ICallContext): TIdentType;
  264. function Defined(const Name: string): Boolean;
  265. function TypeOf(const Name: string): Byte;
  266. function DimOf(const Name: string): Integer;
  267. { ICallContext }
  268. procedure Add(const Name: string; const Value: TIsppVariant);
  269. function Call: TIsppVariant;
  270. procedure Clone(out NewContext: ICallContext);
  271. end;
  272. constructor TMacroLocalArrayCallContext.Create(MacroContext: TMacroCallContext);
  273. begin
  274. FMacroContext := MacroContext;
  275. FIndex := -1;
  276. end;
  277. procedure TMacroLocalArrayCallContext.Add(const Name: String;
  278. const Value: TIsppVariant);
  279. begin
  280. if (Name = '') or (CompareText(Name, 'INDEX') = 0) then
  281. begin
  282. if FIndex <> -1 then ErrorDefined('Index');
  283. try
  284. FIndex := ToInt(Value).AsInteger;
  285. except on E: Exception do
  286. raise EIdentError.Create(E.Message);
  287. end;
  288. end
  289. else
  290. raise EIdentError.CreateFmt(SUnknownParam, [Name]);
  291. if (FIndex < 0) or (FIndex >= MaxLocalArraySize) then
  292. raise EIdentError.Create(SLocalArraysIndexError);
  293. end;
  294. function TMacroLocalArrayCallContext.Call: TIsppVariant;
  295. begin
  296. if FIndex = -1 then FIndex := 0;
  297. FMacroContext.AdjustLocalArray(FIndex);
  298. Result.Typ := evLValue;
  299. Result.AsPtr := FMacroContext.FLocalVars[FIndex];
  300. end;
  301. function TMacroLocalArrayCallContext.GroupingStyle: TArgGroupingStyle;
  302. begin
  303. Result := agsBrackets;
  304. end;
  305. constructor TMacroCallContext.Create(const IdentManager: IIdentManager;
  306. Macro: PMacro);
  307. begin
  308. FIdentManager := IdentManager;
  309. FMacro := Macro;
  310. FList := AllocMem(SizeOf(TMacroArgument) * Macro^.ParamCount);
  311. end;
  312. destructor TMacroCallContext.Destroy;
  313. begin
  314. if Assigned(FLocalVars) then begin
  315. for var I := 0 to FLocalVars.Count - 1 do
  316. Dispose(PIsppVariant(FLocalVars[I]));
  317. FLocalVars.Free;
  318. end;
  319. FreeMem(FList)
  320. end;
  321. procedure TMacroCallContext.Add(const Name: string;
  322. const Value: TIsppVariant);
  323. var
  324. ParamIndex: Integer;
  325. begin
  326. if Name <> '' then
  327. ParamIndex := FindFormalParam(Name)
  328. else
  329. ParamIndex := FCurrentParam;
  330. if ParamIndex >= FMacro.ParamCount then
  331. ErrorTooMany;
  332. if FList[ParamIndex].Defined then
  333. ErrorDefined(FMacro.Params[ParamIndex].Name);
  334. if Value.Typ = evSpecial then //parser is in "skip" state
  335. else
  336. if Value.Typ = evNull then
  337. if pfHasDefault in FMacro.Params[ParamIndex].ParamFlags then
  338. FList[ParamIndex].Value.Value[0] := FMacro.Params[ParamIndex].DefValue
  339. else
  340. ErrorNotSpecified(FMacro.Params[ParamIndex].Name)
  341. else
  342. if (pfByRef in FMacro.Params[ParamIndex].ParamFlags) and
  343. (Value.Typ <> evLValue) then
  344. raise EIdentError.CreateFmt(SLValueRequiredForByRefParam, [FMacro.Params[ParamIndex].Name])
  345. else
  346. if (pfTypeDefined in FMacro.Params[ParamIndex].ParamFlags) and
  347. (GetRValue(Value).Typ <> FMacro.Params[ParamIndex].DefValue.Typ) then
  348. ErrorWrongType(FMacro.Params[ParamIndex].Name)
  349. else
  350. if pfByRef in FMacro.Params[ParamIndex].ParamFlags then
  351. begin
  352. FList[ParamIndex].Value.Value[0] := Value;
  353. SimplifyLValue(FList[ParamIndex].Value.Value[0]);
  354. end
  355. else
  356. begin
  357. if FMacro.Params[ParamIndex].DefValue.Typ = evCallContext then
  358. begin
  359. if (pfFunc in FMacro.Params[ParamIndex].ParamFlags) and
  360. (Value.AsCallContext.GroupingStyle <> agsParenteses) or
  361. not (pfFunc in FMacro.Params[ParamIndex].ParamFlags) and
  362. (Value.AsCallContext.GroupingStyle <> agsBrackets) then
  363. ErrorWrongType(FMacro.Params[ParamIndex].Name);
  364. end;
  365. FList[ParamIndex].Value.Value[0] := GetRValue(Value);
  366. end;
  367. FList[ParamIndex].Defined := True;
  368. FList[ParamIndex].Value.Name := FMacro.Params[ParamIndex].Name;
  369. FList[ParamIndex].Value.Dim := 0;
  370. Inc(FCurrentParam);
  371. end;
  372. function TMacroCallContext.Call: TIsppVariant;
  373. var
  374. I: Integer;
  375. Msg: string;
  376. begin
  377. PushMacro(FMacro.Name);
  378. try
  379. for I := 0 to FMacro.ParamCount - 1 do
  380. if not FList[I].Defined then
  381. if not (pfHasDefault in FMacro.Params[I].ParamFlags) then
  382. ErrorNotSpecified(FMacro.Params[I].Name)
  383. //raise EMacroError.CreateFmt(SNoReqParam, [FMacro.Params[I].Name])
  384. else
  385. begin
  386. FList[I].Value.Name := FMacro.Params[I].Name;
  387. FList[I].Value.Dim := 0;
  388. FList[I].Value.Value[0] := FMacro.Params[I].DefValue;
  389. FList[I].Defined := True;
  390. end;
  391. try
  392. Result := Parse(Self, FMacro.Expression, FMacro.DeclPos.Column,
  393. @FMacro.ParserOptions);
  394. except
  395. on E: EParsingError do
  396. begin
  397. if E.Position > 0 then
  398. begin
  399. if FMacro.DeclPos.FileIndex > 0 then
  400. Msg := Format(SErrorExecutingMacroFile, [FMacro.Name,
  401. PeekPreproc.IncludedFiles[FMacro.DeclPos.FileIndex],
  402. FMacro.DeclPos.Line, E.Position, E.Message])
  403. else
  404. Msg := Format(SErrorExecutingMacro, [FMacro.Name,
  405. FMacro.DeclPos.Line, E.Position, E.Message]);
  406. E.Message := Msg;
  407. E.Position := 0;
  408. end;
  409. raise;
  410. end;
  411. on E: Exception do
  412. begin
  413. E.Message := Format(SErrorExecutingMacroUnexpected, [FMacro.Name, E.Message]);
  414. raise;
  415. end;
  416. end;
  417. VerboseMsg(9, SSuccessfullyCalledMacro, [FMacro.Name]);
  418. finally
  419. PopMacro;
  420. end;
  421. end;
  422. function TMacroCallContext.Defined(const Name: string): Boolean;
  423. var
  424. I: Integer;
  425. begin
  426. Result := True;
  427. if CompareText(Name, SLocal) = 0 then Exit;
  428. for I := 0 to FMacro^.ParamCount - 1 do
  429. if CompareText(FMacro^.Params[I].Name, Name) = 0 then
  430. Exit;
  431. Result := FIdentManager.Defined(Name)
  432. end;
  433. function TMacroCallContext.FindFormalParam(const Name: string): Integer;
  434. begin
  435. for Result := 0 to FMacro.ParamCount - 1 do
  436. if CompareText(FMacro.Params[Result].Name, Name) = 0 then Exit;
  437. raise EMacroError.CreateFmt(SUnknownParam, [Name]);
  438. end;
  439. function TMacroCallContext.GetIdent(const Name: string;
  440. out CallContext: ICallContext): TIdentType;
  441. var
  442. I: Integer;
  443. begin
  444. Result := itVariable;
  445. if CompareText(SLocal, Name) = 0 then
  446. begin
  447. CallContext := TMacroLocalArrayCallContext.Create(Self);
  448. Exit;
  449. end
  450. else
  451. for I := 0 to FMacro^.ParamCount - 1 do
  452. if CompareText(FMacro^.Params[I].Name, Name) = 0 then
  453. begin
  454. if FMacro^.Params[I].DefValue.Typ = evCallContext then
  455. FList[I].Value.Value[0].AsCallContext.Clone(CallContext)
  456. else
  457. CallContext := TVarCallContext.Create(@FList[I].Value);
  458. Exit;
  459. end;
  460. Result := FIdentManager.GetIdent(Name, CallContext)
  461. end;
  462. function TMacroCallContext.TypeOf(const Name: string): Byte;
  463. var
  464. I: Integer;
  465. begin
  466. if CompareText(Name, SLocal) = 0 then
  467. begin
  468. Result := TYPE_ARRAY;
  469. Exit;
  470. end;
  471. for I := 0 to FMacro^.ParamCount - 1 do
  472. if CompareText(FMacro^.Params[I].Name, Name) = 0 then
  473. begin
  474. case GetRValue(FList[I].Value.Value[0]).Typ of
  475. evNull: Result := TYPE_NULL;
  476. evInt: Result := TYPE_INTEGER
  477. else
  478. Result := TYPE_STRING
  479. end;
  480. Exit;
  481. end;
  482. Result := FIdentManager.TypeOf(Name)
  483. end;
  484. {TFuncParam}
  485. type
  486. TFuncParam = class(TInterfacedObject, IIsppFuncParam)
  487. private
  488. FValue: PIsppVariant;
  489. protected
  490. constructor Create(Value: PIsppVariant);
  491. function GetType: TIsppVarType; stdcall;
  492. function GetAsInt64: Int64; stdcall;
  493. function GetAsString(Buf: PChar; BufSize: Cardinal): Integer; stdcall;
  494. end;
  495. constructor TFuncParam.Create(Value: PIsppVariant);
  496. begin
  497. FValue := Value
  498. end;
  499. function TFuncParam.GetAsInt64: Int64;
  500. begin
  501. Result := FValue^.AsInt64
  502. end;
  503. function TFuncParam.GetAsString(Buf: PChar; BufSize: Cardinal): Integer;
  504. begin
  505. StrLCopy(Buf, PChar(FValue^.AsStr), BufSize);
  506. Result := Length(FValue^.AsStr)
  507. end;
  508. function TFuncParam.GetType: TIsppVarType;
  509. begin
  510. Result := FValue^.Typ
  511. end;
  512. { TFuncCallContext }
  513. type
  514. TFuncCallContext = class(TCallContext, ICallContext, IInternalFuncParams,
  515. IIsppFuncResult)
  516. private
  517. FSender: NativeInt;
  518. FFunc: PFunc;
  519. FResult: TIsppVariant;
  520. FParams: TList;
  521. protected
  522. constructor Create(Sender: NativeInt; Func: PFunc);
  523. destructor Destroy; override;
  524. { IIsppFuncParams }
  525. function Get(Index: NativeInt): IIsppFuncParam; stdcall;
  526. function GetCount: NativeInt; stdcall;
  527. { IInternalFuncParams }
  528. function IInternalFuncParams.Get = InternalGet;
  529. function InternalGet(Index: NativeInt): PIsppVariant;
  530. function ResPtr: PIsppVariant;
  531. { IIsppFuncResult }
  532. procedure SetAsInt(Value: Int64); stdcall;
  533. procedure SetAsString(Value: PChar); stdcall;
  534. procedure SetAsNull; stdcall;
  535. procedure Error(Message: PChar); stdcall;
  536. { ICallContext }
  537. procedure Add(const Name: string; const Value: TIsppVariant);
  538. function Call: TIsppVariant;
  539. procedure Clone(out NewContext: ICallContext);
  540. end;
  541. constructor TFuncCallContext.Create(Sender: NativeInt; Func: PFunc);
  542. begin
  543. FSender := Sender;
  544. FFunc := Func;
  545. FParams := TList.Create;
  546. end;
  547. destructor TFuncCallContext.Destroy;
  548. begin
  549. FParams.Free;
  550. end;
  551. procedure TFuncCallContext.Add(const Name: string;
  552. const Value: TIsppVariant);
  553. var
  554. V: PIsppVariant;
  555. begin
  556. if Name <> '' then
  557. raise EIdentError.Create(SFuncsNoSupportNamedParams);
  558. New(V);
  559. CopyExpVar(Value, V^);
  560. FParams.Add(V);
  561. end;
  562. function TFuncCallContext.Call: TIsppVariant;
  563. var
  564. InternalParams: IInternalFuncParams;
  565. Error: TIsppFuncResult;
  566. Ext: NativeInt;
  567. begin
  568. InternalParams := Self;
  569. if FFunc.Ext = -1 then
  570. Ext := FSender
  571. else
  572. Ext := FFunc.Ext;
  573. Error := FFunc.Code(Ext, InternalParams, Self);
  574. case Error.Error of
  575. ISPPFUNC_FAIL: raise EIdentError.CreateFmt(SFuncError, [FFunc^.Name]);
  576. ISPPFUNC_MANYARGS: ErrorTooMany;
  577. ISPPFUNC_INSUFARGS: ErrorTooFew;
  578. ISPPFUNC_INTWANTED: raise EIdentError.Create(SIntegerExpressionExpected);
  579. ISPPFUNC_STRWANTED: raise EIdentError.Create(SStringExpressionExpected);
  580. end;
  581. Result := FResult;
  582. VerboseMsg(9, SSuccessfullyCalledFunction, [FFunc.Name]);
  583. end;
  584. procedure TFuncCallContext.Error(Message: PChar);
  585. begin
  586. raise Exception.Create(Message)
  587. end;
  588. function TFuncCallContext.Get(Index: NativeInt): IIsppFuncParam;
  589. begin
  590. Result := TFuncParam.Create(FParams[Index]);
  591. end;
  592. function TFuncCallContext.GetCount: NativeInt;
  593. begin
  594. Result := FParams.Count
  595. end;
  596. function TFuncCallContext.InternalGet(Index: NativeInt): PIsppVariant;
  597. begin
  598. Result := FParams[Index]
  599. end;
  600. function TFuncCallContext.ResPtr: PIsppVariant;
  601. begin
  602. Result := @FResult
  603. end;
  604. procedure TFuncCallContext.SetAsInt(Value: Int64);
  605. begin
  606. MakeInt(FResult, Value)
  607. end;
  608. procedure TFuncCallContext.SetAsNull;
  609. begin
  610. FResult := NULL
  611. end;
  612. procedure TFuncCallContext.SetAsString(Value: PChar);
  613. begin
  614. MakeStr(FResult, Value)
  615. end;
  616. { TIdentManager }
  617. constructor TIdentManager.Create(const CustomIdents: IIdentManager; FuncSender: NativeInt);
  618. begin
  619. FCustomIdents := CustomIdents;
  620. FVarMan := TList.Create;
  621. FFuncSender := FuncSender;
  622. end;
  623. destructor TIdentManager.Destroy;
  624. begin
  625. for var I := 0 to FVarMan.Count - 1 do
  626. FreeItem(FVarMan[I]);
  627. FVarMan.Free;
  628. end;
  629. function TIdentManager.Defined(const Name: string): Boolean;
  630. begin
  631. Result := Find(Name, dsAny) <> nil
  632. end;
  633. procedure TIdentManager.DefineFunction(const Name: string;
  634. Handler: TIsppFunction; Ext: NativeInt);
  635. var
  636. F: PFunc;
  637. begin
  638. if Find(Name, dsAny) <> nil then Exit;
  639. F := AllocMem(SizeOf(TFunc));
  640. F.Name := Name;
  641. F.Hash := MakeHash(Name);
  642. F.IdentType := itFunc;
  643. F.Code := Handler;
  644. F.Ext := Ext;
  645. FVarMan.Add(F);
  646. end;
  647. procedure TIdentManager.DefineMacro(const Name, Expression: string;
  648. ExprPos: TExprPosition; const ParserOptions: TIsppParserOptions;
  649. Params: array of TIsppMacroParam; Scope: TDefineScope);
  650. var
  651. P: PMacro;
  652. begin
  653. if Scope = dsAny then Scope := dsPublic;
  654. Delete(Name, Scope);
  655. for var I := 1 to High(Params) do
  656. for var J := 0 to I - 1 do
  657. if CompareText(Params[I].Name, Params[J].Name) = 0 then
  658. raise EIdentError.CreateFmt(SRedeclaredIdentifier, [Params[I].Name]);
  659. P := AllocMem(SizeOf(TMacro) + SizeOf(TIsppMacroParam) * Length(Params));
  660. try
  661. P^.Name := Name;
  662. P^.Hash := MakeHash(Name);
  663. P^.IdentType := itMacro;
  664. P^.Scope.IsProtected := Scope = dsProtected;
  665. if Scope >= dsProtected then P^.Scope.LocalLevel := FLocalLevel;
  666. P^.Expression := Expression;
  667. P^.DeclPos := ExprPos;
  668. P^.ParserOptions := ParserOptions;
  669. P^.ParamCount := Integer(Length(Params));
  670. for var I := 0 to High(Params) do
  671. P^.Params[I] := Params[I];
  672. FVarMan.Add(P);
  673. except
  674. FreeMem(P)
  675. end;
  676. VerboseMsg(4, SMacroDefined, [GL[Scope], Name]);
  677. end;
  678. procedure TIdentManager.DefineVariable(const Name: string; Index: Integer;
  679. const Value: TIsppVariant; Scope: TDefineScope);
  680. var
  681. V: PVariable;
  682. Ident: PIdent;
  683. begin
  684. if Scope = dsAny then Scope := dsPublic;
  685. Ident := Find(Name, Scope);
  686. if (Ident <> nil) and (Ident.IdentType = itVariable) and (PVariable(Ident).Dim <> 0) then
  687. begin
  688. V := PVariable(Ident);
  689. if (Index < 0) or (Index >= V.Dim) then
  690. raise EIdentError.CreateFmt(SIndexIsOutOfArraySize, [Index, Name]);
  691. V.Value[Index] := Value;
  692. end
  693. else
  694. begin
  695. if Index <> -1 then
  696. raise EIdentError.CreateFmt(SUndeclaredIdentifier, [Name]);
  697. Delete(Name, Scope);
  698. V := AllocMem(SizeOf(TVariable));
  699. V^.Name := Name;
  700. V^.Hash := MakeHash(Name);
  701. V^.IdentType := itVariable;
  702. V^.Scope.IsProtected := Scope = dsProtected;
  703. if Scope >= dsProtected then V^.Scope.LocalLevel := FLocalLevel;
  704. V^.Dim := 0;
  705. V^.Value[0] := Value;
  706. FVarMan.Add(V);
  707. end;
  708. VerboseMsg(4, SVariableDefined, [GL[Scope], Name]);
  709. end;
  710. procedure TIdentManager.Delete(const Name: string; Scope: TDefineScope);
  711. var
  712. P: PIdent;
  713. S: TDefineScope;
  714. const
  715. VM: array[itVariable..itMacro] of string = ('variable', 'macro');
  716. begin
  717. {if Scope = dsAny then
  718. begin
  719. P := Find(Name, dsPrivate);
  720. if P = nil then P := Find(Name, dsProtected);
  721. if P = nil then P := Find(Name, dsPublic)
  722. end
  723. else}
  724. P := Find(Name, Scope);
  725. if (P <> nil) and (P.IdentType in [itVariable, itMacro]) then
  726. begin
  727. //if PDefinable(P).Scope.Locality <> FLocalLevel then Exit;
  728. S := dsPublic;
  729. with PDefinable(P).Scope do
  730. if LocalLevel <> 0 then
  731. if IsProtected then
  732. S := dsProtected
  733. else
  734. S := dsPrivate;
  735. VerboseMsg(4, SUndefined, [GL[S],
  736. VM[P.IdentType], P.Name]);
  737. FVarMan.Remove(P);
  738. FreeItem(P);
  739. end
  740. end;
  741. procedure TIdentManager.DimVariable(const Name: string; Length: Integer;
  742. Scope: TDefineScope; var ReDim: Boolean);
  743. var
  744. V, VOld: PVariable;
  745. I, ReDimIndex: Integer;
  746. Msg: String;
  747. begin
  748. if Length > 0 then begin
  749. if Scope = dsAny then Scope := dsPublic;
  750. if ReDim then begin
  751. ReDimIndex := FindIndex(Name, Scope);
  752. if (ReDimIndex <> -1) and
  753. ((PIdent(FVarMan[ReDimIndex]).IdentType <> itVariable) or
  754. (PVariable(FVarMan[ReDimIndex]).Dim = 0)) then
  755. ReDimIndex := -1; //not a variable or not an array, #dim normally
  756. ReDim := ReDimIndex <> -1;
  757. end else
  758. ReDimIndex := -1;
  759. V := AllocMem(SizeOf(TVariable) + SizeOf(TIsppVariant) * (Length - 1));
  760. V.Name := Name;
  761. V.Hash := MakeHash(Name);
  762. V.IdentType := itVariable;
  763. V.Dim := Length;
  764. V^.Scope.IsProtected := Scope = dsProtected;
  765. if Scope >= dsProtected then V^.Scope.LocalLevel := FLocalLevel;
  766. if ReDimIndex = -1 then begin
  767. Delete(Name, Scope);
  768. for I := 0 to Length - 1 do
  769. V.Value[I] := NULL;
  770. FVarMan.Add(V);
  771. Msg := SArrayDeclared;
  772. end else begin
  773. VOld := PVariable(FVarMan[ReDimIndex]);
  774. for I := 0 to VOld.Dim - 1 do
  775. if I < Length then
  776. V.Value[I] := VOld.Value[I];
  777. for I := VOld.Dim to Length - 1 do
  778. V.Value[I] := NULL;
  779. FVarMan[ReDimIndex] := V;
  780. FreeItem(VOld);
  781. Msg := SArrayReDimmed;
  782. end;
  783. VerboseMsg(4, Msg, [GL[Scope], Name]);
  784. end else
  785. raise EIdentError.Create(SBadLength);
  786. end;
  787. function TIdentManager.FindIndex(const Name: string; AScope: TDefineScope): Integer;
  788. begin
  789. Result := -1;
  790. var H := MakeHash(Name);
  791. for var I := FVarMan.Count - 1 downto 0 do
  792. if (H = PIdent(FVarMan[I]).Hash) and (
  793. CompareText(PIdent(FVarMan[I]).Name, Name) = 0) then
  794. begin
  795. if (PIdent(FVarMan[I]).IdentType in [itVariable, itMacro]) then
  796. with PDefinable(FVarMan[I])^.Scope do
  797. case AScope of
  798. dsAny:
  799. if not ((LocalLevel = 0) or (LocalLevel = FLocalLevel) or IsProtected) then Continue;
  800. dsPublic:
  801. if LocalLevel <> 0 then Continue;
  802. dsProtected:
  803. if not (IsProtected and (LocalLevel <= FLocalLevel)) then Continue;
  804. else
  805. if IsProtected or (LocalLevel <> FLocalLevel) then Continue;
  806. end;
  807. Result := Integer(I);
  808. Exit
  809. end;
  810. end;
  811. function TIdentManager.Find(const Name: string; AScope: TDefineScope): PIdent;
  812. var
  813. I: Integer;
  814. begin
  815. I := FindIndex(Name, AScope);
  816. if I >= 0 then
  817. Result := FVarMan[I]
  818. else
  819. Result := nil;
  820. end;
  821. function TIdentManager.GetIdent(const Name: string;
  822. out CallContext: ICallContext): TIdentType;
  823. var
  824. P: PIdent;
  825. begin
  826. if CompareText(Name, 'DEFINED') = 0 then
  827. Result := itDefinedFunc
  828. else if CompareText(Name, 'TYPEOF') = 0 then
  829. Result := itTypeOfFunc
  830. else if CompareText(Name, 'DIMOF') = 0 then
  831. Result := itDimOfFunc
  832. else
  833. begin
  834. P := Find(Name, dsAny);
  835. if P <> nil then
  836. begin
  837. Result := P.IdentType;
  838. case P.IdentType of
  839. itVariable: CallContext := TVarCallContext.Create(PVariable(P));
  840. itMacro: CallContext := TMacroCallContext.Create(MacroIdents, PMacro(P));
  841. itFunc: CallContext := TFuncCallContext.Create(FFuncSender, PFunc(P));
  842. else
  843. Assert(False)
  844. end;
  845. end
  846. else
  847. Result := itUnknown;
  848. end;
  849. end;
  850. function TIdentManager.QueryInterface(const IID: TGUID; out Obj): HRESULT;
  851. begin
  852. if GetInterface(IID, Obj) then Result := 0 else Result := E_NOINTERFACE;
  853. end;
  854. function TIdentManager.TypeOf(const Name: string): Byte;
  855. var
  856. P: PIdent;
  857. begin
  858. Result := TYPE_ERROR;
  859. P := Find(Name, dsAny);
  860. if P <> nil then
  861. case P.IdentType of
  862. itVariable:
  863. if PVariable(P).Dim > 0 then
  864. Result := TYPE_ARRAY
  865. else
  866. case PVariable(P).Value[0].Typ of
  867. evNull: Result := TYPE_NULL;
  868. evInt: Result := TYPE_INTEGER;
  869. evStr: Result := TYPE_STRING
  870. end;
  871. itMacro: Result := TYPE_MACRO;
  872. itFunc: Result := TYPE_FUNC
  873. end
  874. end;
  875. function TIdentManager._AddRef: Integer;
  876. begin
  877. Result := InterlockedIncrement(FRefCount)
  878. end;
  879. function TIdentManager._Release: Integer;
  880. begin
  881. Result := InterlockedDecrement(FRefCount);
  882. if Result = 0 then
  883. Destroy;
  884. end;
  885. procedure TIdentManager.BeginLocal;
  886. begin
  887. Inc(FLocalLevel);
  888. end;
  889. procedure TIdentManager.EndLocal;
  890. begin
  891. for var I := FVarMan.Count - 1 downto 0 do
  892. if (PIdent(FVarMan.Items[I]).IdentType in [itVariable, itMacro]) and
  893. (PDefinable(FVarMan.Items[I]).Scope.LocalLevel = FLocalLevel) then
  894. begin
  895. FreeItem(FVarMan[I]);
  896. FVarMan.Delete(I);
  897. end;
  898. Dec(FLocalLevel);
  899. end;
  900. procedure TIdentManager.FreeItem(Item: Pointer);
  901. function ZeroToOne(A: Integer): Integer;
  902. begin
  903. if A = 0 then Result := 1 else Result := A
  904. end;
  905. begin
  906. with PIdent(Item)^ do
  907. begin
  908. Finalize(Name);
  909. case IdentType of
  910. itVariable: with PVariable(Item)^ do Finalize(Value[0], ZeroToOne(Dim));
  911. itMacro:
  912. with PMacro(Item)^ do
  913. begin
  914. Finalize(Params[0], ParamCount);
  915. Finalize(Expression);
  916. end;
  917. end;
  918. end;
  919. FreeMem(Item);
  920. end;
  921. function TIdentManager.MacroIdents: IIdentManager;
  922. begin
  923. if FCustomIdents <> nil then
  924. Result := FCustomIdents
  925. else
  926. Result := Self
  927. end;
  928. procedure TMacroCallContext.AdjustLocalArray(Index: Integer);
  929. var
  930. V: PIsppVariant;
  931. begin
  932. if not Assigned(FLocalVars) then
  933. FLocalVars := TList.Create;
  934. if FLocalVars.Count > Index then Exit;
  935. VerboseMsg(10, SAllocatingMacroLocalArrayUpToEle, [FMacro.Name, Index]);
  936. for var I := FLocalVars.Count to Index do begin
  937. New(V);
  938. V.Typ := evNull;
  939. FLocalVars.Add(V);
  940. end;
  941. end;
  942. procedure TVarCallContext.Clone(out NewContext: ICallContext);
  943. begin
  944. if FVariable.Dim = 0 then
  945. NewContext := Self
  946. else
  947. NewContext := TVarCallContext.Create(FVariable);
  948. end;
  949. procedure TMacroLocalArrayCallContext.Clone(out NewContext: ICallContext);
  950. begin
  951. NewContext := TMacroLocalArrayCallContext.Create(FMacroContext);
  952. end;
  953. procedure TMacroCallContext.Clone(out NewContext: ICallContext);
  954. begin
  955. NewContext := TMacroCallContext.Create(FIdentManager, FMacro);
  956. end;
  957. procedure TFuncCallContext.Clone(out NewContext: ICallContext);
  958. begin
  959. NewContext := TFuncCallContext.Create(FSender, FFunc);
  960. end;
  961. function TIdentManager.DimOf(const Name: String): Integer;
  962. var
  963. Ident: PIdent;
  964. begin
  965. Ident := Find(Name, dsAny);
  966. if Assigned(Ident) and (Ident.IdentType = itVariable) then
  967. Result := PVariable(Ident)^.Dim
  968. else
  969. Result := 0;
  970. end;
  971. function TMacroCallContext.DimOf(const Name: string): Integer;
  972. begin
  973. if CompareText(Name, SLocal) = 0 then
  974. Result := MaxLocalArraySize
  975. else
  976. Result := FIdentManager.DimOf(Name);
  977. end;
  978. end.