pgenutil.pas 69 KB

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