ptype.pas 66 KB

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