pgenutil.pas 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469
  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. { parser }
  27. pgentype,
  28. { symtable }
  29. symtype,symdef,symbase;
  30. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;_prettyname:string;parsedtype:tdef;symname:string;parsedpos:tfileposinfo);inline;
  31. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;_prettyname:string);inline;
  32. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef):tdef;inline;
  33. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef;parsedtype:tdef;symname:string;parsedpos:tfileposinfo):tdef;
  34. function generate_specialization_phase2(context:tspecializationcontext;genericdef:tstoreddef;parse_class_parent:boolean;_prettyname:ansistring):tdef;
  35. function parse_generic_parameters(allowconstraints:boolean):tfphashobjectlist;
  36. function parse_generic_specialization_types(genericdeflist:tfpobjectlist;poslist:tfplist;out prettyname,specializename:ansistring):boolean;
  37. procedure insert_generic_parameter_types(def:tstoreddef;genericdef:tstoreddef;genericlist:tfphashobjectlist);
  38. procedure maybe_insert_generic_rename_symbol(const name:tidstring;genericlist:tfphashobjectlist);
  39. function generate_generic_name(const name:tidstring;specializename:ansistring;owner_hierarchy:string):tidstring;
  40. procedure split_generic_name(const name:tidstring;out nongeneric:string;out count:longint);
  41. function resolve_generic_dummysym(const name:tidstring):tsym;
  42. function could_be_generic(const name:tidstring):boolean;inline;
  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,defcmp,procinfo,
  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. begin
  166. if (oo_is_forward in paraobjdef.objectoptions) and
  167. (paraobjdef.objecttype=formalobjdef.objecttype) and
  168. (df_genconstraint in formalobjdef.defoptions) and
  169. (
  170. (formalobjdef.objecttype=odt_interfacecom) and
  171. (formalobjdef.childof=interface_iunknown)
  172. )
  173. or
  174. (
  175. (formalobjdef.objecttype=odt_interfacecorba) and
  176. (formalobjdef.childof=nil)
  177. ) then
  178. continue;
  179. if not def_is_related(paraobjdef,formalobjdef.childof) then
  180. begin
  181. MessagePos2(filepos,type_e_incompatible_types,paraobjdef.typename,formalobjdef.childof.typename);
  182. result:=false;
  183. end;
  184. end;
  185. odt_class,
  186. odt_javaclass:
  187. begin
  188. objdef:=paraobjdef;
  189. intffound:=false;
  190. while assigned(objdef) do
  191. begin
  192. for j:=0 to objdef.implementedinterfaces.count-1 do
  193. if timplementedinterface(objdef.implementedinterfaces[j]).intfdef=formalobjdef.childof then
  194. begin
  195. intffound:=true;
  196. break;
  197. end;
  198. if intffound then
  199. break;
  200. objdef:=objdef.childof;
  201. end;
  202. result:=intffound;
  203. if not result then
  204. MessagePos2(filepos,parser_e_class_doesnt_implement_interface,paraobjdef.typename,formalobjdef.childof.typename);
  205. end;
  206. else
  207. begin
  208. MessagePos1(filepos,type_e_class_or_interface_type_expected,paraobjdef.typename);
  209. result:=false;
  210. end;
  211. end;
  212. end
  213. else
  214. begin
  215. { this is either a "class" or a concrete instance with
  216. or without implemented interfaces }
  217. if not (paraobjdef.objecttype in [odt_class,odt_javaclass]) then
  218. begin
  219. MessagePos1(filepos,type_e_class_type_expected,paraobjdef.typename);
  220. result:=false;
  221. continue;
  222. end;
  223. { for forward declared classes we allow pure TObject/class declarations }
  224. if (oo_is_forward in paraobjdef.objectoptions) and
  225. (df_genconstraint in formaldef.defoptions) then
  226. begin
  227. if (formalobjdef.childof=class_tobject) and
  228. not formalobjdef.implements_any_interfaces then
  229. continue;
  230. end;
  231. if assigned(formalobjdef.childof) and
  232. not def_is_related(paradef,formalobjdef.childof) then
  233. begin
  234. MessagePos2(filepos,type_e_incompatible_types,paraobjdef.typename,formalobjdef.childof.typename);
  235. result:=false;
  236. end;
  237. intfcount:=0;
  238. for j:=0 to formalobjdef.implementedinterfaces.count-1 do
  239. begin
  240. objdef:=paraobjdef;
  241. while assigned(objdef) do
  242. begin
  243. intffound:=assigned(
  244. find_implemented_interface(objdef,
  245. timplementedinterface(formalobjdef.implementedinterfaces[j]).intfdef
  246. )
  247. );
  248. if intffound then
  249. break;
  250. objdef:=objdef.childof;
  251. end;
  252. if intffound then
  253. inc(intfcount)
  254. else
  255. MessagePos2(filepos,parser_e_class_doesnt_implement_interface,paraobjdef.typename,timplementedinterface(formalobjdef.implementedinterfaces[j]).intfdef.typename);
  256. end;
  257. if intfcount<>formalobjdef.implementedinterfaces.count then
  258. result:=false;
  259. end;
  260. end;
  261. end;
  262. end;
  263. end;
  264. function parse_generic_specialization_types_internal(genericdeflist:tfpobjectlist;poslist:tfplist;out prettyname,specializename:ansistring;parsedtype:tdef;parsedpos:tfileposinfo):boolean;
  265. var
  266. old_block_type : tblock_type;
  267. first : boolean;
  268. typeparam : tnode;
  269. parampos : pfileposinfo;
  270. tmpparampos : tfileposinfo;
  271. namepart : string;
  272. prettynamepart : ansistring;
  273. begin
  274. result:=true;
  275. if genericdeflist=nil then
  276. internalerror(2012061401);
  277. { set the block type to type, so that the parsed type are returned as
  278. ttypenode (e.g. classes are in non type-compatible blocks returned as
  279. tloadvmtaddrnode) }
  280. old_block_type:=block_type;
  281. { if parsedtype is set, then the first type identifer was already parsed
  282. (happens in inline specializations) and thus we only need to parse
  283. the remaining types and do as if the first one was already given }
  284. first:=not assigned(parsedtype);
  285. if assigned(parsedtype) then
  286. begin
  287. genericdeflist.Add(parsedtype);
  288. specializename:='$'+parsedtype.fulltypename;
  289. prettyname:=parsedtype.typesym.prettyname;
  290. if assigned(poslist) then
  291. begin
  292. New(parampos);
  293. parampos^:=parsedpos;
  294. poslist.add(parampos);
  295. end;
  296. end
  297. else
  298. begin
  299. specializename:='';
  300. prettyname:='';
  301. end;
  302. while not (token in [_GT,_RSHARPBRACKET]) do
  303. begin
  304. { "first" is set to false at the end of the loop! }
  305. if not first then
  306. consume(_COMMA);
  307. block_type:=bt_type;
  308. tmpparampos:=current_filepos;
  309. typeparam:=factor(false,true);
  310. if typeparam.nodetype=typen then
  311. begin
  312. if tstoreddef(typeparam.resultdef).is_generic and
  313. (
  314. not parse_generic or
  315. not defs_belong_to_same_generic(typeparam.resultdef,current_genericdef)
  316. ) then
  317. Message(parser_e_no_generics_as_params);
  318. if assigned(poslist) then
  319. begin
  320. New(parampos);
  321. parampos^:=tmpparampos;
  322. poslist.add(parampos);
  323. end;
  324. genericdeflist.Add(typeparam.resultdef);
  325. if not assigned(typeparam.resultdef.typesym) then
  326. message(type_e_generics_cannot_reference_itself)
  327. else
  328. begin
  329. { we use the full name of the type to uniquely identify it }
  330. if (symtablestack.top.symtabletype=parasymtable) and
  331. (typeparam.resultdef.owner=symtablestack.top) then
  332. begin
  333. { special handling for specializations inside generic function declarations }
  334. if symtablestack.top.defowner.typ<>procdef then
  335. internalerror(2015080101);
  336. str(symtablestack.top.defowner.defid,namepart);
  337. namepart:='genproc'+namepart+'_'+tdef(symtablestack.top.defowner).fullownerhierarchyname+'_'+tprocdef(symtablestack.top.defowner).procsym.realname+'_'+typeparam.resultdef.typename;
  338. prettynamepart:=tdef(symtablestack.top.defowner).fullownerhierarchyname+tprocdef(symtablestack.top.defowner).procsym.prettyname;
  339. end
  340. else
  341. begin
  342. namepart:=typeparam.resultdef.fulltypename;
  343. prettynamepart:=typeparam.resultdef.fullownerhierarchyname;
  344. end;
  345. specializename:=specializename+'$'+namepart;
  346. if not first then
  347. prettyname:=prettyname+',';
  348. prettyname:=prettyname+prettynamepart+typeparam.resultdef.typesym.prettyname;
  349. end;
  350. end
  351. else
  352. begin
  353. Message(type_e_type_id_expected);
  354. result:=false;
  355. end;
  356. typeparam.free;
  357. first:=false;
  358. end;
  359. block_type:=old_block_type;
  360. end;
  361. function parse_generic_specialization_types(genericdeflist:tfpobjectlist;poslist:tfplist;out prettyname,specializename:ansistring):boolean;
  362. var
  363. dummypos : tfileposinfo;
  364. begin
  365. FillChar(dummypos, SizeOf(tfileposinfo), 0);
  366. result:=parse_generic_specialization_types_internal(genericdeflist,poslist,prettyname,specializename,nil,dummypos);
  367. end;
  368. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;_prettyname:string);
  369. var
  370. dummypos : tfileposinfo;
  371. begin
  372. FillChar(dummypos, SizeOf(tfileposinfo), 0);
  373. generate_specialization(tt,parse_class_parent,_prettyname,nil,'',dummypos);
  374. end;
  375. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef):tdef;
  376. var
  377. dummypos : tfileposinfo;
  378. {$push}
  379. {$warn 5036 off}
  380. begin
  381. result:=generate_specialization_phase1(context,genericdef,nil,'',dummypos);
  382. end;
  383. {$pop}
  384. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef;parsedtype:tdef;symname:string;parsedpos:tfileposinfo):tdef;
  385. var
  386. pt2 : tnode;
  387. errorrecovery,
  388. found,
  389. first,
  390. err : boolean;
  391. i,
  392. gencount : longint;
  393. def : tstoreddef;
  394. countstr,genname,ugenname : string;
  395. srsym : tsym;
  396. st : tsymtable;
  397. begin
  398. context:=nil;
  399. result:=nil;
  400. { either symname must be given or genericdef needs to be valid }
  401. errorrecovery:=false;
  402. if (symname='') and
  403. (not assigned(genericdef) or
  404. not assigned(genericdef.typesym) or
  405. (genericdef.typesym.typ<>typesym)) then
  406. begin
  407. errorrecovery:=true;
  408. result:=generrordef;
  409. end;
  410. { Only parse the parameters for recovery or
  411. for recording in genericbuf }
  412. if errorrecovery then
  413. begin
  414. first:=assigned(parsedtype);
  415. if not first and not try_to_consume(_LT) then
  416. consume(_LSHARPBRACKET);
  417. gencount:=0;
  418. { handle "<>" }
  419. if not first and ((token=_RSHARPBRACKET) or (token=_GT)) then
  420. Message(type_e_type_id_expected)
  421. else
  422. repeat
  423. if not first then
  424. begin
  425. pt2:=factor(false,true);
  426. pt2.free;
  427. end;
  428. first:=false;
  429. inc(gencount);
  430. until not try_to_consume(_COMMA);
  431. if not try_to_consume(_GT) then
  432. consume(_RSHARPBRACKET);
  433. { we need to return a def that can later pass some checks like
  434. whether it's an interface or not }
  435. if not errorrecovery and
  436. (not assigned(result) or (result.typ=undefineddef)) then
  437. begin
  438. if (symname='') and tstoreddef(genericdef).is_generic then
  439. { this happens in non-Delphi modes }
  440. result:=genericdef
  441. else
  442. begin
  443. { find the corresponding generic symbol so that any checks
  444. done on the returned def will be handled correctly }
  445. str(gencount,countstr);
  446. if symname='' then
  447. genname:=ttypesym(genericdef.typesym).realname
  448. else
  449. genname:=symname;
  450. genname:=genname+'$'+countstr;
  451. ugenname:=upper(genname);
  452. { first check whether the found name is the same as that of
  453. the current def or one of its (generic) surrounding defs;
  454. this is necessary as the symbol of the generic can not yet
  455. be used for lookup as it still contains a reference to an
  456. errordef) }
  457. def:=current_genericdef;
  458. repeat
  459. if def.typ in [objectdef,recorddef] then
  460. if tabstractrecorddef(def).objname^=ugenname then
  461. begin
  462. result:=def;
  463. break;
  464. end;
  465. def:=tstoreddef(def.owner.defowner);
  466. until not assigned(def) or not (df_generic in def.defoptions);
  467. { it's not part of the current object hierarchy, so search
  468. for the symbol }
  469. if not assigned(result) then
  470. begin
  471. srsym:=nil;
  472. if not searchsym(ugenname,srsym,st) or
  473. (srsym.typ<>typesym) then
  474. begin
  475. identifier_not_found(genname);
  476. result:=generrordef;
  477. exit;
  478. end;
  479. result:=ttypesym(srsym).typedef;
  480. { this happens in non-Delphi modes if we encounter a
  481. specialization of the generic class or record we're
  482. currently parsing }
  483. if (result.typ=errordef) and assigned(current_structdef) and
  484. (current_structdef.objname^=ugenname) then
  485. result:=current_structdef;
  486. end;
  487. end;
  488. end;
  489. exit;
  490. end;
  491. if not assigned(parsedtype) and not try_to_consume(_LT) then
  492. begin
  493. consume(_LSHARPBRACKET);
  494. { handle "<>" }
  495. if (token=_GT) or (token=_RSHARPBRACKET) then
  496. begin
  497. Message(type_e_type_id_expected);
  498. if not try_to_consume(_GT) then
  499. try_to_consume(_RSHARPBRACKET);
  500. result:=generrordef;
  501. exit;
  502. end;
  503. end;
  504. context:=tspecializationcontext.create;
  505. { Parse type parameters }
  506. err:=not parse_generic_specialization_types_internal(context.genericdeflist,context.poslist,context.prettyname,context.specializename,parsedtype,parsedpos);
  507. if err then
  508. begin
  509. if not try_to_consume(_GT) then
  510. try_to_consume(_RSHARPBRACKET);
  511. context.free;
  512. context:=nil;
  513. result:=generrordef;
  514. exit;
  515. end;
  516. { use the name of the symbol as procvars return a user friendly version
  517. of the name }
  518. if symname='' then
  519. genname:=ttypesym(genericdef.typesym).realname
  520. else
  521. genname:=symname;
  522. { in case of non-Delphi mode the type name could already be a generic
  523. def (but maybe the wrong one) }
  524. if assigned(genericdef) and
  525. ([df_generic,df_specialization]*genericdef.defoptions<>[]) then
  526. begin
  527. { remove the type count suffix from the generic's name }
  528. for i:=Length(genname) downto 1 do
  529. if genname[i]='$' then
  530. begin
  531. genname:=copy(genname,1,i-1);
  532. break;
  533. end;
  534. { in case of a specialization we've only reached the specialization
  535. checksum yet }
  536. if df_specialization in genericdef.defoptions then
  537. for i:=length(genname) downto 1 do
  538. if genname[i]='$' then
  539. begin
  540. genname:=copy(genname,1,i-1);
  541. break;
  542. end;
  543. end
  544. else
  545. begin
  546. split_generic_name(genname,ugenname,gencount);
  547. if genname<>ugenname then
  548. genname:=ugenname;
  549. end;
  550. { search a generic with the given count of params }
  551. countstr:='';
  552. str(context.genericdeflist.Count,countstr);
  553. genname:=genname+'$'+countstr;
  554. ugenname:=upper(genname);
  555. context.genname:=genname;
  556. if assigned(genericdef) and (genericdef.owner.symtabletype in [objectsymtable,recordsymtable]) then
  557. begin
  558. if genericdef.owner.symtabletype = objectsymtable then
  559. found:=searchsym_in_class(tobjectdef(genericdef.owner.defowner),tobjectdef(genericdef.owner.defowner),ugenname,context.sym,context.symtable,[])
  560. else
  561. found:=searchsym_in_record(tabstractrecorddef(genericdef.owner.defowner),ugenname,context.sym,context.symtable);
  562. if not found then
  563. found:=searchsym(ugenname,context.sym,context.symtable);
  564. end
  565. else
  566. found:=searchsym(ugenname,context.sym,context.symtable);
  567. if not found or not (context.sym.typ in [typesym,procsym]) then
  568. begin
  569. identifier_not_found(genname);
  570. if not try_to_consume(_GT) then
  571. try_to_consume(_RSHARPBRACKET);
  572. context.free;
  573. context:=nil;
  574. result:=generrordef;
  575. exit;
  576. end;
  577. { we've found the correct def }
  578. if context.sym.typ=typesym then
  579. result:=tstoreddef(ttypesym(context.sym).typedef)
  580. else
  581. begin
  582. if tprocsym(context.sym).procdeflist.count=0 then
  583. internalerror(2015061203);
  584. result:=tstoreddef(tprocsym(context.sym).procdefList[0]);
  585. end;
  586. if not try_to_consume(_GT) then
  587. consume(_RSHARPBRACKET);
  588. end;
  589. function generate_specialization_phase2(context:tspecializationcontext;genericdef:tstoreddef;parse_class_parent:boolean;_prettyname:ansistring):tdef;
  590. procedure unset_forwarddef(def: tdef);
  591. var
  592. st : TSymtable;
  593. i : longint;
  594. begin
  595. case def.typ of
  596. procdef:
  597. tprocdef(def).forwarddef:=false;
  598. objectdef,
  599. recorddef:
  600. begin
  601. st:=def.getsymtable(gs_record);
  602. for i:=0 to st.deflist.count-1 do
  603. unset_forwarddef(tdef(st.deflist[i]));
  604. end;
  605. end;
  606. end;
  607. procedure retrieve_genericdef_or_procsym(sym:tsym;out gendef:tdef;out psym:tsym);
  608. var
  609. i : longint;
  610. begin
  611. gendef:=nil;
  612. psym:=nil;
  613. case sym.typ of
  614. typesym:
  615. begin
  616. gendef:=ttypesym(sym).typedef
  617. end;
  618. procsym:
  619. begin
  620. for i:=0 to tprocsym(sym).procdeflist.count-1 do
  621. if tstoreddef(tprocsym(sym).procdeflist[i]).genericdef=genericdef then
  622. begin
  623. gendef:=tdef(tprocsym(sym).procdeflist[i]);
  624. break;
  625. end;
  626. psym:=sym;
  627. end
  628. else
  629. internalerror(200710171);
  630. end;
  631. end;
  632. var
  633. finalspecializename,
  634. ufinalspecializename : tidstring;
  635. prettyname : ansistring;
  636. generictypelist : tfphashobjectlist;
  637. specializest : tsymtable;
  638. hashedid : thashedidstring;
  639. tempst : tglobalsymtable;
  640. psym,
  641. srsym : tsym;
  642. def : tdef;
  643. old_block_type : tblock_type;
  644. state : tspecializationstate;
  645. old_current_structdef : tabstractrecorddef;
  646. old_current_specializedef,
  647. old_current_genericdef : tstoreddef;
  648. hmodule : tmodule;
  649. oldcurrent_filepos : tfileposinfo;
  650. recordbuf : tdynamicarray;
  651. hadtypetoken : boolean;
  652. vmtbuilder : tvmtbuilder;
  653. i,
  654. replaydepth : longint;
  655. item : tobject;
  656. hintsprocessed : boolean;
  657. pd : tprocdef;
  658. begin
  659. if not assigned(context) then
  660. internalerror(2015052203);
  661. result:=nil;
  662. if not check_generic_constraints(genericdef,context.genericdeflist,context.poslist) then
  663. begin
  664. { the parameters didn't fit the constraints, so don't continue with the
  665. specialization }
  666. result:=generrordef;
  667. exit;
  668. end;
  669. { build the new type's name }
  670. finalspecializename:=generate_generic_name(context.genname,context.specializename,genericdef.ownerhierarchyname);
  671. ufinalspecializename:=upper(finalspecializename);
  672. if genericdef.typ=procdef then
  673. prettyname:=tprocdef(genericdef).procsym.prettyname
  674. else
  675. prettyname:=genericdef.typesym.prettyname;
  676. prettyname:=prettyname+'<'+context.prettyname+'>';
  677. generictypelist:=tfphashobjectlist.create(false);
  678. { build the list containing the types for the generic params }
  679. if not assigned(genericdef.genericparas) then
  680. internalerror(2013092601);
  681. if context.genericdeflist.count<>genericdef.genericparas.count then
  682. internalerror(2013092603);
  683. for i:=0 to genericdef.genericparas.Count-1 do
  684. begin
  685. srsym:=tsym(genericdef.genericparas[i]);
  686. if not (sp_generic_para in srsym.symoptions) then
  687. internalerror(2013092602);
  688. generictypelist.add(srsym.realname,tdef(context.genericdeflist[i]).typesym);
  689. end;
  690. { Special case if we are referencing the current defined object }
  691. if assigned(current_structdef) and
  692. (current_structdef.objname^=ufinalspecializename) then
  693. result:=current_structdef;
  694. { Can we reuse an already specialized type? }
  695. { for this first check whether we are currently specializing a nested
  696. type of the current (main) specialization (this is necessary, because
  697. during that time the symbol of the main specialization will still
  698. contain a reference to an errordef) }
  699. if not assigned(result) and assigned(current_specializedef) then
  700. begin
  701. def:=current_specializedef;
  702. repeat
  703. if def.typ in [objectdef,recorddef] then
  704. if tabstractrecorddef(def).objname^=ufinalspecializename then begin
  705. result:=def;
  706. break;
  707. end;
  708. def:=tstoreddef(def.owner.defowner);
  709. until not assigned(def) or not (df_specialization in def.defoptions);
  710. end;
  711. { if the genericdef is the def we are currently parsing (or one of its parents) then we can
  712. not use it for specializing as the tokenbuffer is not yet set (and we aren't done with
  713. parsing anyway), so for now we treat those still as generic defs without doing a partial
  714. specialization }
  715. if not assigned(result) then
  716. begin
  717. def:=current_genericdef;
  718. while assigned(def) and (def.typ in [recorddef,objectdef]) do
  719. begin
  720. if def=genericdef then
  721. begin
  722. result:=def;
  723. break;
  724. end;
  725. def:=tstoreddef(def.owner.defowner);
  726. end;
  727. end;
  728. { decide in which symtable to put the specialization }
  729. if parse_generic and not assigned(result) then
  730. begin
  731. if not assigned(current_genericdef) then
  732. internalerror(2014050901);
  733. if assigned(current_procinfo) and (df_generic in current_procinfo.procdef.defoptions) then
  734. { if we are parsing the definition of a method we specialize into
  735. the local symtable of it }
  736. specializest:=current_procinfo.procdef.getsymtable(gs_local)
  737. else
  738. { we specialize the partial specialization into the symtable of the currently parsed
  739. generic }
  740. case current_genericdef.typ of
  741. procvardef,
  742. procdef:
  743. specializest:=current_genericdef.getsymtable(gs_local);
  744. objectdef,
  745. recorddef:
  746. specializest:=current_genericdef.getsymtable(gs_record);
  747. arraydef:
  748. specializest:=tarraydef(current_genericdef).symtable;
  749. else
  750. internalerror(2014050902);
  751. end;
  752. end
  753. else
  754. if current_module.is_unit and current_module.in_interface then
  755. specializest:=current_module.globalsymtable
  756. else
  757. specializest:=current_module.localsymtable;
  758. if not assigned(specializest) then
  759. internalerror(2014050910);
  760. { now check whether there is a specialization somewhere else }
  761. psym:=nil;
  762. if not assigned(result) then
  763. begin
  764. hashedid.id:=ufinalspecializename;
  765. srsym:=tsym(specializest.findwithhash(hashedid));
  766. if assigned(srsym) then
  767. begin
  768. retrieve_genericdef_or_procsym(srsym,result,psym);
  769. end
  770. else
  771. { the generic could have been specialized in the globalsymtable
  772. already, so search there as well }
  773. if (specializest<>current_module.globalsymtable) and assigned(current_module.globalsymtable) then
  774. begin
  775. srsym:=tsym(current_module.globalsymtable.findwithhash(hashedid));
  776. if assigned(srsym) then
  777. begin
  778. retrieve_genericdef_or_procsym(srsym,result,psym);
  779. end;
  780. end;
  781. end;
  782. if not assigned(result) then
  783. begin
  784. specialization_init(genericdef,state);
  785. { push a temporary global symtable so that the specialization is
  786. added to the correct symtable; this symtable does not contain
  787. any other symbols, so that the type resolution can not be
  788. influenced by symbols in the current unit }
  789. tempst:=tspecializesymtable.create(current_module.modulename^,current_module.moduleid);
  790. symtablestack.push(tempst);
  791. { Reparse the original type definition }
  792. begin
  793. old_current_specializedef:=nil;
  794. old_current_genericdef:=nil;
  795. old_current_structdef:=nil;
  796. if parse_class_parent then
  797. begin
  798. old_current_structdef:=current_structdef;
  799. old_current_genericdef:=current_genericdef;
  800. old_current_specializedef:=current_specializedef;
  801. if genericdef.owner.symtabletype in [recordsymtable,objectsymtable] then
  802. current_structdef:=tabstractrecorddef(genericdef.owner.defowner)
  803. else
  804. current_structdef:=nil;
  805. current_genericdef:=nil;
  806. current_specializedef:=nil;
  807. end;
  808. maybe_add_waiting_unit(genericdef);
  809. { First a new sym so we can reuse this specialization and
  810. references to this specialization can be handled }
  811. srsym:=ctypesym.create(finalspecializename,generrordef,true);
  812. if genericdef.typ=procdef then
  813. srsym:=cprocsym.create(finalspecializename)
  814. else
  815. srsym:=ctypesym.create(finalspecializename,generrordef,true);
  816. { insert the symbol only if we don't know already that we have
  817. a procsym to add it to }
  818. if not assigned(psym) then
  819. specializest.insert(srsym);
  820. { specializations are declarations as such it is the wisest to
  821. declare set the blocktype to "type"; otherwise we'll
  822. experience unexpected side effects like the addition of
  823. classrefdefs if we have a generic that's derived from another
  824. generic }
  825. old_block_type:=block_type;
  826. block_type:=bt_type;
  827. if not assigned(genericdef.generictokenbuf) then
  828. internalerror(200511171);
  829. hmodule:=find_module_from_symtable(genericdef.owner);
  830. if hmodule=nil then
  831. internalerror(2012051202);
  832. oldcurrent_filepos:=current_filepos;
  833. { use the index the module got from the current compilation process }
  834. current_filepos.moduleindex:=hmodule.unit_index;
  835. current_tokenpos:=current_filepos;
  836. if parse_generic then
  837. begin
  838. recordbuf:=current_scanner.recordtokenbuf;
  839. current_scanner.recordtokenbuf:=nil;
  840. end
  841. else
  842. recordbuf:=nil;
  843. replaydepth:=current_scanner.replay_stack_depth;
  844. current_scanner.startreplaytokens(genericdef.generictokenbuf);
  845. hadtypetoken:=false;
  846. read_named_type(result,srsym,genericdef,generictypelist,false,hadtypetoken);
  847. current_filepos:=oldcurrent_filepos;
  848. ttypesym(srsym).typedef:=result;
  849. result.typesym:=srsym;
  850. if _prettyname<>'' then
  851. ttypesym(result.typesym).fprettyname:=_prettyname
  852. else
  853. ttypesym(result.typesym).fprettyname:=prettyname;
  854. { Note regarding hint directives:
  855. There is no need to remove the flags for them from the
  856. specialized generic symbol, because hint directives that
  857. follow the specialization are handled by the code in
  858. pdecl.types_dec and added to the type symbol.
  859. E.g.: TFoo = TBar<Blubb> deprecated;
  860. Here the symbol TBar$1$Blubb will contain the
  861. "sp_hint_deprecated" flag while the TFoo symbol won't.}
  862. case result.typ of
  863. { Build VMT indexes for classes and read hint directives }
  864. objectdef:
  865. begin
  866. if replaydepth>current_scanner.replay_stack_depth then
  867. begin
  868. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  869. if replaydepth>current_scanner.replay_stack_depth then
  870. consume(_SEMICOLON);
  871. end;
  872. vmtbuilder:=TVMTBuilder.Create(tobjectdef(result));
  873. vmtbuilder.generate_vmt;
  874. vmtbuilder.free;
  875. end;
  876. { handle params, calling convention, etc }
  877. procvardef:
  878. begin
  879. hintsprocessed:=false;
  880. if replaydepth>current_scanner.replay_stack_depth then
  881. begin
  882. if not check_proc_directive(true) then
  883. begin
  884. hintsprocessed:=try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg);
  885. if replaydepth>current_scanner.replay_stack_depth then
  886. consume(_SEMICOLON);
  887. end
  888. else
  889. hintsprocessed:=true;
  890. end;
  891. if replaydepth>current_scanner.replay_stack_depth then
  892. parse_var_proc_directives(ttypesym(srsym));
  893. handle_calling_convention(tprocvardef(result));
  894. if not hintsprocessed and (replaydepth>current_scanner.replay_stack_depth) then
  895. begin
  896. try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg);
  897. if replaydepth>current_scanner.replay_stack_depth then
  898. consume(_SEMICOLON);
  899. end;
  900. end;
  901. procdef:
  902. begin
  903. handle_calling_convention(tprocdef(result),hcc_all);
  904. proc_add_definition(tprocdef(result));
  905. end;
  906. else
  907. { parse hint directives for records and arrays }
  908. if replaydepth>current_scanner.replay_stack_depth then begin
  909. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  910. if replaydepth>current_scanner.replay_stack_depth then
  911. consume(_SEMICOLON);
  912. end;
  913. end;
  914. { Consume the remainder of the buffer }
  915. while current_scanner.replay_stack_depth>replaydepth do
  916. consume(token);
  917. if assigned(recordbuf) then
  918. begin
  919. if assigned(current_scanner.recordtokenbuf) then
  920. internalerror(2014050909);
  921. current_scanner.recordtokenbuf:=recordbuf;
  922. end;
  923. block_type:=old_block_type;
  924. if parse_class_parent then
  925. begin
  926. current_structdef:=old_current_structdef;
  927. current_genericdef:=old_current_genericdef;
  928. current_specializedef:=old_current_specializedef;
  929. end;
  930. end;
  931. { extract all created symbols and defs from the temporary symtable
  932. and add them to the specializest }
  933. for i:=tempst.SymList.Count-1 downto 0 do
  934. begin
  935. item:=tempst.SymList.Items[i];
  936. { using changeowner the symbol is automatically added to the
  937. new symtable }
  938. tsym(item).ChangeOwner(specializest);
  939. end;
  940. for i:=tempst.DefList.Count-1 downto 0 do
  941. begin
  942. item:=tempst.DefList.Items[i];
  943. { using changeowner the def is automatically added to the new
  944. symtable }
  945. tdef(item).ChangeOwner(specializest);
  946. { for partial specializations we implicitely declare any methods as having their
  947. implementations although we'll not specialize them in reality }
  948. if parse_generic then
  949. unset_forwarddef(tdef(item));
  950. end;
  951. { if a generic was declared during the specialization we need to
  952. flag the specialize symtable accordingly }
  953. if sto_has_generic in tempst.tableoptions then
  954. specializest.includeoption(sto_has_generic);
  955. tempst.free;
  956. specialization_done(state);
  957. end;
  958. generictypelist.free;
  959. if assigned(genericdef) then
  960. begin
  961. { check the hints of the found generic symbol }
  962. if genericdef.typ=procdef then
  963. srsym:=tprocdef(genericdef).procsym
  964. else
  965. srsym:=genericdef.typesym;
  966. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  967. end;
  968. end;
  969. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;_prettyname:string;parsedtype:tdef;symname:string;parsedpos:tfileposinfo);
  970. var
  971. context : tspecializationcontext;
  972. genericdef : tstoreddef;
  973. begin
  974. genericdef:=tstoreddef(generate_specialization_phase1(context,tt,parsedtype,symname,parsedpos));
  975. if genericdef<>generrordef then
  976. genericdef:=tstoreddef(generate_specialization_phase2(context,genericdef,parse_class_parent,_prettyname));
  977. tt:=genericdef;
  978. if assigned(context) then
  979. context.free;
  980. end;
  981. function parse_generic_parameters(allowconstraints:boolean):tfphashobjectlist;
  982. var
  983. generictype : ttypesym;
  984. i,firstidx : longint;
  985. srsymtable : tsymtable;
  986. basedef,def : tdef;
  987. defname : tidstring;
  988. allowconstructor,
  989. doconsume : boolean;
  990. constraintdata : tgenericconstraintdata;
  991. old_block_type : tblock_type;
  992. begin
  993. result:=tfphashobjectlist.create(false);
  994. firstidx:=0;
  995. old_block_type:=block_type;
  996. block_type:=bt_type;
  997. repeat
  998. if token=_ID then
  999. begin
  1000. generictype:=ctypesym.create(orgpattern,cundefinedtype,false);
  1001. { type parameters need to be added as strict private }
  1002. generictype.visibility:=vis_strictprivate;
  1003. include(generictype.symoptions,sp_generic_para);
  1004. result.add(orgpattern,generictype);
  1005. end;
  1006. consume(_ID);
  1007. if try_to_consume(_COLON) then
  1008. begin
  1009. if not allowconstraints then
  1010. { TODO }
  1011. Message(parser_e_illegal_expression{ parser_e_generic_constraints_not_allowed_here});
  1012. { construct a name which can be used for a type specification }
  1013. constraintdata:=tgenericconstraintdata.create;
  1014. defname:='';
  1015. str(current_module.deflist.count,defname);
  1016. defname:='$gendef'+defname;
  1017. allowconstructor:=m_delphi in current_settings.modeswitches;
  1018. basedef:=generrordef;
  1019. repeat
  1020. doconsume:=true;
  1021. case token of
  1022. _CONSTRUCTOR:
  1023. begin
  1024. if not allowconstructor or (gcf_constructor in constraintdata.flags) then
  1025. Message(parser_e_illegal_expression);
  1026. include(constraintdata.flags,gcf_constructor);
  1027. allowconstructor:=false;
  1028. end;
  1029. _CLASS:
  1030. begin
  1031. if gcf_class in constraintdata.flags then
  1032. Message(parser_e_illegal_expression);
  1033. if basedef=generrordef then
  1034. include(constraintdata.flags,gcf_class)
  1035. else
  1036. Message(parser_e_illegal_expression);
  1037. end;
  1038. _RECORD:
  1039. begin
  1040. if ([gcf_constructor,gcf_class]*constraintdata.flags<>[])
  1041. or (constraintdata.interfaces.count>0) then
  1042. Message(parser_e_illegal_expression)
  1043. else
  1044. begin
  1045. srsymtable:=trecordsymtable.create(defname,0,1,1);
  1046. basedef:=crecorddef.create(defname,srsymtable);
  1047. include(constraintdata.flags,gcf_record);
  1048. allowconstructor:=false;
  1049. end;
  1050. end;
  1051. else
  1052. begin
  1053. { after single_type "token" is the trailing ",", ";" or
  1054. ">"! }
  1055. doconsume:=false;
  1056. { def is already set to a class or record }
  1057. if gcf_record in constraintdata.flags then
  1058. Message(parser_e_illegal_expression);
  1059. single_type(def, [stoAllowSpecialization]);
  1060. { only types that are inheritable are allowed }
  1061. if (def.typ<>objectdef) or
  1062. not (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_interfacejava,odt_javaclass]) then
  1063. Message1(type_e_class_or_interface_type_expected,def.typename)
  1064. else
  1065. case tobjectdef(def).objecttype of
  1066. odt_class,
  1067. odt_javaclass:
  1068. begin
  1069. if gcf_class in constraintdata.flags then
  1070. { "class" + concrete class is not allowed }
  1071. Message(parser_e_illegal_expression)
  1072. else
  1073. { do we already have a concrete class? }
  1074. if basedef<>generrordef then
  1075. Message(parser_e_illegal_expression)
  1076. else
  1077. basedef:=def;
  1078. end;
  1079. odt_interfacecom,
  1080. odt_interfacecorba,
  1081. odt_interfacejava,
  1082. odt_dispinterface:
  1083. constraintdata.interfaces.add(def);
  1084. end;
  1085. end;
  1086. end;
  1087. if doconsume then
  1088. consume(token);
  1089. until not try_to_consume(_COMMA);
  1090. if ([gcf_class,gcf_constructor]*constraintdata.flags<>[]) or
  1091. (constraintdata.interfaces.count>1) or
  1092. (
  1093. (basedef.typ=objectdef) and
  1094. (tobjectdef(basedef).objecttype in [odt_javaclass,odt_class])
  1095. ) then
  1096. begin
  1097. if basedef.typ=errordef then
  1098. { don't pass an errordef as a parent to a tobjectdef }
  1099. basedef:=class_tobject
  1100. else
  1101. if (basedef.typ<>objectdef) or
  1102. not (tobjectdef(basedef).objecttype in [odt_javaclass,odt_class]) then
  1103. internalerror(2012101101);
  1104. basedef:=cobjectdef.create(tobjectdef(basedef).objecttype,defname,tobjectdef(basedef),false);
  1105. for i:=0 to constraintdata.interfaces.count-1 do
  1106. tobjectdef(basedef).implementedinterfaces.add(
  1107. timplementedinterface.create(tobjectdef(constraintdata.interfaces[i])));
  1108. end
  1109. else
  1110. if constraintdata.interfaces.count=1 then
  1111. begin
  1112. if basedef.typ<>errordef then
  1113. internalerror(2013021601);
  1114. def:=tdef(constraintdata.interfaces[0]);
  1115. basedef:=cobjectdef.create(tobjectdef(def).objecttype,defname,tobjectdef(def),false);
  1116. constraintdata.interfaces.delete(0);
  1117. end;
  1118. if basedef.typ<>errordef then
  1119. with tstoreddef(basedef) do
  1120. begin
  1121. genconstraintdata:=tgenericconstraintdata.create;
  1122. genconstraintdata.flags:=constraintdata.flags;
  1123. genconstraintdata.interfaces.assign(constraintdata.interfaces);
  1124. include(defoptions,df_genconstraint);
  1125. end;
  1126. for i:=firstidx to result.count-1 do
  1127. ttypesym(result[i]).typedef:=basedef;
  1128. { we need a typesym in case we do a Delphi-mode inline
  1129. specialization with this parameter; so just use the first sym }
  1130. if not assigned(basedef.typesym) then
  1131. basedef.typesym:=ttypesym(result[firstidx]);
  1132. firstidx:=result.count;
  1133. constraintdata.free;
  1134. end
  1135. else
  1136. begin
  1137. if token=_SEMICOLON then
  1138. begin
  1139. { two different typeless parameters are considered as incompatible }
  1140. for i:=firstidx to result.count-1 do
  1141. begin
  1142. ttypesym(result[i]).typedef:=cundefineddef.create(false);
  1143. ttypesym(result[i]).typedef.typesym:=ttypesym(result[i]);
  1144. end;
  1145. { a semicolon terminates a type parameter group }
  1146. firstidx:=result.count;
  1147. end;
  1148. end;
  1149. until not (try_to_consume(_COMMA) or try_to_consume(_SEMICOLON));
  1150. { two different typeless parameters are considered as incompatible }
  1151. for i:=firstidx to result.count-1 do
  1152. begin
  1153. ttypesym(result[i]).typedef:=cundefineddef.create(false);
  1154. ttypesym(result[i]).typedef.typesym:=ttypesym(result[i]);
  1155. end;
  1156. block_type:=old_block_type;
  1157. end;
  1158. procedure insert_generic_parameter_types(def:tstoreddef;genericdef:tstoreddef;genericlist:tfphashobjectlist);
  1159. var
  1160. i : longint;
  1161. generictype,sym : ttypesym;
  1162. st : tsymtable;
  1163. begin
  1164. def.genericdef:=genericdef;
  1165. if not assigned(genericlist) then
  1166. exit;
  1167. if assigned(genericdef) then
  1168. include(def.defoptions,df_specialization)
  1169. else
  1170. if genericlist.count>0 then
  1171. include(def.defoptions,df_generic);
  1172. case def.typ of
  1173. recorddef,objectdef: st:=tabstractrecorddef(def).symtable;
  1174. arraydef: st:=tarraydef(def).symtable;
  1175. procvardef,procdef: st:=tabstractprocdef(def).parast;
  1176. else
  1177. internalerror(201101020);
  1178. end;
  1179. if (genericlist.count>0) and not assigned(def.genericparas) then
  1180. def.genericparas:=tfphashobjectlist.create(false);
  1181. for i:=0 to genericlist.count-1 do
  1182. begin
  1183. generictype:=ttypesym(genericlist[i]);
  1184. if assigned(generictype.owner) then
  1185. begin
  1186. sym:=ctypesym.create(genericlist.nameofindex(i),generictype.typedef,true);
  1187. { type parameters need to be added as strict private }
  1188. sym.visibility:=vis_strictprivate;
  1189. st.insert(sym);
  1190. include(sym.symoptions,sp_generic_para);
  1191. end
  1192. else
  1193. begin
  1194. if (generictype.typedef.typ=undefineddef) and (generictype.typedef<>cundefinedtype) then
  1195. begin
  1196. { the generic parameters were parsed before the genericdef existed thus the
  1197. undefineddefs were added as part of the parent symtable }
  1198. if assigned(generictype.typedef.owner) then
  1199. generictype.typedef.owner.DefList.Extract(generictype.typedef);
  1200. generictype.typedef.changeowner(st);
  1201. end;
  1202. st.insert(generictype);
  1203. include(generictype.symoptions,sp_generic_para);
  1204. end;
  1205. def.genericparas.add(genericlist.nameofindex(i),generictype);
  1206. end;
  1207. end;
  1208. procedure maybe_insert_generic_rename_symbol(const name:tidstring;genericlist:tfphashobjectlist);
  1209. var
  1210. gensym : ttypesym;
  1211. begin
  1212. { for generics in non-Delphi modes we insert a private type symbol
  1213. that has the same base name as the currently parsed generic and
  1214. that references this defs }
  1215. if not (m_delphi in current_settings.modeswitches) and
  1216. (
  1217. (
  1218. parse_generic and
  1219. assigned(genericlist) and
  1220. (genericlist.count>0)
  1221. ) or
  1222. (
  1223. assigned(current_specializedef) and
  1224. assigned(current_structdef.genericdef) and
  1225. (current_structdef.genericdef.typ in [objectdef,recorddef]) and
  1226. (pos('$',name)>0)
  1227. )
  1228. ) then
  1229. begin
  1230. { we need to pass nil as def here, because the constructor wants
  1231. to set the typesym of the def which is not what we want }
  1232. gensym:=ctypesym.create(copy(name,1,pos('$',name)-1),nil,true);
  1233. gensym.typedef:=current_structdef;
  1234. include(gensym.symoptions,sp_internal);
  1235. { the symbol should be only visible to the generic class
  1236. itself }
  1237. gensym.visibility:=vis_strictprivate;
  1238. symtablestack.top.insert(gensym);
  1239. end;
  1240. end;
  1241. function generate_generic_name(const name:tidstring;specializename:ansistring;owner_hierarchy:string):tidstring;
  1242. var
  1243. crc : cardinal;
  1244. begin
  1245. if specializename='' then
  1246. internalerror(2012061901);
  1247. { build the new type's name }
  1248. crc:=UpdateCrc32(0,specializename[1],length(specializename));
  1249. result:=name+'$crc'+hexstr(crc,8);
  1250. if owner_hierarchy<>'' then
  1251. begin
  1252. crc:=UpdateCrc32(0,owner_hierarchy[1],length(owner_hierarchy));
  1253. result:=result+'$crc'+hexstr(crc,8);
  1254. end;
  1255. end;
  1256. procedure split_generic_name(const name:tidstring;out nongeneric:string;out count:longint);
  1257. var
  1258. i,code : longint;
  1259. countstr : string;
  1260. begin
  1261. for i:=length(name) downto 1 do
  1262. if name[i]='$' then
  1263. begin
  1264. nongeneric:=copy(name,1,i-1);
  1265. countstr:=copy(name,i+1,length(name)-i);
  1266. val(countstr,count,code);
  1267. if code<>0 then
  1268. break;
  1269. exit;
  1270. end;
  1271. nongeneric:=name;
  1272. count:=0;
  1273. end;
  1274. function resolve_generic_dummysym(const name:tidstring):tsym;
  1275. var
  1276. list : tfpobjectlist;
  1277. begin
  1278. list:=tfpobjectlist(current_module.genericdummysyms.find(name));
  1279. if assigned(list) and (list.count>0) then
  1280. result:=tgenericdummyentry(list.last).resolvedsym
  1281. else
  1282. result:=nil;
  1283. end;
  1284. function could_be_generic(const name:tidstring):boolean;
  1285. begin
  1286. result:=(name<>'') and
  1287. (current_module.genericdummysyms.findindexof(name)>=0);
  1288. end;
  1289. procedure specialization_init(genericdef:tdef;var state: tspecializationstate);
  1290. var
  1291. pu : tused_unit;
  1292. hmodule : tmodule;
  1293. unitsyms : TFPHashObjectList;
  1294. sym : tsym;
  1295. i : Integer;
  1296. begin
  1297. if not assigned(genericdef) then
  1298. internalerror(200705151);
  1299. { Setup symtablestack at definition time
  1300. to get types right, however this is not perfect, we should probably record
  1301. the resolved symbols }
  1302. state.oldsymtablestack:=symtablestack;
  1303. state.oldextendeddefs:=current_module.extendeddefs;
  1304. state.oldgenericdummysyms:=current_module.genericdummysyms;
  1305. current_module.extendeddefs:=TFPHashObjectList.create(true);
  1306. current_module.genericdummysyms:=tfphashobjectlist.create(true);
  1307. symtablestack:=tdefawaresymtablestack.create;
  1308. hmodule:=find_module_from_symtable(genericdef.owner);
  1309. if hmodule=nil then
  1310. internalerror(200705152);
  1311. { collect all unit syms in the generic's unit as we need to establish
  1312. their unitsym.module link again so that unit identifiers can be used }
  1313. unitsyms:=tfphashobjectlist.create(false);
  1314. if (hmodule<>current_module) and assigned(hmodule.globalsymtable) then
  1315. for i:=0 to hmodule.globalsymtable.symlist.count-1 do
  1316. begin
  1317. sym:=tsym(hmodule.globalsymtable.symlist[i]);
  1318. if sym.typ=unitsym then
  1319. unitsyms.add(upper(sym.realname),sym);
  1320. end;
  1321. { add all units if we are specializing inside the current unit (as the
  1322. generic could have been declared in the implementation part), but load
  1323. only interface units, if we are in a different unit as then the generic
  1324. needs to be in the interface section }
  1325. pu:=tused_unit(hmodule.used_units.first);
  1326. while assigned(pu) do
  1327. begin
  1328. if not assigned(pu.u.globalsymtable) then
  1329. { in certain circular, but valid unit constellations it can happen
  1330. that we specialize a generic in a different unit that was used
  1331. in the implementation section of the generic's unit and were the
  1332. interface is still being parsed and thus the localsymtable is in
  1333. reality the global symtable }
  1334. if pu.u.in_interface then
  1335. symtablestack.push(pu.u.localsymtable)
  1336. else
  1337. internalerror(200705153)
  1338. else
  1339. symtablestack.push(pu.u.globalsymtable);
  1340. sym:=tsym(unitsyms.find(pu.u.modulename^));
  1341. if assigned(sym) and not assigned(tunitsym(sym).module) then
  1342. tunitsym(sym).module:=pu.u;
  1343. pu:=tused_unit(pu.next);
  1344. end;
  1345. unitsyms.free;
  1346. if assigned(hmodule.globalsymtable) then
  1347. symtablestack.push(hmodule.globalsymtable);
  1348. { push the localsymtable if needed }
  1349. if ((hmodule<>current_module) or not current_module.in_interface)
  1350. and assigned(hmodule.localsymtable) then
  1351. symtablestack.push(hmodule.localsymtable);
  1352. end;
  1353. procedure specialization_done(var state: tspecializationstate);
  1354. begin
  1355. { Restore symtablestack }
  1356. current_module.extendeddefs.free;
  1357. current_module.extendeddefs:=state.oldextendeddefs;
  1358. current_module.genericdummysyms.free;
  1359. current_module.genericdummysyms:=state.oldgenericdummysyms;
  1360. symtablestack.free;
  1361. symtablestack:=state.oldsymtablestack;
  1362. { clear the state record to be on the safe side }
  1363. fillchar(state, sizeof(state), 0);
  1364. end;
  1365. end.