pgenutil.pas 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434
  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;parsedtype:tdef;symname:string;parsedpos:tfileposinfo):tdef;
  34. function generate_specialization_phase2(context:tspecializationcontext;genericdef:tstoreddef;parse_class_parent:boolean;_prettyname:ansistring):tdef;
  35. function parse_generic_parameters(allowconstraints:boolean):tfphashobjectlist;
  36. function parse_generic_specialization_types(genericdeflist:tfpobjectlist;poslist:tfplist;out prettyname,specializename:ansistring):boolean;
  37. procedure insert_generic_parameter_types(def:tstoreddef;genericdef:tstoreddef;genericlist:tfphashobjectlist);
  38. procedure maybe_insert_generic_rename_symbol(const name:tidstring;genericlist:tfphashobjectlist);
  39. function generate_generic_name(const name:tidstring;specializename:ansistring;owner_hierarchy:string):tidstring;
  40. procedure split_generic_name(const name:tidstring;out nongeneric:string;out count:longint);
  41. function resolve_generic_dummysym(const name:tidstring):tsym;
  42. function could_be_generic(const name:tidstring):boolean;inline;
  43. procedure specialization_init(genericdef:tdef;var state:tspecializationstate);
  44. procedure specialization_done(var state:tspecializationstate);
  45. implementation
  46. uses
  47. { common }
  48. cutils,fpccrc,
  49. { global }
  50. globals,tokens,verbose,finput,
  51. { symtable }
  52. symconst,symsym,symtable,defcmp,procinfo,
  53. { modules }
  54. fmodule,
  55. { pass 1 }
  56. htypechk,
  57. node,nobj,nmem,
  58. { parser }
  59. scanner,
  60. pbase,pexpr,pdecsub,ptype;
  61. procedure maybe_add_waiting_unit(tt:tdef);
  62. var
  63. hmodule : tmodule;
  64. begin
  65. if not assigned(tt) or
  66. not (df_generic in tt.defoptions) then
  67. exit;
  68. hmodule:=find_module_from_symtable(tt.owner);
  69. if not assigned(hmodule) then
  70. internalerror(2012092401);
  71. if hmodule=current_module then
  72. exit;
  73. if hmodule.state<>ms_compiled then
  74. begin
  75. {$ifdef DEBUG_UNITWAITING}
  76. Writeln('Unit ', current_module.modulename^,
  77. ' waiting for ', hmodule.modulename^);
  78. {$endif DEBUG_UNITWAITING}
  79. if current_module.waitingforunit.indexof(hmodule)<0 then
  80. current_module.waitingforunit.add(hmodule);
  81. if hmodule.waitingunits.indexof(current_module)<0 then
  82. hmodule.waitingunits.add(current_module);
  83. end;
  84. end;
  85. function check_generic_constraints(genericdef:tstoreddef;paradeflist:tfpobjectlist;poslist:tfplist):boolean;
  86. var
  87. i,j,
  88. intfcount : longint;
  89. formaldef,
  90. paradef : tstoreddef;
  91. objdef,
  92. paraobjdef,
  93. formalobjdef : tobjectdef;
  94. intffound : boolean;
  95. filepos : tfileposinfo;
  96. begin
  97. { check whether the given specialization parameters fit to the eventual
  98. constraints of the generic }
  99. if not assigned(genericdef.genericparas) or (genericdef.genericparas.count=0) then
  100. internalerror(2012101001);
  101. if genericdef.genericparas.count<>paradeflist.count then
  102. internalerror(2012101002);
  103. if paradeflist.count<>poslist.count then
  104. internalerror(2012120801);
  105. result:=true;
  106. for i:=0 to genericdef.genericparas.count-1 do
  107. begin
  108. filepos:=pfileposinfo(poslist[i])^;
  109. formaldef:=tstoreddef(ttypesym(genericdef.genericparas[i]).typedef);
  110. if formaldef.typ=undefineddef then
  111. { the parameter is of unspecified type, so no need to check }
  112. continue;
  113. if not (df_genconstraint in formaldef.defoptions) or
  114. not assigned(formaldef.genconstraintdata) then
  115. internalerror(2013021602);
  116. paradef:=tstoreddef(paradeflist[i]);
  117. { undefineddef is compatible with anything }
  118. if formaldef.typ=undefineddef then
  119. continue;
  120. if paradef.typ<>formaldef.typ then
  121. begin
  122. case formaldef.typ of
  123. recorddef:
  124. MessagePos(filepos,type_e_record_type_expected);
  125. objectdef:
  126. case tobjectdef(formaldef).objecttype of
  127. odt_class,
  128. odt_javaclass:
  129. MessagePos1(filepos,type_e_class_type_expected,paradef.typename);
  130. odt_interfacecom,
  131. odt_interfacecorba,
  132. odt_dispinterface,
  133. odt_interfacejava:
  134. MessagePos1(filepos,type_e_interface_type_expected,paradef.typename);
  135. else
  136. internalerror(2012101003);
  137. end;
  138. errordef:
  139. { ignore }
  140. ;
  141. else
  142. internalerror(2012101004);
  143. end;
  144. result:=false;
  145. end
  146. else
  147. begin
  148. { the paradef types are the same, so do special checks for the
  149. cases in which they are needed }
  150. if formaldef.typ=objectdef then
  151. begin
  152. paraobjdef:=tobjectdef(paradef);
  153. formalobjdef:=tobjectdef(formaldef);
  154. if not (formalobjdef.objecttype in [odt_class,odt_javaclass,odt_interfacecom,odt_interfacecorba,odt_interfacejava,odt_dispinterface]) then
  155. internalerror(2012101102);
  156. if formalobjdef.objecttype in [odt_interfacecom,odt_interfacecorba,odt_interfacejava,odt_dispinterface] then
  157. begin
  158. { this is either a concerete interface or class type (the
  159. latter without specific implemented interfaces) }
  160. case paraobjdef.objecttype of
  161. odt_interfacecom,
  162. odt_interfacecorba,
  163. odt_interfacejava,
  164. odt_dispinterface:
  165. begin
  166. if (oo_is_forward in paraobjdef.objectoptions) and
  167. (paraobjdef.objecttype=formalobjdef.objecttype) and
  168. (df_genconstraint in formalobjdef.defoptions) and
  169. (
  170. (formalobjdef.objecttype=odt_interfacecom) and
  171. (formalobjdef.childof=interface_iunknown)
  172. )
  173. or
  174. (
  175. (formalobjdef.objecttype=odt_interfacecorba) and
  176. (formalobjdef.childof=nil)
  177. ) then
  178. continue;
  179. if not def_is_related(paraobjdef,formalobjdef.childof) then
  180. begin
  181. MessagePos2(filepos,type_e_incompatible_types,paraobjdef.typename,formalobjdef.childof.typename);
  182. result:=false;
  183. end;
  184. end;
  185. odt_class,
  186. odt_javaclass:
  187. begin
  188. objdef:=paraobjdef;
  189. intffound:=false;
  190. while assigned(objdef) do
  191. begin
  192. for j:=0 to objdef.implementedinterfaces.count-1 do
  193. if timplementedinterface(objdef.implementedinterfaces[j]).intfdef=formalobjdef.childof then
  194. begin
  195. intffound:=true;
  196. break;
  197. end;
  198. if intffound then
  199. break;
  200. objdef:=objdef.childof;
  201. end;
  202. result:=intffound;
  203. if not result then
  204. MessagePos2(filepos,parser_e_class_doesnt_implement_interface,paraobjdef.typename,formalobjdef.childof.typename);
  205. end;
  206. else
  207. begin
  208. MessagePos1(filepos,type_e_class_or_interface_type_expected,paraobjdef.typename);
  209. result:=false;
  210. end;
  211. end;
  212. end
  213. else
  214. begin
  215. { this is either a "class" or a concrete instance with
  216. or without implemented interfaces }
  217. if not (paraobjdef.objecttype in [odt_class,odt_javaclass]) then
  218. begin
  219. MessagePos1(filepos,type_e_class_type_expected,paraobjdef.typename);
  220. result:=false;
  221. continue;
  222. end;
  223. { for forward declared classes we allow pure TObject/class declarations }
  224. if (oo_is_forward in paraobjdef.objectoptions) and
  225. (df_genconstraint in formaldef.defoptions) then
  226. begin
  227. if (formalobjdef.childof=class_tobject) and
  228. not formalobjdef.implements_any_interfaces then
  229. continue;
  230. end;
  231. if assigned(formalobjdef.childof) and
  232. not def_is_related(paradef,formalobjdef.childof) then
  233. begin
  234. MessagePos2(filepos,type_e_incompatible_types,paraobjdef.typename,formalobjdef.childof.typename);
  235. result:=false;
  236. end;
  237. intfcount:=0;
  238. for j:=0 to formalobjdef.implementedinterfaces.count-1 do
  239. begin
  240. objdef:=paraobjdef;
  241. while assigned(objdef) do
  242. begin
  243. intffound:=assigned(
  244. find_implemented_interface(objdef,
  245. timplementedinterface(formalobjdef.implementedinterfaces[j]).intfdef
  246. )
  247. );
  248. if intffound then
  249. break;
  250. objdef:=objdef.childof;
  251. end;
  252. if intffound then
  253. inc(intfcount)
  254. else
  255. MessagePos2(filepos,parser_e_class_doesnt_implement_interface,paraobjdef.typename,timplementedinterface(formalobjdef.implementedinterfaces[j]).intfdef.typename);
  256. end;
  257. if intfcount<>formalobjdef.implementedinterfaces.count then
  258. result:=false;
  259. end;
  260. end;
  261. end;
  262. end;
  263. end;
  264. function parse_generic_specialization_types_internal(genericdeflist:tfpobjectlist;poslist:tfplist;out prettyname,specializename:ansistring;parsedtype:tdef;parsedpos:tfileposinfo):boolean;
  265. var
  266. old_block_type : tblock_type;
  267. first : boolean;
  268. typeparam : tnode;
  269. parampos : pfileposinfo;
  270. tmpparampos : tfileposinfo;
  271. namepart : string;
  272. prettynamepart : ansistring;
  273. begin
  274. result:=true;
  275. if genericdeflist=nil then
  276. internalerror(2012061401);
  277. { set the block type to type, so that the parsed type are returned as
  278. ttypenode (e.g. classes are in non type-compatible blocks returned as
  279. tloadvmtaddrnode) }
  280. old_block_type:=block_type;
  281. { if parsedtype is set, then the first type identifer was already parsed
  282. (happens in inline specializations) and thus we only need to parse
  283. the remaining types and do as if the first one was already given }
  284. first:=not assigned(parsedtype);
  285. if assigned(parsedtype) then
  286. begin
  287. genericdeflist.Add(parsedtype);
  288. specializename:='$'+parsedtype.fulltypename;
  289. prettyname:=parsedtype.typesym.prettyname;
  290. if assigned(poslist) then
  291. begin
  292. New(parampos);
  293. parampos^:=parsedpos;
  294. poslist.add(parampos);
  295. end;
  296. end
  297. else
  298. begin
  299. specializename:='';
  300. prettyname:='';
  301. end;
  302. while not (token in [_GT,_RSHARPBRACKET]) do
  303. begin
  304. { "first" is set to false at the end of the loop! }
  305. if not first then
  306. consume(_COMMA);
  307. block_type:=bt_type;
  308. tmpparampos:=current_filepos;
  309. typeparam:=factor(false,true);
  310. if typeparam.nodetype=typen then
  311. begin
  312. if tstoreddef(typeparam.resultdef).is_generic and
  313. (
  314. not parse_generic or
  315. not defs_belong_to_same_generic(typeparam.resultdef,current_genericdef)
  316. ) then
  317. Message(parser_e_no_generics_as_params);
  318. if assigned(poslist) then
  319. begin
  320. New(parampos);
  321. parampos^:=tmpparampos;
  322. poslist.add(parampos);
  323. end;
  324. genericdeflist.Add(typeparam.resultdef);
  325. if not assigned(typeparam.resultdef.typesym) then
  326. message(type_e_generics_cannot_reference_itself)
  327. else
  328. begin
  329. { we use the full name of the type to uniquely identify it }
  330. if (symtablestack.top.symtabletype=parasymtable) and
  331. (typeparam.resultdef.owner=symtablestack.top) then
  332. begin
  333. { special handling for specializations inside generic function declarations }
  334. if symtablestack.top.defowner.typ<>procdef then
  335. internalerror(2015080101);
  336. str(symtablestack.top.defowner.defid,namepart);
  337. namepart:='genproc'+namepart+'_'+tdef(symtablestack.top.defowner).fullownerhierarchyname+'_'+tprocdef(symtablestack.top.defowner).procsym.realname+'_'+typeparam.resultdef.typename;
  338. prettynamepart:=tdef(symtablestack.top.defowner).fullownerhierarchyname+tprocdef(symtablestack.top.defowner).procsym.prettyname;
  339. end
  340. else
  341. begin
  342. namepart:=typeparam.resultdef.fulltypename;
  343. prettynamepart:=typeparam.resultdef.fullownerhierarchyname;
  344. end;
  345. specializename:=specializename+'$'+namepart;
  346. if not first then
  347. prettyname:=prettyname+',';
  348. prettyname:=prettyname+prettynamepart+typeparam.resultdef.typesym.prettyname;
  349. end;
  350. end
  351. else
  352. begin
  353. Message(type_e_type_id_expected);
  354. result:=false;
  355. end;
  356. typeparam.free;
  357. first:=false;
  358. end;
  359. block_type:=old_block_type;
  360. end;
  361. function parse_generic_specialization_types(genericdeflist:tfpobjectlist;poslist:tfplist;out prettyname,specializename:ansistring):boolean;
  362. var
  363. dummypos : tfileposinfo;
  364. begin
  365. FillChar(dummypos, SizeOf(tfileposinfo), 0);
  366. result:=parse_generic_specialization_types_internal(genericdeflist,poslist,prettyname,specializename,nil,dummypos);
  367. end;
  368. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;_prettyname:string);
  369. var
  370. dummypos : tfileposinfo;
  371. begin
  372. FillChar(dummypos, SizeOf(tfileposinfo), 0);
  373. generate_specialization(tt,parse_class_parent,_prettyname,nil,'',dummypos);
  374. end;
  375. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef):tdef;
  376. var
  377. dummypos : tfileposinfo;
  378. {$push}
  379. {$warn 5036 off}
  380. begin
  381. result:=generate_specialization_phase1(context,genericdef,nil,'',dummypos);
  382. end;
  383. {$pop}
  384. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef;parsedtype:tdef;symname:string;parsedpos:tfileposinfo):tdef;
  385. var
  386. pt2 : tnode;
  387. errorrecovery,
  388. found,
  389. first,
  390. err : boolean;
  391. i,
  392. gencount : longint;
  393. def : tstoreddef;
  394. countstr,genname,ugenname : string;
  395. srsym : tsym;
  396. st : tsymtable;
  397. begin
  398. context:=nil;
  399. result:=nil;
  400. { either symname must be given or genericdef needs to be valid }
  401. errorrecovery:=false;
  402. if (symname='') and
  403. (not assigned(genericdef) or
  404. not assigned(genericdef.typesym) or
  405. (genericdef.typesym.typ<>typesym)) then
  406. begin
  407. errorrecovery:=true;
  408. result:=generrordef;
  409. end;
  410. { Only parse the parameters for recovery or
  411. for recording in genericbuf }
  412. if errorrecovery then
  413. begin
  414. first:=assigned(parsedtype);
  415. if not first and not try_to_consume(_LT) then
  416. consume(_LSHARPBRACKET);
  417. gencount:=0;
  418. { handle "<>" }
  419. if not first and ((token=_RSHARPBRACKET) or (token=_GT)) then
  420. Message(type_e_type_id_expected)
  421. else
  422. repeat
  423. if not first then
  424. begin
  425. pt2:=factor(false,true);
  426. pt2.free;
  427. end;
  428. first:=false;
  429. inc(gencount);
  430. until not try_to_consume(_COMMA);
  431. if not try_to_consume(_GT) then
  432. consume(_RSHARPBRACKET);
  433. { we need to return a def that can later pass some checks like
  434. whether it's an interface or not }
  435. if not errorrecovery and
  436. (not assigned(result) or (result.typ=undefineddef)) then
  437. begin
  438. if (symname='') and tstoreddef(genericdef).is_generic then
  439. { this happens in non-Delphi modes }
  440. result:=genericdef
  441. else
  442. begin
  443. { find the corresponding generic symbol so that any checks
  444. done on the returned def will be handled correctly }
  445. str(gencount,countstr);
  446. if symname='' then
  447. genname:=ttypesym(genericdef.typesym).realname
  448. else
  449. genname:=symname;
  450. genname:=genname+'$'+countstr;
  451. ugenname:=upper(genname);
  452. { first check whether the found name is the same as that of
  453. the current def or one of its (generic) surrounding defs;
  454. this is necessary as the symbol of the generic can not yet
  455. be used for lookup as it still contains a reference to an
  456. errordef) }
  457. def:=current_genericdef;
  458. repeat
  459. if def.typ in [objectdef,recorddef] then
  460. if tabstractrecorddef(def).objname^=ugenname then
  461. begin
  462. result:=def;
  463. break;
  464. end;
  465. def:=tstoreddef(def.owner.defowner);
  466. until not assigned(def) or not (df_generic in def.defoptions);
  467. { it's not part of the current object hierarchy, so search
  468. for the symbol }
  469. if not assigned(result) then
  470. begin
  471. srsym:=nil;
  472. if not searchsym(ugenname,srsym,st) or
  473. (srsym.typ<>typesym) then
  474. begin
  475. identifier_not_found(genname);
  476. result:=generrordef;
  477. exit;
  478. end;
  479. result:=ttypesym(srsym).typedef;
  480. { this happens in non-Delphi modes if we encounter a
  481. specialization of the generic class or record we're
  482. currently parsing }
  483. if (result.typ=errordef) and assigned(current_structdef) and
  484. (current_structdef.objname^=ugenname) then
  485. result:=current_structdef;
  486. end;
  487. end;
  488. end;
  489. exit;
  490. end;
  491. if not assigned(parsedtype) and not try_to_consume(_LT) then
  492. begin
  493. consume(_LSHARPBRACKET);
  494. { handle "<>" }
  495. if (token=_GT) or (token=_RSHARPBRACKET) then
  496. begin
  497. Message(type_e_type_id_expected);
  498. if not try_to_consume(_GT) then
  499. try_to_consume(_RSHARPBRACKET);
  500. result:=generrordef;
  501. exit;
  502. end;
  503. end;
  504. context:=tspecializationcontext.create;
  505. { Parse type parameters }
  506. err:=not parse_generic_specialization_types_internal(context.genericdeflist,context.poslist,context.prettyname,context.specializename,parsedtype,parsedpos);
  507. if err then
  508. begin
  509. if not try_to_consume(_GT) then
  510. try_to_consume(_RSHARPBRACKET);
  511. context.free;
  512. context:=nil;
  513. result:=generrordef;
  514. exit;
  515. end;
  516. { use the name of the symbol as procvars return a user friendly version
  517. of the name }
  518. if symname='' then
  519. genname:=ttypesym(genericdef.typesym).realname
  520. else
  521. genname:=symname;
  522. { in case of non-Delphi mode the type name could already be a generic
  523. def (but maybe the wrong one) }
  524. if assigned(genericdef) and
  525. ([df_generic,df_specialization]*genericdef.defoptions<>[]) then
  526. begin
  527. { remove the type count suffix from the generic's name }
  528. for i:=Length(genname) downto 1 do
  529. if genname[i]='$' then
  530. begin
  531. genname:=copy(genname,1,i-1);
  532. break;
  533. end;
  534. { in case of a specialization we've only reached the specialization
  535. checksum yet }
  536. if df_specialization in genericdef.defoptions then
  537. for i:=length(genname) downto 1 do
  538. if genname[i]='$' then
  539. begin
  540. genname:=copy(genname,1,i-1);
  541. break;
  542. end;
  543. end
  544. else
  545. begin
  546. split_generic_name(genname,ugenname,gencount);
  547. if genname<>ugenname then
  548. genname:=ugenname;
  549. end;
  550. { search a generic with the given count of params }
  551. countstr:='';
  552. str(context.genericdeflist.Count,countstr);
  553. genname:=genname+'$'+countstr;
  554. ugenname:=upper(genname);
  555. context.genname:=genname;
  556. if assigned(genericdef) and (genericdef.owner.symtabletype in [objectsymtable,recordsymtable]) then
  557. begin
  558. if genericdef.owner.symtabletype = objectsymtable then
  559. found:=searchsym_in_class(tobjectdef(genericdef.owner.defowner),tobjectdef(genericdef.owner.defowner),ugenname,context.sym,context.symtable,[])
  560. else
  561. found:=searchsym_in_record(tabstractrecorddef(genericdef.owner.defowner),ugenname,context.sym,context.symtable);
  562. if not found then
  563. found:=searchsym(ugenname,context.sym,context.symtable);
  564. end
  565. else
  566. found:=searchsym(ugenname,context.sym,context.symtable);
  567. if not found or (context.sym.typ<>typesym) then
  568. begin
  569. identifier_not_found(genname);
  570. if not try_to_consume(_GT) then
  571. try_to_consume(_RSHARPBRACKET);
  572. context.free;
  573. context:=nil;
  574. result:=generrordef;
  575. exit;
  576. end;
  577. { we've found the correct def }
  578. result:=tstoreddef(ttypesym(context.sym).typedef);
  579. if not try_to_consume(_GT) then
  580. consume(_RSHARPBRACKET);
  581. end;
  582. function generate_specialization_phase2(context:tspecializationcontext;genericdef:tstoreddef;parse_class_parent:boolean;_prettyname:ansistring):tdef;
  583. procedure unset_forwarddef(def: tdef);
  584. var
  585. st : TSymtable;
  586. i : longint;
  587. begin
  588. case def.typ of
  589. procdef:
  590. tprocdef(def).forwarddef:=false;
  591. objectdef,
  592. recorddef:
  593. begin
  594. st:=def.getsymtable(gs_record);
  595. for i:=0 to st.deflist.count-1 do
  596. unset_forwarddef(tdef(st.deflist[i]));
  597. end;
  598. end;
  599. end;
  600. var
  601. finalspecializename,
  602. ufinalspecializename : tidstring;
  603. prettyname : ansistring;
  604. generictypelist : tfphashobjectlist;
  605. st,
  606. specializest : tsymtable;
  607. hashedid : thashedidstring;
  608. tempst : tglobalsymtable;
  609. srsym : tsym;
  610. def : tdef;
  611. old_block_type : tblock_type;
  612. state : tspecializationstate;
  613. old_current_structdef : tabstractrecorddef;
  614. old_current_specializedef,
  615. old_current_genericdef : tstoreddef;
  616. hmodule : tmodule;
  617. oldcurrent_filepos : tfileposinfo;
  618. recordbuf : tdynamicarray;
  619. hadtypetoken : boolean;
  620. vmtbuilder : tvmtbuilder;
  621. i,
  622. replaydepth : longint;
  623. item : tobject;
  624. hintsprocessed : boolean;
  625. begin
  626. if not assigned(context) then
  627. internalerror(2015052203);
  628. result:=nil;
  629. if not check_generic_constraints(genericdef,context.genericdeflist,context.poslist) then
  630. begin
  631. { the parameters didn't fit the constraints, so don't continue with the
  632. specialization }
  633. result:=generrordef;
  634. exit;
  635. end;
  636. { build the new type's name }
  637. finalspecializename:=generate_generic_name(context.genname,context.specializename,genericdef.ownerhierarchyname);
  638. ufinalspecializename:=upper(finalspecializename);
  639. prettyname:=genericdef.typesym.prettyname+'<'+context.prettyname+'>';
  640. { select the symtable containing the params }
  641. case genericdef.typ of
  642. procdef:
  643. st:=genericdef.GetSymtable(gs_para);
  644. objectdef,
  645. recorddef:
  646. st:=genericdef.GetSymtable(gs_record);
  647. arraydef:
  648. st:=tarraydef(genericdef).symtable;
  649. procvardef:
  650. st:=genericdef.GetSymtable(gs_para);
  651. else
  652. internalerror(200511182);
  653. end;
  654. generictypelist:=tfphashobjectlist.create(false);
  655. { build the list containing the types for the generic params }
  656. if not assigned(genericdef.genericparas) then
  657. internalerror(2013092601);
  658. if context.genericdeflist.count<>genericdef.genericparas.count then
  659. internalerror(2013092603);
  660. for i:=0 to genericdef.genericparas.Count-1 do
  661. begin
  662. srsym:=tsym(genericdef.genericparas[i]);
  663. if not (sp_generic_para in srsym.symoptions) then
  664. internalerror(2013092602);
  665. generictypelist.add(srsym.realname,tdef(context.genericdeflist[i]).typesym);
  666. end;
  667. { Special case if we are referencing the current defined object }
  668. if assigned(current_structdef) and
  669. (current_structdef.objname^=ufinalspecializename) then
  670. result:=current_structdef;
  671. { Can we reuse an already specialized type? }
  672. { for this first check whether we are currently specializing a nested
  673. type of the current (main) specialization (this is necessary, because
  674. during that time the symbol of the main specialization will still
  675. contain a reference to an errordef) }
  676. if not assigned(result) and assigned(current_specializedef) then
  677. begin
  678. def:=current_specializedef;
  679. repeat
  680. if def.typ in [objectdef,recorddef] then
  681. if tabstractrecorddef(def).objname^=ufinalspecializename then begin
  682. result:=def;
  683. break;
  684. end;
  685. def:=tstoreddef(def.owner.defowner);
  686. until not assigned(def) or not (df_specialization in def.defoptions);
  687. end;
  688. { if the genericdef is the def we are currently parsing (or one of its parents) then we can
  689. not use it for specializing as the tokenbuffer is not yet set (and we aren't done with
  690. parsing anyway), so for now we treat those still as generic defs without doing a partial
  691. specialization }
  692. if not assigned(result) then
  693. begin
  694. def:=current_genericdef;
  695. while assigned(def) and (def.typ in [recorddef,objectdef]) do
  696. begin
  697. if def=genericdef then
  698. begin
  699. result:=def;
  700. break;
  701. end;
  702. def:=tstoreddef(def.owner.defowner);
  703. end;
  704. end;
  705. { decide in which symtable to put the specialization }
  706. if parse_generic and not assigned(result) then
  707. begin
  708. if not assigned(current_genericdef) then
  709. internalerror(2014050901);
  710. if assigned(current_procinfo) and (df_generic in current_procinfo.procdef.defoptions) then
  711. { if we are parsing the definition of a method we specialize into
  712. the local symtable of it }
  713. specializest:=current_procinfo.procdef.getsymtable(gs_local)
  714. else
  715. { we specialize the partial specialization into the symtable of the currently parsed
  716. generic }
  717. case current_genericdef.typ of
  718. procvardef,
  719. procdef:
  720. specializest:=current_genericdef.getsymtable(gs_local);
  721. objectdef,
  722. recorddef:
  723. specializest:=current_genericdef.getsymtable(gs_record);
  724. arraydef:
  725. specializest:=tarraydef(current_genericdef).symtable;
  726. else
  727. internalerror(2014050902);
  728. end;
  729. end
  730. else
  731. if current_module.is_unit and current_module.in_interface then
  732. specializest:=current_module.globalsymtable
  733. else
  734. specializest:=current_module.localsymtable;
  735. if not assigned(specializest) then
  736. internalerror(2014050910);
  737. { now check whether there is a specialization somewhere else }
  738. if not assigned(result) then
  739. begin
  740. hashedid.id:=ufinalspecializename;
  741. srsym:=tsym(specializest.findwithhash(hashedid));
  742. if assigned(srsym) then
  743. begin
  744. if srsym.typ<>typesym then
  745. internalerror(200710171);
  746. result:=ttypesym(srsym).typedef;
  747. end
  748. else
  749. { the generic could have been specialized in the globalsymtable
  750. already, so search there as well }
  751. if (specializest<>current_module.globalsymtable) and assigned(current_module.globalsymtable) then
  752. begin
  753. srsym:=tsym(current_module.globalsymtable.findwithhash(hashedid));
  754. if assigned(srsym) then
  755. begin
  756. if srsym.typ<>typesym then
  757. internalerror(2011121101);
  758. result:=ttypesym(srsym).typedef;
  759. end;
  760. end;
  761. end;
  762. if not assigned(result) then
  763. begin
  764. specialization_init(genericdef,state);
  765. { push a temporary global symtable so that the specialization is
  766. added to the correct symtable; this symtable does not contain
  767. any other symbols, so that the type resolution can not be
  768. influenced by symbols in the current unit }
  769. tempst:=tspecializesymtable.create(current_module.modulename^,current_module.moduleid);
  770. symtablestack.push(tempst);
  771. { Reparse the original type definition }
  772. begin
  773. old_current_specializedef:=nil;
  774. old_current_genericdef:=nil;
  775. old_current_structdef:=nil;
  776. if parse_class_parent then
  777. begin
  778. old_current_structdef:=current_structdef;
  779. old_current_genericdef:=current_genericdef;
  780. old_current_specializedef:=current_specializedef;
  781. if genericdef.owner.symtabletype in [recordsymtable,objectsymtable] then
  782. current_structdef:=tabstractrecorddef(genericdef.owner.defowner)
  783. else
  784. current_structdef:=nil;
  785. current_genericdef:=nil;
  786. current_specializedef:=nil;
  787. end;
  788. maybe_add_waiting_unit(genericdef);
  789. { First a new typesym so we can reuse this specialization and
  790. references to this specialization can be handled }
  791. srsym:=ctypesym.create(finalspecializename,generrordef,true);
  792. specializest.insert(srsym);
  793. { specializations are declarations as such it is the wisest to
  794. declare set the blocktype to "type"; otherwise we'll
  795. experience unexpected side effects like the addition of
  796. classrefdefs if we have a generic that's derived from another
  797. generic }
  798. old_block_type:=block_type;
  799. block_type:=bt_type;
  800. if not assigned(genericdef.generictokenbuf) then
  801. internalerror(200511171);
  802. hmodule:=find_module_from_symtable(genericdef.owner);
  803. if hmodule=nil then
  804. internalerror(2012051202);
  805. oldcurrent_filepos:=current_filepos;
  806. { use the index the module got from the current compilation process }
  807. current_filepos.moduleindex:=hmodule.unit_index;
  808. current_tokenpos:=current_filepos;
  809. if parse_generic then
  810. begin
  811. recordbuf:=current_scanner.recordtokenbuf;
  812. current_scanner.recordtokenbuf:=nil;
  813. end
  814. else
  815. recordbuf:=nil;
  816. replaydepth:=current_scanner.replay_stack_depth;
  817. current_scanner.startreplaytokens(genericdef.generictokenbuf);
  818. hadtypetoken:=false;
  819. read_named_type(result,srsym,genericdef,generictypelist,false,hadtypetoken);
  820. current_filepos:=oldcurrent_filepos;
  821. ttypesym(srsym).typedef:=result;
  822. result.typesym:=srsym;
  823. if _prettyname<>'' then
  824. ttypesym(result.typesym).fprettyname:=_prettyname
  825. else
  826. ttypesym(result.typesym).fprettyname:=prettyname;
  827. { Note regarding hint directives:
  828. There is no need to remove the flags for them from the
  829. specialized generic symbol, because hint directives that
  830. follow the specialization are handled by the code in
  831. pdecl.types_dec and added to the type symbol.
  832. E.g.: TFoo = TBar<Blubb> deprecated;
  833. Here the symbol TBar$1$Blubb will contain the
  834. "sp_hint_deprecated" flag while the TFoo symbol won't.}
  835. case result.typ of
  836. { Build VMT indexes for classes and read hint directives }
  837. objectdef:
  838. begin
  839. if replaydepth>current_scanner.replay_stack_depth then
  840. begin
  841. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  842. if replaydepth>current_scanner.replay_stack_depth then
  843. consume(_SEMICOLON);
  844. end;
  845. vmtbuilder:=TVMTBuilder.Create(tobjectdef(result));
  846. vmtbuilder.generate_vmt;
  847. vmtbuilder.free;
  848. end;
  849. { handle params, calling convention, etc }
  850. procvardef:
  851. begin
  852. hintsprocessed:=false;
  853. if replaydepth>current_scanner.replay_stack_depth then
  854. begin
  855. if not check_proc_directive(true) then
  856. begin
  857. hintsprocessed:=try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg);
  858. if replaydepth>current_scanner.replay_stack_depth then
  859. consume(_SEMICOLON);
  860. end
  861. else
  862. hintsprocessed:=true;
  863. end;
  864. if replaydepth>current_scanner.replay_stack_depth then
  865. parse_var_proc_directives(ttypesym(srsym));
  866. handle_calling_convention(tprocvardef(result));
  867. if not hintsprocessed and (replaydepth>current_scanner.replay_stack_depth) then
  868. begin
  869. try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg);
  870. if replaydepth>current_scanner.replay_stack_depth then
  871. consume(_SEMICOLON);
  872. end;
  873. end;
  874. else
  875. { parse hint directives for records and arrays }
  876. if replaydepth>current_scanner.replay_stack_depth then begin
  877. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  878. if replaydepth>current_scanner.replay_stack_depth then
  879. consume(_SEMICOLON);
  880. end;
  881. end;
  882. { Consume the remainder of the buffer }
  883. while current_scanner.replay_stack_depth>replaydepth do
  884. consume(token);
  885. if assigned(recordbuf) then
  886. begin
  887. if assigned(current_scanner.recordtokenbuf) then
  888. internalerror(2014050909);
  889. current_scanner.recordtokenbuf:=recordbuf;
  890. end;
  891. block_type:=old_block_type;
  892. if parse_class_parent then
  893. begin
  894. current_structdef:=old_current_structdef;
  895. current_genericdef:=old_current_genericdef;
  896. current_specializedef:=old_current_specializedef;
  897. end;
  898. end;
  899. { extract all created symbols and defs from the temporary symtable
  900. and add them to the specializest }
  901. for i:=tempst.SymList.Count-1 downto 0 do
  902. begin
  903. item:=tempst.SymList.Items[i];
  904. { using changeowner the symbol is automatically added to the
  905. new symtable }
  906. tsym(item).ChangeOwner(specializest);
  907. end;
  908. for i:=tempst.DefList.Count-1 downto 0 do
  909. begin
  910. item:=tempst.DefList.Items[i];
  911. { using changeowner the def is automatically added to the new
  912. symtable }
  913. tdef(item).ChangeOwner(specializest);
  914. { for partial specializations we implicitely declare any methods as having their
  915. implementations although we'll not specialize them in reality }
  916. if parse_generic then
  917. unset_forwarddef(tdef(item));
  918. end;
  919. { if a generic was declared during the specialization we need to
  920. flag the specialize symtable accordingly }
  921. if sto_has_generic in tempst.tableoptions then
  922. specializest.includeoption(sto_has_generic);
  923. tempst.free;
  924. specialization_done(state);
  925. end;
  926. generictypelist.free;
  927. if assigned(genericdef) then
  928. begin
  929. { check the hints of the found generic symbol }
  930. srsym:=genericdef.typesym;
  931. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  932. end;
  933. end;
  934. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;_prettyname:string;parsedtype:tdef;symname:string;parsedpos:tfileposinfo);
  935. var
  936. context : tspecializationcontext;
  937. genericdef : tstoreddef;
  938. begin
  939. genericdef:=tstoreddef(generate_specialization_phase1(context,tt,parsedtype,symname,parsedpos));
  940. if genericdef<>generrordef then
  941. genericdef:=tstoreddef(generate_specialization_phase2(context,genericdef,parse_class_parent,_prettyname));
  942. tt:=genericdef;
  943. if assigned(context) then
  944. context.free;
  945. end;
  946. function parse_generic_parameters(allowconstraints:boolean):tfphashobjectlist;
  947. var
  948. generictype : ttypesym;
  949. i,firstidx : longint;
  950. srsymtable : tsymtable;
  951. basedef,def : tdef;
  952. defname : tidstring;
  953. allowconstructor,
  954. doconsume : boolean;
  955. constraintdata : tgenericconstraintdata;
  956. old_block_type : tblock_type;
  957. begin
  958. result:=tfphashobjectlist.create(false);
  959. firstidx:=0;
  960. old_block_type:=block_type;
  961. block_type:=bt_type;
  962. repeat
  963. if token=_ID then
  964. begin
  965. generictype:=ctypesym.create(orgpattern,cundefinedtype,true);
  966. { type parameters need to be added as strict private }
  967. generictype.visibility:=vis_strictprivate;
  968. include(generictype.symoptions,sp_generic_para);
  969. result.add(orgpattern,generictype);
  970. end;
  971. consume(_ID);
  972. if try_to_consume(_COLON) then
  973. begin
  974. if not allowconstraints then
  975. { TODO }
  976. Message(parser_e_illegal_expression{ parser_e_generic_constraints_not_allowed_here});
  977. { construct a name which can be used for a type specification }
  978. constraintdata:=tgenericconstraintdata.create;
  979. defname:='';
  980. str(current_module.deflist.count,defname);
  981. defname:='$gendef'+defname;
  982. allowconstructor:=m_delphi in current_settings.modeswitches;
  983. basedef:=generrordef;
  984. repeat
  985. doconsume:=true;
  986. case token of
  987. _CONSTRUCTOR:
  988. begin
  989. if not allowconstructor or (gcf_constructor in constraintdata.flags) then
  990. Message(parser_e_illegal_expression);
  991. include(constraintdata.flags,gcf_constructor);
  992. allowconstructor:=false;
  993. end;
  994. _CLASS:
  995. begin
  996. if gcf_class in constraintdata.flags then
  997. Message(parser_e_illegal_expression);
  998. if basedef=generrordef then
  999. include(constraintdata.flags,gcf_class)
  1000. else
  1001. Message(parser_e_illegal_expression);
  1002. end;
  1003. _RECORD:
  1004. begin
  1005. if ([gcf_constructor,gcf_class]*constraintdata.flags<>[])
  1006. or (constraintdata.interfaces.count>0) then
  1007. Message(parser_e_illegal_expression)
  1008. else
  1009. begin
  1010. srsymtable:=trecordsymtable.create(defname,0,1,1);
  1011. basedef:=crecorddef.create(defname,srsymtable);
  1012. include(constraintdata.flags,gcf_record);
  1013. allowconstructor:=false;
  1014. end;
  1015. end;
  1016. else
  1017. begin
  1018. { after single_type "token" is the trailing ",", ";" or
  1019. ">"! }
  1020. doconsume:=false;
  1021. { def is already set to a class or record }
  1022. if gcf_record in constraintdata.flags then
  1023. Message(parser_e_illegal_expression);
  1024. single_type(def, [stoAllowSpecialization]);
  1025. { only types that are inheritable are allowed }
  1026. if (def.typ<>objectdef) or
  1027. not (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_interfacejava,odt_javaclass]) then
  1028. Message1(type_e_class_or_interface_type_expected,def.typename)
  1029. else
  1030. case tobjectdef(def).objecttype of
  1031. odt_class,
  1032. odt_javaclass:
  1033. begin
  1034. if gcf_class in constraintdata.flags then
  1035. { "class" + concrete class is not allowed }
  1036. Message(parser_e_illegal_expression)
  1037. else
  1038. { do we already have a concrete class? }
  1039. if basedef<>generrordef then
  1040. Message(parser_e_illegal_expression)
  1041. else
  1042. basedef:=def;
  1043. end;
  1044. odt_interfacecom,
  1045. odt_interfacecorba,
  1046. odt_interfacejava,
  1047. odt_dispinterface:
  1048. constraintdata.interfaces.add(def);
  1049. end;
  1050. end;
  1051. end;
  1052. if doconsume then
  1053. consume(token);
  1054. until not try_to_consume(_COMMA);
  1055. if ([gcf_class,gcf_constructor]*constraintdata.flags<>[]) or
  1056. (constraintdata.interfaces.count>1) or
  1057. (
  1058. (basedef.typ=objectdef) and
  1059. (tobjectdef(basedef).objecttype in [odt_javaclass,odt_class])
  1060. ) then
  1061. begin
  1062. if basedef.typ=errordef then
  1063. { don't pass an errordef as a parent to a tobjectdef }
  1064. basedef:=class_tobject
  1065. else
  1066. if (basedef.typ<>objectdef) or
  1067. not (tobjectdef(basedef).objecttype in [odt_javaclass,odt_class]) then
  1068. internalerror(2012101101);
  1069. basedef:=cobjectdef.create(tobjectdef(basedef).objecttype,defname,tobjectdef(basedef),true);
  1070. for i:=0 to constraintdata.interfaces.count-1 do
  1071. tobjectdef(basedef).implementedinterfaces.add(
  1072. timplementedinterface.create(tobjectdef(constraintdata.interfaces[i])));
  1073. end
  1074. else
  1075. if constraintdata.interfaces.count=1 then
  1076. begin
  1077. if basedef.typ<>errordef then
  1078. internalerror(2013021601);
  1079. def:=tdef(constraintdata.interfaces[0]);
  1080. basedef:=cobjectdef.create(tobjectdef(def).objecttype,defname,tobjectdef(def),true);
  1081. constraintdata.interfaces.delete(0);
  1082. end;
  1083. if basedef.typ<>errordef then
  1084. with tstoreddef(basedef) do
  1085. begin
  1086. genconstraintdata:=tgenericconstraintdata.create;
  1087. genconstraintdata.flags:=constraintdata.flags;
  1088. genconstraintdata.interfaces.assign(constraintdata.interfaces);
  1089. include(defoptions,df_genconstraint);
  1090. end;
  1091. for i:=firstidx to result.count-1 do
  1092. ttypesym(result[i]).typedef:=basedef;
  1093. { we need a typesym in case we do a Delphi-mode inline
  1094. specialization with this parameter; so just use the first sym }
  1095. if not assigned(basedef.typesym) then
  1096. basedef.typesym:=ttypesym(result[firstidx]);
  1097. firstidx:=result.count;
  1098. constraintdata.free;
  1099. end
  1100. else
  1101. begin
  1102. if token=_SEMICOLON then
  1103. begin
  1104. { two different typeless parameters are considered as incompatible }
  1105. for i:=firstidx to result.count-1 do
  1106. begin
  1107. ttypesym(result[i]).typedef:=cundefineddef.create;
  1108. ttypesym(result[i]).typedef.typesym:=ttypesym(result[i]);
  1109. end;
  1110. { a semicolon terminates a type parameter group }
  1111. firstidx:=result.count;
  1112. end;
  1113. end;
  1114. until not (try_to_consume(_COMMA) or try_to_consume(_SEMICOLON));
  1115. { two different typeless parameters are considered as incompatible }
  1116. for i:=firstidx to result.count-1 do
  1117. begin
  1118. ttypesym(result[i]).typedef:=cundefineddef.create;
  1119. ttypesym(result[i]).typedef.typesym:=ttypesym(result[i]);
  1120. end;
  1121. block_type:=old_block_type;
  1122. end;
  1123. procedure insert_generic_parameter_types(def:tstoreddef;genericdef:tstoreddef;genericlist:tfphashobjectlist);
  1124. var
  1125. i : longint;
  1126. generictype,sym : ttypesym;
  1127. st : tsymtable;
  1128. begin
  1129. def.genericdef:=genericdef;
  1130. if not assigned(genericlist) then
  1131. exit;
  1132. if assigned(genericdef) then
  1133. include(def.defoptions,df_specialization)
  1134. else
  1135. if genericlist.count>0 then
  1136. include(def.defoptions,df_generic);
  1137. case def.typ of
  1138. recorddef,objectdef: st:=tabstractrecorddef(def).symtable;
  1139. arraydef: st:=tarraydef(def).symtable;
  1140. procvardef,procdef: st:=tabstractprocdef(def).parast;
  1141. else
  1142. internalerror(201101020);
  1143. end;
  1144. if (genericlist.count>0) and not assigned(def.genericparas) then
  1145. def.genericparas:=tfphashobjectlist.create(false);
  1146. for i:=0 to genericlist.count-1 do
  1147. begin
  1148. generictype:=ttypesym(genericlist[i]);
  1149. if assigned(generictype.owner) then
  1150. begin
  1151. sym:=ctypesym.create(genericlist.nameofindex(i),generictype.typedef,true);
  1152. { type parameters need to be added as strict private }
  1153. sym.visibility:=vis_strictprivate;
  1154. st.insert(sym);
  1155. include(sym.symoptions,sp_generic_para);
  1156. end
  1157. else
  1158. begin
  1159. if (generictype.typedef.typ=undefineddef) and (generictype.typedef<>cundefinedtype) then
  1160. begin
  1161. { the generic parameters were parsed before the genericdef existed thus the
  1162. undefineddefs were added as part of the parent symtable }
  1163. if assigned(generictype.typedef.owner) then
  1164. generictype.typedef.owner.DefList.Extract(generictype.typedef);
  1165. generictype.typedef.changeowner(st);
  1166. end;
  1167. st.insert(generictype);
  1168. include(generictype.symoptions,sp_generic_para);
  1169. end;
  1170. def.genericparas.add(genericlist.nameofindex(i),generictype);
  1171. end;
  1172. end;
  1173. procedure maybe_insert_generic_rename_symbol(const name:tidstring;genericlist:tfphashobjectlist);
  1174. var
  1175. gensym : ttypesym;
  1176. begin
  1177. { for generics in non-Delphi modes we insert a private type symbol
  1178. that has the same base name as the currently parsed generic and
  1179. that references this defs }
  1180. if not (m_delphi in current_settings.modeswitches) and
  1181. (
  1182. (
  1183. parse_generic and
  1184. assigned(genericlist) and
  1185. (genericlist.count>0)
  1186. ) or
  1187. (
  1188. assigned(current_specializedef) and
  1189. assigned(current_structdef.genericdef) and
  1190. (current_structdef.genericdef.typ in [objectdef,recorddef]) and
  1191. (pos('$',name)>0)
  1192. )
  1193. ) then
  1194. begin
  1195. { we need to pass nil as def here, because the constructor wants
  1196. to set the typesym of the def which is not what we want }
  1197. gensym:=ctypesym.create(copy(name,1,pos('$',name)-1),nil,true);
  1198. gensym.typedef:=current_structdef;
  1199. include(gensym.symoptions,sp_internal);
  1200. { the symbol should be only visible to the generic class
  1201. itself }
  1202. gensym.visibility:=vis_strictprivate;
  1203. symtablestack.top.insert(gensym);
  1204. end;
  1205. end;
  1206. function generate_generic_name(const name:tidstring;specializename:ansistring;owner_hierarchy:string):tidstring;
  1207. var
  1208. crc : cardinal;
  1209. begin
  1210. if specializename='' then
  1211. internalerror(2012061901);
  1212. { build the new type's name }
  1213. crc:=UpdateCrc32(0,specializename[1],length(specializename));
  1214. result:=name+'$crc'+hexstr(crc,8);
  1215. if owner_hierarchy<>'' then
  1216. begin
  1217. crc:=UpdateCrc32(0,owner_hierarchy[1],length(owner_hierarchy));
  1218. result:=result+'$crc'+hexstr(crc,8);
  1219. end;
  1220. end;
  1221. procedure split_generic_name(const name:tidstring;out nongeneric:string;out count:longint);
  1222. var
  1223. i,code : longint;
  1224. countstr : string;
  1225. begin
  1226. for i:=length(name) downto 1 do
  1227. if name[i]='$' then
  1228. begin
  1229. nongeneric:=copy(name,1,i-1);
  1230. countstr:=copy(name,i+1,length(name)-i);
  1231. val(countstr,count,code);
  1232. if code<>0 then
  1233. break;
  1234. exit;
  1235. end;
  1236. nongeneric:=name;
  1237. count:=0;
  1238. end;
  1239. function resolve_generic_dummysym(const name:tidstring):tsym;
  1240. var
  1241. list : tfpobjectlist;
  1242. begin
  1243. list:=tfpobjectlist(current_module.genericdummysyms.find(name));
  1244. if assigned(list) and (list.count>0) then
  1245. result:=tgenericdummyentry(list.last).resolvedsym
  1246. else
  1247. result:=nil;
  1248. end;
  1249. function could_be_generic(const name:tidstring):boolean;
  1250. begin
  1251. result:=(name<>'') and
  1252. (current_module.genericdummysyms.findindexof(name)>=0);
  1253. end;
  1254. procedure specialization_init(genericdef:tdef;var state: tspecializationstate);
  1255. var
  1256. pu : tused_unit;
  1257. hmodule : tmodule;
  1258. unitsyms : TFPHashObjectList;
  1259. sym : tsym;
  1260. i : Integer;
  1261. begin
  1262. if not assigned(genericdef) then
  1263. internalerror(200705151);
  1264. { Setup symtablestack at definition time
  1265. to get types right, however this is not perfect, we should probably record
  1266. the resolved symbols }
  1267. state.oldsymtablestack:=symtablestack;
  1268. state.oldextendeddefs:=current_module.extendeddefs;
  1269. state.oldgenericdummysyms:=current_module.genericdummysyms;
  1270. current_module.extendeddefs:=TFPHashObjectList.create(true);
  1271. current_module.genericdummysyms:=tfphashobjectlist.create(true);
  1272. symtablestack:=tdefawaresymtablestack.create;
  1273. hmodule:=find_module_from_symtable(genericdef.owner);
  1274. if hmodule=nil then
  1275. internalerror(200705152);
  1276. { collect all unit syms in the generic's unit as we need to establish
  1277. their unitsym.module link again so that unit identifiers can be used }
  1278. unitsyms:=tfphashobjectlist.create(false);
  1279. if (hmodule<>current_module) and assigned(hmodule.globalsymtable) then
  1280. for i:=0 to hmodule.globalsymtable.symlist.count-1 do
  1281. begin
  1282. sym:=tsym(hmodule.globalsymtable.symlist[i]);
  1283. if sym.typ=unitsym then
  1284. unitsyms.add(upper(sym.realname),sym);
  1285. end;
  1286. { add all units if we are specializing inside the current unit (as the
  1287. generic could have been declared in the implementation part), but load
  1288. only interface units, if we are in a different unit as then the generic
  1289. needs to be in the interface section }
  1290. pu:=tused_unit(hmodule.used_units.first);
  1291. while assigned(pu) do
  1292. begin
  1293. if not assigned(pu.u.globalsymtable) then
  1294. { in certain circular, but valid unit constellations it can happen
  1295. that we specialize a generic in a different unit that was used
  1296. in the implementation section of the generic's unit and were the
  1297. interface is still being parsed and thus the localsymtable is in
  1298. reality the global symtable }
  1299. if pu.u.in_interface then
  1300. symtablestack.push(pu.u.localsymtable)
  1301. else
  1302. internalerror(200705153)
  1303. else
  1304. symtablestack.push(pu.u.globalsymtable);
  1305. sym:=tsym(unitsyms.find(pu.u.modulename^));
  1306. if assigned(sym) and not assigned(tunitsym(sym).module) then
  1307. tunitsym(sym).module:=pu.u;
  1308. pu:=tused_unit(pu.next);
  1309. end;
  1310. unitsyms.free;
  1311. if assigned(hmodule.globalsymtable) then
  1312. symtablestack.push(hmodule.globalsymtable);
  1313. { push the localsymtable if needed }
  1314. if ((hmodule<>current_module) or not current_module.in_interface)
  1315. and assigned(hmodule.localsymtable) then
  1316. symtablestack.push(hmodule.localsymtable);
  1317. end;
  1318. procedure specialization_done(var state: tspecializationstate);
  1319. begin
  1320. { Restore symtablestack }
  1321. current_module.extendeddefs.free;
  1322. current_module.extendeddefs:=state.oldextendeddefs;
  1323. current_module.genericdummysyms.free;
  1324. current_module.genericdummysyms:=state.oldgenericdummysyms;
  1325. symtablestack.free;
  1326. symtablestack:=state.oldsymtablestack;
  1327. { clear the state record to be on the safe side }
  1328. fillchar(state, sizeof(state), 0);
  1329. end;
  1330. end.