pgenutil.pas 49 KB

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