ptype.pas 69 KB

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