ptype.pas 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512
  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 and (current_structdef.symtable.currentvisibility <> vis_public) then
  698. Message(parser_w_constructor_should_be_public);
  699. { only 1 class constructor is allowed }
  700. if is_classdef and (oo_has_class_constructor in current_structdef.objectoptions) then
  701. Message1(parser_e_only_one_class_constructor_allowed, current_structdef.objrealname^);
  702. oldparse_only:=parse_only;
  703. parse_only:=true;
  704. if is_classdef then
  705. pd:=class_constructor_head
  706. else
  707. pd:=constructor_head;
  708. parse_record_proc_directives(pd);
  709. handle_calling_convention(pd);
  710. { add definition to procsym }
  711. proc_add_definition(pd);
  712. maybe_parse_hint_directives(pd);
  713. parse_only:=oldparse_only;
  714. fields_allowed:=false;
  715. is_classdef:=false;
  716. end;
  717. _DESTRUCTOR :
  718. begin
  719. if not is_classdef then
  720. Message(parser_e_no_destructor_in_records);
  721. { only 1 class destructor is allowed }
  722. if is_classdef and (oo_has_class_destructor in current_structdef.objectoptions) then
  723. Message1(parser_e_only_one_class_destructor_allowed, current_structdef.objrealname^);
  724. oldparse_only:=parse_only;
  725. parse_only:=true;
  726. if is_classdef then
  727. pd:=class_destructor_head
  728. else
  729. pd:=destructor_head;
  730. parse_record_proc_directives(pd);
  731. handle_calling_convention(pd);
  732. { add definition to procsym }
  733. proc_add_definition(pd);
  734. maybe_parse_hint_directives(pd);
  735. parse_only:=oldparse_only;
  736. fields_allowed:=false;
  737. is_classdef:=false;
  738. end;
  739. _END :
  740. begin
  741. consume(_END);
  742. break;
  743. end;
  744. else
  745. consume(_ID); { Give a ident expected message, like tp7 }
  746. end;
  747. until false;
  748. testcurobject:=0;
  749. end;
  750. { reads a record declaration }
  751. function record_dec(const n:tidstring):tdef;
  752. var
  753. old_current_structdef : tabstractrecorddef;
  754. recst : trecordsymtable;
  755. begin
  756. old_current_structdef:=current_structdef;
  757. { create recdef }
  758. recst:=trecordsymtable.create(n,current_settings.packrecords);
  759. current_structdef:=trecorddef.create(n,recst);
  760. result:=current_structdef;
  761. { insert in symtablestack }
  762. symtablestack.push(recst);
  763. { parse record }
  764. consume(_RECORD);
  765. parse_record_members;
  766. { make the record size aligned }
  767. recst.addalignmentpadding;
  768. { restore symtable stack }
  769. symtablestack.pop(recst);
  770. if trecorddef(current_structdef).is_packed and is_managed_type(current_structdef) then
  771. Message(type_e_no_packed_inittable);
  772. current_structdef:=old_current_structdef;
  773. end;
  774. { reads a type definition and returns a pointer to it }
  775. procedure read_named_type(var def : tdef;const name : TIDString;genericdef:tstoreddef;genericlist:TFPObjectList;parseprocvardir:boolean);
  776. var
  777. pt : tnode;
  778. tt2 : tdef;
  779. aktenumdef : tenumdef;
  780. s : TIDString;
  781. l,v : TConstExprInt;
  782. oldpackrecords : longint;
  783. defpos,storepos : tfileposinfo;
  784. procedure expr_type;
  785. var
  786. pt1,pt2 : tnode;
  787. lv,hv : TConstExprInt;
  788. old_block_type : tblock_type;
  789. dospecialize : boolean;
  790. structdef: TDef;
  791. begin
  792. old_block_type:=block_type;
  793. dospecialize:=false;
  794. { use of current parsed object:
  795. - classes can be used also in classes
  796. - objects can be parameters }
  797. if (token=_ID) then
  798. begin
  799. structdef:=current_structdef;
  800. while Assigned(structdef) and (structdef.typ in [objectdef,recorddef]) do
  801. begin
  802. if (tabstractrecorddef(structdef).objname^=pattern) and
  803. (
  804. (testcurobject=2) or
  805. is_class_or_interface_or_objc(structdef)
  806. ) then
  807. begin
  808. consume(_ID);
  809. def:=structdef;
  810. exit;
  811. end;
  812. structdef:=tdef(tabstractrecorddef(structdef).owner.defowner);
  813. end;
  814. end;
  815. { Generate a specialization? }
  816. if try_to_consume(_SPECIALIZE) then
  817. dospecialize:=true;
  818. { we can't accept a equal in type }
  819. pt1:=comp_expr(false,true);
  820. if not dospecialize and
  821. try_to_consume(_POINTPOINT) then
  822. begin
  823. { get high value of range }
  824. pt2:=comp_expr(false,false);
  825. { make both the same type or give an error. This is not
  826. done when both are integer values, because typecasting
  827. between -3200..3200 will result in a signed-unsigned
  828. conflict and give a range check error (PFV) }
  829. if not(is_integer(pt1.resultdef) and is_integer(pt2.resultdef)) then
  830. inserttypeconv(pt1,pt2.resultdef);
  831. { both must be evaluated to constants now }
  832. if (pt1.nodetype=ordconstn) and
  833. (pt2.nodetype=ordconstn) then
  834. begin
  835. lv:=tordconstnode(pt1).value;
  836. hv:=tordconstnode(pt2).value;
  837. { Check bounds }
  838. if hv<lv then
  839. message(parser_e_upper_lower_than_lower)
  840. else if (lv.signed and (lv.svalue<0)) and (not hv.signed and (hv.uvalue>qword(high(int64)))) then
  841. message(type_e_cant_eval_constant_expr)
  842. else
  843. begin
  844. { All checks passed, create the new def }
  845. case pt1.resultdef.typ of
  846. enumdef :
  847. def:=tenumdef.create_subrange(tenumdef(pt1.resultdef),lv.svalue,hv.svalue);
  848. orddef :
  849. begin
  850. if is_char(pt1.resultdef) then
  851. def:=torddef.create(uchar,lv,hv)
  852. else
  853. if is_boolean(pt1.resultdef) then
  854. def:=torddef.create(pasbool,lv,hv)
  855. else if is_signed(pt1.resultdef) then
  856. def:=torddef.create(range_to_basetype(lv,hv),lv,hv)
  857. else
  858. def:=torddef.create(range_to_basetype(lv,hv),lv,hv);
  859. end;
  860. end;
  861. end;
  862. end
  863. else
  864. Message(sym_e_error_in_type_def);
  865. pt2.free;
  866. end
  867. else
  868. begin
  869. { a simple type renaming or generic specialization }
  870. if (pt1.nodetype=typen) then
  871. begin
  872. def:=ttypenode(pt1).resultdef;
  873. if dospecialize then
  874. generate_specialization(def)
  875. else
  876. begin
  877. if assigned(current_specializedef) and (def=current_specializedef.genericdef) then
  878. begin
  879. def:=current_specializedef
  880. end
  881. else if (def=current_genericdef) then
  882. begin
  883. def:=current_genericdef
  884. end
  885. else if (df_generic in def.defoptions) then
  886. begin
  887. Message(parser_e_no_generics_as_types);
  888. def:=generrordef;
  889. end
  890. else if is_objccategory(def) then
  891. begin
  892. Message(parser_e_no_category_as_types);
  893. def:=generrordef
  894. end
  895. end;
  896. end
  897. else
  898. Message(sym_e_error_in_type_def);
  899. end;
  900. pt1.free;
  901. block_type:=old_block_type;
  902. end;
  903. procedure set_dec;
  904. begin
  905. consume(_SET);
  906. consume(_OF);
  907. read_anon_type(tt2,true);
  908. if assigned(tt2) then
  909. begin
  910. case tt2.typ of
  911. { don't forget that min can be negativ PM }
  912. enumdef :
  913. if (tenumdef(tt2).min>=0) and
  914. (tenumdef(tt2).max<=255) then
  915. // !! def:=tsetdef.create(tt2,tenumdef(tt2.def).min,tenumdef(tt2.def).max))
  916. def:=tsetdef.create(tt2,tenumdef(tt2).min,tenumdef(tt2).max)
  917. else
  918. Message(sym_e_ill_type_decl_set);
  919. orddef :
  920. begin
  921. if (torddef(tt2).ordtype<>uvoid) and
  922. (torddef(tt2).ordtype<>uwidechar) and
  923. (torddef(tt2).low>=0) then
  924. // !! def:=tsetdef.create(tt2,torddef(tt2.def).low,torddef(tt2.def).high))
  925. if Torddef(tt2).high>int64(high(byte)) then
  926. message(sym_e_ill_type_decl_set)
  927. else
  928. def:=tsetdef.create(tt2,torddef(tt2).low.svalue,torddef(tt2).high.svalue)
  929. else
  930. Message(sym_e_ill_type_decl_set);
  931. end;
  932. else
  933. Message(sym_e_ill_type_decl_set);
  934. end;
  935. end
  936. else
  937. def:=generrordef;
  938. end;
  939. procedure array_dec(is_packed: boolean);
  940. var
  941. lowval,
  942. highval : TConstExprInt;
  943. indexdef : tdef;
  944. hdef : tdef;
  945. arrdef : tarraydef;
  946. procedure setdefdecl(def:tdef);
  947. begin
  948. case def.typ of
  949. enumdef :
  950. begin
  951. lowval:=tenumdef(def).min;
  952. highval:=tenumdef(def).max;
  953. if (m_fpc in current_settings.modeswitches) and
  954. (tenumdef(def).has_jumps) then
  955. Message(type_e_array_index_enums_with_assign_not_possible);
  956. indexdef:=def;
  957. end;
  958. orddef :
  959. begin
  960. if torddef(def).ordtype in [uchar,
  961. u8bit,u16bit,
  962. s8bit,s16bit,s32bit,
  963. {$ifdef cpu64bitaddr}
  964. u32bit,s64bit,
  965. {$endif cpu64bitaddr}
  966. pasbool,bool8bit,bool16bit,bool32bit,bool64bit,
  967. uwidechar] then
  968. begin
  969. lowval:=torddef(def).low;
  970. highval:=torddef(def).high;
  971. indexdef:=def;
  972. end
  973. else
  974. Message1(parser_e_type_cant_be_used_in_array_index,def.typename);
  975. end;
  976. else
  977. Message(sym_e_error_in_type_def);
  978. end;
  979. end;
  980. begin
  981. arrdef:=nil;
  982. consume(_ARRAY);
  983. { open array? }
  984. if try_to_consume(_LECKKLAMMER) then
  985. begin
  986. { defaults }
  987. indexdef:=generrordef;
  988. { use defaults which don't overflow the compiler }
  989. lowval:=0;
  990. highval:=0;
  991. repeat
  992. { read the expression and check it, check apart if the
  993. declaration is an enum declaration because that needs to
  994. be parsed by readtype (PFV) }
  995. if token=_LKLAMMER then
  996. begin
  997. read_anon_type(hdef,true);
  998. setdefdecl(hdef);
  999. end
  1000. else
  1001. begin
  1002. pt:=expr(true);
  1003. if pt.nodetype=typen then
  1004. setdefdecl(pt.resultdef)
  1005. else
  1006. begin
  1007. if (pt.nodetype=rangen) then
  1008. begin
  1009. if (trangenode(pt).left.nodetype=ordconstn) and
  1010. (trangenode(pt).right.nodetype=ordconstn) then
  1011. begin
  1012. { make both the same type or give an error. This is not
  1013. done when both are integer values, because typecasting
  1014. between -3200..3200 will result in a signed-unsigned
  1015. conflict and give a range check error (PFV) }
  1016. if not(is_integer(trangenode(pt).left.resultdef) and is_integer(trangenode(pt).left.resultdef)) then
  1017. inserttypeconv(trangenode(pt).left,trangenode(pt).right.resultdef);
  1018. lowval:=tordconstnode(trangenode(pt).left).value;
  1019. highval:=tordconstnode(trangenode(pt).right).value;
  1020. if highval<lowval then
  1021. begin
  1022. Message(parser_e_array_lower_less_than_upper_bound);
  1023. highval:=lowval;
  1024. end
  1025. else if (lowval<int64(low(aint))) or
  1026. (highval > high(aint)) then
  1027. begin
  1028. Message(parser_e_array_range_out_of_bounds);
  1029. lowval :=0;
  1030. highval:=0;
  1031. end;
  1032. if is_integer(trangenode(pt).left.resultdef) then
  1033. range_to_type(lowval,highval,indexdef)
  1034. else
  1035. indexdef:=trangenode(pt).left.resultdef;
  1036. end
  1037. else
  1038. Message(type_e_cant_eval_constant_expr);
  1039. end
  1040. else
  1041. Message(sym_e_error_in_type_def)
  1042. end;
  1043. pt.free;
  1044. end;
  1045. { if the array is already created add the new arrray
  1046. as element of the existing array, otherwise create a new array }
  1047. if assigned(arrdef) then
  1048. begin
  1049. arrdef.elementdef:=tarraydef.create(lowval.svalue,highval.svalue,indexdef);
  1050. arrdef:=tarraydef(arrdef.elementdef);
  1051. end
  1052. else
  1053. begin
  1054. arrdef:=tarraydef.create(lowval.svalue,highval.svalue,indexdef);
  1055. def:=arrdef;
  1056. end;
  1057. if is_packed then
  1058. include(arrdef.arrayoptions,ado_IsBitPacked);
  1059. if token=_COMMA then
  1060. consume(_COMMA)
  1061. else
  1062. break;
  1063. until false;
  1064. consume(_RECKKLAMMER);
  1065. end
  1066. else
  1067. begin
  1068. if is_packed then
  1069. Message(parser_e_packed_dynamic_open_array);
  1070. arrdef:=tarraydef.create(0,-1,s32inttype);
  1071. include(arrdef.arrayoptions,ado_IsDynamicArray);
  1072. def:=arrdef;
  1073. end;
  1074. consume(_OF);
  1075. read_anon_type(tt2,true);
  1076. { set element type of the last array definition }
  1077. if assigned(arrdef) then
  1078. begin
  1079. arrdef.elementdef:=tt2;
  1080. if is_packed and
  1081. is_managed_type(tt2) then
  1082. Message(type_e_no_packed_inittable);
  1083. end;
  1084. end;
  1085. var
  1086. p : tnode;
  1087. hdef : tdef;
  1088. pd : tabstractprocdef;
  1089. is_func,
  1090. enumdupmsg, first : boolean;
  1091. newtype : ttypesym;
  1092. oldlocalswitches : tlocalswitches;
  1093. bitpacking: boolean;
  1094. begin
  1095. def:=nil;
  1096. case token of
  1097. _STRING,_FILE:
  1098. begin
  1099. single_type(def,false,true);
  1100. end;
  1101. _LKLAMMER:
  1102. begin
  1103. consume(_LKLAMMER);
  1104. first := true;
  1105. { allow negativ value_str }
  1106. l:=int64(-1);
  1107. enumdupmsg:=false;
  1108. aktenumdef:=tenumdef.create;
  1109. repeat
  1110. s:=orgpattern;
  1111. defpos:=current_tokenpos;
  1112. consume(_ID);
  1113. { only allow assigning of specific numbers under fpc mode }
  1114. if not(m_tp7 in current_settings.modeswitches) and
  1115. (
  1116. { in fpc mode also allow := to be compatible
  1117. with previous 1.0.x versions }
  1118. ((m_fpc in current_settings.modeswitches) and
  1119. try_to_consume(_ASSIGNMENT)) or
  1120. try_to_consume(_EQUAL)
  1121. ) then
  1122. begin
  1123. oldlocalswitches:=current_settings.localswitches;
  1124. include(current_settings.localswitches,cs_allow_enum_calc);
  1125. p:=comp_expr(true,false);
  1126. current_settings.localswitches:=oldlocalswitches;
  1127. if (p.nodetype=ordconstn) then
  1128. begin
  1129. { we expect an integer or an enum of the
  1130. same type }
  1131. if is_integer(p.resultdef) or
  1132. is_char(p.resultdef) or
  1133. equal_defs(p.resultdef,aktenumdef) then
  1134. v:=tordconstnode(p).value
  1135. else
  1136. IncompatibleTypes(p.resultdef,s32inttype);
  1137. end
  1138. else
  1139. Message(parser_e_illegal_expression);
  1140. p.free;
  1141. { please leave that a note, allows type save }
  1142. { declarations in the win32 units ! }
  1143. if (not first) and (v<=l) and (not enumdupmsg) then
  1144. begin
  1145. Message(parser_n_duplicate_enum);
  1146. enumdupmsg:=true;
  1147. end;
  1148. l:=v;
  1149. end
  1150. else
  1151. inc(l.svalue);
  1152. first := false;
  1153. storepos:=current_tokenpos;
  1154. current_tokenpos:=defpos;
  1155. tenumsymtable(aktenumdef.symtable).insert(tenumsym.create(s,aktenumdef,longint(l.svalue)));
  1156. if not (cs_scopedenums in current_settings.localswitches) then
  1157. tstoredsymtable(aktenumdef.owner).insert(tenumsym.create(s,aktenumdef,longint(l.svalue)));
  1158. current_tokenpos:=storepos;
  1159. until not try_to_consume(_COMMA);
  1160. def:=aktenumdef;
  1161. consume(_RKLAMMER);
  1162. end;
  1163. _ARRAY:
  1164. begin
  1165. array_dec(false);
  1166. end;
  1167. _SET:
  1168. begin
  1169. set_dec;
  1170. end;
  1171. _CARET:
  1172. begin
  1173. consume(_CARET);
  1174. single_type(tt2,(block_type=bt_type),false);
  1175. def:=tpointerdef.create(tt2);
  1176. if tt2.typ=forwarddef then
  1177. current_module.checkforwarddefs.add(def);
  1178. end;
  1179. _RECORD:
  1180. begin
  1181. def:=record_dec(name);
  1182. end;
  1183. _PACKED,
  1184. _BITPACKED:
  1185. begin
  1186. bitpacking :=
  1187. (cs_bitpacking in current_settings.localswitches) or
  1188. (token = _BITPACKED);
  1189. consume(token);
  1190. if token=_ARRAY then
  1191. array_dec(bitpacking)
  1192. else if token=_SET then
  1193. set_dec
  1194. else if token=_FILE then
  1195. single_type(def,false,true)
  1196. else
  1197. begin
  1198. oldpackrecords:=current_settings.packrecords;
  1199. if (not bitpacking) or
  1200. (token in [_CLASS,_OBJECT]) then
  1201. current_settings.packrecords:=1
  1202. else
  1203. current_settings.packrecords:=bit_alignment;
  1204. case token of
  1205. _CLASS :
  1206. begin
  1207. consume(_CLASS);
  1208. def:=object_dec(odt_class,name,genericdef,genericlist,nil);
  1209. end;
  1210. _OBJECT :
  1211. begin
  1212. consume(_OBJECT);
  1213. def:=object_dec(odt_object,name,genericdef,genericlist,nil);
  1214. end;
  1215. else
  1216. def:=record_dec(name);
  1217. end;
  1218. current_settings.packrecords:=oldpackrecords;
  1219. end;
  1220. end;
  1221. _DISPINTERFACE :
  1222. begin
  1223. { need extra check here since interface is a keyword
  1224. in all pascal modes }
  1225. if not(m_class in current_settings.modeswitches) then
  1226. Message(parser_f_need_objfpc_or_delphi_mode);
  1227. consume(token);
  1228. def:=object_dec(odt_dispinterface,name,genericdef,genericlist,nil);
  1229. end;
  1230. _CLASS :
  1231. begin
  1232. consume(token);
  1233. { Delphi only allows class of in type blocks }
  1234. if (token=_OF) and
  1235. (
  1236. not(m_delphi in current_settings.modeswitches) or
  1237. (block_type=bt_type)
  1238. ) then
  1239. begin
  1240. consume(_OF);
  1241. single_type(hdef,(block_type=bt_type),false);
  1242. if is_class(hdef) or
  1243. is_objcclass(hdef) then
  1244. def:=tclassrefdef.create(hdef)
  1245. else
  1246. if hdef.typ=forwarddef then
  1247. begin
  1248. def:=tclassrefdef.create(hdef);
  1249. current_module.checkforwarddefs.add(def);
  1250. end
  1251. else
  1252. Message1(type_e_class_or_objcclass_type_expected,hdef.typename);
  1253. end
  1254. else
  1255. def:=object_dec(odt_class,name,genericdef,genericlist,nil);
  1256. end;
  1257. _CPPCLASS :
  1258. begin
  1259. consume(token);
  1260. def:=object_dec(odt_cppclass,name,genericdef,genericlist,nil);
  1261. end;
  1262. _OBJCCLASS :
  1263. begin
  1264. if not(m_objectivec1 in current_settings.modeswitches) then
  1265. Message(parser_f_need_objc);
  1266. consume(token);
  1267. def:=object_dec(odt_objcclass,name,genericdef,genericlist,nil);
  1268. end;
  1269. _INTERFACE :
  1270. begin
  1271. { need extra check here since interface is a keyword
  1272. in all pascal modes }
  1273. if not(m_class in current_settings.modeswitches) then
  1274. Message(parser_f_need_objfpc_or_delphi_mode);
  1275. consume(token);
  1276. if current_settings.interfacetype=it_interfacecom then
  1277. def:=object_dec(odt_interfacecom,name,genericdef,genericlist,nil)
  1278. else {it_interfacecorba}
  1279. def:=object_dec(odt_interfacecorba,name,genericdef,genericlist,nil);
  1280. end;
  1281. _OBJCPROTOCOL :
  1282. begin
  1283. if not(m_objectivec1 in current_settings.modeswitches) then
  1284. Message(parser_f_need_objc);
  1285. consume(token);
  1286. def:=object_dec(odt_objcprotocol,name,genericdef,genericlist,nil);
  1287. end;
  1288. _OBJCCATEGORY :
  1289. begin
  1290. if not(m_objectivec1 in current_settings.modeswitches) then
  1291. Message(parser_f_need_objc);
  1292. consume(token);
  1293. def:=object_dec(odt_objccategory,name,genericdef,genericlist,nil);
  1294. end;
  1295. _OBJECT :
  1296. begin
  1297. consume(token);
  1298. def:=object_dec(odt_object,name,genericdef,genericlist,nil);
  1299. end;
  1300. _PROCEDURE,
  1301. _FUNCTION:
  1302. begin
  1303. is_func:=(token=_FUNCTION);
  1304. consume(token);
  1305. pd:=tprocvardef.create(normal_function_level);
  1306. if token=_LKLAMMER then
  1307. parse_parameter_dec(pd);
  1308. if is_func then
  1309. begin
  1310. consume(_COLON);
  1311. single_type(pd.returndef,false,false);
  1312. end;
  1313. if try_to_consume(_OF) then
  1314. begin
  1315. consume(_OBJECT);
  1316. include(pd.procoptions,po_methodpointer);
  1317. end
  1318. else if (m_nested_procvars in current_settings.modeswitches) and
  1319. try_to_consume(_IS) then
  1320. begin
  1321. consume(_NESTED);
  1322. pd.parast.symtablelevel:=normal_function_level+1;
  1323. pd.check_mark_as_nested;
  1324. end;
  1325. def:=pd;
  1326. { possible proc directives }
  1327. if parseprocvardir then
  1328. begin
  1329. if check_proc_directive(true) then
  1330. begin
  1331. newtype:=ttypesym.create('unnamed',def);
  1332. parse_var_proc_directives(tsym(newtype));
  1333. newtype.typedef:=nil;
  1334. def.typesym:=nil;
  1335. newtype.free;
  1336. end;
  1337. { Add implicit hidden parameters and function result }
  1338. handle_calling_convention(pd);
  1339. end;
  1340. end;
  1341. else
  1342. if (token=_KLAMMERAFFE) and (m_iso in current_settings.modeswitches) then
  1343. begin
  1344. consume(_KLAMMERAFFE);
  1345. single_type(tt2,(block_type=bt_type),false);
  1346. def:=tpointerdef.create(tt2);
  1347. if tt2.typ=forwarddef then
  1348. current_module.checkforwarddefs.add(def);
  1349. end
  1350. else
  1351. expr_type;
  1352. end;
  1353. if def=nil then
  1354. def:=generrordef;
  1355. end;
  1356. procedure read_anon_type(var def : tdef;parseprocvardir:boolean);
  1357. begin
  1358. read_named_type(def,'',nil,nil,parseprocvardir);
  1359. end;
  1360. procedure write_persistent_type_info(st:tsymtable);
  1361. var
  1362. i : longint;
  1363. def : tdef;
  1364. vmtwriter : TVMTWriter;
  1365. begin
  1366. for i:=0 to st.DefList.Count-1 do
  1367. begin
  1368. def:=tdef(st.DefList[i]);
  1369. case def.typ of
  1370. recorddef :
  1371. write_persistent_type_info(trecorddef(def).symtable);
  1372. objectdef :
  1373. begin
  1374. { Skip generics and forward defs }
  1375. if (df_generic in def.defoptions) or
  1376. (oo_is_forward in tobjectdef(def).objectoptions) then
  1377. continue;
  1378. write_persistent_type_info(tobjectdef(def).symtable);
  1379. { Write also VMT if not done yet }
  1380. if not(ds_vmt_written in def.defstates) then
  1381. begin
  1382. vmtwriter:=TVMTWriter.create(tobjectdef(def));
  1383. if is_interface(tobjectdef(def)) then
  1384. vmtwriter.writeinterfaceids;
  1385. if (oo_has_vmt in tobjectdef(def).objectoptions) then
  1386. vmtwriter.writevmt;
  1387. vmtwriter.free;
  1388. include(def.defstates,ds_vmt_written);
  1389. end;
  1390. end;
  1391. procdef :
  1392. begin
  1393. if assigned(tprocdef(def).localst) and
  1394. (tprocdef(def).localst.symtabletype=localsymtable) then
  1395. write_persistent_type_info(tprocdef(def).localst);
  1396. if assigned(tprocdef(def).parast) then
  1397. write_persistent_type_info(tprocdef(def).parast);
  1398. end;
  1399. end;
  1400. { generate always persistent tables for types in the interface so it can
  1401. be reused in other units and give always the same pointer location. }
  1402. { Init }
  1403. if (
  1404. assigned(def.typesym) and
  1405. (st.symtabletype=globalsymtable) and
  1406. not is_objc_class_or_protocol(def)
  1407. ) or
  1408. is_managed_type(def) or
  1409. (ds_init_table_used in def.defstates) then
  1410. RTTIWriter.write_rtti(def,initrtti);
  1411. { RTTI }
  1412. if (
  1413. assigned(def.typesym) and
  1414. (st.symtabletype=globalsymtable) and
  1415. not is_objc_class_or_protocol(def)
  1416. ) or
  1417. (ds_rtti_table_used in def.defstates) then
  1418. RTTIWriter.write_rtti(def,fullrtti);
  1419. end;
  1420. end;
  1421. end.