pgenutil.pas 47 KB

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