pgenutil.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. { symtable }
  25. symtype,symdef;
  26. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;parsedtype:tdef);
  27. function parse_generic_parameters:TFPObjectList;
  28. procedure insert_generic_parameter_types(def:tstoreddef;genericdef:tstoreddef;genericlist:TFPObjectList);
  29. implementation
  30. uses
  31. { common }
  32. cutils,
  33. { global }
  34. globals,tokens,verbose,
  35. { symtable }
  36. symconst,symbase,symsym,symtable,
  37. { modules }
  38. fmodule,
  39. { pass 1 }
  40. node,nobj,
  41. { parser }
  42. scanner,
  43. pbase,pexpr,pdecsub,ptype;
  44. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;parsedtype:tdef);
  45. var
  46. st : TSymtable;
  47. srsym : tsym;
  48. pt2 : tnode;
  49. first,
  50. err : boolean;
  51. i,
  52. gencount : longint;
  53. genericdef : tstoreddef;
  54. genericsym,
  55. generictype : ttypesym;
  56. genericdeflist : TFPObjectList;
  57. generictypelist : TFPObjectList;
  58. oldsymtablestack : tsymtablestack;
  59. oldextendeddefs : TFPHashObjectList;
  60. hmodule : tmodule;
  61. pu : tused_unit;
  62. uspecializename,
  63. countstr,genname,ugenname,specializename : string;
  64. vmtbuilder : TVMTBuilder;
  65. onlyparsepara : boolean;
  66. specializest : tsymtable;
  67. item: psymtablestackitem;
  68. begin
  69. { retrieve generic def that we are going to replace }
  70. genericdef:=tstoreddef(tt);
  71. tt:=nil;
  72. onlyparsepara:=false;
  73. if not assigned(genericdef.typesym) or
  74. (genericdef.typesym.typ<>typesym) then
  75. internalerror(2011042701);
  76. genericsym:=ttypesym(genericdef.typesym);
  77. { only need to record the tokens, then we don't know the type yet ... }
  78. if parse_generic then
  79. begin
  80. { ... but we have to insert a def into the symtable else the deflist
  81. of generic and specialization might not be equally sized which
  82. is later assumed }
  83. tt:=tundefineddef.create;
  84. if parse_class_parent then
  85. tt:=genericdef;
  86. onlyparsepara:=true;
  87. end;
  88. { Only parse the parameters for recovery or
  89. for recording in genericbuf }
  90. if onlyparsepara then
  91. begin
  92. consume(_LSHARPBRACKET);
  93. gencount:=0;
  94. repeat
  95. pt2:=factor(false,true);
  96. pt2.free;
  97. inc(gencount);
  98. until not try_to_consume(_COMMA);
  99. consume(_RSHARPBRACKET);
  100. if parse_generic and parse_class_parent then
  101. begin
  102. if df_generic in genericdef.defoptions then
  103. { this happens in non-Delphi modes }
  104. tt:=genericdef
  105. else
  106. begin
  107. { find the corresponding generic symbol so that any checks
  108. done on the returned def will be handled correctly }
  109. str(gencount,countstr);
  110. genname:=ttypesym(genericdef.typesym).realname+'$'+countstr;
  111. ugenname:=upper(genname);
  112. if not searchsym(ugenname,srsym,st) or
  113. (srsym.typ<>typesym) then
  114. begin
  115. identifier_not_found(genname);
  116. exit;
  117. end;
  118. tt:=ttypesym(srsym).typedef;
  119. end;
  120. end;
  121. exit;
  122. end;
  123. if not assigned(parsedtype) and not try_to_consume(_LT) then
  124. consume(_LSHARPBRACKET);
  125. generictypelist:=TFPObjectList.create(false);
  126. genericdeflist:=TFPObjectList.Create(false);
  127. { Parse type parameters }
  128. if not assigned(genericdef.typesym) then
  129. internalerror(200710173);
  130. err:=false;
  131. { if parsedtype is set, then the first type identifer was already parsed
  132. (happens in inline specializations) and thus we only need to parse
  133. the remaining types and do as if the first one was already given }
  134. first:=not assigned(parsedtype);
  135. if assigned(parsedtype) then
  136. begin
  137. genericdeflist.Add(parsedtype);
  138. specializename:='$'+parsedtype.typesym.realname;
  139. end
  140. else
  141. specializename:='';
  142. while not (token in [_GT,_RSHARPBRACKET]) do
  143. begin
  144. if not first then
  145. consume(_COMMA)
  146. else
  147. first:=false;
  148. pt2:=factor(false,true);
  149. if pt2.nodetype=typen then
  150. begin
  151. if df_generic in pt2.resultdef.defoptions then
  152. Message(parser_e_no_generics_as_params);
  153. genericdeflist.Add(pt2.resultdef);
  154. if not assigned(pt2.resultdef.typesym) then
  155. message(type_e_generics_cannot_reference_itself)
  156. else
  157. specializename:=specializename+'$'+pt2.resultdef.typesym.realname;
  158. end
  159. else
  160. begin
  161. Message(type_e_type_id_expected);
  162. err:=true;
  163. end;
  164. pt2.free;
  165. end;
  166. if err then
  167. begin
  168. try_to_consume(_RSHARPBRACKET);
  169. exit;
  170. end;
  171. { search a generic with the given count of params }
  172. countstr:='';
  173. str(genericdeflist.Count,countstr);
  174. { use the name of the symbol as procvars return a user friendly version
  175. of the name }
  176. genname:=ttypesym(genericdef.typesym).realname;
  177. { in case of non-Delphi mode the type name could already be a generic
  178. def (but maybe the wrong one) }
  179. if df_generic in genericdef.defoptions then
  180. begin
  181. { remove the type count suffix from the generic's name }
  182. for i:=Length(genname) downto 1 do
  183. if genname[i]='$' then
  184. begin
  185. genname:=copy(genname,1,i-1);
  186. break;
  187. end;
  188. end;
  189. genname:=genname+'$'+countstr;
  190. ugenname:=upper(genname);
  191. if not searchsym(ugenname,srsym,st)
  192. or (srsym.typ<>typesym) then
  193. begin
  194. identifier_not_found(genname);
  195. genericdeflist.Free;
  196. generictypelist.Free;
  197. exit;
  198. end;
  199. { we've found the correct def }
  200. genericdef:=tstoreddef(ttypesym(srsym).typedef);
  201. { build the new type's name }
  202. specializename:=genname+specializename;
  203. uspecializename:=upper(specializename);
  204. { select the symtable containing the params }
  205. case genericdef.typ of
  206. procdef:
  207. st:=genericdef.GetSymtable(gs_para);
  208. objectdef,
  209. recorddef:
  210. st:=genericdef.GetSymtable(gs_record);
  211. arraydef:
  212. st:=tarraydef(genericdef).symtable;
  213. procvardef:
  214. st:=genericdef.GetSymtable(gs_para);
  215. else
  216. internalerror(200511182);
  217. end;
  218. { build the list containing the types for the generic params }
  219. gencount:=0;
  220. for i:=0 to st.SymList.Count-1 do
  221. begin
  222. srsym:=tsym(st.SymList[i]);
  223. if sp_generic_para in srsym.symoptions then
  224. begin
  225. if gencount=genericdeflist.Count then
  226. internalerror(2011042702);
  227. generictype:=ttypesym.create(srsym.realname,tdef(genericdeflist[gencount]));
  228. generictypelist.add(generictype);
  229. inc(gencount);
  230. end;
  231. end;
  232. { Special case if we are referencing the current defined object }
  233. if assigned(current_structdef) and
  234. (current_structdef.objname^=uspecializename) then
  235. tt:=current_structdef;
  236. { for units specializations can already be needed in the interface, therefor we
  237. will use the global symtable. Programs don't have a globalsymtable and there we
  238. use the localsymtable }
  239. if current_module.is_unit then
  240. specializest:=current_module.globalsymtable
  241. else
  242. specializest:=current_module.localsymtable;
  243. { Can we reuse an already specialized type? }
  244. if not assigned(tt) then
  245. begin
  246. srsym:=tsym(specializest.find(uspecializename));
  247. if assigned(srsym) then
  248. begin
  249. if srsym.typ<>typesym then
  250. internalerror(200710171);
  251. tt:=ttypesym(srsym).typedef;
  252. end;
  253. end;
  254. if not assigned(tt) then
  255. begin
  256. { Setup symtablestack at definition time
  257. to get types right, however this is not perfect, we should probably record
  258. the resolved symbols }
  259. oldsymtablestack:=symtablestack;
  260. oldextendeddefs:=current_module.extendeddefs;
  261. current_module.extendeddefs:=TFPHashObjectList.create(true);
  262. symtablestack:=tdefawaresymtablestack.create;
  263. if not assigned(genericdef) then
  264. internalerror(200705151);
  265. hmodule:=find_module_from_symtable(genericdef.owner);
  266. if hmodule=nil then
  267. internalerror(200705152);
  268. pu:=tused_unit(hmodule.used_units.first);
  269. while assigned(pu) do
  270. begin
  271. if not assigned(pu.u.globalsymtable) then
  272. internalerror(200705153);
  273. symtablestack.push(pu.u.globalsymtable);
  274. pu:=tused_unit(pu.next);
  275. end;
  276. if assigned(hmodule.globalsymtable) then
  277. symtablestack.push(hmodule.globalsymtable);
  278. { hacky, but necessary to insert the newly generated class properly }
  279. item:=oldsymtablestack.stack;
  280. while assigned(item) and (item^.symtable.symtablelevel>main_program_level) do
  281. item:=item^.next;
  282. if assigned(item) and (item^.symtable<>symtablestack.top) then
  283. symtablestack.push(item^.symtable);
  284. { Reparse the original type definition }
  285. if not err then
  286. begin
  287. { First a new typesym so we can reuse this specialization and
  288. references to this specialization can be handled }
  289. srsym:=ttypesym.create(specializename,generrordef);
  290. specializest.insert(srsym);
  291. if not assigned(genericdef.generictokenbuf) then
  292. internalerror(200511171);
  293. current_scanner.startreplaytokens(genericdef.generictokenbuf);
  294. read_named_type(tt,specializename,genericdef,generictypelist,false);
  295. ttypesym(srsym).typedef:=tt;
  296. tt.typesym:=srsym;
  297. case tt.typ of
  298. { Build VMT indexes for classes }
  299. objectdef:
  300. begin
  301. vmtbuilder:=TVMTBuilder.Create(tobjectdef(tt));
  302. vmtbuilder.generate_vmt;
  303. vmtbuilder.free;
  304. end;
  305. { handle params, calling convention, etc }
  306. procvardef:
  307. begin
  308. if not check_proc_directive(true) then
  309. begin
  310. try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg);
  311. consume(_SEMICOLON);
  312. end;
  313. parse_var_proc_directives(ttypesym(srsym));
  314. handle_calling_convention(tprocvardef(tt));
  315. if try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg) then
  316. consume(_SEMICOLON);
  317. end;
  318. end;
  319. { Consume the semicolon if it is also recorded }
  320. try_to_consume(_SEMICOLON);
  321. end;
  322. { Restore symtablestack }
  323. current_module.extendeddefs.free;
  324. current_module.extendeddefs:=oldextendeddefs;
  325. symtablestack.free;
  326. symtablestack:=oldsymtablestack;
  327. end
  328. else
  329. begin
  330. { There is comment few lines before ie 200512115
  331. saying "We are parsing the same objectdef, the def index numbers
  332. are the same". This is wrong (index numbers are not same)
  333. in case there is specialization (S2 in this case) inside
  334. specialized generic (G2 in this case) which is equal to
  335. some previous specialization (S1 in this case). In that case,
  336. new symbol is not added to currently specialized type
  337. (S in this case) for that specializations (S2 in this case),
  338. and this results in that specialization and generic definition
  339. don't have same number of elements in their object symbol tables.
  340. This patch adds undefined def to ensure that those
  341. two symbol tables will have same number of elements.
  342. }
  343. tundefineddef.create;
  344. end;
  345. genericdeflist.free;
  346. generictypelist.free;
  347. if not try_to_consume(_GT) then
  348. consume(_RSHARPBRACKET);
  349. end;
  350. function parse_generic_parameters:TFPObjectList;
  351. var
  352. generictype : ttypesym;
  353. begin
  354. result:=TFPObjectList.Create(false);
  355. repeat
  356. if token=_ID then
  357. begin
  358. generictype:=ttypesym.create(orgpattern,cundefinedtype);
  359. include(generictype.symoptions,sp_generic_para);
  360. result.add(generictype);
  361. end;
  362. consume(_ID);
  363. until not try_to_consume(_COMMA) ;
  364. end;
  365. procedure insert_generic_parameter_types(def:tstoreddef;genericdef:tstoreddef;genericlist:TFPObjectList);
  366. var
  367. i: longint;
  368. generictype: ttypesym;
  369. st: tsymtable;
  370. begin
  371. def.genericdef:=genericdef;
  372. if not assigned(genericlist) then
  373. exit;
  374. case def.typ of
  375. recorddef,objectdef: st:=tabstractrecorddef(def).symtable;
  376. arraydef: st:=tarraydef(def).symtable;
  377. procvardef,procdef: st:=tabstractprocdef(def).parast;
  378. else
  379. internalerror(201101020);
  380. end;
  381. for i:=0 to genericlist.count-1 do
  382. begin
  383. generictype:=ttypesym(genericlist[i]);
  384. if generictype.typedef.typ=undefineddef then
  385. include(def.defoptions,df_generic)
  386. else
  387. include(def.defoptions,df_specialization);
  388. st.insert(generictype);
  389. end;
  390. end;
  391. end.