ptype.pas 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Does parsing types for Free Pascal
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ptype;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,cclasses,
  22. symtype,symdef,symbase;
  23. type
  24. TSingleTypeOption=(
  25. stoIsForwardDef, { foward declaration }
  26. stoAllowTypeDef, { allow type definitions }
  27. stoAllowSpecialization, { allow type specialization }
  28. stoParseClassParent { parse of parent class type }
  29. );
  30. TSingleTypeOptions=set of TSingleTypeOption;
  31. procedure resolve_forward_types;
  32. { reads a string, file type or a type identifier }
  33. procedure single_type(var def:tdef;options:TSingleTypeOptions);
  34. { reads any type declaration, where the resulting type will get name as type identifier }
  35. procedure read_named_type(var def:tdef;const name : TIDString;genericdef:tstoreddef;genericlist:TFPObjectList;parseprocvardir:boolean);
  36. { reads any type declaration }
  37. procedure read_anon_type(var def : tdef;parseprocvardir:boolean);
  38. { generate persistent type information like VMT, RTTI and inittables }
  39. procedure write_persistent_type_info(st:tsymtable);
  40. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;_prettyname : string);
  41. implementation
  42. uses
  43. { common }
  44. cutils,
  45. { global }
  46. globals,tokens,verbose,constexp,
  47. systems,
  48. { target }
  49. paramgr,procinfo,
  50. { symtable }
  51. symconst,symsym,symtable,
  52. defutil,defcmp,
  53. { modules }
  54. fmodule,
  55. { pass 1 }
  56. node,ncgrtti,nobj,
  57. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,
  58. { parser }
  59. scanner,
  60. pbase,pexpr,pdecsub,pdecvar,pdecobj,pdecl;
  61. procedure resolve_forward_types;
  62. var
  63. i: longint;
  64. hpd,
  65. def : tdef;
  66. srsym : tsym;
  67. srsymtable : TSymtable;
  68. hs : string;
  69. begin
  70. for i:=0 to current_module.checkforwarddefs.Count-1 do
  71. begin
  72. def:=tdef(current_module.checkforwarddefs[i]);
  73. case def.typ of
  74. pointerdef,
  75. classrefdef :
  76. begin
  77. { classrefdef inherits from pointerdef }
  78. hpd:=tabstractpointerdef(def).pointeddef;
  79. { still a forward def ? }
  80. if hpd.typ=forwarddef then
  81. begin
  82. { try to resolve the forward }
  83. if not assigned(tforwarddef(hpd).tosymname) then
  84. internalerror(200211201);
  85. hs:=tforwarddef(hpd).tosymname^;
  86. searchsym(upper(hs),srsym,srsymtable);
  87. { we don't need the forwarddef anymore, dispose it }
  88. hpd.free;
  89. tabstractpointerdef(def).pointeddef:=nil; { if error occurs }
  90. { was a type sym found ? }
  91. if assigned(srsym) and
  92. (srsym.typ=typesym) then
  93. begin
  94. tabstractpointerdef(def).pointeddef:=ttypesym(srsym).typedef;
  95. { avoid wrong unused warnings web bug 801 PM }
  96. inc(ttypesym(srsym).refs);
  97. { we need a class type for classrefdef }
  98. if (def.typ=classrefdef) and
  99. not(is_class(ttypesym(srsym).typedef)) and
  100. not(is_objcclass(ttypesym(srsym).typedef)) then
  101. MessagePos1(def.typesym.fileinfo,type_e_class_type_expected,ttypesym(srsym).typedef.typename);
  102. end
  103. else
  104. begin
  105. Message1(sym_e_forward_type_not_resolved,hs);
  106. { try to recover }
  107. tabstractpointerdef(def).pointeddef:=generrordef;
  108. end;
  109. end;
  110. end;
  111. objectdef :
  112. begin
  113. { give an error as the implementation may follow in an
  114. other type block which is allowed by FPC modes }
  115. if not(m_fpc in current_settings.modeswitches) and
  116. (oo_is_forward in tobjectdef(def).objectoptions) then
  117. MessagePos1(def.typesym.fileinfo,type_e_type_is_not_completly_defined,def.typename);
  118. end;
  119. else
  120. internalerror(200811071);
  121. end;
  122. end;
  123. current_module.checkforwarddefs.clear;
  124. end;
  125. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;_prettyname : string);
  126. var
  127. st : TSymtable;
  128. srsym : tsym;
  129. pt2 : tnode;
  130. first,
  131. err : boolean;
  132. i : longint;
  133. sym : tsym;
  134. genericdef : tstoreddef;
  135. generictype : ttypesym;
  136. generictypelist : TFPObjectList;
  137. oldsymtablestack : tsymtablestack;
  138. oldextendeddefs : TFPHashObjectList;
  139. hmodule : tmodule;
  140. pu : tused_unit;
  141. prettyname : ansistring;
  142. uspecializename,
  143. specializename : string;
  144. vmtbuilder : TVMTBuilder;
  145. onlyparsepara : boolean;
  146. specializest : tsymtable;
  147. item: psymtablestackitem;
  148. begin
  149. { retrieve generic def that we are going to replace }
  150. genericdef:=tstoreddef(tt);
  151. tt:=nil;
  152. onlyparsepara:=false;
  153. if not(df_generic in genericdef.defoptions) then
  154. begin
  155. Message(parser_e_special_onlygenerics);
  156. tt:=generrordef;
  157. onlyparsepara:=true;
  158. end;
  159. { only need to record the tokens, then we don't know the type yet ... }
  160. if parse_generic then
  161. begin
  162. { ... but we have to insert a def into the symtable else the deflist
  163. of generic and specialization might not be equally sized which
  164. is later assumed }
  165. tt:=tundefineddef.create;
  166. if parse_class_parent then
  167. tt:=genericdef;
  168. onlyparsepara:=true;
  169. end;
  170. { Only parse the parameters for recovery or
  171. for recording in genericbuf }
  172. if onlyparsepara then
  173. begin
  174. consume(_LSHARPBRACKET);
  175. repeat
  176. pt2:=factor(false,true);
  177. pt2.free;
  178. until not try_to_consume(_COMMA);
  179. consume(_RSHARPBRACKET);
  180. exit;
  181. end;
  182. if not try_to_consume(_LT) then
  183. consume(_LSHARPBRACKET);
  184. { Parse generic parameters, for each undefineddef in the symtable of
  185. the genericdef we need to have a new def }
  186. err:=false;
  187. first:=true;
  188. generictypelist:=TFPObjectList.create(false);
  189. case genericdef.typ of
  190. procdef:
  191. st:=genericdef.GetSymtable(gs_para);
  192. objectdef,
  193. recorddef:
  194. st:=genericdef.GetSymtable(gs_record);
  195. arraydef:
  196. st:=tarraydef(genericdef).symtable;
  197. procvardef:
  198. st:=genericdef.GetSymtable(gs_para);
  199. else
  200. internalerror(200511182);
  201. end;
  202. { Parse type parameters }
  203. if not assigned(genericdef.typesym) then
  204. internalerror(200710173);
  205. specializename:=genericdef.typesym.realname;
  206. prettyname:=genericdef.typesym.prettyname+'<';
  207. for i:=0 to st.SymList.Count-1 do
  208. begin
  209. sym:=tsym(st.SymList[i]);
  210. if (sp_generic_para in sym.symoptions) then
  211. begin
  212. if not first then
  213. consume(_COMMA)
  214. else
  215. first:=false;
  216. pt2:=factor(false,true);
  217. if pt2.nodetype=typen then
  218. begin
  219. if df_generic in pt2.resultdef.defoptions then
  220. Message(parser_e_no_generics_as_params);
  221. generictype:=ttypesym.create(sym.realname,pt2.resultdef);
  222. generictypelist.add(generictype);
  223. if not assigned(pt2.resultdef.typesym) then
  224. message(type_e_generics_cannot_reference_itself)
  225. else
  226. begin
  227. specializename:=specializename+'$'+pt2.resultdef.typesym.realname;
  228. if i=0 then
  229. prettyname:=prettyname+pt2.resultdef.typesym.prettyname
  230. else
  231. prettyname:=prettyname+','+pt2.resultdef.typesym.prettyname;
  232. end;
  233. end
  234. else
  235. begin
  236. Message(type_e_type_id_expected);
  237. err:=true;
  238. end;
  239. pt2.free;
  240. end;
  241. end;
  242. prettyname:=prettyname+'>';
  243. uspecializename:=upper(specializename);
  244. { force correct error location if too much type parameters are passed }
  245. if not (token in [_RSHARPBRACKET,_GT]) then
  246. consume(_RSHARPBRACKET);
  247. { Special case if we are referencing the current defined object }
  248. if assigned(current_structdef) and
  249. (current_structdef.objname^=uspecializename) then
  250. tt:=current_structdef;
  251. { for units specializations can already be needed in the interface, therefor we
  252. will use the global symtable. Programs don't have a globalsymtable and there we
  253. use the localsymtable }
  254. if current_module.is_unit then
  255. specializest:=current_module.globalsymtable
  256. else
  257. specializest:=current_module.localsymtable;
  258. { Can we reuse an already specialized type? }
  259. if not assigned(tt) then
  260. begin
  261. srsym:=tsym(specializest.find(uspecializename));
  262. if assigned(srsym) then
  263. begin
  264. if srsym.typ<>typesym then
  265. internalerror(200710171);
  266. tt:=ttypesym(srsym).typedef;
  267. end;
  268. end;
  269. if not assigned(tt) then
  270. begin
  271. { Setup symtablestack at definition time
  272. to get types right, however this is not perfect, we should probably record
  273. the resolved symbols }
  274. oldsymtablestack:=symtablestack;
  275. oldextendeddefs:=current_module.extendeddefs;
  276. current_module.extendeddefs:=TFPHashObjectList.create(true);
  277. symtablestack:=tdefawaresymtablestack.create;
  278. if not assigned(genericdef) then
  279. internalerror(200705151);
  280. hmodule:=find_module_from_symtable(genericdef.owner);
  281. if hmodule=nil then
  282. internalerror(200705152);
  283. pu:=tused_unit(hmodule.used_units.first);
  284. while assigned(pu) do
  285. begin
  286. if not assigned(pu.u.globalsymtable) then
  287. internalerror(200705153);
  288. symtablestack.push(pu.u.globalsymtable);
  289. pu:=tused_unit(pu.next);
  290. end;
  291. if assigned(hmodule.globalsymtable) then
  292. symtablestack.push(hmodule.globalsymtable);
  293. { hacky, but necessary to insert the newly generated class properly }
  294. item:=oldsymtablestack.stack;
  295. while assigned(item) and (item^.symtable.symtablelevel>main_program_level) do
  296. item:=item^.next;
  297. if assigned(item) and (item^.symtable<>symtablestack.top) then
  298. symtablestack.push(item^.symtable);
  299. { Reparse the original type definition }
  300. if not err then
  301. begin
  302. { First a new typesym so we can reuse this specialization and
  303. references to this specialization can be handled }
  304. srsym:=ttypesym.create(specializename,generrordef);
  305. specializest.insert(srsym);
  306. if not assigned(genericdef.generictokenbuf) then
  307. internalerror(200511171);
  308. current_scanner.startreplaytokens(genericdef.generictokenbuf,
  309. genericdef.change_endian);
  310. read_named_type(tt,specializename,genericdef,generictypelist,false);
  311. ttypesym(srsym).typedef:=tt;
  312. tt.typesym:=srsym;
  313. if _prettyname<>'' then
  314. ttypesym(tt.typesym).fprettyname:=_prettyname
  315. else
  316. ttypesym(tt.typesym).fprettyname:=prettyname;
  317. case tt.typ of
  318. { Build VMT indexes for classes }
  319. objectdef:
  320. begin
  321. vmtbuilder:=TVMTBuilder.Create(tobjectdef(tt));
  322. vmtbuilder.generate_vmt;
  323. vmtbuilder.free;
  324. end;
  325. { handle params, calling convention, etc }
  326. procvardef:
  327. begin
  328. if not check_proc_directive(true) then
  329. begin
  330. try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg);
  331. consume(_SEMICOLON);
  332. end;
  333. parse_var_proc_directives(ttypesym(srsym));
  334. handle_calling_convention(tprocvardef(tt));
  335. if try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg) then
  336. consume(_SEMICOLON);
  337. end;
  338. end;
  339. { Consume the semicolon if it is also recorded }
  340. try_to_consume(_SEMICOLON);
  341. end;
  342. { Restore symtablestack }
  343. current_module.extendeddefs.free;
  344. current_module.extendeddefs:=oldextendeddefs;
  345. symtablestack.free;
  346. symtablestack:=oldsymtablestack;
  347. end
  348. else
  349. begin
  350. { There is comment few lines before ie 200512115
  351. saying "We are parsing the same objectdef, the def index numbers
  352. are the same". This is wrong (index numbers are not same)
  353. in case there is specialization (S2 in this case) inside
  354. specialized generic (G2 in this case) which is equal to
  355. some previous specialization (S1 in this case). In that case,
  356. new symbol is not added to currently specialized type
  357. (S in this case) for that specializations (S2 in this case),
  358. and this results in that specialization and generic definition
  359. don't have same number of elements in their object symbol tables.
  360. This patch adds undefined def to ensure that those
  361. two symbol tables will have same number of elements.
  362. }
  363. tundefineddef.create;
  364. end;
  365. generictypelist.free;
  366. if not try_to_consume(_GT) then
  367. consume(_RSHARPBRACKET);
  368. end;
  369. procedure id_type(var def : tdef;isforwarddef,checkcurrentrecdef:boolean); forward;
  370. { def is the outermost type in which other types have to be searched
  371. isforward indicates whether the current definition can be a forward definition
  372. if assigned, currentstructstack is a list of tabstractrecorddefs that, from
  373. last to first, are child types of def that are not yet visible via the
  374. normal symtable searching routines because they are types that are currently
  375. being parsed (so using id_type on them after pushing def on the
  376. symtablestack would result in errors because they'd come back as errordef)
  377. }
  378. procedure parse_nested_types(var def: tdef; isforwarddef: boolean; currentstructstack: tfpobjectlist);
  379. var
  380. t2: tdef;
  381. structstackindex: longint;
  382. begin
  383. if assigned(currentstructstack) then
  384. structstackindex:=currentstructstack.count-1
  385. else
  386. structstackindex:=-1;
  387. { handle types inside classes, e.g. TNode.TLongint }
  388. while (token=_POINT) do
  389. begin
  390. if parse_generic then
  391. begin
  392. consume(_POINT);
  393. consume(_ID);
  394. end
  395. else if is_class_or_object(def) or is_record(def) then
  396. begin
  397. consume(_POINT);
  398. if (structstackindex>=0) and
  399. (tabstractrecorddef(currentstructstack[structstackindex]).objname^=pattern) then
  400. begin
  401. def:=tdef(currentstructstack[structstackindex]);
  402. dec(structstackindex);
  403. consume(_ID);
  404. end
  405. else
  406. begin
  407. structstackindex:=-1;
  408. symtablestack.push(tabstractrecorddef(def).symtable);
  409. t2:=generrordef;
  410. id_type(t2,isforwarddef,false);
  411. symtablestack.pop(tabstractrecorddef(def).symtable);
  412. def:=t2;
  413. end;
  414. end
  415. else
  416. break;
  417. end;
  418. end;
  419. function try_parse_structdef_nested_type(out def: tdef; basedef: tabstractrecorddef; isfowarddef: boolean): boolean;
  420. var
  421. structdef : tdef;
  422. structdefstack : tfpobjectlist;
  423. begin
  424. { use of current parsed object:
  425. classes, objects, records can be used also in themself }
  426. structdef:=basedef;
  427. structdefstack:=nil;
  428. while assigned(structdef) and (structdef.typ in [objectdef,recorddef]) do
  429. begin
  430. if (tabstractrecorddef(structdef).objname^=pattern) then
  431. begin
  432. consume(_ID);
  433. def:=structdef;
  434. { we found the top-most match, now check how far down we can
  435. follow }
  436. structdefstack:=tfpobjectlist.create(false);
  437. structdef:=basedef;
  438. while (structdef<>def) do
  439. begin
  440. structdefstack.add(structdef);
  441. structdef:=tabstractrecorddef(structdef.owner.defowner);
  442. end;
  443. parse_nested_types(def,isfowarddef,structdefstack);
  444. structdefstack.free;
  445. result:=true;
  446. exit;
  447. end;
  448. structdef:=tdef(tabstractrecorddef(structdef).owner.defowner);
  449. end;
  450. result:=false;
  451. end;
  452. procedure id_type(var def : tdef;isforwarddef,checkcurrentrecdef:boolean);
  453. { reads a type definition }
  454. { to a appropriating tdef, s gets the name of }
  455. { the type to allow name mangling }
  456. var
  457. is_unit_specific : boolean;
  458. pos : tfileposinfo;
  459. srsym : tsym;
  460. srsymtable : TSymtable;
  461. s,sorg : TIDString;
  462. t : ttoken;
  463. begin
  464. s:=pattern;
  465. sorg:=orgpattern;
  466. pos:=current_tokenpos;
  467. { use of current parsed object:
  468. classes, objects, records can be used also in themself }
  469. if checkcurrentrecdef and
  470. try_parse_structdef_nested_type(def,current_structdef,isforwarddef) then
  471. exit;
  472. { Use the special searchsym_type that search only types }
  473. searchsym_type(s,srsym,srsymtable);
  474. { handle unit specification like System.Writeln }
  475. is_unit_specific:=try_consume_unitsym(srsym,srsymtable,t,true);
  476. consume(t);
  477. { Types are first defined with an error def before assigning
  478. the real type so check if it's an errordef. if so then
  479. give an error. Only check for typesyms in the current symbol
  480. table as forwarddef are not resolved directly }
  481. if assigned(srsym) and
  482. (srsym.typ=typesym) and
  483. (ttypesym(srsym).typedef.typ=errordef) then
  484. begin
  485. Message1(type_e_type_is_not_completly_defined,ttypesym(srsym).realname);
  486. def:=generrordef;
  487. exit;
  488. end;
  489. { are we parsing a possible forward def ? }
  490. if isforwarddef and
  491. not(is_unit_specific) then
  492. begin
  493. def:=tforwarddef.create(sorg,pos);
  494. exit;
  495. end;
  496. { unknown sym ? }
  497. if not assigned(srsym) then
  498. begin
  499. Message1(sym_e_id_not_found,sorg);
  500. def:=generrordef;
  501. exit;
  502. end;
  503. { type sym ? }
  504. if (srsym.typ<>typesym) then
  505. begin
  506. Message(type_e_type_id_expected);
  507. def:=generrordef;
  508. exit;
  509. end;
  510. { Give an error when referring to an errordef }
  511. if (ttypesym(srsym).typedef.typ=errordef) then
  512. begin
  513. Message(sym_e_error_in_type_def);
  514. def:=generrordef;
  515. exit;
  516. end;
  517. def:=ttypesym(srsym).typedef;
  518. end;
  519. procedure single_type(var def:tdef;options:TSingleTypeOptions);
  520. var
  521. t2 : tdef;
  522. dospecialize,
  523. again : boolean;
  524. begin
  525. dospecialize:=false;
  526. repeat
  527. again:=false;
  528. case token of
  529. _STRING:
  530. string_dec(def,stoAllowTypeDef in options);
  531. _FILE:
  532. begin
  533. consume(_FILE);
  534. if (token=_OF) then
  535. begin
  536. if not(stoAllowTypeDef in options) then
  537. Message(parser_e_no_local_para_def);
  538. consume(_OF);
  539. single_type(t2,[]);
  540. if is_managed_type(t2) then
  541. Message(parser_e_no_refcounted_typed_file);
  542. def:=tfiledef.createtyped(t2);
  543. end
  544. else
  545. def:=cfiletype;
  546. end;
  547. _ID:
  548. begin
  549. if try_to_consume(_SPECIALIZE) then
  550. begin
  551. if ([stoAllowSpecialization,stoAllowTypeDef] * options = []) then
  552. begin
  553. Message(parser_e_no_local_para_def);
  554. { try to recover }
  555. while token<>_SEMICOLON do
  556. consume(token);
  557. def:=generrordef;
  558. end
  559. else
  560. begin
  561. dospecialize:=true;
  562. again:=true;
  563. end;
  564. end
  565. else
  566. begin
  567. id_type(def,stoIsForwardDef in options,true);
  568. parse_nested_types(def,stoIsForwardDef in options,nil);
  569. end;
  570. end;
  571. else
  572. begin
  573. message(type_e_type_id_expected);
  574. def:=generrordef;
  575. end;
  576. end;
  577. until not again;
  578. if ([stoAllowSpecialization,stoAllowTypeDef] * options <> []) and
  579. (m_delphi in current_settings.modeswitches) then
  580. dospecialize:=token=_LSHARPBRACKET;
  581. if dospecialize then
  582. generate_specialization(def,stoParseClassParent in options,'')
  583. else
  584. begin
  585. if assigned(current_specializedef) and (def=current_specializedef.genericdef) then
  586. begin
  587. def:=current_specializedef
  588. end
  589. else if (def=current_genericdef) then
  590. begin
  591. def:=current_genericdef
  592. end
  593. else if (df_generic in def.defoptions) then
  594. begin
  595. Message(parser_e_no_generics_as_types);
  596. def:=generrordef;
  597. end
  598. else if is_classhelper(def) and
  599. not (stoParseClassParent in options) then
  600. begin
  601. Message(parser_e_no_category_as_types);
  602. def:=generrordef
  603. end
  604. end;
  605. end;
  606. procedure parse_record_members;
  607. procedure maybe_parse_hint_directives(pd:tprocdef);
  608. var
  609. dummysymoptions : tsymoptions;
  610. deprecatedmsg : pshortstring;
  611. begin
  612. dummysymoptions:=[];
  613. deprecatedmsg:=nil;
  614. while try_consume_hintdirective(dummysymoptions,deprecatedmsg) do
  615. Consume(_SEMICOLON);
  616. if assigned(pd) then
  617. begin
  618. pd.symoptions:=pd.symoptions+dummysymoptions;
  619. pd.deprecatedmsg:=deprecatedmsg;
  620. end
  621. else
  622. stringdispose(deprecatedmsg);
  623. end;
  624. var
  625. pd : tprocdef;
  626. oldparse_only: boolean;
  627. member_blocktype : tblock_type;
  628. fields_allowed, is_classdef, classfields: boolean;
  629. vdoptions: tvar_dec_options;
  630. begin
  631. { empty record declaration ? }
  632. if (token=_SEMICOLON) then
  633. Exit;
  634. current_structdef.symtable.currentvisibility:=vis_public;
  635. fields_allowed:=true;
  636. is_classdef:=false;
  637. classfields:=false;
  638. member_blocktype:=bt_general;
  639. repeat
  640. case token of
  641. _TYPE :
  642. begin
  643. consume(_TYPE);
  644. member_blocktype:=bt_type;
  645. { local and anonymous records can not have inner types. skip top record symtable }
  646. if (current_structdef.objname^='') or
  647. not(symtablestack.stack^.next^.symtable.symtabletype in [globalsymtable,staticsymtable,objectsymtable,recordsymtable]) then
  648. Message(parser_e_no_types_in_local_anonymous_records);
  649. end;
  650. _VAR :
  651. begin
  652. consume(_VAR);
  653. fields_allowed:=true;
  654. member_blocktype:=bt_general;
  655. classfields:=is_classdef;
  656. is_classdef:=false;
  657. end;
  658. _CONST:
  659. begin
  660. consume(_CONST);
  661. member_blocktype:=bt_const;
  662. end;
  663. _ID, _CASE, _OPERATOR :
  664. begin
  665. case idtoken of
  666. _PRIVATE :
  667. begin
  668. consume(_PRIVATE);
  669. current_structdef.symtable.currentvisibility:=vis_private;
  670. include(current_structdef.objectoptions,oo_has_private);
  671. fields_allowed:=true;
  672. is_classdef:=false;
  673. classfields:=false;
  674. member_blocktype:=bt_general;
  675. end;
  676. _PROTECTED :
  677. begin
  678. consume(_PROTECTED);
  679. current_structdef.symtable.currentvisibility:=vis_protected;
  680. include(current_structdef.objectoptions,oo_has_protected);
  681. fields_allowed:=true;
  682. is_classdef:=false;
  683. classfields:=false;
  684. member_blocktype:=bt_general;
  685. end;
  686. _PUBLIC :
  687. begin
  688. consume(_PUBLIC);
  689. current_structdef.symtable.currentvisibility:=vis_public;
  690. fields_allowed:=true;
  691. is_classdef:=false;
  692. classfields:=false;
  693. member_blocktype:=bt_general;
  694. end;
  695. _PUBLISHED :
  696. begin
  697. Message(parser_e_no_record_published);
  698. consume(_PUBLISHED);
  699. current_structdef.symtable.currentvisibility:=vis_published;
  700. fields_allowed:=true;
  701. is_classdef:=false;
  702. classfields:=false;
  703. member_blocktype:=bt_general;
  704. end;
  705. _STRICT :
  706. begin
  707. consume(_STRICT);
  708. if token=_ID then
  709. begin
  710. case idtoken of
  711. _PRIVATE:
  712. begin
  713. consume(_PRIVATE);
  714. current_structdef.symtable.currentvisibility:=vis_strictprivate;
  715. include(current_structdef.objectoptions,oo_has_strictprivate);
  716. end;
  717. _PROTECTED:
  718. begin
  719. consume(_PROTECTED);
  720. current_structdef.symtable.currentvisibility:=vis_strictprotected;
  721. include(current_structdef.objectoptions,oo_has_strictprotected);
  722. end;
  723. else
  724. message(parser_e_protected_or_private_expected);
  725. end;
  726. end
  727. else
  728. message(parser_e_protected_or_private_expected);
  729. fields_allowed:=true;
  730. is_classdef:=false;
  731. classfields:=false;
  732. member_blocktype:=bt_general;
  733. end
  734. else
  735. if is_classdef and (idtoken=_OPERATOR) then
  736. begin
  737. oldparse_only:=parse_only;
  738. parse_only:=true;
  739. pd:=parse_proc_dec(is_classdef,current_structdef);
  740. { this is for error recovery as well as forward }
  741. { interface mappings, i.e. mapping to a method }
  742. { which isn't declared yet }
  743. if assigned(pd) then
  744. begin
  745. parse_record_proc_directives(pd);
  746. handle_calling_convention(pd);
  747. { add definition to procsym }
  748. proc_add_definition(pd);
  749. end;
  750. maybe_parse_hint_directives(pd);
  751. parse_only:=oldparse_only;
  752. fields_allowed:=false;
  753. is_classdef:=false;
  754. end
  755. else
  756. begin
  757. if member_blocktype=bt_general then
  758. begin
  759. if (not fields_allowed) then
  760. Message(parser_e_field_not_allowed_here);
  761. vdoptions:=[vd_record];
  762. if classfields then
  763. include(vdoptions,vd_class);
  764. read_record_fields(vdoptions);
  765. end
  766. else if member_blocktype=bt_type then
  767. types_dec(true)
  768. else if member_blocktype=bt_const then
  769. consts_dec(true)
  770. else
  771. internalerror(201001110);
  772. end;
  773. end;
  774. end;
  775. _PROPERTY :
  776. begin
  777. struct_property_dec(is_classdef);
  778. fields_allowed:=false;
  779. is_classdef:=false;
  780. end;
  781. _CLASS:
  782. begin
  783. is_classdef:=false;
  784. { read class method/field/property }
  785. consume(_CLASS);
  786. { class modifier is only allowed for procedures, functions, }
  787. { constructors, destructors, fields and properties }
  788. if not(token in [_FUNCTION,_PROCEDURE,_PROPERTY,_VAR,_CONSTRUCTOR,_DESTRUCTOR,_OPERATOR]) and
  789. not((token=_ID) and (idtoken=_OPERATOR)) then
  790. Message(parser_e_procedure_or_function_expected);
  791. is_classdef:=true;
  792. end;
  793. _PROCEDURE,
  794. _FUNCTION:
  795. begin
  796. oldparse_only:=parse_only;
  797. parse_only:=true;
  798. pd:=parse_proc_dec(is_classdef,current_structdef);
  799. { this is for error recovery as well as forward }
  800. { interface mappings, i.e. mapping to a method }
  801. { which isn't declared yet }
  802. if assigned(pd) then
  803. begin
  804. parse_record_proc_directives(pd);
  805. { since records have no inheritance don't allow non static
  806. class methods. delphi do so. }
  807. if is_classdef and not (po_staticmethod in pd.procoptions) then
  808. MessagePos(pd.fileinfo, parser_e_class_methods_only_static_in_records);
  809. handle_calling_convention(pd);
  810. { add definition to procsym }
  811. proc_add_definition(pd);
  812. end;
  813. maybe_parse_hint_directives(pd);
  814. parse_only:=oldparse_only;
  815. fields_allowed:=false;
  816. is_classdef:=false;
  817. end;
  818. _CONSTRUCTOR :
  819. begin
  820. if not is_classdef then
  821. Message(parser_e_no_constructor_in_records);
  822. if not is_classdef and (current_structdef.symtable.currentvisibility <> vis_public) then
  823. Message(parser_w_constructor_should_be_public);
  824. { only 1 class constructor is allowed }
  825. if is_classdef and (oo_has_class_constructor in current_structdef.objectoptions) then
  826. Message1(parser_e_only_one_class_constructor_allowed, current_structdef.objrealname^);
  827. oldparse_only:=parse_only;
  828. parse_only:=true;
  829. if is_classdef then
  830. pd:=class_constructor_head
  831. else
  832. pd:=constructor_head;
  833. parse_record_proc_directives(pd);
  834. handle_calling_convention(pd);
  835. { add definition to procsym }
  836. proc_add_definition(pd);
  837. maybe_parse_hint_directives(pd);
  838. parse_only:=oldparse_only;
  839. fields_allowed:=false;
  840. is_classdef:=false;
  841. end;
  842. _DESTRUCTOR :
  843. begin
  844. if not is_classdef then
  845. Message(parser_e_no_destructor_in_records);
  846. { only 1 class destructor is allowed }
  847. if is_classdef and (oo_has_class_destructor in current_structdef.objectoptions) then
  848. Message1(parser_e_only_one_class_destructor_allowed, current_structdef.objrealname^);
  849. oldparse_only:=parse_only;
  850. parse_only:=true;
  851. if is_classdef then
  852. pd:=class_destructor_head
  853. else
  854. pd:=destructor_head;
  855. parse_record_proc_directives(pd);
  856. handle_calling_convention(pd);
  857. { add definition to procsym }
  858. proc_add_definition(pd);
  859. maybe_parse_hint_directives(pd);
  860. parse_only:=oldparse_only;
  861. fields_allowed:=false;
  862. is_classdef:=false;
  863. end;
  864. _END :
  865. begin
  866. consume(_END);
  867. break;
  868. end;
  869. else
  870. consume(_ID); { Give a ident expected message, like tp7 }
  871. end;
  872. until false;
  873. end;
  874. { reads a record declaration }
  875. function record_dec(const n:tidstring;genericdef:tstoreddef;genericlist:TFPObjectList):tdef;
  876. var
  877. old_current_structdef: tabstractrecorddef;
  878. old_current_genericdef,
  879. old_current_specializedef: tstoreddef;
  880. old_parse_generic: boolean;
  881. recst: trecordsymtable;
  882. begin
  883. old_current_structdef:=current_structdef;
  884. old_current_genericdef:=current_genericdef;
  885. old_current_specializedef:=current_specializedef;
  886. old_parse_generic:=parse_generic;
  887. current_genericdef:=nil;
  888. current_specializedef:=nil;
  889. { create recdef }
  890. recst:=trecordsymtable.create(n,current_settings.packrecords);
  891. current_structdef:=trecorddef.create(n,recst);
  892. result:=current_structdef;
  893. { insert in symtablestack }
  894. symtablestack.push(recst);
  895. { usage of specialized type inside its generic template }
  896. if assigned(genericdef) then
  897. current_specializedef:=current_structdef
  898. { reject declaration of generic class inside generic class }
  899. else if assigned(genericlist) then
  900. current_genericdef:=current_structdef;
  901. insert_generic_parameter_types(current_structdef,genericdef,genericlist);
  902. parse_generic:=(df_generic in current_structdef.defoptions);
  903. if m_advanced_records in current_settings.modeswitches then
  904. parse_record_members
  905. else
  906. begin
  907. read_record_fields([vd_record]);
  908. consume(_END);
  909. end;
  910. { make the record size aligned }
  911. recst.addalignmentpadding;
  912. { restore symtable stack }
  913. symtablestack.pop(recst);
  914. if trecorddef(current_structdef).is_packed and is_managed_type(current_structdef) then
  915. Message(type_e_no_packed_inittable);
  916. { restore old state }
  917. parse_generic:=old_parse_generic;
  918. current_structdef:=old_current_structdef;
  919. current_genericdef:=old_current_genericdef;
  920. current_specializedef:=old_current_specializedef;
  921. end;
  922. { reads a type definition and returns a pointer to it }
  923. procedure read_named_type(var def : tdef;const name : TIDString;genericdef:tstoreddef;genericlist:TFPObjectList;parseprocvardir:boolean);
  924. var
  925. pt : tnode;
  926. tt2 : tdef;
  927. aktenumdef : tenumdef;
  928. s : TIDString;
  929. l,v : TConstExprInt;
  930. oldpackrecords : longint;
  931. defpos,storepos : tfileposinfo;
  932. procedure expr_type;
  933. var
  934. pt1,pt2 : tnode;
  935. lv,hv : TConstExprInt;
  936. old_block_type : tblock_type;
  937. dospecialize : boolean;
  938. begin
  939. old_block_type:=block_type;
  940. dospecialize:=false;
  941. { use of current parsed object:
  942. classes, objects, records can be used also in themself }
  943. if (token=_ID) then
  944. if try_parse_structdef_nested_type(def,current_structdef,false) then
  945. exit;
  946. { Generate a specialization in FPC mode? }
  947. dospecialize:=not(m_delphi in current_settings.modeswitches) and try_to_consume(_SPECIALIZE);
  948. { we can't accept a equal in type }
  949. pt1:=comp_expr(false,true);
  950. if not dospecialize and
  951. try_to_consume(_POINTPOINT) then
  952. begin
  953. { get high value of range }
  954. pt2:=comp_expr(false,false);
  955. { make both the same type or give an error. This is not
  956. done when both are integer values, because typecasting
  957. between -3200..3200 will result in a signed-unsigned
  958. conflict and give a range check error (PFV) }
  959. if not(is_integer(pt1.resultdef) and is_integer(pt2.resultdef)) then
  960. inserttypeconv(pt1,pt2.resultdef);
  961. { both must be evaluated to constants now }
  962. if (pt1.nodetype=ordconstn) and
  963. (pt2.nodetype=ordconstn) then
  964. begin
  965. lv:=tordconstnode(pt1).value;
  966. hv:=tordconstnode(pt2).value;
  967. { Check bounds }
  968. if hv<lv then
  969. message(parser_e_upper_lower_than_lower)
  970. else if (lv.signed and (lv.svalue<0)) and (not hv.signed and (hv.uvalue>qword(high(int64)))) then
  971. message(type_e_cant_eval_constant_expr)
  972. else
  973. begin
  974. { All checks passed, create the new def }
  975. case pt1.resultdef.typ of
  976. enumdef :
  977. def:=tenumdef.create_subrange(tenumdef(pt1.resultdef),lv.svalue,hv.svalue);
  978. orddef :
  979. begin
  980. if is_char(pt1.resultdef) then
  981. def:=torddef.create(uchar,lv,hv)
  982. else
  983. if is_boolean(pt1.resultdef) then
  984. def:=torddef.create(pasbool8,lv,hv)
  985. else if is_signed(pt1.resultdef) then
  986. def:=torddef.create(range_to_basetype(lv,hv),lv,hv)
  987. else
  988. def:=torddef.create(range_to_basetype(lv,hv),lv,hv);
  989. end;
  990. end;
  991. end;
  992. end
  993. else
  994. Message(sym_e_error_in_type_def);
  995. pt2.free;
  996. end
  997. else
  998. begin
  999. { a simple type renaming or generic specialization }
  1000. if (pt1.nodetype=typen) then
  1001. begin
  1002. def:=ttypenode(pt1).resultdef;
  1003. { Delphi mode specialization? }
  1004. if (m_delphi in current_settings.modeswitches) then
  1005. dospecialize:=token=_LSHARPBRACKET;
  1006. if dospecialize then
  1007. generate_specialization(def,false,name)
  1008. else
  1009. begin
  1010. if assigned(current_specializedef) and (def=current_specializedef.genericdef) then
  1011. begin
  1012. def:=current_specializedef
  1013. end
  1014. else if (def=current_genericdef) then
  1015. begin
  1016. def:=current_genericdef
  1017. end
  1018. else if (df_generic in def.defoptions) then
  1019. begin
  1020. Message(parser_e_no_generics_as_types);
  1021. def:=generrordef;
  1022. end
  1023. else if is_classhelper(def) then
  1024. begin
  1025. Message(parser_e_no_category_as_types);
  1026. def:=generrordef
  1027. end
  1028. end;
  1029. end
  1030. else
  1031. Message(sym_e_error_in_type_def);
  1032. end;
  1033. pt1.free;
  1034. block_type:=old_block_type;
  1035. end;
  1036. procedure set_dec;
  1037. begin
  1038. consume(_SET);
  1039. consume(_OF);
  1040. read_anon_type(tt2,true);
  1041. if assigned(tt2) then
  1042. begin
  1043. case tt2.typ of
  1044. { don't forget that min can be negativ PM }
  1045. enumdef :
  1046. if (tenumdef(tt2).min>=0) and
  1047. (tenumdef(tt2).max<=255) then
  1048. // !! def:=tsetdef.create(tt2,tenumdef(tt2.def).min,tenumdef(tt2.def).max))
  1049. def:=tsetdef.create(tt2,tenumdef(tt2).min,tenumdef(tt2).max)
  1050. else
  1051. Message(sym_e_ill_type_decl_set);
  1052. orddef :
  1053. begin
  1054. if (torddef(tt2).ordtype<>uvoid) and
  1055. (torddef(tt2).ordtype<>uwidechar) and
  1056. (torddef(tt2).low>=0) then
  1057. // !! def:=tsetdef.create(tt2,torddef(tt2.def).low,torddef(tt2.def).high))
  1058. if Torddef(tt2).high>int64(high(byte)) then
  1059. message(sym_e_ill_type_decl_set)
  1060. else
  1061. def:=tsetdef.create(tt2,torddef(tt2).low.svalue,torddef(tt2).high.svalue)
  1062. else
  1063. Message(sym_e_ill_type_decl_set);
  1064. end;
  1065. else
  1066. Message(sym_e_ill_type_decl_set);
  1067. end;
  1068. end
  1069. else
  1070. def:=generrordef;
  1071. end;
  1072. procedure array_dec(is_packed:boolean;genericdef:tstoreddef;genericlist:TFPObjectList);
  1073. var
  1074. lowval,
  1075. highval : TConstExprInt;
  1076. indexdef : tdef;
  1077. hdef : tdef;
  1078. arrdef : tarraydef;
  1079. procedure setdefdecl(def:tdef);
  1080. begin
  1081. case def.typ of
  1082. enumdef :
  1083. begin
  1084. lowval:=tenumdef(def).min;
  1085. highval:=tenumdef(def).max;
  1086. if (m_fpc in current_settings.modeswitches) and
  1087. (tenumdef(def).has_jumps) then
  1088. Message(type_e_array_index_enums_with_assign_not_possible);
  1089. indexdef:=def;
  1090. end;
  1091. orddef :
  1092. begin
  1093. if torddef(def).ordtype in [uchar,
  1094. u8bit,u16bit,
  1095. s8bit,s16bit,s32bit,
  1096. {$ifdef cpu64bitaddr}
  1097. u32bit,s64bit,
  1098. {$endif cpu64bitaddr}
  1099. pasbool8,pasbool16,pasbool32,pasbool64,
  1100. bool8bit,bool16bit,bool32bit,bool64bit,
  1101. uwidechar] then
  1102. begin
  1103. lowval:=torddef(def).low;
  1104. highval:=torddef(def).high;
  1105. indexdef:=def;
  1106. end
  1107. else
  1108. Message1(parser_e_type_cant_be_used_in_array_index,def.typename);
  1109. end;
  1110. else
  1111. Message(sym_e_error_in_type_def);
  1112. end;
  1113. end;
  1114. var
  1115. old_current_genericdef,
  1116. old_current_specializedef: tstoreddef;
  1117. old_parse_generic: boolean;
  1118. begin
  1119. old_current_genericdef:=current_genericdef;
  1120. old_current_specializedef:=current_specializedef;
  1121. old_parse_generic:=parse_generic;
  1122. current_genericdef:=nil;
  1123. current_specializedef:=nil;
  1124. arrdef:=nil;
  1125. consume(_ARRAY);
  1126. { open array? }
  1127. if try_to_consume(_LECKKLAMMER) then
  1128. begin
  1129. { defaults }
  1130. indexdef:=generrordef;
  1131. { use defaults which don't overflow the compiler }
  1132. lowval:=0;
  1133. highval:=0;
  1134. repeat
  1135. { read the expression and check it, check apart if the
  1136. declaration is an enum declaration because that needs to
  1137. be parsed by readtype (PFV) }
  1138. if token=_LKLAMMER then
  1139. begin
  1140. read_anon_type(hdef,true);
  1141. setdefdecl(hdef);
  1142. end
  1143. else
  1144. begin
  1145. pt:=expr(true);
  1146. if pt.nodetype=typen then
  1147. setdefdecl(pt.resultdef)
  1148. else
  1149. begin
  1150. if (pt.nodetype=rangen) then
  1151. begin
  1152. if (trangenode(pt).left.nodetype=ordconstn) and
  1153. (trangenode(pt).right.nodetype=ordconstn) then
  1154. begin
  1155. { make both the same type or give an error. This is not
  1156. done when both are integer values, because typecasting
  1157. between -3200..3200 will result in a signed-unsigned
  1158. conflict and give a range check error (PFV) }
  1159. if not(is_integer(trangenode(pt).left.resultdef) and is_integer(trangenode(pt).left.resultdef)) then
  1160. inserttypeconv(trangenode(pt).left,trangenode(pt).right.resultdef);
  1161. lowval:=tordconstnode(trangenode(pt).left).value;
  1162. highval:=tordconstnode(trangenode(pt).right).value;
  1163. if highval<lowval then
  1164. begin
  1165. Message(parser_e_array_lower_less_than_upper_bound);
  1166. highval:=lowval;
  1167. end
  1168. else if (lowval<int64(low(asizeint))) or
  1169. (highval>high(asizeint)) then
  1170. begin
  1171. Message(parser_e_array_range_out_of_bounds);
  1172. lowval :=0;
  1173. highval:=0;
  1174. end;
  1175. if is_integer(trangenode(pt).left.resultdef) then
  1176. range_to_type(lowval,highval,indexdef)
  1177. else
  1178. indexdef:=trangenode(pt).left.resultdef;
  1179. end
  1180. else
  1181. Message(type_e_cant_eval_constant_expr);
  1182. end
  1183. else
  1184. Message(sym_e_error_in_type_def)
  1185. end;
  1186. pt.free;
  1187. end;
  1188. { if the array is already created add the new arrray
  1189. as element of the existing array, otherwise create a new array }
  1190. if assigned(arrdef) then
  1191. begin
  1192. arrdef.elementdef:=tarraydef.create(lowval.svalue,highval.svalue,indexdef);
  1193. arrdef:=tarraydef(arrdef.elementdef);
  1194. end
  1195. else
  1196. begin
  1197. arrdef:=tarraydef.create(lowval.svalue,highval.svalue,indexdef);
  1198. def:=arrdef;
  1199. end;
  1200. if is_packed then
  1201. include(arrdef.arrayoptions,ado_IsBitPacked);
  1202. if token=_COMMA then
  1203. consume(_COMMA)
  1204. else
  1205. break;
  1206. until false;
  1207. consume(_RECKKLAMMER);
  1208. end
  1209. else
  1210. begin
  1211. if is_packed then
  1212. Message(parser_e_packed_dynamic_open_array);
  1213. arrdef:=tarraydef.create(0,-1,s32inttype);
  1214. include(arrdef.arrayoptions,ado_IsDynamicArray);
  1215. def:=arrdef;
  1216. end;
  1217. if assigned(arrdef) then
  1218. begin
  1219. { usage of specialized type inside its generic template }
  1220. if assigned(genericdef) then
  1221. current_specializedef:=arrdef
  1222. { reject declaration of generic class inside generic class }
  1223. else if assigned(genericlist) then
  1224. current_genericdef:=arrdef;
  1225. symtablestack.push(arrdef.symtable);
  1226. insert_generic_parameter_types(arrdef,genericdef,genericlist);
  1227. parse_generic:=(df_generic in arrdef.defoptions);
  1228. end;
  1229. consume(_OF);
  1230. read_anon_type(tt2,true);
  1231. { set element type of the last array definition }
  1232. if assigned(arrdef) then
  1233. begin
  1234. symtablestack.pop(arrdef.symtable);
  1235. arrdef.elementdef:=tt2;
  1236. if is_packed and
  1237. is_managed_type(tt2) then
  1238. Message(type_e_no_packed_inittable);
  1239. end;
  1240. { restore old state }
  1241. parse_generic:=old_parse_generic;
  1242. current_genericdef:=old_current_genericdef;
  1243. current_specializedef:=old_current_specializedef;
  1244. end;
  1245. function procvar_dec(genericdef:tstoreddef;genericlist:TFPObjectList):tdef;
  1246. var
  1247. is_func:boolean;
  1248. pd:tabstractprocdef;
  1249. newtype:ttypesym;
  1250. old_current_genericdef,
  1251. old_current_specializedef: tstoreddef;
  1252. old_parse_generic: boolean;
  1253. begin
  1254. old_current_genericdef:=current_genericdef;
  1255. old_current_specializedef:=current_specializedef;
  1256. old_parse_generic:=parse_generic;
  1257. current_genericdef:=nil;
  1258. current_specializedef:=nil;
  1259. is_func:=(token=_FUNCTION);
  1260. consume(token);
  1261. pd:=tprocvardef.create(normal_function_level);
  1262. { usage of specialized type inside its generic template }
  1263. if assigned(genericdef) then
  1264. current_specializedef:=pd
  1265. { reject declaration of generic class inside generic class }
  1266. else if assigned(genericlist) then
  1267. current_genericdef:=pd;
  1268. symtablestack.push(pd.parast);
  1269. insert_generic_parameter_types(pd,genericdef,genericlist);
  1270. parse_generic:=(df_generic in pd.defoptions);
  1271. { don't allow to add defs to the symtable - use it for type param search only }
  1272. tparasymtable(pd.parast).readonly:=true;
  1273. if token=_LKLAMMER then
  1274. parse_parameter_dec(pd);
  1275. if is_func then
  1276. begin
  1277. consume(_COLON);
  1278. single_type(pd.returndef,[]);
  1279. end;
  1280. if try_to_consume(_OF) then
  1281. begin
  1282. consume(_OBJECT);
  1283. include(pd.procoptions,po_methodpointer);
  1284. end
  1285. else if (m_nested_procvars in current_settings.modeswitches) and
  1286. try_to_consume(_IS) then
  1287. begin
  1288. consume(_NESTED);
  1289. pd.parast.symtablelevel:=normal_function_level+1;
  1290. pd.check_mark_as_nested;
  1291. end;
  1292. symtablestack.pop(pd.parast);
  1293. tparasymtable(pd.parast).readonly:=false;
  1294. result:=pd;
  1295. { possible proc directives }
  1296. if parseprocvardir then
  1297. begin
  1298. if check_proc_directive(true) then
  1299. begin
  1300. newtype:=ttypesym.create('unnamed',result);
  1301. parse_var_proc_directives(tsym(newtype));
  1302. newtype.typedef:=nil;
  1303. result.typesym:=nil;
  1304. newtype.free;
  1305. end;
  1306. { Add implicit hidden parameters and function result }
  1307. handle_calling_convention(pd);
  1308. end;
  1309. { restore old state }
  1310. parse_generic:=old_parse_generic;
  1311. current_genericdef:=old_current_genericdef;
  1312. current_specializedef:=old_current_specializedef;
  1313. end;
  1314. const
  1315. SingleTypeOptionsInTypeBlock:array[Boolean] of TSingleTypeOptions = ([],[stoIsForwardDef]);
  1316. var
  1317. p : tnode;
  1318. hdef : tdef;
  1319. enumdupmsg, first, is_specialize : boolean;
  1320. oldlocalswitches : tlocalswitches;
  1321. bitpacking: boolean;
  1322. stitem: psymtablestackitem;
  1323. sym: tsym;
  1324. st: tsymtable;
  1325. begin
  1326. def:=nil;
  1327. case token of
  1328. _STRING,_FILE:
  1329. begin
  1330. single_type(def,[stoAllowTypeDef]);
  1331. end;
  1332. _LKLAMMER:
  1333. begin
  1334. consume(_LKLAMMER);
  1335. first:=true;
  1336. { allow negativ value_str }
  1337. l:=int64(-1);
  1338. enumdupmsg:=false;
  1339. { check that we are not adding an enum from specialization
  1340. we can't just use current_specializedef because of inner types
  1341. like specialize array of record }
  1342. is_specialize:=false;
  1343. stitem:=symtablestack.stack;
  1344. while assigned(stitem) do
  1345. begin
  1346. { check records, classes and arrays because they can be specialized }
  1347. if stitem^.symtable.symtabletype in [recordsymtable,ObjectSymtable,arraysymtable] then
  1348. begin
  1349. is_specialize:=is_specialize or (df_specialization in tstoreddef(stitem^.symtable.defowner).defoptions);
  1350. stitem:=stitem^.next;
  1351. end
  1352. else
  1353. break;
  1354. end;
  1355. if not is_specialize then
  1356. aktenumdef:=tenumdef.create
  1357. else
  1358. aktenumdef:=nil;
  1359. repeat
  1360. { if it is a specialization then search the first enum member
  1361. and get the member owner instead of just created enumdef }
  1362. if not assigned(aktenumdef) then
  1363. begin
  1364. searchsym(pattern,sym,st);
  1365. if sym.typ=enumsym then
  1366. aktenumdef:=tenumsym(sym).definition
  1367. else
  1368. internalerror(201101021);
  1369. end;
  1370. s:=orgpattern;
  1371. defpos:=current_tokenpos;
  1372. consume(_ID);
  1373. { only allow assigning of specific numbers under fpc mode }
  1374. if not(m_tp7 in current_settings.modeswitches) and
  1375. (
  1376. { in fpc mode also allow := to be compatible
  1377. with previous 1.0.x versions }
  1378. ((m_fpc in current_settings.modeswitches) and
  1379. try_to_consume(_ASSIGNMENT)) or
  1380. try_to_consume(_EQ)
  1381. ) then
  1382. begin
  1383. oldlocalswitches:=current_settings.localswitches;
  1384. include(current_settings.localswitches,cs_allow_enum_calc);
  1385. p:=comp_expr(true,false);
  1386. current_settings.localswitches:=oldlocalswitches;
  1387. if (p.nodetype=ordconstn) then
  1388. begin
  1389. { we expect an integer or an enum of the
  1390. same type }
  1391. if is_integer(p.resultdef) or
  1392. is_char(p.resultdef) or
  1393. equal_defs(p.resultdef,aktenumdef) then
  1394. v:=tordconstnode(p).value
  1395. else
  1396. IncompatibleTypes(p.resultdef,s32inttype);
  1397. end
  1398. else
  1399. Message(parser_e_illegal_expression);
  1400. p.free;
  1401. { please leave that a note, allows type save }
  1402. { declarations in the win32 units ! }
  1403. if (not first) and (v<=l) and (not enumdupmsg) then
  1404. begin
  1405. Message(parser_n_duplicate_enum);
  1406. enumdupmsg:=true;
  1407. end;
  1408. l:=v;
  1409. end
  1410. else
  1411. inc(l.svalue);
  1412. first:=false;
  1413. { don't generate enum members is this is a specialization because aktenumdef is copied from the generic type }
  1414. if not is_specialize then
  1415. begin
  1416. storepos:=current_tokenpos;
  1417. current_tokenpos:=defpos;
  1418. tenumsymtable(aktenumdef.symtable).insert(tenumsym.create(s,aktenumdef,longint(l.svalue)));
  1419. if not (cs_scopedenums in current_settings.localswitches) then
  1420. tstoredsymtable(aktenumdef.owner).insert(tenumsym.create(s,aktenumdef,longint(l.svalue)));
  1421. current_tokenpos:=storepos;
  1422. end;
  1423. until not try_to_consume(_COMMA);
  1424. def:=aktenumdef;
  1425. consume(_RKLAMMER);
  1426. end;
  1427. _ARRAY:
  1428. begin
  1429. array_dec(false,genericdef,genericlist);
  1430. end;
  1431. _SET:
  1432. begin
  1433. set_dec;
  1434. end;
  1435. _CARET:
  1436. begin
  1437. consume(_CARET);
  1438. single_type(tt2,SingleTypeOptionsInTypeBlock[block_type=bt_type]);
  1439. def:=tpointerdef.create(tt2);
  1440. if tt2.typ=forwarddef then
  1441. current_module.checkforwarddefs.add(def);
  1442. end;
  1443. _RECORD:
  1444. begin
  1445. consume(token);
  1446. if (idtoken=_HELPER) and (m_advanced_records in current_settings.modeswitches) then
  1447. begin
  1448. consume(_HELPER);
  1449. def:=object_dec(odt_helper,name,genericdef,genericlist,nil,ht_record);
  1450. end
  1451. else
  1452. def:=record_dec(name,genericdef,genericlist);
  1453. end;
  1454. _PACKED,
  1455. _BITPACKED:
  1456. begin
  1457. bitpacking :=
  1458. (cs_bitpacking in current_settings.localswitches) or
  1459. (token = _BITPACKED);
  1460. consume(token);
  1461. if token=_ARRAY then
  1462. array_dec(bitpacking,genericdef,genericlist)
  1463. else if token=_SET then
  1464. set_dec
  1465. else if token=_FILE then
  1466. single_type(def,[stoAllowTypeDef])
  1467. else
  1468. begin
  1469. oldpackrecords:=current_settings.packrecords;
  1470. if (not bitpacking) or
  1471. (token in [_CLASS,_OBJECT]) then
  1472. current_settings.packrecords:=1
  1473. else
  1474. current_settings.packrecords:=bit_alignment;
  1475. case token of
  1476. _CLASS :
  1477. begin
  1478. consume(_CLASS);
  1479. def:=object_dec(odt_class,name,genericdef,genericlist,nil,ht_none);
  1480. end;
  1481. _OBJECT :
  1482. begin
  1483. consume(_OBJECT);
  1484. def:=object_dec(odt_object,name,genericdef,genericlist,nil,ht_none);
  1485. end;
  1486. else begin
  1487. consume(_RECORD);
  1488. def:=record_dec(name,genericdef,genericlist);
  1489. end;
  1490. end;
  1491. current_settings.packrecords:=oldpackrecords;
  1492. end;
  1493. end;
  1494. _DISPINTERFACE :
  1495. begin
  1496. { need extra check here since interface is a keyword
  1497. in all pascal modes }
  1498. if not(m_class in current_settings.modeswitches) then
  1499. Message(parser_f_need_objfpc_or_delphi_mode);
  1500. consume(token);
  1501. def:=object_dec(odt_dispinterface,name,genericdef,genericlist,nil,ht_none);
  1502. end;
  1503. _CLASS :
  1504. begin
  1505. consume(token);
  1506. { Delphi only allows class of in type blocks }
  1507. if (token=_OF) and
  1508. (
  1509. not(m_delphi in current_settings.modeswitches) or
  1510. (block_type=bt_type)
  1511. ) then
  1512. begin
  1513. consume(_OF);
  1514. single_type(hdef,SingleTypeOptionsInTypeBlock[block_type=bt_type]);
  1515. if is_class(hdef) or
  1516. is_objcclass(hdef) then
  1517. def:=tclassrefdef.create(hdef)
  1518. else
  1519. if hdef.typ=forwarddef then
  1520. begin
  1521. def:=tclassrefdef.create(hdef);
  1522. current_module.checkforwarddefs.add(def);
  1523. end
  1524. else
  1525. Message1(type_e_class_or_objcclass_type_expected,hdef.typename);
  1526. end
  1527. else
  1528. if (idtoken=_HELPER) then
  1529. begin
  1530. consume(_HELPER);
  1531. def:=object_dec(odt_helper,name,genericdef,genericlist,nil,ht_class);
  1532. end
  1533. else
  1534. def:=object_dec(odt_class,name,genericdef,genericlist,nil,ht_none);
  1535. end;
  1536. _CPPCLASS :
  1537. begin
  1538. consume(token);
  1539. def:=object_dec(odt_cppclass,name,genericdef,genericlist,nil,ht_none);
  1540. end;
  1541. _OBJCCLASS :
  1542. begin
  1543. if not(m_objectivec1 in current_settings.modeswitches) then
  1544. Message(parser_f_need_objc);
  1545. consume(token);
  1546. def:=object_dec(odt_objcclass,name,genericdef,genericlist,nil,ht_none);
  1547. end;
  1548. _INTERFACE :
  1549. begin
  1550. { need extra check here since interface is a keyword
  1551. in all pascal modes }
  1552. if not(m_class in current_settings.modeswitches) then
  1553. Message(parser_f_need_objfpc_or_delphi_mode);
  1554. consume(token);
  1555. if current_settings.interfacetype=it_interfacecom then
  1556. def:=object_dec(odt_interfacecom,name,genericdef,genericlist,nil,ht_none)
  1557. else {it_interfacecorba}
  1558. def:=object_dec(odt_interfacecorba,name,genericdef,genericlist,nil,ht_none);
  1559. end;
  1560. _OBJCPROTOCOL :
  1561. begin
  1562. if not(m_objectivec1 in current_settings.modeswitches) then
  1563. Message(parser_f_need_objc);
  1564. consume(token);
  1565. def:=object_dec(odt_objcprotocol,name,genericdef,genericlist,nil,ht_none);
  1566. end;
  1567. _OBJCCATEGORY :
  1568. begin
  1569. if not(m_objectivec1 in current_settings.modeswitches) then
  1570. Message(parser_f_need_objc);
  1571. consume(token);
  1572. def:=object_dec(odt_objccategory,name,genericdef,genericlist,nil,ht_none);
  1573. end;
  1574. _OBJECT :
  1575. begin
  1576. consume(token);
  1577. def:=object_dec(odt_object,name,genericdef,genericlist,nil,ht_none);
  1578. end;
  1579. _PROCEDURE,
  1580. _FUNCTION:
  1581. begin
  1582. def:=procvar_dec(genericdef,genericlist);
  1583. end;
  1584. else
  1585. if (token=_KLAMMERAFFE) and (m_iso in current_settings.modeswitches) then
  1586. begin
  1587. consume(_KLAMMERAFFE);
  1588. single_type(tt2,SingleTypeOptionsInTypeBlock[block_type=bt_type]);
  1589. def:=tpointerdef.create(tt2);
  1590. if tt2.typ=forwarddef then
  1591. current_module.checkforwarddefs.add(def);
  1592. end
  1593. else
  1594. expr_type;
  1595. end;
  1596. if def=nil then
  1597. def:=generrordef;
  1598. end;
  1599. procedure read_anon_type(var def : tdef;parseprocvardir:boolean);
  1600. begin
  1601. read_named_type(def,'',nil,nil,parseprocvardir);
  1602. end;
  1603. procedure write_persistent_type_info(st:tsymtable);
  1604. var
  1605. i : longint;
  1606. def : tdef;
  1607. vmtwriter : TVMTWriter;
  1608. begin
  1609. for i:=0 to st.DefList.Count-1 do
  1610. begin
  1611. def:=tdef(st.DefList[i]);
  1612. case def.typ of
  1613. recorddef :
  1614. write_persistent_type_info(trecorddef(def).symtable);
  1615. objectdef :
  1616. begin
  1617. { Skip generics and forward defs }
  1618. if (df_generic in def.defoptions) or
  1619. (oo_is_forward in tobjectdef(def).objectoptions) then
  1620. continue;
  1621. write_persistent_type_info(tobjectdef(def).symtable);
  1622. { Write also VMT if not done yet }
  1623. if not(ds_vmt_written in def.defstates) then
  1624. begin
  1625. vmtwriter:=TVMTWriter.create(tobjectdef(def));
  1626. if is_interface(tobjectdef(def)) then
  1627. vmtwriter.writeinterfaceids;
  1628. if (oo_has_vmt in tobjectdef(def).objectoptions) then
  1629. vmtwriter.writevmt;
  1630. vmtwriter.free;
  1631. include(def.defstates,ds_vmt_written);
  1632. end;
  1633. end;
  1634. procdef :
  1635. begin
  1636. if assigned(tprocdef(def).localst) and
  1637. (tprocdef(def).localst.symtabletype=localsymtable) then
  1638. write_persistent_type_info(tprocdef(def).localst);
  1639. if assigned(tprocdef(def).parast) then
  1640. write_persistent_type_info(tprocdef(def).parast);
  1641. end;
  1642. end;
  1643. { generate always persistent tables for types in the interface so it can
  1644. be reused in other units and give always the same pointer location. }
  1645. { Init }
  1646. if (
  1647. assigned(def.typesym) and
  1648. (st.symtabletype=globalsymtable) and
  1649. not is_objc_class_or_protocol(def)
  1650. ) or
  1651. is_managed_type(def) or
  1652. (ds_init_table_used in def.defstates) then
  1653. RTTIWriter.write_rtti(def,initrtti);
  1654. { RTTI }
  1655. if (
  1656. assigned(def.typesym) and
  1657. (st.symtabletype=globalsymtable) and
  1658. not is_objc_class_or_protocol(def)
  1659. ) or
  1660. (ds_rtti_table_used in def.defstates) then
  1661. RTTIWriter.write_rtti(def,fullrtti);
  1662. end;
  1663. end;
  1664. end.