pgenutil.pas 49 KB

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