pgenutil.pas 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  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 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 (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. begin
  459. consume(_LSHARPBRACKET);
  460. { handle "<>" }
  461. if (token=_GT) or (token=_RSHARPBRACKET) then
  462. begin
  463. Message(type_e_type_id_expected);
  464. if not try_to_consume(_GT) then
  465. try_to_consume(_RSHARPBRACKET);
  466. tt:=generrordef;
  467. exit;
  468. end;
  469. end;
  470. genericdeflist:=TFPObjectList.Create(false);
  471. poslist:=tfplist.create;
  472. { Parse type parameters }
  473. err:=not parse_generic_specialization_types_internal(genericdeflist,poslist,prettyname,specializename,parsedtype,parsedpos);
  474. if err then
  475. begin
  476. if not try_to_consume(_GT) then
  477. try_to_consume(_RSHARPBRACKET);
  478. genericdeflist.free;
  479. for i:=0 to poslist.count-1 do
  480. dispose(pfileposinfo(poslist[i]));
  481. poslist.free;
  482. tt:=generrordef;
  483. exit;
  484. end;
  485. { use the name of the symbol as procvars return a user friendly version
  486. of the name }
  487. if symname='' then
  488. genname:=ttypesym(genericdef.typesym).realname
  489. else
  490. genname:=symname;
  491. { in case of non-Delphi mode the type name could already be a generic
  492. def (but maybe the wrong one) }
  493. if assigned(genericdef) and
  494. ([df_generic,df_specialization]*genericdef.defoptions<>[]) then
  495. begin
  496. { remove the type count suffix from the generic's name }
  497. for i:=Length(genname) downto 1 do
  498. if genname[i]='$' then
  499. begin
  500. genname:=copy(genname,1,i-1);
  501. break;
  502. end;
  503. { in case of a specialization we've only reached the specialization
  504. checksum yet }
  505. if df_specialization in genericdef.defoptions then
  506. for i:=length(genname) downto 1 do
  507. if genname[i]='$' then
  508. begin
  509. genname:=copy(genname,1,i-1);
  510. break;
  511. end;
  512. end
  513. else
  514. { search for a potential suffix }
  515. for i:=length(genname) downto 1 do
  516. if genname[i]='$' then
  517. begin
  518. { if the part right of the $ is a number we assume that the left
  519. part is the name of the generic, otherwise we assume that the
  520. complete name is the name of the generic }
  521. countstr:=copy(genname,i+1,length(genname)-i);
  522. gencount:=0;
  523. val(countstr,gencount,errval);
  524. if errval=0 then
  525. genname:=copy(genname,1,i-1);
  526. break;
  527. end;
  528. { search a generic with the given count of params }
  529. countstr:='';
  530. str(genericdeflist.Count,countstr);
  531. genname:=genname+'$'+countstr;
  532. ugenname:=upper(genname);
  533. if assigned(genericdef) and (genericdef.owner.symtabletype in [objectsymtable,recordsymtable]) then
  534. begin
  535. if genericdef.owner.symtabletype = objectsymtable then
  536. found:=searchsym_in_class(tobjectdef(genericdef.owner.defowner),tobjectdef(genericdef.owner.defowner),ugenname,srsym,st,false)
  537. else
  538. found:=searchsym_in_record(tabstractrecorddef(genericdef.owner.defowner),ugenname,srsym,st);
  539. end
  540. else
  541. found:=searchsym(ugenname,srsym,st);
  542. if not found or (srsym.typ<>typesym) then
  543. begin
  544. identifier_not_found(genname);
  545. if not try_to_consume(_GT) then
  546. try_to_consume(_RSHARPBRACKET);
  547. for i:=0 to poslist.count-1 do
  548. dispose(pfileposinfo(poslist[i]));
  549. poslist.free;
  550. genericdeflist.Free;
  551. tt:=generrordef;
  552. exit;
  553. end;
  554. { we've found the correct def }
  555. genericdef:=tstoreddef(ttypesym(srsym).typedef);
  556. if not check_generic_constraints(genericdef,genericdeflist,poslist) then
  557. begin
  558. { the parameters didn't fit the constraints, so don't continue with the
  559. specialization }
  560. genericdeflist.free;
  561. for i:=0 to poslist.count-1 do
  562. dispose(pfileposinfo(poslist[i]));
  563. poslist.free;
  564. tt:=generrordef;
  565. if not try_to_consume(_GT) then
  566. try_to_consume(_RSHARPBRACKET);
  567. exit;
  568. end;
  569. { build the new type's name }
  570. finalspecializename:=generate_generic_name(genname,specializename);
  571. ufinalspecializename:=upper(finalspecializename);
  572. prettyname:=genericdef.typesym.prettyname+'<'+prettyname+'>';
  573. { select the symtable containing the params }
  574. case genericdef.typ of
  575. procdef:
  576. st:=genericdef.GetSymtable(gs_para);
  577. objectdef,
  578. recorddef:
  579. st:=genericdef.GetSymtable(gs_record);
  580. arraydef:
  581. st:=tarraydef(genericdef).symtable;
  582. procvardef:
  583. st:=genericdef.GetSymtable(gs_para);
  584. else
  585. internalerror(200511182);
  586. end;
  587. generictypelist:=tfpobjectlist.create(false);
  588. { build the list containing the types for the generic params }
  589. gencount:=0;
  590. for i:=0 to st.SymList.Count-1 do
  591. begin
  592. srsym:=tsym(st.SymList[i]);
  593. if sp_generic_para in srsym.symoptions then
  594. begin
  595. if gencount=genericdeflist.Count then
  596. internalerror(2011042702);
  597. generictype:=ttypesym.create(srsym.realname,tdef(genericdeflist[gencount]));
  598. generictypelist.add(generictype);
  599. inc(gencount);
  600. end;
  601. end;
  602. { Special case if we are referencing the current defined object }
  603. if assigned(current_structdef) and
  604. (current_structdef.objname^=ufinalspecializename) then
  605. tt:=current_structdef;
  606. { decide in which symtable to put the specialization }
  607. if current_module.is_unit and current_module.in_interface then
  608. specializest:=current_module.globalsymtable
  609. else
  610. specializest:=current_module.localsymtable;
  611. { Can we reuse an already specialized type? }
  612. { for this first check whether we are currently specializing a nested
  613. type of the current (main) specialization (this is necessary, because
  614. during that time the symbol of the main specialization will still
  615. contain a reference to an errordef) }
  616. if not assigned(tt) and assigned(current_specializedef) then
  617. begin
  618. def:=current_specializedef;
  619. repeat
  620. if def.typ in [objectdef,recorddef] then
  621. if tabstractrecorddef(def).objname^=ufinalspecializename then begin
  622. tt:=def;
  623. break;
  624. end;
  625. def:=tstoreddef(def.owner.defowner);
  626. until not assigned(def) or not (df_specialization in def.defoptions);
  627. end;
  628. { now check whether there is a specialization somewhere else }
  629. if not assigned(tt) then
  630. begin
  631. hashedid.id:=ufinalspecializename;
  632. srsym:=tsym(specializest.findwithhash(hashedid));
  633. if assigned(srsym) then
  634. begin
  635. if srsym.typ<>typesym then
  636. internalerror(200710171);
  637. tt:=ttypesym(srsym).typedef;
  638. end
  639. else
  640. { the generic could have been specialized in the globalsymtable
  641. already, so search there as well }
  642. if (specializest<>current_module.globalsymtable) and assigned(current_module.globalsymtable) then
  643. begin
  644. srsym:=tsym(current_module.globalsymtable.findwithhash(hashedid));
  645. if assigned(srsym) then
  646. begin
  647. if srsym.typ<>typesym then
  648. internalerror(2011121101);
  649. tt:=ttypesym(srsym).typedef;
  650. end;
  651. end;
  652. end;
  653. if not assigned(tt) then
  654. begin
  655. specialization_init(genericdef,state);
  656. { push a temporary global symtable so that the specialization is
  657. added to the correct symtable; this symtable does not contain
  658. any other symbols, so that the type resolution can not be
  659. influenced by symbols in the current unit }
  660. tempst:=tspecializesymtable.create(current_module.modulename^,current_module.moduleid);
  661. symtablestack.push(tempst);
  662. { Reparse the original type definition }
  663. if not err then
  664. begin
  665. if parse_class_parent then
  666. begin
  667. old_current_structdef:=current_structdef;
  668. old_current_genericdef:=current_genericdef;
  669. old_current_specializedef:=current_specializedef;
  670. if genericdef.owner.symtabletype in [recordsymtable,objectsymtable] then
  671. current_structdef:=tabstractrecorddef(genericdef.owner.defowner)
  672. else
  673. current_structdef:=nil;
  674. current_genericdef:=nil;
  675. current_specializedef:=nil;
  676. end;
  677. maybe_add_waiting_unit(genericdef);
  678. { First a new typesym so we can reuse this specialization and
  679. references to this specialization can be handled }
  680. srsym:=ttypesym.create(finalspecializename,generrordef);
  681. specializest.insert(srsym);
  682. { specializations are declarations as such it is the wisest to
  683. declare set the blocktype to "type"; otherwise we'll
  684. experience unexpected side effects like the addition of
  685. classrefdefs if we have a generic that's derived from another
  686. generic }
  687. old_block_type:=block_type;
  688. block_type:=bt_type;
  689. if not assigned(genericdef.generictokenbuf) then
  690. internalerror(200511171);
  691. hmodule:=find_module_from_symtable(genericdef.owner);
  692. if hmodule=nil then
  693. internalerror(2012051202);
  694. oldcurrent_filepos:=current_filepos;
  695. { use the index the module got from the current compilation process }
  696. current_filepos.moduleindex:=hmodule.unit_index;
  697. current_tokenpos:=current_filepos;
  698. current_scanner.startreplaytokens(genericdef.generictokenbuf);
  699. read_named_type(tt,srsym,genericdef,generictypelist,false,false);
  700. current_filepos:=oldcurrent_filepos;
  701. ttypesym(srsym).typedef:=tt;
  702. tt.typesym:=srsym;
  703. if _prettyname<>'' then
  704. ttypesym(tt.typesym).fprettyname:=_prettyname
  705. else
  706. ttypesym(tt.typesym).fprettyname:=prettyname;
  707. { Note regarding hint directives:
  708. There is no need to remove the flags for them from the
  709. specialized generic symbol, because hint directives that
  710. follow the specialization are handled by the code in
  711. pdecl.types_dec and added to the type symbol.
  712. E.g.: TFoo = TBar<Blubb> deprecated;
  713. Here the symbol TBar$1$Blubb will contain the
  714. "sp_hint_deprecated" flag while the TFoo symbol won't.}
  715. case tt.typ of
  716. { Build VMT indexes for classes and read hint directives }
  717. objectdef:
  718. begin
  719. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  720. consume(_SEMICOLON);
  721. vmtbuilder:=TVMTBuilder.Create(tobjectdef(tt));
  722. vmtbuilder.generate_vmt;
  723. vmtbuilder.free;
  724. end;
  725. { handle params, calling convention, etc }
  726. procvardef:
  727. begin
  728. if not check_proc_directive(true) then
  729. begin
  730. try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg);
  731. consume(_SEMICOLON);
  732. end;
  733. parse_var_proc_directives(ttypesym(srsym));
  734. handle_calling_convention(tprocvardef(tt));
  735. if try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg) then
  736. consume(_SEMICOLON);
  737. end;
  738. else
  739. { parse hint directives for records and arrays }
  740. begin
  741. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  742. consume(_SEMICOLON);
  743. end;
  744. end;
  745. { Consume the semicolon if it is also recorded }
  746. try_to_consume(_SEMICOLON);
  747. block_type:=old_block_type;
  748. if parse_class_parent then
  749. begin
  750. current_structdef:=old_current_structdef;
  751. current_genericdef:=old_current_genericdef;
  752. current_specializedef:=old_current_specializedef;
  753. end;
  754. end;
  755. { extract all created symbols and defs from the temporary symtable
  756. and add them to the specializest }
  757. for i:=tempst.SymList.Count-1 downto 0 do
  758. begin
  759. item:=tempst.SymList.Items[i];
  760. { using changeowner the symbol is automatically added to the
  761. new symtable }
  762. tsym(item).ChangeOwner(specializest);
  763. end;
  764. for i:=tempst.DefList.Count-1 downto 0 do
  765. begin
  766. item:=tempst.DefList.Items[i];
  767. { using changeowner the def is automatically added to the new
  768. symtable }
  769. tdef(item).ChangeOwner(specializest);
  770. end;
  771. { if a generic was declared during the specialization we need to
  772. flag the specialize symtable accordingly }
  773. if sto_has_generic in tempst.tableoptions then
  774. specializest.includeoption(sto_has_generic);
  775. tempst.free;
  776. specialization_done(state);
  777. end;
  778. if not (token in [_GT, _RSHARPBRACKET]) then
  779. begin
  780. consume(_RSHARPBRACKET);
  781. exit;
  782. end
  783. else
  784. consume(token);
  785. genericdeflist.free;
  786. generictypelist.free;
  787. if assigned(genericdef) then
  788. begin
  789. { check the hints of the found generic symbol }
  790. srsym:=genericdef.typesym;
  791. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  792. end;
  793. end;
  794. function parse_generic_parameters(allowconstraints:boolean):TFPObjectList;
  795. var
  796. generictype : ttypesym;
  797. i,firstidx : longint;
  798. srsymtable : tsymtable;
  799. def : tdef;
  800. defname : tidstring;
  801. allowconstructor,
  802. doconsume : boolean;
  803. constraintdata : tgenericconstraintdata;
  804. old_block_type : tblock_type;
  805. begin
  806. result:=TFPObjectList.Create(false);
  807. firstidx:=0;
  808. old_block_type:=block_type;
  809. block_type:=bt_type;
  810. repeat
  811. if token=_ID then
  812. begin
  813. generictype:=ttypesym.create(orgpattern,cundefinedtype);
  814. include(generictype.symoptions,sp_generic_para);
  815. result.add(generictype);
  816. end;
  817. consume(_ID);
  818. if try_to_consume(_COLON) then
  819. begin
  820. if not allowconstraints then
  821. { TODO }
  822. Message(parser_e_illegal_expression{ parser_e_generic_constraints_not_allowed_here});
  823. { construct a name which can be used for a type specification }
  824. constraintdata:=tgenericconstraintdata.create;
  825. defname:='';
  826. str(current_module.deflist.count,defname);
  827. defname:='$gendef'+defname;
  828. allowconstructor:=m_delphi in current_settings.modeswitches;
  829. constraintdata.basedef:=generrordef;
  830. repeat
  831. doconsume:=true;
  832. case token of
  833. _CONSTRUCTOR:
  834. begin
  835. if not allowconstructor or (gcf_constructor in constraintdata.flags) then
  836. Message(parser_e_illegal_expression);
  837. include(constraintdata.flags,gcf_constructor);
  838. allowconstructor:=false;
  839. end;
  840. _CLASS:
  841. begin
  842. if gcf_class in constraintdata.flags then
  843. Message(parser_e_illegal_expression);
  844. if constraintdata.basedef=generrordef then
  845. include(constraintdata.flags,gcf_class)
  846. else
  847. Message(parser_e_illegal_expression);
  848. end;
  849. _RECORD:
  850. begin
  851. if ([gcf_constructor,gcf_class]*constraintdata.flags<>[])
  852. or (constraintdata.interfaces.count>0) then
  853. Message(parser_e_illegal_expression)
  854. else
  855. begin
  856. srsymtable:=trecordsymtable.create(defname,0);
  857. constraintdata.basedef:=trecorddef.create(defname,srsymtable);
  858. include(constraintdata.flags,gcf_record);
  859. allowconstructor:=false;
  860. end;
  861. end;
  862. else
  863. begin
  864. { after single_type "token" is the trailing ",", ";" or
  865. ">"! }
  866. doconsume:=false;
  867. { def is already set to a class or record }
  868. if gcf_record in constraintdata.flags then
  869. Message(parser_e_illegal_expression);
  870. single_type(def, [stoAllowSpecialization]);
  871. { only types that are inheritable are allowed }
  872. if (def.typ<>objectdef) or
  873. not (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_interfacejava,odt_javaclass]) then
  874. Message1(type_e_class_or_interface_type_expected,def.typename)
  875. else
  876. case tobjectdef(def).objecttype of
  877. odt_class,
  878. odt_javaclass:
  879. begin
  880. if gcf_class in constraintdata.flags then
  881. { "class" + concrete class is not allowed }
  882. Message(parser_e_illegal_expression)
  883. else
  884. { do we already have a concrete class? }
  885. if constraintdata.basedef<>generrordef then
  886. Message(parser_e_illegal_expression)
  887. else
  888. constraintdata.basedef:=def;
  889. end;
  890. odt_interfacecom,
  891. odt_interfacecorba,
  892. odt_interfacejava,
  893. odt_dispinterface:
  894. constraintdata.interfaces.add(def);
  895. end;
  896. end;
  897. end;
  898. if doconsume then
  899. consume(token);
  900. until not try_to_consume(_COMMA);
  901. if ([gcf_class,gcf_constructor]*constraintdata.flags<>[]) or
  902. ((constraintdata.interfaces.count>1) and (constraintdata.basedef=generrordef)) or
  903. ((constraintdata.interfaces.count>0) and (constraintdata.basedef<>generrordef)) then
  904. begin
  905. if constraintdata.basedef.typ=errordef then
  906. { don't pass an errordef as a parent to a tobjectdef }
  907. constraintdata.basedef:=nil
  908. else
  909. if constraintdata.basedef.typ<>objectdef then
  910. internalerror(2012101101);
  911. constraintdata.basedef:=tobjectdef.create({$ifdef jvm}odt_javaclass{$else}odt_class{$endif},defname,tobjectdef(constraintdata.basedef));
  912. include(constraintdata.basedef.defoptions,df_genconstraint);
  913. for i:=0 to constraintdata.interfaces.count-1 do
  914. tobjectdef(constraintdata.basedef).implementedinterfaces.add(
  915. timplementedinterface.create(tobjectdef(constraintdata.interfaces[i])));
  916. end
  917. else
  918. if constraintdata.interfaces.count=1 then
  919. begin
  920. constraintdata.basedef:=tdef(constraintdata.interfaces[0]);
  921. constraintdata.interfaces.delete(0);
  922. end;
  923. for i:=firstidx to result.count-1 do
  924. with ttypesym(result[i]) do
  925. begin
  926. genconstraintdata:=tgenericconstraintdata.create;
  927. genconstraintdata.basedef:=constraintdata.basedef;
  928. genconstraintdata.flags:=constraintdata.flags;
  929. genconstraintdata.interfaces.assign(constraintdata.interfaces);
  930. typedef:=constraintdata.basedef;
  931. end;
  932. firstidx:=result.count;
  933. constraintdata.free;
  934. end;
  935. until not (try_to_consume(_COMMA) or try_to_consume(_SEMICOLON));
  936. block_type:=old_block_type;
  937. end;
  938. procedure insert_generic_parameter_types(def:tstoreddef;genericdef:tstoreddef;genericlist:TFPObjectList);
  939. var
  940. i: longint;
  941. generictype: ttypesym;
  942. st: tsymtable;
  943. begin
  944. def.genericdef:=genericdef;
  945. if not assigned(genericlist) then
  946. exit;
  947. if assigned(genericdef) then
  948. include(def.defoptions,df_specialization)
  949. else
  950. if genericlist.count>0 then
  951. include(def.defoptions,df_generic);
  952. case def.typ of
  953. recorddef,objectdef: st:=tabstractrecorddef(def).symtable;
  954. arraydef: st:=tarraydef(def).symtable;
  955. procvardef,procdef: st:=tabstractprocdef(def).parast;
  956. else
  957. internalerror(201101020);
  958. end;
  959. for i:=0 to genericlist.count-1 do
  960. begin
  961. generictype:=ttypesym(genericlist[i]);
  962. st.insert(generictype);
  963. include(generictype.symoptions,sp_generic_para);
  964. def.genericparas.add(generictype.name,generictype);
  965. end;
  966. end;
  967. procedure maybe_insert_generic_rename_symbol(const name:tidstring;genericlist:tfpobjectlist);
  968. var
  969. gensym : ttypesym;
  970. begin
  971. { for generics in non-Delphi modes we insert a private type symbol
  972. that has the same base name as the currently parsed generic and
  973. that references this defs }
  974. if not (m_delphi in current_settings.modeswitches) and
  975. (
  976. (
  977. parse_generic and
  978. assigned(genericlist) and
  979. (genericlist.count>0)
  980. ) or
  981. (
  982. assigned(current_specializedef) and
  983. assigned(current_structdef.genericdef) and
  984. (current_structdef.genericdef.typ in [objectdef,recorddef]) and
  985. (pos('$',name)>0)
  986. )
  987. ) then
  988. begin
  989. { we need to pass nil as def here, because the constructor wants
  990. to set the typesym of the def which is not what we want }
  991. gensym:=ttypesym.create(copy(name,1,pos('$',name)-1),nil);
  992. gensym.typedef:=current_structdef;
  993. include(gensym.symoptions,sp_internal);
  994. { the symbol should be only visible to the generic class
  995. itself }
  996. gensym.visibility:=vis_strictprivate;
  997. symtablestack.top.insert(gensym);
  998. end;
  999. end;
  1000. function generate_generic_name(const name:tidstring;specializename:ansistring):tidstring;
  1001. var
  1002. crc : cardinal;
  1003. begin
  1004. if specializename='' then
  1005. internalerror(2012061901);
  1006. { build the new type's name }
  1007. crc:=UpdateCrc32(0,specializename[1],length(specializename));
  1008. result:=name+'$crc'+hexstr(crc,8);
  1009. end;
  1010. procedure specialization_init(genericdef:tdef;var state: tspecializationstate);
  1011. var
  1012. pu : tused_unit;
  1013. hmodule : tmodule;
  1014. unitsyms : TFPHashObjectList;
  1015. sym : tsym;
  1016. i : Integer;
  1017. begin
  1018. if not assigned(genericdef) then
  1019. internalerror(200705151);
  1020. { Setup symtablestack at definition time
  1021. to get types right, however this is not perfect, we should probably record
  1022. the resolved symbols }
  1023. state.oldsymtablestack:=symtablestack;
  1024. state.oldextendeddefs:=current_module.extendeddefs;
  1025. current_module.extendeddefs:=TFPHashObjectList.create(true);
  1026. symtablestack:=tdefawaresymtablestack.create;
  1027. hmodule:=find_module_from_symtable(genericdef.owner);
  1028. if hmodule=nil then
  1029. internalerror(200705152);
  1030. { collect all unit syms in the generic's unit as we need to establish
  1031. their unitsym.module link again so that unit identifiers can be used }
  1032. unitsyms:=tfphashobjectlist.create(false);
  1033. if (hmodule<>current_module) and assigned(hmodule.globalsymtable) then
  1034. for i:=0 to hmodule.globalsymtable.symlist.count-1 do
  1035. begin
  1036. sym:=tsym(hmodule.globalsymtable.symlist[i]);
  1037. if sym.typ=unitsym then
  1038. unitsyms.add(upper(sym.realname),sym);
  1039. end;
  1040. { add all units if we are specializing inside the current unit (as the
  1041. generic could have been declared in the implementation part), but load
  1042. only interface units, if we are in a different unit as then the generic
  1043. needs to be in the interface section }
  1044. pu:=tused_unit(hmodule.used_units.first);
  1045. while assigned(pu) do
  1046. begin
  1047. if not assigned(pu.u.globalsymtable) then
  1048. { in certain circular, but valid unit constellations it can happen
  1049. that we specialize a generic in a different unit that was used
  1050. in the implementation section of the generic's unit and were the
  1051. interface is still being parsed and thus the localsymtable is in
  1052. reality the global symtable }
  1053. if pu.u.in_interface then
  1054. symtablestack.push(pu.u.localsymtable)
  1055. else
  1056. internalerror(200705153)
  1057. else
  1058. symtablestack.push(pu.u.globalsymtable);
  1059. sym:=tsym(unitsyms.find(pu.u.modulename^));
  1060. if assigned(sym) and not assigned(tunitsym(sym).module) then
  1061. tunitsym(sym).module:=pu.u;
  1062. pu:=tused_unit(pu.next);
  1063. end;
  1064. unitsyms.free;
  1065. if assigned(hmodule.globalsymtable) then
  1066. symtablestack.push(hmodule.globalsymtable);
  1067. { push the localsymtable if needed }
  1068. if (hmodule<>current_module) or not current_module.in_interface then
  1069. symtablestack.push(hmodule.localsymtable);
  1070. end;
  1071. procedure specialization_done(var state: tspecializationstate);
  1072. begin
  1073. { Restore symtablestack }
  1074. current_module.extendeddefs.free;
  1075. current_module.extendeddefs:=state.oldextendeddefs;
  1076. symtablestack.free;
  1077. symtablestack:=state.oldsymtablestack;
  1078. { clear the state record to be on the safe side }
  1079. fillchar(state, sizeof(state), 0);
  1080. end;
  1081. end.