pgenutil.pas 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827
  1. {
  2. Copyright (c) 2011
  3. Contains different functions that are used in the context of
  4. parsing generics.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit pgenutil;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. { common }
  23. cclasses,
  24. { global }
  25. globtype,
  26. { parser }
  27. pgentype,
  28. { symtable }
  29. symtype,symdef,symbase;
  30. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;_prettyname:string;parsedtype:tdef;symname:string;parsedpos:tfileposinfo);inline;
  31. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;_prettyname:string);inline;
  32. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef):tdef;inline;
  33. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef;symname:string):tdef;inline;
  34. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef;parsedtype:tdef;symname:string;parsedpos:tfileposinfo):tdef;
  35. function generate_specialization_phase2(context:tspecializationcontext;genericdef:tstoreddef;parse_class_parent:boolean;_prettyname:ansistring):tdef;
  36. function check_generic_constraints(genericdef:tstoreddef;paradeflist:tfpobjectlist;poslist:tfplist):boolean;
  37. function parse_generic_parameters(allowconstraints:boolean):tfphashobjectlist;
  38. function parse_generic_specialization_types(genericdeflist:tfpobjectlist;poslist:tfplist;out prettyname,specializename:ansistring):boolean;
  39. procedure insert_generic_parameter_types(def:tstoreddef;genericdef:tstoreddef;genericlist:tfphashobjectlist);
  40. procedure maybe_insert_generic_rename_symbol(const name:tidstring;genericlist:tfphashobjectlist);
  41. function generate_generic_name(const name:tidstring;specializename:ansistring;owner_hierarchy:string):tidstring;
  42. procedure split_generic_name(const name:tidstring;out nongeneric:string;out count:longint);
  43. procedure add_generic_dummysym(sym:tsym);
  44. function resolve_generic_dummysym(const name:tidstring):tsym;
  45. function could_be_generic(const name:tidstring):boolean;inline;
  46. procedure generate_specialization_procs;
  47. procedure maybe_add_pending_specialization(def:tdef);
  48. procedure specialization_init(genericdef:tdef;var state:tspecializationstate);
  49. procedure specialization_done(var state:tspecializationstate);
  50. implementation
  51. uses
  52. { common }
  53. cutils,fpccrc,
  54. { global }
  55. globals,tokens,verbose,finput,
  56. { symtable }
  57. symconst,symsym,symtable,defcmp,procinfo,
  58. { modules }
  59. fmodule,
  60. node,nobj,
  61. { parser }
  62. scanner,
  63. pbase,pexpr,pdecsub,ptype,psub,pparautl;
  64. procedure maybe_add_waiting_unit(tt:tdef);
  65. var
  66. hmodule : tmodule;
  67. begin
  68. if not assigned(tt) or
  69. not (df_generic in tt.defoptions) then
  70. exit;
  71. hmodule:=find_module_from_symtable(tt.owner);
  72. if not assigned(hmodule) then
  73. internalerror(2012092401);
  74. if hmodule=current_module then
  75. exit;
  76. if hmodule.state<>ms_compiled then
  77. begin
  78. {$ifdef DEBUG_UNITWAITING}
  79. Writeln('Unit ', current_module.modulename^,
  80. ' waiting for ', hmodule.modulename^);
  81. {$endif DEBUG_UNITWAITING}
  82. if current_module.waitingforunit.indexof(hmodule)<0 then
  83. current_module.waitingforunit.add(hmodule);
  84. if hmodule.waitingunits.indexof(current_module)<0 then
  85. hmodule.waitingunits.add(current_module);
  86. end;
  87. end;
  88. function check_generic_constraints(genericdef:tstoreddef;paradeflist:tfpobjectlist;poslist:tfplist):boolean;
  89. var
  90. i,j,
  91. intfcount : longint;
  92. formaldef,
  93. paradef : tstoreddef;
  94. objdef,
  95. paraobjdef,
  96. formalobjdef : tobjectdef;
  97. intffound : boolean;
  98. filepos : tfileposinfo;
  99. begin
  100. { check whether the given specialization parameters fit to the eventual
  101. constraints of the generic }
  102. if not assigned(genericdef.genericparas) or (genericdef.genericparas.count=0) then
  103. internalerror(2012101001);
  104. if genericdef.genericparas.count<>paradeflist.count then
  105. internalerror(2012101002);
  106. if paradeflist.count<>poslist.count then
  107. internalerror(2012120801);
  108. result:=true;
  109. for i:=0 to genericdef.genericparas.count-1 do
  110. begin
  111. filepos:=pfileposinfo(poslist[i])^;
  112. formaldef:=tstoreddef(ttypesym(genericdef.genericparas[i]).typedef);
  113. if formaldef.typ=undefineddef then
  114. { the parameter is of unspecified type, so no need to check }
  115. continue;
  116. if not (df_genconstraint in formaldef.defoptions) or
  117. not assigned(formaldef.genconstraintdata) then
  118. internalerror(2013021602);
  119. paradef:=tstoreddef(paradeflist[i]);
  120. { undefineddef is compatible with anything }
  121. if formaldef.typ=undefineddef then
  122. continue;
  123. if paradef.typ<>formaldef.typ then
  124. begin
  125. case formaldef.typ of
  126. recorddef:
  127. { delphi has own fantasy about record constraint
  128. (almost non-nullable/non-nilable value type) }
  129. if m_delphi in current_settings.modeswitches then
  130. case paradef.typ of
  131. floatdef,enumdef,orddef:
  132. continue;
  133. objectdef:
  134. if tobjectdef(paradef).objecttype=odt_object then
  135. continue
  136. else
  137. MessagePos(filepos,type_e_record_type_expected);
  138. else
  139. MessagePos(filepos,type_e_record_type_expected);
  140. end
  141. else
  142. MessagePos(filepos,type_e_record_type_expected);
  143. objectdef:
  144. case tobjectdef(formaldef).objecttype of
  145. odt_class,
  146. odt_javaclass:
  147. MessagePos1(filepos,type_e_class_type_expected,paradef.typename);
  148. odt_interfacecom,
  149. odt_interfacecorba,
  150. odt_dispinterface,
  151. odt_interfacejava:
  152. MessagePos1(filepos,type_e_interface_type_expected,paradef.typename);
  153. else
  154. internalerror(2012101003);
  155. end;
  156. errordef:
  157. { ignore }
  158. ;
  159. else
  160. internalerror(2012101004);
  161. end;
  162. result:=false;
  163. end
  164. else
  165. begin
  166. { the paradef types are the same, so do special checks for the
  167. cases in which they are needed }
  168. if formaldef.typ=objectdef then
  169. begin
  170. paraobjdef:=tobjectdef(paradef);
  171. formalobjdef:=tobjectdef(formaldef);
  172. if not (formalobjdef.objecttype in [odt_class,odt_javaclass,odt_interfacecom,odt_interfacecorba,odt_interfacejava,odt_dispinterface]) then
  173. internalerror(2012101102);
  174. if formalobjdef.objecttype in [odt_interfacecom,odt_interfacecorba,odt_interfacejava,odt_dispinterface] then
  175. begin
  176. { this is either a concerete interface or class type (the
  177. latter without specific implemented interfaces) }
  178. case paraobjdef.objecttype of
  179. odt_interfacecom,
  180. odt_interfacecorba,
  181. odt_interfacejava,
  182. odt_dispinterface:
  183. begin
  184. if (oo_is_forward in paraobjdef.objectoptions) and
  185. (paraobjdef.objecttype=formalobjdef.objecttype) and
  186. (df_genconstraint in formalobjdef.defoptions) and
  187. (
  188. (formalobjdef.objecttype=odt_interfacecom) and
  189. (formalobjdef.childof=interface_iunknown)
  190. )
  191. or
  192. (
  193. (formalobjdef.objecttype=odt_interfacecorba) and
  194. (formalobjdef.childof=nil)
  195. ) then
  196. continue;
  197. if not def_is_related(paraobjdef,formalobjdef.childof) then
  198. begin
  199. MessagePos2(filepos,type_e_incompatible_types,paraobjdef.typename,formalobjdef.childof.typename);
  200. result:=false;
  201. end;
  202. end;
  203. odt_class,
  204. odt_javaclass:
  205. begin
  206. objdef:=paraobjdef;
  207. intffound:=false;
  208. while assigned(objdef) do
  209. begin
  210. for j:=0 to objdef.implementedinterfaces.count-1 do
  211. if timplementedinterface(objdef.implementedinterfaces[j]).intfdef=formalobjdef.childof then
  212. begin
  213. intffound:=true;
  214. break;
  215. end;
  216. if intffound then
  217. break;
  218. objdef:=objdef.childof;
  219. end;
  220. result:=intffound;
  221. if not result then
  222. MessagePos2(filepos,parser_e_class_doesnt_implement_interface,paraobjdef.typename,formalobjdef.childof.typename);
  223. end;
  224. else
  225. begin
  226. MessagePos1(filepos,type_e_class_or_interface_type_expected,paraobjdef.typename);
  227. result:=false;
  228. end;
  229. end;
  230. end
  231. else
  232. begin
  233. { this is either a "class" or a concrete instance with
  234. or without implemented interfaces }
  235. if not (paraobjdef.objecttype in [odt_class,odt_javaclass]) then
  236. begin
  237. MessagePos1(filepos,type_e_class_type_expected,paraobjdef.typename);
  238. result:=false;
  239. continue;
  240. end;
  241. { for forward declared classes we allow pure TObject/class declarations }
  242. if (oo_is_forward in paraobjdef.objectoptions) and
  243. (df_genconstraint in formaldef.defoptions) then
  244. begin
  245. if (formalobjdef.childof=class_tobject) and
  246. not formalobjdef.implements_any_interfaces then
  247. continue;
  248. end;
  249. if assigned(formalobjdef.childof) and
  250. not def_is_related(paradef,formalobjdef.childof) then
  251. begin
  252. MessagePos2(filepos,type_e_incompatible_types,paraobjdef.typename,formalobjdef.childof.typename);
  253. result:=false;
  254. end;
  255. intfcount:=0;
  256. for j:=0 to formalobjdef.implementedinterfaces.count-1 do
  257. begin
  258. objdef:=paraobjdef;
  259. while assigned(objdef) do
  260. begin
  261. intffound:=assigned(
  262. find_implemented_interface(objdef,
  263. timplementedinterface(formalobjdef.implementedinterfaces[j]).intfdef
  264. )
  265. );
  266. if intffound then
  267. break;
  268. objdef:=objdef.childof;
  269. end;
  270. if intffound then
  271. inc(intfcount)
  272. else
  273. MessagePos2(filepos,parser_e_class_doesnt_implement_interface,paraobjdef.typename,timplementedinterface(formalobjdef.implementedinterfaces[j]).intfdef.typename);
  274. end;
  275. if intfcount<>formalobjdef.implementedinterfaces.count then
  276. result:=false;
  277. end;
  278. end;
  279. end;
  280. end;
  281. end;
  282. function parse_generic_specialization_types_internal(genericdeflist:tfpobjectlist;poslist:tfplist;out prettyname,specializename:ansistring;parsedtype:tdef;parsedpos:tfileposinfo):boolean;
  283. var
  284. old_block_type : tblock_type;
  285. first : boolean;
  286. typeparam : tnode;
  287. parampos : pfileposinfo;
  288. tmpparampos : tfileposinfo;
  289. namepart : string;
  290. prettynamepart : ansistring;
  291. module : tmodule;
  292. begin
  293. result:=true;
  294. if genericdeflist=nil then
  295. internalerror(2012061401);
  296. { set the block type to type, so that the parsed type are returned as
  297. ttypenode (e.g. classes are in non type-compatible blocks returned as
  298. tloadvmtaddrnode) }
  299. old_block_type:=block_type;
  300. { if parsedtype is set, then the first type identifer was already parsed
  301. (happens in inline specializations) and thus we only need to parse
  302. the remaining types and do as if the first one was already given }
  303. first:=not assigned(parsedtype);
  304. if assigned(parsedtype) then
  305. begin
  306. genericdeflist.Add(parsedtype);
  307. module:=find_module_from_symtable(parsedtype.owner);
  308. if not assigned(module) then
  309. internalerror(2016112801);
  310. namepart:='_$'+hexstr(module.moduleid,8)+'$$'+parsedtype.unique_id_str;
  311. specializename:='$'+namepart;
  312. prettyname:=parsedtype.fullownerhierarchyname(true)+parsedtype.typesym.prettyname;
  313. if assigned(poslist) then
  314. begin
  315. New(parampos);
  316. parampos^:=parsedpos;
  317. poslist.add(parampos);
  318. end;
  319. end
  320. else
  321. begin
  322. specializename:='$';
  323. prettyname:='';
  324. end;
  325. while not (token in [_GT,_RSHARPBRACKET]) do
  326. begin
  327. { "first" is set to false at the end of the loop! }
  328. if not first then
  329. consume(_COMMA);
  330. block_type:=bt_type;
  331. tmpparampos:=current_filepos;
  332. typeparam:=factor(false,[ef_type_only]);
  333. if typeparam.nodetype=typen then
  334. begin
  335. if tstoreddef(typeparam.resultdef).is_generic and
  336. (
  337. not parse_generic or
  338. not defs_belong_to_same_generic(typeparam.resultdef,current_genericdef)
  339. ) then
  340. Message(parser_e_no_generics_as_params);
  341. if assigned(poslist) then
  342. begin
  343. New(parampos);
  344. parampos^:=tmpparampos;
  345. poslist.add(parampos);
  346. end;
  347. if typeparam.resultdef.typ<>errordef then
  348. begin
  349. if not assigned(typeparam.resultdef.typesym) then
  350. message(type_e_generics_cannot_reference_itself)
  351. else if (typeparam.resultdef.typ<>errordef) then
  352. begin
  353. genericdeflist.Add(typeparam.resultdef);
  354. module:=find_module_from_symtable(typeparam.resultdef.owner);
  355. if not assigned(module) then
  356. internalerror(2016112802);
  357. namepart:='_$'+hexstr(module.moduleid,8)+'$$'+typeparam.resultdef.unique_id_str;
  358. { we use the full name of the type to uniquely identify it }
  359. if (symtablestack.top.symtabletype=parasymtable) and
  360. (symtablestack.top.defowner.typ=procdef) and
  361. (typeparam.resultdef.owner=symtablestack.top) then
  362. begin
  363. { special handling for specializations inside generic function declarations }
  364. prettynamepart:=tdef(symtablestack.top.defowner).fullownerhierarchyname(true)+tprocdef(symtablestack.top.defowner).procsym.prettyname;
  365. end
  366. else
  367. begin
  368. prettynamepart:=typeparam.resultdef.fullownerhierarchyname(true);
  369. end;
  370. specializename:=specializename+namepart;
  371. if not first then
  372. prettyname:=prettyname+',';
  373. prettyname:=prettyname+prettynamepart+typeparam.resultdef.typesym.prettyname;
  374. end;
  375. end
  376. else
  377. begin
  378. result:=false;
  379. end;
  380. end
  381. else
  382. begin
  383. Message(type_e_type_id_expected);
  384. result:=false;
  385. end;
  386. typeparam.free;
  387. first:=false;
  388. end;
  389. block_type:=old_block_type;
  390. end;
  391. function parse_generic_specialization_types(genericdeflist:tfpobjectlist;poslist:tfplist;out prettyname,specializename:ansistring):boolean;
  392. var
  393. dummypos : tfileposinfo;
  394. begin
  395. FillChar(dummypos, SizeOf(tfileposinfo), 0);
  396. result:=parse_generic_specialization_types_internal(genericdeflist,poslist,prettyname,specializename,nil,dummypos);
  397. end;
  398. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;_prettyname:string);
  399. var
  400. dummypos : tfileposinfo;
  401. begin
  402. FillChar(dummypos, SizeOf(tfileposinfo), 0);
  403. generate_specialization(tt,parse_class_parent,_prettyname,nil,'',dummypos);
  404. end;
  405. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef):tdef;
  406. var
  407. dummypos : tfileposinfo;
  408. {$push}
  409. {$warn 5036 off}
  410. begin
  411. result:=generate_specialization_phase1(context,genericdef,nil,'',dummypos);
  412. end;
  413. {$pop}
  414. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef;symname:string):tdef;
  415. var
  416. dummypos : tfileposinfo;
  417. {$push}
  418. {$warn 5036 off}
  419. begin
  420. result:=generate_specialization_phase1(context,genericdef,nil,symname,dummypos);
  421. end;
  422. {$pop}
  423. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef;parsedtype:tdef;symname:string;parsedpos:tfileposinfo):tdef;
  424. var
  425. pt2 : tnode;
  426. errorrecovery,
  427. found,
  428. first,
  429. err : boolean;
  430. i,
  431. gencount : longint;
  432. def : tstoreddef;
  433. countstr,genname,ugenname : string;
  434. srsym : tsym;
  435. st : tsymtable;
  436. begin
  437. context:=nil;
  438. result:=nil;
  439. { either symname must be given or genericdef needs to be valid }
  440. errorrecovery:=false;
  441. if (symname='') and
  442. (not assigned(genericdef) or
  443. not assigned(genericdef.typesym) or
  444. (genericdef.typesym.typ<>typesym)) then
  445. begin
  446. errorrecovery:=true;
  447. result:=generrordef;
  448. end;
  449. { Only parse the parameters for recovery or
  450. for recording in genericbuf }
  451. if errorrecovery then
  452. begin
  453. first:=assigned(parsedtype);
  454. if not first and not try_to_consume(_LT) then
  455. consume(_LSHARPBRACKET);
  456. gencount:=0;
  457. { handle "<>" }
  458. if not first and ((token=_RSHARPBRACKET) or (token=_GT)) then
  459. Message(type_e_type_id_expected)
  460. else
  461. repeat
  462. if not first then
  463. begin
  464. pt2:=factor(false,[ef_type_only]);
  465. pt2.free;
  466. end;
  467. first:=false;
  468. inc(gencount);
  469. until not try_to_consume(_COMMA);
  470. if not try_to_consume(_GT) then
  471. consume(_RSHARPBRACKET);
  472. { we need to return a def that can later pass some checks like
  473. whether it's an interface or not }
  474. if not errorrecovery and
  475. (not assigned(result) or (result.typ=undefineddef)) then
  476. begin
  477. if (symname='') and tstoreddef(genericdef).is_generic then
  478. { this happens in non-Delphi modes }
  479. result:=genericdef
  480. else
  481. begin
  482. { find the corresponding generic symbol so that any checks
  483. done on the returned def will be handled correctly }
  484. str(gencount,countstr);
  485. if symname='' then
  486. genname:=ttypesym(genericdef.typesym).realname
  487. else
  488. genname:=symname;
  489. genname:=genname+'$'+countstr;
  490. ugenname:=upper(genname);
  491. { first check whether the found name is the same as that of
  492. the current def or one of its (generic) surrounding defs;
  493. this is necessary as the symbol of the generic can not yet
  494. be used for lookup as it still contains a reference to an
  495. errordef) }
  496. def:=current_genericdef;
  497. repeat
  498. if def.typ in [objectdef,recorddef] then
  499. if tabstractrecorddef(def).objname^=ugenname then
  500. begin
  501. result:=def;
  502. break;
  503. end;
  504. def:=tstoreddef(def.owner.defowner);
  505. until not assigned(def) or not (df_generic in def.defoptions);
  506. { it's not part of the current object hierarchy, so search
  507. for the symbol }
  508. if not assigned(result) then
  509. begin
  510. srsym:=nil;
  511. if not searchsym(ugenname,srsym,st) or
  512. (srsym.typ<>typesym) then
  513. begin
  514. identifier_not_found(genname);
  515. result:=generrordef;
  516. exit;
  517. end;
  518. result:=ttypesym(srsym).typedef;
  519. { this happens in non-Delphi modes if we encounter a
  520. specialization of the generic class or record we're
  521. currently parsing }
  522. if (result.typ=errordef) and assigned(current_structdef) and
  523. (current_structdef.objname^=ugenname) then
  524. result:=current_structdef;
  525. end;
  526. end;
  527. end;
  528. exit;
  529. end;
  530. if not assigned(parsedtype) and not try_to_consume(_LT) then
  531. begin
  532. consume(_LSHARPBRACKET);
  533. { handle "<>" }
  534. if (token=_GT) or (token=_RSHARPBRACKET) then
  535. begin
  536. Message(type_e_type_id_expected);
  537. if not try_to_consume(_GT) then
  538. try_to_consume(_RSHARPBRACKET);
  539. result:=generrordef;
  540. exit;
  541. end;
  542. end;
  543. context:=tspecializationcontext.create;
  544. { Parse type parameters }
  545. err:=not parse_generic_specialization_types_internal(context.genericdeflist,context.poslist,context.prettyname,context.specializename,parsedtype,parsedpos);
  546. if err then
  547. begin
  548. if not try_to_consume(_GT) then
  549. try_to_consume(_RSHARPBRACKET);
  550. context.free;
  551. context:=nil;
  552. result:=generrordef;
  553. exit;
  554. end;
  555. { use the name of the symbol as procvars return a user friendly version
  556. of the name }
  557. if symname='' then
  558. genname:=ttypesym(genericdef.typesym).realname
  559. else
  560. genname:=symname;
  561. { in case of non-Delphi mode the type name could already be a generic
  562. def (but maybe the wrong one) }
  563. if assigned(genericdef) and
  564. ([df_generic,df_specialization]*genericdef.defoptions<>[]) then
  565. begin
  566. { remove the type count suffix from the generic's name }
  567. for i:=Length(genname) downto 1 do
  568. if genname[i]='$' then
  569. begin
  570. genname:=copy(genname,1,i-1);
  571. break;
  572. end;
  573. { in case of a specialization we've only reached the specialization
  574. checksum yet }
  575. if df_specialization in genericdef.defoptions then
  576. for i:=length(genname) downto 1 do
  577. if genname[i]='$' then
  578. begin
  579. genname:=copy(genname,1,i-1);
  580. break;
  581. end;
  582. end
  583. else
  584. begin
  585. split_generic_name(genname,ugenname,gencount);
  586. if genname<>ugenname then
  587. genname:=ugenname;
  588. end;
  589. { search a generic with the given count of params }
  590. countstr:='';
  591. str(context.genericdeflist.Count,countstr);
  592. genname:=genname+'$'+countstr;
  593. ugenname:=upper(genname);
  594. context.genname:=genname;
  595. if assigned(genericdef) and (genericdef.owner.symtabletype in [objectsymtable,recordsymtable]) then
  596. begin
  597. if genericdef.owner.symtabletype = objectsymtable then
  598. found:=searchsym_in_class(tobjectdef(genericdef.owner.defowner),tobjectdef(genericdef.owner.defowner),ugenname,context.sym,context.symtable,[])
  599. else
  600. found:=searchsym_in_record(tabstractrecorddef(genericdef.owner.defowner),ugenname,context.sym,context.symtable);
  601. if not found then
  602. found:=searchsym(ugenname,context.sym,context.symtable);
  603. end
  604. else
  605. found:=searchsym(ugenname,context.sym,context.symtable);
  606. if not found or not (context.sym.typ in [typesym,procsym]) then
  607. begin
  608. identifier_not_found(genname);
  609. if not try_to_consume(_GT) then
  610. try_to_consume(_RSHARPBRACKET);
  611. context.free;
  612. context:=nil;
  613. result:=generrordef;
  614. exit;
  615. end;
  616. { we've found the correct def }
  617. if context.sym.typ=typesym then
  618. result:=tstoreddef(ttypesym(context.sym).typedef)
  619. else
  620. begin
  621. if tprocsym(context.sym).procdeflist.count=0 then
  622. internalerror(2015061203);
  623. result:=tstoreddef(tprocsym(context.sym).procdefList[0]);
  624. end;
  625. if not try_to_consume(_GT) then
  626. consume(_RSHARPBRACKET);
  627. end;
  628. function generate_specialization_phase2(context:tspecializationcontext;genericdef:tstoreddef;parse_class_parent:boolean;_prettyname:ansistring):tdef;
  629. procedure unset_forwarddef(def: tdef);
  630. var
  631. st : TSymtable;
  632. i : longint;
  633. begin
  634. case def.typ of
  635. procdef:
  636. tprocdef(def).forwarddef:=false;
  637. objectdef,
  638. recorddef:
  639. begin
  640. st:=def.getsymtable(gs_record);
  641. for i:=0 to st.deflist.count-1 do
  642. unset_forwarddef(tdef(st.deflist[i]));
  643. end;
  644. else
  645. ;
  646. end;
  647. end;
  648. procedure retrieve_genericdef_or_procsym(sym:tsym;out gendef:tdef;out psym:tsym);
  649. var
  650. i : longint;
  651. begin
  652. gendef:=nil;
  653. psym:=nil;
  654. case sym.typ of
  655. typesym:
  656. begin
  657. gendef:=ttypesym(sym).typedef
  658. end;
  659. procsym:
  660. begin
  661. for i:=0 to tprocsym(sym).procdeflist.count-1 do
  662. if tstoreddef(tprocsym(sym).procdeflist[i]).genericdef=genericdef then
  663. begin
  664. gendef:=tdef(tprocsym(sym).procdeflist[i]);
  665. break;
  666. end;
  667. psym:=sym;
  668. end
  669. else
  670. internalerror(200710171);
  671. end;
  672. end;
  673. var
  674. finalspecializename,
  675. ufinalspecializename : tidstring;
  676. prettyname : ansistring;
  677. generictypelist : tfphashobjectlist;
  678. srsymtable,
  679. specializest : tsymtable;
  680. hashedid : thashedidstring;
  681. tempst : tglobalsymtable;
  682. psym,
  683. srsym : tsym;
  684. def : tdef;
  685. old_block_type : tblock_type;
  686. state : tspecializationstate;
  687. old_current_structdef : tabstractrecorddef;
  688. old_current_specializedef,
  689. old_current_genericdef : tstoreddef;
  690. old_current_procinfo : tprocinfo;
  691. old_module_procinfo : tobject;
  692. hmodule : tmodule;
  693. oldcurrent_filepos : tfileposinfo;
  694. recordbuf : tdynamicarray;
  695. hadtypetoken : boolean;
  696. vmtbuilder : tvmtbuilder;
  697. i,
  698. replaydepth : longint;
  699. item : tobject;
  700. allequal,
  701. hintsprocessed : boolean;
  702. pd : tprocdef;
  703. pdflags : tpdflags;
  704. begin
  705. if not assigned(context) then
  706. internalerror(2015052203);
  707. result:=nil;
  708. pd:=nil;
  709. if not check_generic_constraints(genericdef,context.genericdeflist,context.poslist) then
  710. begin
  711. { the parameters didn't fit the constraints, so don't continue with the
  712. specialization }
  713. result:=generrordef;
  714. exit;
  715. end;
  716. { build the new type's name }
  717. finalspecializename:=generate_generic_name(context.genname,context.specializename,genericdef.ownerhierarchyname);
  718. ufinalspecializename:=upper(finalspecializename);
  719. if genericdef.typ=procdef then
  720. prettyname:=tprocdef(genericdef).procsym.prettyname
  721. else
  722. prettyname:=genericdef.typesym.prettyname;
  723. prettyname:=prettyname+'<'+context.prettyname+'>';
  724. generictypelist:=tfphashobjectlist.create(false);
  725. { build the list containing the types for the generic params }
  726. if not assigned(genericdef.genericparas) then
  727. internalerror(2013092601);
  728. if context.genericdeflist.count<>genericdef.genericparas.count then
  729. internalerror(2013092603);
  730. for i:=0 to genericdef.genericparas.Count-1 do
  731. begin
  732. srsym:=tsym(genericdef.genericparas[i]);
  733. if not (sp_generic_para in srsym.symoptions) then
  734. internalerror(2013092602);
  735. generictypelist.add(srsym.realname,tdef(context.genericdeflist[i]).typesym);
  736. end;
  737. { Special case if we are referencing the current defined object }
  738. if assigned(current_structdef) and
  739. (current_structdef.objname^=ufinalspecializename) then
  740. result:=current_structdef;
  741. { Can we reuse an already specialized type? }
  742. { for this first check whether we are currently specializing a nested
  743. type of the current (main) specialization (this is necessary, because
  744. during that time the symbol of the main specialization will still
  745. contain a reference to an errordef) }
  746. if not assigned(result) and assigned(current_specializedef) then
  747. begin
  748. def:=current_specializedef;
  749. repeat
  750. if def.typ in [objectdef,recorddef] then
  751. if tabstractrecorddef(def).objname^=ufinalspecializename then begin
  752. result:=def;
  753. break;
  754. end;
  755. if assigned(def.owner) then
  756. def:=tstoreddef(def.owner.defowner)
  757. else
  758. { this can happen when specializing a generic function }
  759. def:=nil;
  760. until not assigned(def) or not (df_specialization in def.defoptions);
  761. end;
  762. { if the genericdef is the def we are currently parsing (or one of its parents) then we can
  763. not use it for specializing as the tokenbuffer is not yet set (and we aren't done with
  764. parsing anyway), so for now we treat those still as generic defs without doing a partial
  765. specialization }
  766. if not assigned(result) then
  767. begin
  768. def:=current_genericdef;
  769. while assigned(def) and (def.typ in [recorddef,objectdef]) do
  770. begin
  771. if (df_generic in def.defoptions) and (def=genericdef) then
  772. begin
  773. result:=def;
  774. break;
  775. end;
  776. { the following happens when a routine with its parent struct
  777. as parameter is specialized as a parameter or result of a
  778. generic function }
  779. if (df_specialization in def.defoptions) and (tstoreddef(def).genericdef=genericdef) then
  780. begin
  781. if tstoreddef(def).genericparas.count=generictypelist.count then
  782. begin
  783. allequal:=true;
  784. for i:=0 to generictypelist.count-1 do
  785. begin
  786. if not equal_defs(ttypesym(generictypelist[i]).typedef,ttypesym(tstoreddef(def).genericparas[i]).typedef) then
  787. begin
  788. allequal:=false;
  789. break;
  790. end;
  791. end;
  792. if allequal then
  793. begin
  794. result:=def;
  795. break;
  796. end;
  797. end;
  798. end;
  799. def:=tstoreddef(def.owner.defowner);
  800. end;
  801. end;
  802. { decide in which symtable to put the specialization }
  803. if parse_generic and not assigned(result) then
  804. begin
  805. srsymtable:=symtablestack.top;
  806. if (srsymtable.symtabletype in [localsymtable,parasymtable]) and tstoreddef(srsymtable.defowner).is_specialization then
  807. { if we are currently specializing a routine we need to specialize into
  808. the routine's local- or parasymtable so that they are correctly
  809. registered should the specialization be finalized }
  810. specializest:=srsymtable
  811. else if assigned(current_procinfo) and (df_generic in current_procinfo.procdef.defoptions) then
  812. { if we are parsing the definition of a method we specialize into
  813. the local symtable of it }
  814. specializest:=current_procinfo.procdef.getsymtable(gs_local)
  815. else
  816. begin
  817. if not assigned(current_genericdef) then
  818. internalerror(2014050901);
  819. { we specialize the partial specialization into the symtable of the currently parsed
  820. generic }
  821. case current_genericdef.typ of
  822. procvardef:
  823. specializest:=current_genericdef.getsymtable(gs_para);
  824. procdef:
  825. specializest:=current_genericdef.getsymtable(gs_local);
  826. objectdef,
  827. recorddef:
  828. specializest:=current_genericdef.getsymtable(gs_record);
  829. arraydef:
  830. specializest:=tarraydef(current_genericdef).symtable;
  831. else
  832. internalerror(2014050902);
  833. end;
  834. end;
  835. end
  836. else
  837. if current_module.is_unit and current_module.in_interface then
  838. specializest:=current_module.globalsymtable
  839. else
  840. specializest:=current_module.localsymtable;
  841. if not assigned(specializest) then
  842. internalerror(2014050910);
  843. { now check whether there is a specialization somewhere else }
  844. psym:=nil;
  845. if not assigned(result) then
  846. begin
  847. hashedid.id:=ufinalspecializename;
  848. if specializest.symtabletype=objectsymtable then
  849. begin
  850. { search also in parent classes }
  851. if not assigned(current_genericdef) or (current_genericdef.typ<>objectdef) then
  852. internalerror(2016112901);
  853. if not searchsym_in_class(tobjectdef(current_genericdef),tobjectdef(current_genericdef),ufinalspecializename,srsym,srsymtable,[]) then
  854. srsym:=nil;
  855. end
  856. else
  857. srsym:=tsym(specializest.findwithhash(hashedid));
  858. if assigned(srsym) then
  859. begin
  860. retrieve_genericdef_or_procsym(srsym,result,psym);
  861. end
  862. else
  863. { the generic could have been specialized in the globalsymtable
  864. already, so search there as well }
  865. if (specializest<>current_module.globalsymtable) and assigned(current_module.globalsymtable) then
  866. begin
  867. srsym:=tsym(current_module.globalsymtable.findwithhash(hashedid));
  868. if assigned(srsym) then
  869. begin
  870. retrieve_genericdef_or_procsym(srsym,result,psym);
  871. end;
  872. end;
  873. end;
  874. if not assigned(result) then
  875. begin
  876. specialization_init(genericdef,state);
  877. { push a temporary global symtable so that the specialization is
  878. added to the correct symtable; this symtable does not contain
  879. any other symbols, so that the type resolution can not be
  880. influenced by symbols in the current unit }
  881. tempst:=tspecializesymtable.create(current_module.modulename^,current_module.moduleid);
  882. symtablestack.push(tempst);
  883. { Reparse the original type definition }
  884. begin
  885. old_current_specializedef:=nil;
  886. old_current_genericdef:=nil;
  887. old_current_structdef:=nil;
  888. old_current_procinfo:=current_procinfo;
  889. old_module_procinfo:=current_module.procinfo;
  890. current_procinfo:=nil;
  891. current_module.procinfo:=nil;
  892. if parse_class_parent then
  893. begin
  894. old_current_structdef:=current_structdef;
  895. old_current_genericdef:=current_genericdef;
  896. old_current_specializedef:=current_specializedef;
  897. if genericdef.owner.symtabletype in [recordsymtable,objectsymtable] then
  898. current_structdef:=tabstractrecorddef(genericdef.owner.defowner)
  899. else
  900. current_structdef:=nil;
  901. current_genericdef:=nil;
  902. current_specializedef:=nil;
  903. end;
  904. maybe_add_waiting_unit(genericdef);
  905. { First a new sym so we can reuse this specialization and
  906. references to this specialization can be handled }
  907. if genericdef.typ=procdef then
  908. if assigned(psym) then
  909. srsym:=psym
  910. else
  911. srsym:=cprocsym.create(finalspecializename)
  912. else
  913. srsym:=ctypesym.create(finalspecializename,generrordef);
  914. { insert the symbol only if we don't know already that we have
  915. a procsym to add it to }
  916. if not assigned(psym) then
  917. specializest.insert(srsym);
  918. { specializations are declarations as such it is the wisest to
  919. declare set the blocktype to "type"; otherwise we'll
  920. experience unexpected side effects like the addition of
  921. classrefdefs if we have a generic that's derived from another
  922. generic }
  923. old_block_type:=block_type;
  924. block_type:=bt_type;
  925. if (
  926. (genericdef.typ=procdef) and
  927. not assigned(tprocdef(genericdef).genericdecltokenbuf)
  928. ) or (
  929. (genericdef.typ<>procdef) and
  930. not assigned(genericdef.generictokenbuf)
  931. ) then
  932. internalerror(200511171);
  933. hmodule:=find_module_from_symtable(genericdef.owner);
  934. if hmodule=nil then
  935. internalerror(2012051202);
  936. oldcurrent_filepos:=current_filepos;
  937. { use the index the module got from the current compilation process }
  938. current_filepos.moduleindex:=hmodule.unit_index;
  939. current_tokenpos:=current_filepos;
  940. if parse_generic then
  941. begin
  942. recordbuf:=current_scanner.recordtokenbuf;
  943. current_scanner.recordtokenbuf:=nil;
  944. end
  945. else
  946. recordbuf:=nil;
  947. replaydepth:=current_scanner.replay_stack_depth;
  948. if genericdef.typ=procdef then
  949. begin
  950. current_scanner.startreplaytokens(tprocdef(genericdef).genericdecltokenbuf);
  951. parse_proc_head(tprocdef(genericdef).struct,tprocdef(genericdef).proctypeoption,false,genericdef,generictypelist,pd);
  952. if assigned(pd) then
  953. begin
  954. if assigned(psym) then
  955. pd.procsym:=psym
  956. else
  957. pd.procsym:=srsym;
  958. parse_proc_dec_finish(pd,po_classmethod in tprocdef(genericdef).procoptions,tprocdef(genericdef).struct);
  959. end;
  960. result:=pd;
  961. end
  962. else
  963. begin
  964. current_scanner.startreplaytokens(genericdef.generictokenbuf);
  965. hadtypetoken:=false;
  966. read_named_type(result,srsym,genericdef,generictypelist,false,hadtypetoken);
  967. ttypesym(srsym).typedef:=result;
  968. result.typesym:=srsym;
  969. if _prettyname<>'' then
  970. ttypesym(result.typesym).fprettyname:=_prettyname
  971. else
  972. ttypesym(result.typesym).fprettyname:=prettyname;
  973. end;
  974. current_filepos:=oldcurrent_filepos;
  975. { Note regarding hint directives:
  976. There is no need to remove the flags for them from the
  977. specialized generic symbol, because hint directives that
  978. follow the specialization are handled by the code in
  979. pdecl.types_dec and added to the type symbol.
  980. E.g.: TFoo = TBar<Blubb> deprecated;
  981. Here the symbol TBar$1$Blubb will contain the
  982. "sp_hint_deprecated" flag while the TFoo symbol won't.}
  983. case result.typ of
  984. { Build VMT indexes for classes and read hint directives }
  985. objectdef:
  986. begin
  987. if replaydepth>current_scanner.replay_stack_depth then
  988. begin
  989. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  990. if replaydepth>current_scanner.replay_stack_depth then
  991. consume(_SEMICOLON);
  992. end;
  993. vmtbuilder:=TVMTBuilder.Create(tobjectdef(result));
  994. vmtbuilder.generate_vmt;
  995. vmtbuilder.free;
  996. end;
  997. { handle params, calling convention, etc }
  998. procvardef:
  999. begin
  1000. hintsprocessed:=false;
  1001. if replaydepth>current_scanner.replay_stack_depth then
  1002. begin
  1003. if not check_proc_directive(true) then
  1004. begin
  1005. hintsprocessed:=try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg);
  1006. if replaydepth>current_scanner.replay_stack_depth then
  1007. consume(_SEMICOLON);
  1008. end
  1009. else
  1010. hintsprocessed:=true;
  1011. end;
  1012. if replaydepth>current_scanner.replay_stack_depth then
  1013. parse_var_proc_directives(ttypesym(srsym));
  1014. handle_calling_convention(tprocvardef(result),hcc_default_actions_intf);
  1015. if not hintsprocessed and (replaydepth>current_scanner.replay_stack_depth) then
  1016. begin
  1017. try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg);
  1018. if replaydepth>current_scanner.replay_stack_depth then
  1019. consume(_SEMICOLON);
  1020. end;
  1021. end;
  1022. procdef:
  1023. begin
  1024. pdflags:=[pd_body,pd_implemen];
  1025. if genericdef.owner.symtabletype=objectsymtable then
  1026. include(pdflags,pd_object)
  1027. else if genericdef.owner.symtabletype=recordsymtable then
  1028. include(pdflags,pd_record);
  1029. parse_proc_directives(pd,pdflags);
  1030. while try_consume_hintdirective(pd.symoptions,pd.deprecatedmsg) do
  1031. consume(_SEMICOLON);
  1032. if parse_generic then
  1033. handle_calling_convention(tprocdef(result),hcc_default_actions_intf)
  1034. else
  1035. handle_calling_convention(tprocdef(result),hcc_default_actions_impl);
  1036. proc_add_definition(tprocdef(result));
  1037. { for partial specializations we implicitely declare the routine as
  1038. having its implementation although we'll not specialize it in reality }
  1039. if parse_generic then
  1040. unset_forwarddef(result);
  1041. end;
  1042. else
  1043. { parse hint directives for records and arrays }
  1044. if replaydepth>current_scanner.replay_stack_depth then begin
  1045. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  1046. if replaydepth>current_scanner.replay_stack_depth then
  1047. consume(_SEMICOLON);
  1048. end;
  1049. end;
  1050. { Consume the remainder of the buffer }
  1051. while current_scanner.replay_stack_depth>replaydepth do
  1052. consume(token);
  1053. if assigned(recordbuf) then
  1054. begin
  1055. if assigned(current_scanner.recordtokenbuf) then
  1056. internalerror(2014050909);
  1057. current_scanner.recordtokenbuf:=recordbuf;
  1058. end;
  1059. block_type:=old_block_type;
  1060. current_procinfo:=old_current_procinfo;
  1061. current_module.procinfo:=old_module_procinfo;
  1062. if parse_class_parent then
  1063. begin
  1064. current_structdef:=old_current_structdef;
  1065. current_genericdef:=old_current_genericdef;
  1066. current_specializedef:=old_current_specializedef;
  1067. end;
  1068. end;
  1069. { extract all created symbols and defs from the temporary symtable
  1070. and add them to the specializest }
  1071. for i:=tempst.SymList.Count-1 downto 0 do
  1072. begin
  1073. item:=tempst.SymList.Items[i];
  1074. { using changeowner the symbol is automatically added to the
  1075. new symtable }
  1076. tsym(item).ChangeOwner(specializest);
  1077. end;
  1078. for i:=tempst.DefList.Count-1 downto 0 do
  1079. begin
  1080. item:=tempst.DefList.Items[i];
  1081. { using changeowner the def is automatically added to the new
  1082. symtable }
  1083. tdef(item).ChangeOwner(specializest);
  1084. { for partial specializations we implicitely declare any methods as having their
  1085. implementations although we'll not specialize them in reality }
  1086. if parse_generic then
  1087. unset_forwarddef(tdef(item));
  1088. end;
  1089. { if a generic was declared during the specialization we need to
  1090. flag the specialize symtable accordingly }
  1091. if sto_has_generic in tempst.tableoptions then
  1092. specializest.includeoption(sto_has_generic);
  1093. tempst.free;
  1094. specialization_done(state);
  1095. { procdefs are only added once we know which overload we use }
  1096. if not parse_generic and (result.typ<>procdef) then
  1097. current_module.pendingspecializations.add(result.typename,result);
  1098. end;
  1099. generictypelist.free;
  1100. if assigned(genericdef) then
  1101. begin
  1102. { check the hints of the found generic symbol }
  1103. if genericdef.typ=procdef then
  1104. srsym:=tprocdef(genericdef).procsym
  1105. else
  1106. srsym:=genericdef.typesym;
  1107. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1108. end;
  1109. end;
  1110. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;_prettyname:string;parsedtype:tdef;symname:string;parsedpos:tfileposinfo);
  1111. var
  1112. context : tspecializationcontext;
  1113. genericdef : tstoreddef;
  1114. begin
  1115. genericdef:=tstoreddef(generate_specialization_phase1(context,tt,parsedtype,symname,parsedpos));
  1116. if genericdef<>generrordef then
  1117. genericdef:=tstoreddef(generate_specialization_phase2(context,genericdef,parse_class_parent,_prettyname));
  1118. tt:=genericdef;
  1119. if assigned(context) then
  1120. context.free;
  1121. end;
  1122. function parse_generic_parameters(allowconstraints:boolean):tfphashobjectlist;
  1123. var
  1124. generictype : ttypesym;
  1125. i,firstidx : longint;
  1126. srsymtable : tsymtable;
  1127. basedef,def : tdef;
  1128. defname : tidstring;
  1129. allowconstructor,
  1130. doconsume : boolean;
  1131. constraintdata : tgenericconstraintdata;
  1132. old_block_type : tblock_type;
  1133. begin
  1134. result:=tfphashobjectlist.create(false);
  1135. firstidx:=0;
  1136. old_block_type:=block_type;
  1137. block_type:=bt_type;
  1138. repeat
  1139. if token=_ID then
  1140. begin
  1141. generictype:=ctypesym.create(orgpattern,cundefinedtype);
  1142. { type parameters need to be added as strict private }
  1143. generictype.visibility:=vis_strictprivate;
  1144. include(generictype.symoptions,sp_generic_para);
  1145. result.add(orgpattern,generictype);
  1146. end;
  1147. consume(_ID);
  1148. if try_to_consume(_COLON) then
  1149. begin
  1150. if not allowconstraints then
  1151. { TODO }
  1152. Message(parser_e_illegal_expression{ parser_e_generic_constraints_not_allowed_here});
  1153. { construct a name which can be used for a type specification }
  1154. constraintdata:=tgenericconstraintdata.create;
  1155. defname:='';
  1156. str(current_module.deflist.count,defname);
  1157. defname:='$gendef'+defname;
  1158. allowconstructor:=m_delphi in current_settings.modeswitches;
  1159. basedef:=generrordef;
  1160. repeat
  1161. doconsume:=true;
  1162. case token of
  1163. _CONSTRUCTOR:
  1164. begin
  1165. if not allowconstructor or (gcf_constructor in constraintdata.flags) then
  1166. Message(parser_e_illegal_expression);
  1167. include(constraintdata.flags,gcf_constructor);
  1168. allowconstructor:=false;
  1169. end;
  1170. _CLASS:
  1171. begin
  1172. if gcf_class in constraintdata.flags then
  1173. Message(parser_e_illegal_expression);
  1174. if basedef=generrordef then
  1175. include(constraintdata.flags,gcf_class)
  1176. else
  1177. Message(parser_e_illegal_expression);
  1178. end;
  1179. _RECORD:
  1180. begin
  1181. if ([gcf_constructor,gcf_class]*constraintdata.flags<>[])
  1182. or (constraintdata.interfaces.count>0) then
  1183. Message(parser_e_illegal_expression)
  1184. else
  1185. begin
  1186. srsymtable:=trecordsymtable.create(defname,0,1);
  1187. basedef:=crecorddef.create(defname,srsymtable);
  1188. include(constraintdata.flags,gcf_record);
  1189. allowconstructor:=false;
  1190. end;
  1191. end;
  1192. else
  1193. begin
  1194. { after single_type "token" is the trailing ",", ";" or
  1195. ">"! }
  1196. doconsume:=false;
  1197. { def is already set to a class or record }
  1198. if gcf_record in constraintdata.flags then
  1199. Message(parser_e_illegal_expression);
  1200. single_type(def, [stoAllowSpecialization]);
  1201. { only types that are inheritable are allowed }
  1202. if (def.typ<>objectdef) or
  1203. not (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_interfacejava,odt_javaclass]) then
  1204. Message1(type_e_class_or_interface_type_expected,def.typename)
  1205. else
  1206. case tobjectdef(def).objecttype of
  1207. odt_class,
  1208. odt_javaclass:
  1209. begin
  1210. if gcf_class in constraintdata.flags then
  1211. { "class" + concrete class is not allowed }
  1212. Message(parser_e_illegal_expression)
  1213. else
  1214. { do we already have a concrete class? }
  1215. if basedef<>generrordef then
  1216. Message(parser_e_illegal_expression)
  1217. else
  1218. basedef:=def;
  1219. end;
  1220. odt_interfacecom,
  1221. odt_interfacecorba,
  1222. odt_interfacejava,
  1223. odt_dispinterface:
  1224. constraintdata.interfaces.add(def);
  1225. else
  1226. ;
  1227. end;
  1228. end;
  1229. end;
  1230. if doconsume then
  1231. consume(token);
  1232. until not try_to_consume(_COMMA);
  1233. if ([gcf_class,gcf_constructor]*constraintdata.flags<>[]) or
  1234. (constraintdata.interfaces.count>1) or
  1235. (
  1236. (basedef.typ=objectdef) and
  1237. (tobjectdef(basedef).objecttype in [odt_javaclass,odt_class])
  1238. ) then
  1239. begin
  1240. if basedef.typ=errordef then
  1241. { don't pass an errordef as a parent to a tobjectdef }
  1242. basedef:=class_tobject
  1243. else
  1244. if (basedef.typ<>objectdef) or
  1245. not (tobjectdef(basedef).objecttype in [odt_javaclass,odt_class]) then
  1246. internalerror(2012101101);
  1247. basedef:=cobjectdef.create(tobjectdef(basedef).objecttype,defname,tobjectdef(basedef),false);
  1248. for i:=0 to constraintdata.interfaces.count-1 do
  1249. tobjectdef(basedef).implementedinterfaces.add(
  1250. timplementedinterface.create(tobjectdef(constraintdata.interfaces[i])));
  1251. end
  1252. else
  1253. if constraintdata.interfaces.count=1 then
  1254. begin
  1255. if basedef.typ<>errordef then
  1256. internalerror(2013021601);
  1257. def:=tdef(constraintdata.interfaces[0]);
  1258. basedef:=cobjectdef.create(tobjectdef(def).objecttype,defname,tobjectdef(def),false);
  1259. constraintdata.interfaces.delete(0);
  1260. end;
  1261. if basedef.typ<>errordef then
  1262. with tstoreddef(basedef) do
  1263. begin
  1264. genconstraintdata:=tgenericconstraintdata.create;
  1265. genconstraintdata.flags:=constraintdata.flags;
  1266. genconstraintdata.interfaces.assign(constraintdata.interfaces);
  1267. include(defoptions,df_genconstraint);
  1268. end;
  1269. for i:=firstidx to result.count-1 do
  1270. ttypesym(result[i]).typedef:=basedef;
  1271. { we need a typesym in case we do a Delphi-mode inline
  1272. specialization with this parameter; so just use the first sym }
  1273. if not assigned(basedef.typesym) then
  1274. basedef.typesym:=ttypesym(result[firstidx]);
  1275. firstidx:=result.count;
  1276. constraintdata.free;
  1277. end
  1278. else
  1279. begin
  1280. if token=_SEMICOLON then
  1281. begin
  1282. { two different typeless parameters are considered as incompatible }
  1283. for i:=firstidx to result.count-1 do
  1284. begin
  1285. ttypesym(result[i]).typedef:=cundefineddef.create(false);
  1286. ttypesym(result[i]).typedef.typesym:=ttypesym(result[i]);
  1287. end;
  1288. { a semicolon terminates a type parameter group }
  1289. firstidx:=result.count;
  1290. end;
  1291. end;
  1292. until not (try_to_consume(_COMMA) or try_to_consume(_SEMICOLON));
  1293. { two different typeless parameters are considered as incompatible }
  1294. for i:=firstidx to result.count-1 do
  1295. begin
  1296. ttypesym(result[i]).typedef:=cundefineddef.create(false);
  1297. ttypesym(result[i]).typedef.typesym:=ttypesym(result[i]);
  1298. end;
  1299. block_type:=old_block_type;
  1300. end;
  1301. procedure insert_generic_parameter_types(def:tstoreddef;genericdef:tstoreddef;genericlist:tfphashobjectlist);
  1302. var
  1303. i : longint;
  1304. generictype,sym : ttypesym;
  1305. st : tsymtable;
  1306. begin
  1307. def.genericdef:=genericdef;
  1308. if not assigned(genericlist) then
  1309. exit;
  1310. if assigned(genericdef) then
  1311. include(def.defoptions,df_specialization)
  1312. else
  1313. if genericlist.count>0 then
  1314. include(def.defoptions,df_generic);
  1315. case def.typ of
  1316. recorddef,objectdef: st:=tabstractrecorddef(def).symtable;
  1317. arraydef: st:=tarraydef(def).symtable;
  1318. procvardef,procdef: st:=tabstractprocdef(def).parast;
  1319. else
  1320. internalerror(201101020);
  1321. end;
  1322. if (genericlist.count>0) and not assigned(def.genericparas) then
  1323. def.genericparas:=tfphashobjectlist.create(false);
  1324. for i:=0 to genericlist.count-1 do
  1325. begin
  1326. generictype:=ttypesym(genericlist[i]);
  1327. if assigned(generictype.owner) then
  1328. begin
  1329. sym:=ctypesym.create(genericlist.nameofindex(i),generictype.typedef);
  1330. { type parameters need to be added as strict private }
  1331. sym.visibility:=vis_strictprivate;
  1332. st.insert(sym);
  1333. include(sym.symoptions,sp_generic_para);
  1334. end
  1335. else
  1336. begin
  1337. if (generictype.typedef.typ=undefineddef) and (generictype.typedef<>cundefinedtype) then
  1338. begin
  1339. { the generic parameters were parsed before the genericdef existed thus the
  1340. undefineddefs were added as part of the parent symtable }
  1341. if assigned(generictype.typedef.owner) then
  1342. generictype.typedef.owner.DefList.Extract(generictype.typedef);
  1343. generictype.typedef.changeowner(st);
  1344. end;
  1345. st.insert(generictype);
  1346. include(generictype.symoptions,sp_generic_para);
  1347. end;
  1348. def.genericparas.add(genericlist.nameofindex(i),generictype);
  1349. end;
  1350. end;
  1351. procedure maybe_insert_generic_rename_symbol(const name:tidstring;genericlist:tfphashobjectlist);
  1352. var
  1353. gensym : ttypesym;
  1354. begin
  1355. { for generics in non-Delphi modes we insert a private type symbol
  1356. that has the same base name as the currently parsed generic and
  1357. that references this defs }
  1358. if not (m_delphi in current_settings.modeswitches) and
  1359. (
  1360. (
  1361. parse_generic and
  1362. assigned(genericlist) and
  1363. (genericlist.count>0)
  1364. ) or
  1365. (
  1366. assigned(current_specializedef) and
  1367. assigned(current_structdef.genericdef) and
  1368. (current_structdef.genericdef.typ in [objectdef,recorddef]) and
  1369. (pos('$',name)>0)
  1370. )
  1371. ) then
  1372. begin
  1373. { we need to pass nil as def here, because the constructor wants
  1374. to set the typesym of the def which is not what we want }
  1375. gensym:=ctypesym.create(copy(name,1,pos('$',name)-1),nil);
  1376. gensym.typedef:=current_structdef;
  1377. include(gensym.symoptions,sp_internal);
  1378. { the symbol should be only visible to the generic class
  1379. itself }
  1380. gensym.visibility:=vis_strictprivate;
  1381. symtablestack.top.insert(gensym);
  1382. end;
  1383. end;
  1384. function generate_generic_name(const name:tidstring;specializename:ansistring;owner_hierarchy:string):tidstring;
  1385. var
  1386. crc : cardinal;
  1387. begin
  1388. if specializename='' then
  1389. internalerror(2012061901);
  1390. { build the new type's name }
  1391. crc:=UpdateCrc32(0,specializename[1],length(specializename));
  1392. result:=name+'$crc'+hexstr(crc,8);
  1393. if owner_hierarchy<>'' then
  1394. begin
  1395. crc:=UpdateCrc32(0,owner_hierarchy[1],length(owner_hierarchy));
  1396. result:=result+'$crc'+hexstr(crc,8);
  1397. end;
  1398. end;
  1399. procedure split_generic_name(const name:tidstring;out nongeneric:string;out count:longint);
  1400. var
  1401. i,code : longint;
  1402. countstr : string;
  1403. begin
  1404. for i:=length(name) downto 1 do
  1405. if name[i]='$' then
  1406. begin
  1407. nongeneric:=copy(name,1,i-1);
  1408. countstr:=copy(name,i+1,length(name)-i);
  1409. val(countstr,count,code);
  1410. if code<>0 then
  1411. break;
  1412. exit;
  1413. end;
  1414. nongeneric:=name;
  1415. count:=0;
  1416. end;
  1417. procedure add_generic_dummysym(sym:tsym);
  1418. var
  1419. list: TFPObjectList;
  1420. srsym : tsym;
  1421. srsymtable : tsymtable;
  1422. entry : tgenericdummyentry;
  1423. begin
  1424. if sp_generic_dummy in sym.symoptions then
  1425. begin
  1426. { did we already search for a generic with that name? }
  1427. list:=tfpobjectlist(current_module.genericdummysyms.find(sym.name));
  1428. if not assigned(list) then
  1429. begin
  1430. list:=tfpobjectlist.create(true);
  1431. current_module.genericdummysyms.add(sym.name,list);
  1432. end;
  1433. { is the dummy sym still "dummy"? }
  1434. if (sym.typ=typesym) and
  1435. (
  1436. { dummy sym defined in mode Delphi }
  1437. (ttypesym(sym).typedef.typ=undefineddef) or
  1438. { dummy sym defined in non-Delphi mode }
  1439. (tstoreddef(ttypesym(sym).typedef).is_generic)
  1440. ) then
  1441. begin
  1442. { do we have a non-generic type of the same name
  1443. available? }
  1444. if not searchsym_with_flags(sym.name,srsym,srsymtable,[ssf_no_addsymref]) then
  1445. srsym:=nil;
  1446. end
  1447. else if (sym.typ=procsym) and
  1448. (tprocsym(sym).procdeflist.count>0) then
  1449. srsym:=sym
  1450. else
  1451. { dummy symbol is already not so dummy anymore }
  1452. srsym:=nil;
  1453. if assigned(srsym) then
  1454. begin
  1455. entry:=tgenericdummyentry.create;
  1456. entry.resolvedsym:=srsym;
  1457. entry.dummysym:=sym;
  1458. list.add(entry);
  1459. end;
  1460. end;
  1461. end;
  1462. function resolve_generic_dummysym(const name:tidstring):tsym;
  1463. var
  1464. list : tfpobjectlist;
  1465. begin
  1466. list:=tfpobjectlist(current_module.genericdummysyms.find(name));
  1467. if assigned(list) and (list.count>0) then
  1468. result:=tgenericdummyentry(list.last).resolvedsym
  1469. else
  1470. result:=nil;
  1471. end;
  1472. function could_be_generic(const name:tidstring):boolean;
  1473. begin
  1474. result:=(name<>'') and
  1475. (current_module.genericdummysyms.findindexof(name)>=0);
  1476. end;
  1477. procedure specialization_init(genericdef:tdef;var state: tspecializationstate);
  1478. var
  1479. pu : tused_unit;
  1480. hmodule : tmodule;
  1481. unitsyms : TFPHashObjectList;
  1482. sym : tsym;
  1483. i : Integer;
  1484. begin
  1485. if not assigned(genericdef) then
  1486. internalerror(200705151);
  1487. { Setup symtablestack at definition time
  1488. to get types right, however this is not perfect, we should probably record
  1489. the resolved symbols }
  1490. state.oldsymtablestack:=symtablestack;
  1491. state.oldextendeddefs:=current_module.extendeddefs;
  1492. state.oldgenericdummysyms:=current_module.genericdummysyms;
  1493. current_module.extendeddefs:=TFPHashObjectList.create(true);
  1494. current_module.genericdummysyms:=tfphashobjectlist.create(true);
  1495. symtablestack:=tdefawaresymtablestack.create;
  1496. hmodule:=find_module_from_symtable(genericdef.owner);
  1497. if hmodule=nil then
  1498. internalerror(200705152);
  1499. { collect all unit syms in the generic's unit as we need to establish
  1500. their unitsym.module link again so that unit identifiers can be used }
  1501. unitsyms:=tfphashobjectlist.create(false);
  1502. if (hmodule<>current_module) and assigned(hmodule.globalsymtable) then
  1503. for i:=0 to hmodule.globalsymtable.symlist.count-1 do
  1504. begin
  1505. sym:=tsym(hmodule.globalsymtable.symlist[i]);
  1506. if sym.typ=unitsym then
  1507. unitsyms.add(upper(sym.realname),sym);
  1508. end;
  1509. { add all units if we are specializing inside the current unit (as the
  1510. generic could have been declared in the implementation part), but load
  1511. only interface units, if we are in a different unit as then the generic
  1512. needs to be in the interface section }
  1513. pu:=tused_unit(hmodule.used_units.first);
  1514. while assigned(pu) do
  1515. begin
  1516. if not assigned(pu.u.globalsymtable) then
  1517. { in certain circular, but valid unit constellations it can happen
  1518. that we specialize a generic in a different unit that was used
  1519. in the implementation section of the generic's unit and were the
  1520. interface is still being parsed and thus the localsymtable is in
  1521. reality the global symtable }
  1522. if pu.u.in_interface then
  1523. symtablestack.push(pu.u.localsymtable)
  1524. else
  1525. internalerror(200705153)
  1526. else
  1527. symtablestack.push(pu.u.globalsymtable);
  1528. sym:=tsym(unitsyms.find(pu.u.modulename^));
  1529. if assigned(sym) and not assigned(tunitsym(sym).module) then
  1530. tunitsym(sym).module:=pu.u;
  1531. pu:=tused_unit(pu.next);
  1532. end;
  1533. unitsyms.free;
  1534. if assigned(hmodule.globalsymtable) then
  1535. symtablestack.push(hmodule.globalsymtable);
  1536. { push the localsymtable if needed }
  1537. if ((hmodule<>current_module) or not current_module.in_interface)
  1538. and assigned(hmodule.localsymtable) then
  1539. symtablestack.push(hmodule.localsymtable);
  1540. end;
  1541. procedure specialization_done(var state: tspecializationstate);
  1542. begin
  1543. { Restore symtablestack }
  1544. current_module.extendeddefs.free;
  1545. current_module.extendeddefs:=state.oldextendeddefs;
  1546. current_module.genericdummysyms.free;
  1547. current_module.genericdummysyms:=state.oldgenericdummysyms;
  1548. symtablestack.free;
  1549. symtablestack:=state.oldsymtablestack;
  1550. { clear the state record to be on the safe side }
  1551. fillchar(state, sizeof(state), 0);
  1552. end;
  1553. {****************************************************************************
  1554. SPECIALIZATION BODY GENERATION
  1555. ****************************************************************************}
  1556. procedure process_procdef(def:tprocdef;hmodule:tmodule);
  1557. var
  1558. oldcurrent_filepos : tfileposinfo;
  1559. begin
  1560. if assigned(def.genericdef) and
  1561. (def.genericdef.typ=procdef) and
  1562. assigned(tprocdef(def.genericdef).generictokenbuf) then
  1563. begin
  1564. if not assigned(tprocdef(def.genericdef).generictokenbuf) then
  1565. internalerror(2015061902);
  1566. oldcurrent_filepos:=current_filepos;
  1567. current_filepos:=tprocdef(def.genericdef).fileinfo;
  1568. { use the index the module got from the current compilation process }
  1569. current_filepos.moduleindex:=hmodule.unit_index;
  1570. current_tokenpos:=current_filepos;
  1571. current_scanner.startreplaytokens(tprocdef(def.genericdef).generictokenbuf);
  1572. read_proc_body(def);
  1573. current_filepos:=oldcurrent_filepos;
  1574. end
  1575. { synthetic routines will be implemented afterwards }
  1576. else if def.synthetickind=tsk_none then
  1577. MessagePos1(def.fileinfo,sym_e_forward_not_resolved,def.fullprocname(false));
  1578. end;
  1579. function process_abstractrecorddef(def:tabstractrecorddef):boolean;
  1580. var
  1581. i : longint;
  1582. hp : tdef;
  1583. hmodule : tmodule;
  1584. begin
  1585. result:=true;
  1586. hmodule:=find_module_from_symtable(def.genericdef.owner);
  1587. if hmodule=nil then
  1588. internalerror(201202041);
  1589. for i:=0 to def.symtable.DefList.Count-1 do
  1590. begin
  1591. hp:=tdef(def.symtable.DefList[i]);
  1592. if hp.typ=procdef then
  1593. begin
  1594. { only generate the code if we need a body }
  1595. if assigned(tprocdef(hp).struct) and not tprocdef(hp).forwarddef then
  1596. continue;
  1597. { and the body is available already (which is implicitely the
  1598. case if the generic routine is part of another unit) }
  1599. if ((hmodule=current_module) or (hmodule.state=ms_compile)) and
  1600. { may not be assigned in case it's a synthetic procdef that
  1601. still needs to be generated }
  1602. assigned(tprocdef(hp).genericdef) and
  1603. tprocdef(tprocdef(hp).genericdef).forwarddef then
  1604. begin
  1605. result:=false;
  1606. continue;
  1607. end;
  1608. process_procdef(tprocdef(hp),hmodule);
  1609. end
  1610. else
  1611. if hp.typ in [objectdef,recorddef] then
  1612. { generate code for subtypes as well }
  1613. result:=process_abstractrecorddef(tabstractrecorddef(hp)) and result;
  1614. end;
  1615. end;
  1616. procedure generate_specialization_procs;
  1617. var
  1618. i : longint;
  1619. list,
  1620. readdlist : tfpobjectlist;
  1621. def : tstoreddef;
  1622. state : tspecializationstate;
  1623. hmodule : tmodule;
  1624. begin
  1625. { first copy all entries and then work with that list to ensure that
  1626. we don't get an infinite recursion }
  1627. list:=tfpobjectlist.create(false);
  1628. readdlist:=tfpobjectlist.create(false);
  1629. for i:=0 to current_module.pendingspecializations.Count-1 do
  1630. list.add(current_module.pendingspecializations.Items[i]);
  1631. current_module.pendingspecializations.clear;
  1632. for i:=0 to list.count-1 do
  1633. begin
  1634. def:=tstoreddef(list[i]);
  1635. if not tstoreddef(def).is_specialization then
  1636. continue;
  1637. case def.typ of
  1638. procdef:
  1639. begin
  1640. { the use of forwarddef should not backfire as the
  1641. specialization always belongs to the current module }
  1642. if not tprocdef(def).forwarddef then
  1643. continue;
  1644. if not assigned(def.genericdef) then
  1645. internalerror(2015061903);
  1646. hmodule:=find_module_from_symtable(def.genericdef.owner);
  1647. if hmodule=nil then
  1648. internalerror(2015061904);
  1649. { we need to check for a forward declaration only if the
  1650. generic was declared in the same unit (otherwise there
  1651. should be one) }
  1652. if ((hmodule=current_module) or (hmodule.state=ms_compile)) and tprocdef(def.genericdef).forwarddef then
  1653. begin
  1654. readdlist.add(def);
  1655. continue;
  1656. end;
  1657. specialization_init(tstoreddef(def).genericdef,state);
  1658. process_procdef(tprocdef(def),hmodule);
  1659. specialization_done(state);
  1660. end;
  1661. recorddef,
  1662. objectdef:
  1663. begin
  1664. specialization_init(tstoreddef(def).genericdef,state);
  1665. if not process_abstractrecorddef(tabstractrecorddef(def)) then
  1666. readdlist.add(def);
  1667. specialization_done(state);
  1668. end;
  1669. else
  1670. ;
  1671. end;
  1672. end;
  1673. { add those defs back to the pending list for which we don't yet have
  1674. all method bodies }
  1675. for i:=0 to readdlist.count-1 do
  1676. current_module.pendingspecializations.add(tstoreddef(readdlist[i]).typename,readdlist[i]);
  1677. readdlist.free;
  1678. list.free;
  1679. end;
  1680. procedure maybe_add_pending_specialization(def:tdef);
  1681. var
  1682. hmodule : tmodule;
  1683. st : tsymtable;
  1684. begin
  1685. if parse_generic then
  1686. exit;
  1687. st:=def.owner;
  1688. while st.symtabletype in [localsymtable] do
  1689. st:=st.defowner.owner;
  1690. hmodule:=find_module_from_symtable(st);
  1691. if tstoreddef(def).is_specialization and (hmodule=current_module) then
  1692. current_module.pendingspecializations.add(def.typename,def);
  1693. end;
  1694. end.