pgenutil.pas 49 KB

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