ptype.pas 72 KB

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