ptype.pas 57 KB

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