ptype.pas 58 KB

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