pgenutil.pas 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670
  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. srsym:=ctypesym.create(finalspecializename,generrordef,true);
  817. if genericdef.typ=procdef then
  818. srsym:=cprocsym.create(finalspecializename)
  819. else
  820. srsym:=ctypesym.create(finalspecializename,generrordef,true);
  821. { insert the symbol only if we don't know already that we have
  822. a procsym to add it to }
  823. if not assigned(psym) then
  824. specializest.insert(srsym);
  825. { specializations are declarations as such it is the wisest to
  826. declare set the blocktype to "type"; otherwise we'll
  827. experience unexpected side effects like the addition of
  828. classrefdefs if we have a generic that's derived from another
  829. generic }
  830. old_block_type:=block_type;
  831. block_type:=bt_type;
  832. if (
  833. (genericdef.typ=procdef) and
  834. not assigned(tprocdef(genericdef).genericdecltokenbuf)
  835. ) or (
  836. (genericdef.typ<>procdef) and
  837. not assigned(genericdef.generictokenbuf)
  838. ) then
  839. internalerror(200511171);
  840. hmodule:=find_module_from_symtable(genericdef.owner);
  841. if hmodule=nil then
  842. internalerror(2012051202);
  843. oldcurrent_filepos:=current_filepos;
  844. { use the index the module got from the current compilation process }
  845. current_filepos.moduleindex:=hmodule.unit_index;
  846. current_tokenpos:=current_filepos;
  847. if parse_generic then
  848. begin
  849. recordbuf:=current_scanner.recordtokenbuf;
  850. current_scanner.recordtokenbuf:=nil;
  851. end
  852. else
  853. recordbuf:=nil;
  854. replaydepth:=current_scanner.replay_stack_depth;
  855. if genericdef.typ=procdef then
  856. begin
  857. current_scanner.startreplaytokens(tprocdef(genericdef).genericdecltokenbuf);
  858. parse_proc_head(tprocdef(genericdef).struct,tprocdef(genericdef).proctypeoption,false,genericdef,generictypelist,pd);
  859. if assigned(pd) then
  860. begin
  861. if assigned(psym) then
  862. pd.procsym:=psym
  863. else
  864. pd.procsym:=srsym;
  865. parse_proc_dec_finish(pd,po_classmethod in tprocdef(genericdef).procoptions);
  866. end;
  867. result:=pd;
  868. end
  869. else
  870. begin
  871. current_scanner.startreplaytokens(genericdef.generictokenbuf);
  872. hadtypetoken:=false;
  873. read_named_type(result,srsym,genericdef,generictypelist,false,hadtypetoken);
  874. ttypesym(srsym).typedef:=result;
  875. result.typesym:=srsym;
  876. if _prettyname<>'' then
  877. ttypesym(result.typesym).fprettyname:=_prettyname
  878. else
  879. ttypesym(result.typesym).fprettyname:=prettyname;
  880. end;
  881. current_filepos:=oldcurrent_filepos;
  882. { Note regarding hint directives:
  883. There is no need to remove the flags for them from the
  884. specialized generic symbol, because hint directives that
  885. follow the specialization are handled by the code in
  886. pdecl.types_dec and added to the type symbol.
  887. E.g.: TFoo = TBar<Blubb> deprecated;
  888. Here the symbol TBar$1$Blubb will contain the
  889. "sp_hint_deprecated" flag while the TFoo symbol won't.}
  890. case result.typ of
  891. { Build VMT indexes for classes and read hint directives }
  892. objectdef:
  893. begin
  894. if replaydepth>current_scanner.replay_stack_depth then
  895. begin
  896. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  897. if replaydepth>current_scanner.replay_stack_depth then
  898. consume(_SEMICOLON);
  899. end;
  900. vmtbuilder:=TVMTBuilder.Create(tobjectdef(result));
  901. vmtbuilder.generate_vmt;
  902. vmtbuilder.free;
  903. end;
  904. { handle params, calling convention, etc }
  905. procvardef:
  906. begin
  907. hintsprocessed:=false;
  908. if replaydepth>current_scanner.replay_stack_depth then
  909. begin
  910. if not check_proc_directive(true) then
  911. begin
  912. hintsprocessed:=try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg);
  913. if replaydepth>current_scanner.replay_stack_depth then
  914. consume(_SEMICOLON);
  915. end
  916. else
  917. hintsprocessed:=true;
  918. end;
  919. if replaydepth>current_scanner.replay_stack_depth then
  920. parse_var_proc_directives(ttypesym(srsym));
  921. handle_calling_convention(tprocvardef(result));
  922. if not hintsprocessed and (replaydepth>current_scanner.replay_stack_depth) then
  923. begin
  924. try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg);
  925. if replaydepth>current_scanner.replay_stack_depth then
  926. consume(_SEMICOLON);
  927. end;
  928. end;
  929. procdef:
  930. begin
  931. pdflags:=[pd_body,pd_implemen];
  932. if genericdef.owner.symtabletype=objectsymtable then
  933. include(pdflags,pd_object)
  934. else if genericdef.owner.symtabletype=recordsymtable then
  935. include(pdflags,pd_record);
  936. parse_proc_directives(pd,pdflags);
  937. while try_consume_hintdirective(pd.symoptions,pd.deprecatedmsg) do
  938. consume(_SEMICOLON);
  939. handle_calling_convention(tprocdef(result),hcc_all);
  940. proc_add_definition(tprocdef(result));
  941. { for partial specializations we implicitely declare the routine as
  942. having its implementation although we'll not specialize it in reality }
  943. if parse_generic then
  944. unset_forwarddef(result);
  945. end;
  946. else
  947. { parse hint directives for records and arrays }
  948. if replaydepth>current_scanner.replay_stack_depth then begin
  949. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  950. if replaydepth>current_scanner.replay_stack_depth then
  951. consume(_SEMICOLON);
  952. end;
  953. end;
  954. { Consume the remainder of the buffer }
  955. while current_scanner.replay_stack_depth>replaydepth do
  956. consume(token);
  957. if assigned(recordbuf) then
  958. begin
  959. if assigned(current_scanner.recordtokenbuf) then
  960. internalerror(2014050909);
  961. current_scanner.recordtokenbuf:=recordbuf;
  962. end;
  963. block_type:=old_block_type;
  964. if parse_class_parent then
  965. begin
  966. current_structdef:=old_current_structdef;
  967. current_genericdef:=old_current_genericdef;
  968. current_specializedef:=old_current_specializedef;
  969. end;
  970. end;
  971. { extract all created symbols and defs from the temporary symtable
  972. and add them to the specializest }
  973. for i:=tempst.SymList.Count-1 downto 0 do
  974. begin
  975. item:=tempst.SymList.Items[i];
  976. { using changeowner the symbol is automatically added to the
  977. new symtable }
  978. tsym(item).ChangeOwner(specializest);
  979. end;
  980. for i:=tempst.DefList.Count-1 downto 0 do
  981. begin
  982. item:=tempst.DefList.Items[i];
  983. { using changeowner the def is automatically added to the new
  984. symtable }
  985. tdef(item).ChangeOwner(specializest);
  986. { for partial specializations we implicitely declare any methods as having their
  987. implementations although we'll not specialize them in reality }
  988. if parse_generic then
  989. unset_forwarddef(tdef(item));
  990. end;
  991. { if a generic was declared during the specialization we need to
  992. flag the specialize symtable accordingly }
  993. if sto_has_generic in tempst.tableoptions then
  994. specializest.includeoption(sto_has_generic);
  995. tempst.free;
  996. specialization_done(state);
  997. { procdefs are only added once we know which overload we use }
  998. if not parse_generic and (result.typ<>procdef) then
  999. current_module.pendingspecializations.add(result.typename,result);
  1000. end;
  1001. generictypelist.free;
  1002. if assigned(genericdef) then
  1003. begin
  1004. { check the hints of the found generic symbol }
  1005. if genericdef.typ=procdef then
  1006. srsym:=tprocdef(genericdef).procsym
  1007. else
  1008. srsym:=genericdef.typesym;
  1009. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1010. end;
  1011. end;
  1012. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;_prettyname:string;parsedtype:tdef;symname:string;parsedpos:tfileposinfo);
  1013. var
  1014. context : tspecializationcontext;
  1015. genericdef : tstoreddef;
  1016. begin
  1017. genericdef:=tstoreddef(generate_specialization_phase1(context,tt,parsedtype,symname,parsedpos));
  1018. if genericdef<>generrordef then
  1019. genericdef:=tstoreddef(generate_specialization_phase2(context,genericdef,parse_class_parent,_prettyname));
  1020. tt:=genericdef;
  1021. if assigned(context) then
  1022. context.free;
  1023. end;
  1024. function parse_generic_parameters(allowconstraints:boolean):tfphashobjectlist;
  1025. var
  1026. generictype : ttypesym;
  1027. i,firstidx : longint;
  1028. srsymtable : tsymtable;
  1029. basedef,def : tdef;
  1030. defname : tidstring;
  1031. allowconstructor,
  1032. doconsume : boolean;
  1033. constraintdata : tgenericconstraintdata;
  1034. old_block_type : tblock_type;
  1035. begin
  1036. result:=tfphashobjectlist.create(false);
  1037. firstidx:=0;
  1038. old_block_type:=block_type;
  1039. block_type:=bt_type;
  1040. repeat
  1041. if token=_ID then
  1042. begin
  1043. generictype:=ctypesym.create(orgpattern,cundefinedtype,false);
  1044. { type parameters need to be added as strict private }
  1045. generictype.visibility:=vis_strictprivate;
  1046. include(generictype.symoptions,sp_generic_para);
  1047. result.add(orgpattern,generictype);
  1048. end;
  1049. consume(_ID);
  1050. if try_to_consume(_COLON) then
  1051. begin
  1052. if not allowconstraints then
  1053. { TODO }
  1054. Message(parser_e_illegal_expression{ parser_e_generic_constraints_not_allowed_here});
  1055. { construct a name which can be used for a type specification }
  1056. constraintdata:=tgenericconstraintdata.create;
  1057. defname:='';
  1058. str(current_module.deflist.count,defname);
  1059. defname:='$gendef'+defname;
  1060. allowconstructor:=m_delphi in current_settings.modeswitches;
  1061. basedef:=generrordef;
  1062. repeat
  1063. doconsume:=true;
  1064. case token of
  1065. _CONSTRUCTOR:
  1066. begin
  1067. if not allowconstructor or (gcf_constructor in constraintdata.flags) then
  1068. Message(parser_e_illegal_expression);
  1069. include(constraintdata.flags,gcf_constructor);
  1070. allowconstructor:=false;
  1071. end;
  1072. _CLASS:
  1073. begin
  1074. if gcf_class in constraintdata.flags then
  1075. Message(parser_e_illegal_expression);
  1076. if basedef=generrordef then
  1077. include(constraintdata.flags,gcf_class)
  1078. else
  1079. Message(parser_e_illegal_expression);
  1080. end;
  1081. _RECORD:
  1082. begin
  1083. if ([gcf_constructor,gcf_class]*constraintdata.flags<>[])
  1084. or (constraintdata.interfaces.count>0) then
  1085. Message(parser_e_illegal_expression)
  1086. else
  1087. begin
  1088. srsymtable:=trecordsymtable.create(defname,0,1,1);
  1089. basedef:=crecorddef.create(defname,srsymtable);
  1090. include(constraintdata.flags,gcf_record);
  1091. allowconstructor:=false;
  1092. end;
  1093. end;
  1094. else
  1095. begin
  1096. { after single_type "token" is the trailing ",", ";" or
  1097. ">"! }
  1098. doconsume:=false;
  1099. { def is already set to a class or record }
  1100. if gcf_record in constraintdata.flags then
  1101. Message(parser_e_illegal_expression);
  1102. single_type(def, [stoAllowSpecialization]);
  1103. { only types that are inheritable are allowed }
  1104. if (def.typ<>objectdef) or
  1105. not (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_interfacejava,odt_javaclass]) then
  1106. Message1(type_e_class_or_interface_type_expected,def.typename)
  1107. else
  1108. case tobjectdef(def).objecttype of
  1109. odt_class,
  1110. odt_javaclass:
  1111. begin
  1112. if gcf_class in constraintdata.flags then
  1113. { "class" + concrete class is not allowed }
  1114. Message(parser_e_illegal_expression)
  1115. else
  1116. { do we already have a concrete class? }
  1117. if basedef<>generrordef then
  1118. Message(parser_e_illegal_expression)
  1119. else
  1120. basedef:=def;
  1121. end;
  1122. odt_interfacecom,
  1123. odt_interfacecorba,
  1124. odt_interfacejava,
  1125. odt_dispinterface:
  1126. constraintdata.interfaces.add(def);
  1127. end;
  1128. end;
  1129. end;
  1130. if doconsume then
  1131. consume(token);
  1132. until not try_to_consume(_COMMA);
  1133. if ([gcf_class,gcf_constructor]*constraintdata.flags<>[]) or
  1134. (constraintdata.interfaces.count>1) or
  1135. (
  1136. (basedef.typ=objectdef) and
  1137. (tobjectdef(basedef).objecttype in [odt_javaclass,odt_class])
  1138. ) then
  1139. begin
  1140. if basedef.typ=errordef then
  1141. { don't pass an errordef as a parent to a tobjectdef }
  1142. basedef:=class_tobject
  1143. else
  1144. if (basedef.typ<>objectdef) or
  1145. not (tobjectdef(basedef).objecttype in [odt_javaclass,odt_class]) then
  1146. internalerror(2012101101);
  1147. basedef:=cobjectdef.create(tobjectdef(basedef).objecttype,defname,tobjectdef(basedef),false);
  1148. for i:=0 to constraintdata.interfaces.count-1 do
  1149. tobjectdef(basedef).implementedinterfaces.add(
  1150. timplementedinterface.create(tobjectdef(constraintdata.interfaces[i])));
  1151. end
  1152. else
  1153. if constraintdata.interfaces.count=1 then
  1154. begin
  1155. if basedef.typ<>errordef then
  1156. internalerror(2013021601);
  1157. def:=tdef(constraintdata.interfaces[0]);
  1158. basedef:=cobjectdef.create(tobjectdef(def).objecttype,defname,tobjectdef(def),false);
  1159. constraintdata.interfaces.delete(0);
  1160. end;
  1161. if basedef.typ<>errordef then
  1162. with tstoreddef(basedef) do
  1163. begin
  1164. genconstraintdata:=tgenericconstraintdata.create;
  1165. genconstraintdata.flags:=constraintdata.flags;
  1166. genconstraintdata.interfaces.assign(constraintdata.interfaces);
  1167. include(defoptions,df_genconstraint);
  1168. end;
  1169. for i:=firstidx to result.count-1 do
  1170. ttypesym(result[i]).typedef:=basedef;
  1171. { we need a typesym in case we do a Delphi-mode inline
  1172. specialization with this parameter; so just use the first sym }
  1173. if not assigned(basedef.typesym) then
  1174. basedef.typesym:=ttypesym(result[firstidx]);
  1175. firstidx:=result.count;
  1176. constraintdata.free;
  1177. end
  1178. else
  1179. begin
  1180. if token=_SEMICOLON then
  1181. begin
  1182. { two different typeless parameters are considered as incompatible }
  1183. for i:=firstidx to result.count-1 do
  1184. begin
  1185. ttypesym(result[i]).typedef:=cundefineddef.create(false);
  1186. ttypesym(result[i]).typedef.typesym:=ttypesym(result[i]);
  1187. end;
  1188. { a semicolon terminates a type parameter group }
  1189. firstidx:=result.count;
  1190. end;
  1191. end;
  1192. until not (try_to_consume(_COMMA) or try_to_consume(_SEMICOLON));
  1193. { two different typeless parameters are considered as incompatible }
  1194. for i:=firstidx to result.count-1 do
  1195. begin
  1196. ttypesym(result[i]).typedef:=cundefineddef.create(false);
  1197. ttypesym(result[i]).typedef.typesym:=ttypesym(result[i]);
  1198. end;
  1199. block_type:=old_block_type;
  1200. end;
  1201. procedure insert_generic_parameter_types(def:tstoreddef;genericdef:tstoreddef;genericlist:tfphashobjectlist);
  1202. var
  1203. i : longint;
  1204. generictype,sym : ttypesym;
  1205. st : tsymtable;
  1206. begin
  1207. def.genericdef:=genericdef;
  1208. if not assigned(genericlist) then
  1209. exit;
  1210. if assigned(genericdef) then
  1211. include(def.defoptions,df_specialization)
  1212. else
  1213. if genericlist.count>0 then
  1214. include(def.defoptions,df_generic);
  1215. case def.typ of
  1216. recorddef,objectdef: st:=tabstractrecorddef(def).symtable;
  1217. arraydef: st:=tarraydef(def).symtable;
  1218. procvardef,procdef: st:=tabstractprocdef(def).parast;
  1219. else
  1220. internalerror(201101020);
  1221. end;
  1222. if (genericlist.count>0) and not assigned(def.genericparas) then
  1223. def.genericparas:=tfphashobjectlist.create(false);
  1224. for i:=0 to genericlist.count-1 do
  1225. begin
  1226. generictype:=ttypesym(genericlist[i]);
  1227. if assigned(generictype.owner) then
  1228. begin
  1229. sym:=ctypesym.create(genericlist.nameofindex(i),generictype.typedef,true);
  1230. { type parameters need to be added as strict private }
  1231. sym.visibility:=vis_strictprivate;
  1232. st.insert(sym);
  1233. include(sym.symoptions,sp_generic_para);
  1234. end
  1235. else
  1236. begin
  1237. if (generictype.typedef.typ=undefineddef) and (generictype.typedef<>cundefinedtype) then
  1238. begin
  1239. { the generic parameters were parsed before the genericdef existed thus the
  1240. undefineddefs were added as part of the parent symtable }
  1241. if assigned(generictype.typedef.owner) then
  1242. generictype.typedef.owner.DefList.Extract(generictype.typedef);
  1243. generictype.typedef.changeowner(st);
  1244. end;
  1245. st.insert(generictype);
  1246. include(generictype.symoptions,sp_generic_para);
  1247. end;
  1248. def.genericparas.add(genericlist.nameofindex(i),generictype);
  1249. end;
  1250. end;
  1251. procedure maybe_insert_generic_rename_symbol(const name:tidstring;genericlist:tfphashobjectlist);
  1252. var
  1253. gensym : ttypesym;
  1254. begin
  1255. { for generics in non-Delphi modes we insert a private type symbol
  1256. that has the same base name as the currently parsed generic and
  1257. that references this defs }
  1258. if not (m_delphi in current_settings.modeswitches) and
  1259. (
  1260. (
  1261. parse_generic and
  1262. assigned(genericlist) and
  1263. (genericlist.count>0)
  1264. ) or
  1265. (
  1266. assigned(current_specializedef) and
  1267. assigned(current_structdef.genericdef) and
  1268. (current_structdef.genericdef.typ in [objectdef,recorddef]) and
  1269. (pos('$',name)>0)
  1270. )
  1271. ) then
  1272. begin
  1273. { we need to pass nil as def here, because the constructor wants
  1274. to set the typesym of the def which is not what we want }
  1275. gensym:=ctypesym.create(copy(name,1,pos('$',name)-1),nil,true);
  1276. gensym.typedef:=current_structdef;
  1277. include(gensym.symoptions,sp_internal);
  1278. { the symbol should be only visible to the generic class
  1279. itself }
  1280. gensym.visibility:=vis_strictprivate;
  1281. symtablestack.top.insert(gensym);
  1282. end;
  1283. end;
  1284. function generate_generic_name(const name:tidstring;specializename:ansistring;owner_hierarchy:string):tidstring;
  1285. var
  1286. crc : cardinal;
  1287. begin
  1288. if specializename='' then
  1289. internalerror(2012061901);
  1290. { build the new type's name }
  1291. crc:=UpdateCrc32(0,specializename[1],length(specializename));
  1292. result:=name+'$crc'+hexstr(crc,8);
  1293. if owner_hierarchy<>'' then
  1294. begin
  1295. crc:=UpdateCrc32(0,owner_hierarchy[1],length(owner_hierarchy));
  1296. result:=result+'$crc'+hexstr(crc,8);
  1297. end;
  1298. end;
  1299. procedure split_generic_name(const name:tidstring;out nongeneric:string;out count:longint);
  1300. var
  1301. i,code : longint;
  1302. countstr : string;
  1303. begin
  1304. for i:=length(name) downto 1 do
  1305. if name[i]='$' then
  1306. begin
  1307. nongeneric:=copy(name,1,i-1);
  1308. countstr:=copy(name,i+1,length(name)-i);
  1309. val(countstr,count,code);
  1310. if code<>0 then
  1311. break;
  1312. exit;
  1313. end;
  1314. nongeneric:=name;
  1315. count:=0;
  1316. end;
  1317. function resolve_generic_dummysym(const name:tidstring):tsym;
  1318. var
  1319. list : tfpobjectlist;
  1320. begin
  1321. list:=tfpobjectlist(current_module.genericdummysyms.find(name));
  1322. if assigned(list) and (list.count>0) then
  1323. result:=tgenericdummyentry(list.last).resolvedsym
  1324. else
  1325. result:=nil;
  1326. end;
  1327. function could_be_generic(const name:tidstring):boolean;
  1328. begin
  1329. result:=(name<>'') and
  1330. (current_module.genericdummysyms.findindexof(name)>=0);
  1331. end;
  1332. procedure specialization_init(genericdef:tdef;var state: tspecializationstate);
  1333. var
  1334. pu : tused_unit;
  1335. hmodule : tmodule;
  1336. unitsyms : TFPHashObjectList;
  1337. sym : tsym;
  1338. i : Integer;
  1339. begin
  1340. if not assigned(genericdef) then
  1341. internalerror(200705151);
  1342. { Setup symtablestack at definition time
  1343. to get types right, however this is not perfect, we should probably record
  1344. the resolved symbols }
  1345. state.oldsymtablestack:=symtablestack;
  1346. state.oldextendeddefs:=current_module.extendeddefs;
  1347. state.oldgenericdummysyms:=current_module.genericdummysyms;
  1348. current_module.extendeddefs:=TFPHashObjectList.create(true);
  1349. current_module.genericdummysyms:=tfphashobjectlist.create(true);
  1350. symtablestack:=tdefawaresymtablestack.create;
  1351. hmodule:=find_module_from_symtable(genericdef.owner);
  1352. if hmodule=nil then
  1353. internalerror(200705152);
  1354. { collect all unit syms in the generic's unit as we need to establish
  1355. their unitsym.module link again so that unit identifiers can be used }
  1356. unitsyms:=tfphashobjectlist.create(false);
  1357. if (hmodule<>current_module) and assigned(hmodule.globalsymtable) then
  1358. for i:=0 to hmodule.globalsymtable.symlist.count-1 do
  1359. begin
  1360. sym:=tsym(hmodule.globalsymtable.symlist[i]);
  1361. if sym.typ=unitsym then
  1362. unitsyms.add(upper(sym.realname),sym);
  1363. end;
  1364. { add all units if we are specializing inside the current unit (as the
  1365. generic could have been declared in the implementation part), but load
  1366. only interface units, if we are in a different unit as then the generic
  1367. needs to be in the interface section }
  1368. pu:=tused_unit(hmodule.used_units.first);
  1369. while assigned(pu) do
  1370. begin
  1371. if not assigned(pu.u.globalsymtable) then
  1372. { in certain circular, but valid unit constellations it can happen
  1373. that we specialize a generic in a different unit that was used
  1374. in the implementation section of the generic's unit and were the
  1375. interface is still being parsed and thus the localsymtable is in
  1376. reality the global symtable }
  1377. if pu.u.in_interface then
  1378. symtablestack.push(pu.u.localsymtable)
  1379. else
  1380. internalerror(200705153)
  1381. else
  1382. symtablestack.push(pu.u.globalsymtable);
  1383. sym:=tsym(unitsyms.find(pu.u.modulename^));
  1384. if assigned(sym) and not assigned(tunitsym(sym).module) then
  1385. tunitsym(sym).module:=pu.u;
  1386. pu:=tused_unit(pu.next);
  1387. end;
  1388. unitsyms.free;
  1389. if assigned(hmodule.globalsymtable) then
  1390. symtablestack.push(hmodule.globalsymtable);
  1391. { push the localsymtable if needed }
  1392. if ((hmodule<>current_module) or not current_module.in_interface)
  1393. and assigned(hmodule.localsymtable) then
  1394. symtablestack.push(hmodule.localsymtable);
  1395. end;
  1396. procedure specialization_done(var state: tspecializationstate);
  1397. begin
  1398. { Restore symtablestack }
  1399. current_module.extendeddefs.free;
  1400. current_module.extendeddefs:=state.oldextendeddefs;
  1401. current_module.genericdummysyms.free;
  1402. current_module.genericdummysyms:=state.oldgenericdummysyms;
  1403. symtablestack.free;
  1404. symtablestack:=state.oldsymtablestack;
  1405. { clear the state record to be on the safe side }
  1406. fillchar(state, sizeof(state), 0);
  1407. end;
  1408. {****************************************************************************
  1409. SPECIALIZATION BODY GENERATION
  1410. ****************************************************************************}
  1411. procedure process_procdef(def:tprocdef;hmodule:tmodule);
  1412. var
  1413. oldcurrent_filepos : tfileposinfo;
  1414. begin
  1415. if assigned(def.genericdef) and
  1416. (def.genericdef.typ=procdef) and
  1417. assigned(tprocdef(def.genericdef).generictokenbuf) then
  1418. begin
  1419. if not assigned(tprocdef(def.genericdef).generictokenbuf) then
  1420. internalerror(2015061902);
  1421. oldcurrent_filepos:=current_filepos;
  1422. current_filepos:=tprocdef(def.genericdef).fileinfo;
  1423. { use the index the module got from the current compilation process }
  1424. current_filepos.moduleindex:=hmodule.unit_index;
  1425. current_tokenpos:=current_filepos;
  1426. current_scanner.startreplaytokens(tprocdef(def.genericdef).generictokenbuf);
  1427. read_proc_body(def);
  1428. current_filepos:=oldcurrent_filepos;
  1429. end
  1430. { synthetic routines will be implemented afterwards }
  1431. else if def.synthetickind=tsk_none then
  1432. MessagePos1(def.fileinfo,sym_e_forward_not_resolved,def.fullprocname(false));
  1433. end;
  1434. function process_abstractrecorddef(def:tabstractrecorddef):boolean;
  1435. var
  1436. i : longint;
  1437. hp : tdef;
  1438. hmodule : tmodule;
  1439. begin
  1440. result:=true;
  1441. hmodule:=find_module_from_symtable(def.genericdef.owner);
  1442. if hmodule=nil then
  1443. internalerror(201202041);
  1444. for i:=0 to def.symtable.DefList.Count-1 do
  1445. begin
  1446. hp:=tdef(def.symtable.DefList[i]);
  1447. if hp.typ=procdef then
  1448. begin
  1449. { only generate the code if we need a body }
  1450. if assigned(tprocdef(hp).struct) and not tprocdef(hp).forwarddef then
  1451. continue;
  1452. { and the body is available already (which is implicitely the
  1453. case if the generic routine is part of another unit) }
  1454. if ((hmodule=current_module) or (hmodule.state=ms_compile)) and tprocdef(tprocdef(hp).genericdef).forwarddef then
  1455. begin
  1456. result:=false;
  1457. continue;
  1458. end;
  1459. process_procdef(tprocdef(hp),hmodule);
  1460. end
  1461. else
  1462. if hp.typ in [objectdef,recorddef] then
  1463. { generate code for subtypes as well }
  1464. result:=process_abstractrecorddef(tabstractrecorddef(hp)) and result;
  1465. end;
  1466. end;
  1467. procedure generate_specialization_procs;
  1468. var
  1469. i : longint;
  1470. list,
  1471. readdlist : tfpobjectlist;
  1472. def : tstoreddef;
  1473. state : tspecializationstate;
  1474. hmodule : tmodule;
  1475. begin
  1476. { first copy all entries and then work with that list to ensure that
  1477. we don't get an infinite recursion }
  1478. list:=tfpobjectlist.create(false);
  1479. readdlist:=tfpobjectlist.create(false);
  1480. for i:=0 to current_module.pendingspecializations.Count-1 do
  1481. list.add(current_module.pendingspecializations.Items[i]);
  1482. current_module.pendingspecializations.clear;
  1483. for i:=0 to list.count-1 do
  1484. begin
  1485. def:=tstoreddef(list[i]);
  1486. if not tstoreddef(def).is_specialization then
  1487. continue;
  1488. case def.typ of
  1489. procdef:
  1490. begin
  1491. { the use of forwarddef should not backfire as the
  1492. specialization always belongs to the current module }
  1493. if not tprocdef(def).forwarddef then
  1494. continue;
  1495. if not assigned(def.genericdef) then
  1496. internalerror(2015061903);
  1497. hmodule:=find_module_from_symtable(def.genericdef.owner);
  1498. if hmodule=nil then
  1499. internalerror(2015061904);
  1500. { we need to check for a forward declaration only if the
  1501. generic was declared in the same unit (otherwise there
  1502. should be one) }
  1503. if ((hmodule=current_module) or (hmodule.state=ms_compile)) and tprocdef(def.genericdef).forwarddef then
  1504. begin
  1505. readdlist.add(def);
  1506. continue;
  1507. end;
  1508. specialization_init(tstoreddef(def).genericdef,state);
  1509. process_procdef(tprocdef(def),hmodule);
  1510. specialization_done(state);
  1511. end;
  1512. recorddef,
  1513. objectdef:
  1514. begin
  1515. specialization_init(tstoreddef(def).genericdef,state);
  1516. if not process_abstractrecorddef(tabstractrecorddef(def)) then
  1517. readdlist.add(def);
  1518. specialization_done(state);
  1519. end;
  1520. end;
  1521. end;
  1522. { add those defs back to the pending list for which we don't yet have
  1523. all method bodies }
  1524. for i:=0 to readdlist.count-1 do
  1525. current_module.pendingspecializations.add(tstoreddef(readdlist[i]).typename,readdlist[i]);
  1526. readdlist.free;
  1527. list.free;
  1528. end;
  1529. procedure maybe_add_pending_specialization(def:tdef);
  1530. var
  1531. hmodule : tmodule;
  1532. st : tsymtable;
  1533. begin
  1534. if parse_generic then
  1535. exit;
  1536. st:=def.owner;
  1537. while st.symtabletype in [localsymtable] do
  1538. st:=st.defowner.owner;
  1539. hmodule:=find_module_from_symtable(st);
  1540. if tstoreddef(def).is_specialization and (hmodule=current_module) then
  1541. current_module.pendingspecializations.add(def.typename,def);
  1542. end;
  1543. end.