ptype.pas 66 KB

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