ptype.pas 72 KB

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