ptype.pas 92 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202
  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(out def:tdef;options:TSingleTypeOptions);
  34. { ... but rejects types that cannot be returned from functions }
  35. function result_type(options:TSingleTypeOptions):tdef;
  36. { reads any type declaration, where the resulting type will get name as type identifier }
  37. procedure read_named_type(var def:tdef;const newsym:tsym;genericdef:tstoreddef;genericlist:tfphashobjectlist;parseprocvardir:boolean;var hadtypetoken:boolean);
  38. { reads any type declaration }
  39. procedure read_anon_type(var def : tdef;parseprocvardir:boolean;genericdef:tstoreddef);
  40. { parse nested type declaration of the def (typedef) }
  41. procedure parse_nested_types(var def: tdef; isforwarddef,allowspecialization: boolean; currentstructstack: tfpobjectlist);
  42. { add a definition for a method to a record/objectdef that will contain
  43. all code for initialising typed constants (only for targets in
  44. systems.systems_typed_constants_node_init) }
  45. procedure add_typedconst_init_routine(def: tabstractrecorddef);
  46. { parse hint directives (platform, deprecated, ...) for a procdef }
  47. procedure maybe_parse_hint_directives(pd:tprocdef);
  48. implementation
  49. uses
  50. { common }
  51. cutils,
  52. { global }
  53. globals,tokens,verbose,constexp,
  54. systems,
  55. { symtable }
  56. symconst,symsym,symtable,symcreat,
  57. defutil,defcmp,
  58. {$ifdef jvm}
  59. jvmdef,
  60. {$endif}
  61. { modules }
  62. fmodule,
  63. { pass 1 }
  64. node,
  65. nset,ncnv,ncon,nld,
  66. { parser }
  67. scanner,
  68. pbase,pexpr,pdecsub,pdecvar,pdecobj,pdecl,pgenutil,pparautl,procdefutil
  69. {$ifdef jvm}
  70. ,pjvm
  71. {$endif}
  72. ;
  73. procedure maybe_parse_hint_directives(pd:tprocdef);
  74. var
  75. dummysymoptions : tsymoptions;
  76. deprecatedmsg : pshortstring;
  77. begin
  78. if assigned(pd) then
  79. begin
  80. dummysymoptions:=pd.symoptions;
  81. deprecatedmsg:=pd.deprecatedmsg;
  82. end
  83. else
  84. begin
  85. dummysymoptions:=[];
  86. deprecatedmsg:=nil;
  87. end;
  88. while try_consume_hintdirective(dummysymoptions,deprecatedmsg) do
  89. consume(_SEMICOLON);
  90. if assigned(pd) then
  91. begin
  92. pd.symoptions:=pd.symoptions+dummysymoptions;
  93. if sp_has_deprecated_msg in dummysymoptions then
  94. pd.deprecatedmsg:=deprecatedmsg;
  95. end
  96. else
  97. stringdispose(deprecatedmsg);
  98. end;
  99. procedure resolve_forward_types;
  100. var
  101. i: longint;
  102. tmp,
  103. hpd,
  104. def : tdef;
  105. srsym : tsym;
  106. srsymtable : TSymtable;
  107. hs : string;
  108. fileinfo : tfileposinfo;
  109. begin
  110. for i:=0 to current_module.checkforwarddefs.Count-1 do
  111. begin
  112. def:=tdef(current_module.checkforwarddefs[i]);
  113. case def.typ of
  114. pointerdef,
  115. classrefdef :
  116. begin
  117. { classrefdef inherits from pointerdef }
  118. hpd:=tabstractpointerdef(def).pointeddef;
  119. { still a forward def ? }
  120. if hpd.typ=forwarddef then
  121. begin
  122. { try to resolve the forward }
  123. if not assigned(tforwarddef(hpd).tosymname) then
  124. internalerror(200211201);
  125. hs:=tforwarddef(hpd).tosymname^;
  126. searchsym(upper(hs),srsym,srsymtable);
  127. { we don't need the forwarddef anymore, dispose it }
  128. hpd.free;
  129. tabstractpointerdef(def).pointeddef:=nil; { if error occurs }
  130. { was a type sym found ? }
  131. if assigned(srsym) and
  132. (srsym.typ=typesym) then
  133. begin
  134. if (sp_generic_dummy in srsym.symoptions) and
  135. not (ttypesym(srsym).typedef.typ=undefineddef) and
  136. assigned(def.owner.defowner) then
  137. begin
  138. { is the forward def part of a specialization? }
  139. tmp:=tdef(def.owner.defowner);
  140. while not tstoreddef(tmp).is_specialization and assigned(tmp.owner.defowner) do
  141. tmp:=tdef(tmp.owner.defowner);
  142. { if the genericdef of the specialization is the same as the
  143. def the dummy points to, then update the found symbol }
  144. if tstoreddef(tmp).is_specialization and
  145. (tstoreddef(tmp).genericdef=ttypesym(srsym).typedef) then
  146. srsym:=tstoreddef(tmp).typesym;
  147. end;
  148. tabstractpointerdef(def).pointeddef:=ttypesym(srsym).typedef;
  149. { correctly set the generic/specialization flags and the genericdef }
  150. if df_generic in tstoreddef(tabstractpointerdef(def).pointeddef).defoptions then
  151. include(tstoreddef(def).defoptions,df_generic);
  152. if df_specialization in tstoreddef(tabstractpointerdef(def).pointeddef).defoptions then
  153. begin
  154. include(tstoreddef(def).defoptions,df_specialization);
  155. case def.typ of
  156. pointerdef:
  157. tstoreddef(def).genericdef:=cpointerdef.getreusable(tstoreddef(tabstractpointerdef(def).pointeddef).genericdef);
  158. classrefdef:
  159. tstoreddef(def).genericdef:=cclassrefdef.create(tstoreddef(tabstractpointerdef(def).pointeddef).genericdef);
  160. else
  161. internalerror(2016120901);
  162. end;
  163. end;
  164. { avoid wrong unused warnings web bug 801 PM }
  165. inc(ttypesym(srsym).refs);
  166. { we need a class type for classrefdef }
  167. if (def.typ=classrefdef) and
  168. not(is_class(ttypesym(srsym).typedef)) and
  169. not(is_objcclass(ttypesym(srsym).typedef)) and
  170. not(is_javaclass(ttypesym(srsym).typedef)) then
  171. MessagePos1(def.typesym.fileinfo,type_e_class_type_expected,ttypesym(srsym).typedef.typename);
  172. { this could also be a generic dummy that was not
  173. overridden with a specific type }
  174. if (sp_generic_dummy in srsym.symoptions) and
  175. (
  176. (ttypesym(srsym).typedef.typ=undefineddef) or
  177. (
  178. { or an unspecialized generic symbol, which is
  179. the case for generics defined in non-Delphi
  180. modes }
  181. tstoreddef(ttypesym(srsym).typedef).is_generic and
  182. not defs_belong_to_same_generic(def,ttypesym(srsym).typedef)
  183. )
  184. ) then
  185. begin
  186. if assigned(def.typesym) then
  187. fileinfo:=def.typesym.fileinfo
  188. else
  189. { this is the case for inline pointer declarations }
  190. fileinfo:=srsym.fileinfo;
  191. MessagePos(fileinfo,parser_e_no_generics_as_types);
  192. end;
  193. end
  194. else
  195. begin
  196. Message1(sym_e_forward_type_not_resolved,hs);
  197. { try to recover }
  198. tabstractpointerdef(def).pointeddef:=generrordef;
  199. end;
  200. end;
  201. end;
  202. objectdef :
  203. begin
  204. { give an error as the implementation may follow in an
  205. other type block which is allowed by FPC modes }
  206. if not(m_fpc in current_settings.modeswitches) and
  207. (oo_is_forward in tobjectdef(def).objectoptions) then
  208. MessagePos1(def.typesym.fileinfo,type_e_type_is_not_completly_defined,def.typename);
  209. { generate specializations for generic forwarddefs }
  210. if not (oo_is_forward in tobjectdef(def).objectoptions) and
  211. tstoreddef(def).is_generic then
  212. generate_specializations_for_forwarddef(def);
  213. end;
  214. else
  215. internalerror(200811071);
  216. end;
  217. end;
  218. current_module.checkforwarddefs.clear;
  219. end;
  220. procedure id_type(var def : tdef;isforwarddef,checkcurrentrecdef,allowgenericsyms,allowunitsym:boolean;out srsym:tsym;out srsymtable:tsymtable;out is_specialize,is_unit_specific:boolean); forward;
  221. { def is the outermost type in which other types have to be searched
  222. isforward indicates whether the current definition can be a forward definition
  223. if assigned, currentstructstack is a list of tabstractrecorddefs that, from
  224. last to first, are child types of def that are not yet visible via the
  225. normal symtable searching routines because they are types that are currently
  226. being parsed (so using id_type on them after pushing def on the
  227. symtablestack would result in errors because they'd come back as errordef)
  228. }
  229. procedure parse_nested_types(var def: tdef; isforwarddef,allowspecialization: boolean; currentstructstack: tfpobjectlist);
  230. var
  231. t2: tdef;
  232. structstackindex: longint;
  233. srsym: tsym;
  234. srsymtable: tsymtable;
  235. oldsymtablestack: TSymtablestack;
  236. isspecialize,
  237. isunitspecific : boolean;
  238. begin
  239. if assigned(currentstructstack) then
  240. structstackindex:=currentstructstack.count-1
  241. else
  242. structstackindex:=-1;
  243. { handle types inside classes, e.g. TNode.TLongint }
  244. while (token=_POINT) do
  245. begin
  246. if is_class_or_object(def) or is_record(def) or is_java_class_or_interface(def) then
  247. begin
  248. if (def.typ=objectdef) then
  249. def:=find_real_class_definition(tobjectdef(def),false);
  250. consume(_POINT);
  251. if (structstackindex>=0) and
  252. (tabstractrecorddef(currentstructstack[structstackindex]).objname^=pattern) then
  253. begin
  254. def:=tdef(currentstructstack[structstackindex]);
  255. dec(structstackindex);
  256. consume(_ID);
  257. end
  258. else
  259. begin
  260. structstackindex:=-1;
  261. oldsymtablestack:=symtablestack;
  262. symtablestack:=TSymtablestack.create;
  263. symtablestack.push(tabstractrecorddef(def).symtable);
  264. t2:=generrordef;
  265. id_type(t2,isforwarddef,false,false,false,srsym,srsymtable,isspecialize,isunitspecific);
  266. symtablestack.pop(tabstractrecorddef(def).symtable);
  267. symtablestack.free;
  268. symtablestack:=oldsymtablestack;
  269. if isspecialize then
  270. begin
  271. if not allowspecialization then
  272. Message(parser_e_no_local_para_def);
  273. generate_specialization(t2,isunitspecific,false,'',srsym.name,srsymtable);
  274. end;
  275. def:=t2;
  276. end;
  277. end
  278. else
  279. break;
  280. end;
  281. end;
  282. function try_parse_structdef_nested_type(out def: tdef; basedef: tabstractrecorddef; isfowarddef: boolean): boolean;
  283. var
  284. structdef : tdef;
  285. structdefstack : tfpobjectlist;
  286. begin
  287. def:=nil;
  288. { use of current parsed object:
  289. classes, objects, records can be used also in themself }
  290. structdef:=basedef;
  291. structdefstack:=nil;
  292. while assigned(structdef) and (structdef.typ in [objectdef,recorddef]) do
  293. begin
  294. if (tabstractrecorddef(structdef).objname^=pattern) then
  295. begin
  296. consume(_ID);
  297. def:=structdef;
  298. { we found the top-most match, now check how far down we can
  299. follow }
  300. structdefstack:=tfpobjectlist.create(false);
  301. structdef:=basedef;
  302. while (structdef<>def) do
  303. begin
  304. structdefstack.add(structdef);
  305. structdef:=tabstractrecorddef(structdef.owner.defowner);
  306. end;
  307. parse_nested_types(def,isfowarddef,false,structdefstack);
  308. structdefstack.free;
  309. result:=true;
  310. exit;
  311. end;
  312. structdef:=tdef(tabstractrecorddef(structdef).owner.defowner);
  313. end;
  314. result:=false;
  315. end;
  316. procedure id_type(var def : tdef;isforwarddef,checkcurrentrecdef,allowgenericsyms,allowunitsym:boolean;out srsym:tsym;out srsymtable:tsymtable;out is_specialize,is_unit_specific:boolean);
  317. { reads a type definition }
  318. { to a appropriating tdef, s gets the name of }
  319. { the type to allow name mangling }
  320. var
  321. not_a_type : boolean;
  322. pos : tfileposinfo;
  323. s,sorg : TIDString;
  324. t : ttoken;
  325. begin
  326. srsym:=nil;
  327. srsymtable:=nil;
  328. is_specialize:=false;
  329. is_unit_specific:=false;
  330. s:=pattern;
  331. sorg:=orgpattern;
  332. pos:=current_tokenpos;
  333. { use of current parsed object:
  334. classes, objects, records can be used also in themself }
  335. if checkcurrentrecdef and
  336. try_parse_structdef_nested_type(def,current_structdef,isforwarddef) then
  337. exit;
  338. if not allowunitsym and not (m_delphi in current_settings.modeswitches) and (idtoken=_SPECIALIZE) then
  339. begin
  340. consume(_ID);
  341. is_specialize:=true;
  342. s:=pattern;
  343. sorg:=orgpattern;
  344. pos:=current_tokenpos;
  345. end;
  346. { Use the special searchsym_type that search only types }
  347. if not searchsym_type(s,srsym,srsymtable) then
  348. { for a good error message we need to know whether the symbol really did not exist or
  349. whether we found a non-type one }
  350. not_a_type:=searchsym(s,srsym,srsymtable)
  351. else
  352. not_a_type:=false;
  353. { handle unit specification like System.Writeln }
  354. if allowunitsym then
  355. is_unit_specific:=try_consume_unitsym(srsym,srsymtable,t,[cuf_consume_id,cuf_allow_specialize],is_specialize,s)
  356. else
  357. begin
  358. t:=_ID;
  359. is_unit_specific:=false;
  360. end;
  361. consume(t);
  362. if not_a_type then
  363. begin
  364. { reset the symbol and symtable to not leak any unexpected values }
  365. srsym:=nil;
  366. srsymtable:=nil;
  367. end;
  368. { Types are first defined with an error def before assigning
  369. the real type so check if it's an errordef. if so then
  370. give an error. Only check for typesyms in the current symbol
  371. table as forwarddef are not resolved directly }
  372. if assigned(srsym) and
  373. (srsym.typ=typesym) and
  374. ((ttypesym(srsym).typedef.typ=errordef) or
  375. (not allowgenericsyms and
  376. (ttypesym(srsym).typedef.typ=undefineddef) and
  377. not (sp_generic_para in srsym.symoptions) and
  378. not (sp_explicitrename in srsym.symoptions) and
  379. not assigned(srsym.owner.defowner) and
  380. { use df_generic instead of is_generic to allow aliases in nested types as well }
  381. not (df_generic in tstoreddef(srsym.owner.defowner).defoptions))) then
  382. begin
  383. Message1(type_e_type_is_not_completly_defined,ttypesym(srsym).realname);
  384. def:=generrordef;
  385. exit;
  386. end;
  387. { are we parsing a possible forward def ? }
  388. if isforwarddef and
  389. not(is_unit_specific) then
  390. begin
  391. def:=cforwarddef.create(sorg,pos);
  392. exit;
  393. end;
  394. { unknown sym ? }
  395. if not assigned(srsym) and not not_a_type then
  396. begin
  397. Message1(sym_e_id_not_found,sorg);
  398. def:=generrordef;
  399. exit;
  400. end;
  401. { type sym ? }
  402. if not_a_type or (srsym.typ<>typesym) then
  403. begin
  404. Message(type_e_type_id_expected);
  405. def:=generrordef;
  406. exit;
  407. end;
  408. { Give an error when referring to an errordef }
  409. if (ttypesym(srsym).typedef.typ=errordef) then
  410. begin
  411. Message(sym_e_error_in_type_def);
  412. def:=generrordef;
  413. exit;
  414. end;
  415. { In non-Delphi modes the class/record name of a generic might be used
  416. in the declaration of sub types without type parameters; in that case
  417. we need to check by name as the link from the dummy symbol to the
  418. current type is not yet established }
  419. if (sp_generic_dummy in srsym.symoptions) and
  420. assigned(current_structdef) and
  421. (df_generic in current_structdef.defoptions) and
  422. (ttypesym(srsym).typedef.typ=undefineddef) and
  423. not (m_delphi in current_settings.modeswitches) then
  424. begin
  425. def:=get_generic_in_hierarchy_by_name(srsym,current_structdef);
  426. if assigned(def) then
  427. exit;
  428. end;
  429. def:=ttypesym(srsym).typedef;
  430. end;
  431. procedure single_type(out def:tdef;options:TSingleTypeOptions);
  432. function handle_dummysym(sym:tsym):tdef;
  433. begin
  434. sym:=resolve_generic_dummysym(sym.name);
  435. if assigned(sym) and
  436. not (sp_generic_dummy in sym.symoptions) and
  437. (sym.typ=typesym) then
  438. result:=ttypesym(sym).typedef
  439. else
  440. begin
  441. Message(parser_e_no_generics_as_types);
  442. result:=generrordef;
  443. end;
  444. end;
  445. var
  446. t2 : tdef;
  447. isunitspecific,
  448. isspecialize,
  449. dospecialize,
  450. again : boolean;
  451. srsym : tsym;
  452. srsymtable : tsymtable;
  453. symname : tsymstr;
  454. begin
  455. dospecialize:=false;
  456. isunitspecific:=false;
  457. srsymtable:=nil;
  458. srsym:=nil;
  459. repeat
  460. again:=false;
  461. case token of
  462. _STRING:
  463. string_dec(def,stoAllowTypeDef in options);
  464. _FILE:
  465. begin
  466. consume(_FILE);
  467. if (token=_OF) then
  468. begin
  469. if not(stoAllowTypeDef in options) then
  470. Message(parser_e_no_local_para_def);
  471. consume(_OF);
  472. single_type(t2,[stoAllowTypeDef]);
  473. if is_managed_type(t2) then
  474. Message(parser_e_no_refcounted_typed_file);
  475. def:=cfiledef.createtyped(t2);
  476. end
  477. else
  478. def:=cfiletype;
  479. end;
  480. _ID:
  481. begin
  482. if not (m_delphi in current_settings.modeswitches) and try_to_consume(_SPECIALIZE) then
  483. begin
  484. if ([stoAllowSpecialization,stoAllowTypeDef] * options = []) then
  485. begin
  486. Message(parser_e_no_local_para_def);
  487. { try to recover }
  488. while token<>_SEMICOLON do
  489. consume(token);
  490. def:=generrordef;
  491. end
  492. else
  493. begin
  494. dospecialize:=true;
  495. again:=true;
  496. end;
  497. end
  498. else
  499. begin
  500. id_type(def,stoIsForwardDef in options,true,true,not dospecialize or ([stoAllowSpecialization,stoAllowTypeDef]*options=[]),srsym,srsymtable,isspecialize,isunitspecific);
  501. if isspecialize and dospecialize then
  502. internalerror(2015021301);
  503. if isspecialize then
  504. dospecialize:=true;
  505. parse_nested_types(def,stoIsForwardDef in options,[stoAllowSpecialization,stoAllowTypeDef]*options<>[],nil);
  506. end;
  507. end;
  508. else
  509. begin
  510. message(type_e_type_id_expected);
  511. def:=generrordef;
  512. end;
  513. end;
  514. until not again;
  515. if ([stoAllowSpecialization,stoAllowTypeDef] * options <> []) and
  516. (m_delphi in current_settings.modeswitches) then
  517. dospecialize:=token in [_LSHARPBRACKET,_LT];
  518. if dospecialize and
  519. (def.typ=forwarddef) then
  520. begin
  521. if not assigned(srsym) or not (srsym.typ=typesym) then
  522. begin
  523. Message1(type_e_type_is_not_completly_defined,def.typename);
  524. def:=generrordef;
  525. dospecialize:=false;
  526. end;
  527. end;
  528. { recover from error? }
  529. if def.typ=errordef then
  530. begin
  531. while (token<>_SEMICOLON) and (token<>_RKLAMMER) do
  532. consume(token);
  533. end
  534. else if dospecialize then
  535. begin
  536. if def.typ=forwarddef then
  537. def:=ttypesym(srsym).typedef;
  538. if assigned(srsym) then
  539. symname:=srsym.name
  540. else if assigned(def.typesym) then
  541. begin
  542. symname:=def.typesym.name;
  543. srsymtable:=def.typesym.owner;
  544. end
  545. else if (def.typ=objectdef) then
  546. begin
  547. symname:=tobjectdef(def).objname^;
  548. if assigned(def.owner) then
  549. srsymtable:=def.owner
  550. else
  551. srsymtable:=symtablestack.top;
  552. end
  553. else
  554. symname:='';
  555. generate_specialization(def,isunitspecific,stoParseClassParent in options,'',symname,srsymtable);
  556. parse_nested_types(def,stoIsForwardDef in options,[stoAllowSpecialization,stoAllowTypeDef]*options<>[],nil);
  557. end
  558. else
  559. begin
  560. if assigned(current_specializedef) and (def=current_specializedef.genericdef) then
  561. begin
  562. def:=current_specializedef
  563. end
  564. else if (def=current_genericdef) then
  565. begin
  566. def:=current_genericdef
  567. end
  568. { when parsing a nested specialization in non-Delphi mode it might
  569. use the name of the topmost generic without type paramaters, thus
  570. def will contain the generic definition, but we need a reference
  571. to the specialization of that generic }
  572. { TODO : only in non-Delphi modes? }
  573. else if assigned(current_structdef) and
  574. (df_specialization in current_structdef.defoptions) and
  575. return_specialization_of_generic(current_structdef,def,t2) then
  576. begin
  577. def:=t2
  578. end
  579. else if tstoreddef(def).is_generic and
  580. not
  581. (
  582. parse_generic and
  583. (
  584. { if this is a generic parameter than it has already been checked that this is
  585. a valid usage of a generic }
  586. (sp_generic_para in srsym.symoptions) or
  587. (
  588. (current_genericdef.typ in [recorddef,objectdef]) and
  589. (
  590. { if both defs belong to the same generic (e.g. both are
  591. subtypes) then we must allow the usage }
  592. defs_belong_to_same_generic(def,current_genericdef) or
  593. { this is needed to correctly resolve "type Foo=SomeGeneric<T>"
  594. declarations inside a generic }
  595. sym_is_owned_by(srsym,tabstractrecorddef(current_genericdef).symtable)
  596. )
  597. )
  598. )
  599. )
  600. then
  601. begin
  602. def:=handle_dummysym(srsym);
  603. end
  604. else if (def.typ=undefineddef) and
  605. (sp_generic_dummy in srsym.symoptions) then
  606. begin
  607. if parse_generic and
  608. (current_genericdef.typ in [recorddef,objectdef]) and
  609. (Pos(upper(srsym.realname),tabstractrecorddef(current_genericdef).objname^)=1) then
  610. begin
  611. if m_delphi in current_settings.modeswitches then
  612. begin
  613. def:=handle_dummysym(srsym);
  614. end
  615. else
  616. def:=current_genericdef;
  617. end
  618. else
  619. begin
  620. def:=handle_dummysym(srsym);
  621. end;
  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. function result_type(options:TSingleTypeOptions):tdef;
  632. begin
  633. single_type(result,options);
  634. { file types cannot be function results }
  635. if result.typ=filedef then
  636. message(parser_e_illegal_function_result);
  637. end;
  638. procedure parse_record_members(recsym:tsym);
  639. function IsAnonOrLocal: Boolean;
  640. begin
  641. result:=(current_structdef.objname^='') or
  642. not(symtablestack.stack^.next^.symtable.symtabletype in [globalsymtable,staticsymtable,objectsymtable,recordsymtable]);
  643. end;
  644. var
  645. pd : tprocdef;
  646. oldparse_only: boolean;
  647. member_blocktype : tblock_type;
  648. hadgeneric,
  649. fields_allowed, is_classdef, classfields, threadvarfields: boolean;
  650. vdoptions: tvar_dec_options;
  651. rtti_attrs_def: trtti_attribute_list;
  652. fldCount : Integer;
  653. attr_element_count : Integer;
  654. procedure check_unbound_attributes;
  655. begin
  656. if assigned(rtti_attrs_def) and (rtti_attrs_def.get_attribute_count>0) then
  657. Message1(parser_e_unbound_attribute,trtti_attribute(rtti_attrs_def.rtti_attributes[0]).typesym.prettyname);
  658. rtti_attrs_def.free;
  659. rtti_attrs_def:=nil;
  660. end;
  661. begin
  662. { empty record declaration ? }
  663. if (token=_SEMICOLON) then
  664. Exit;
  665. current_structdef.symtable.currentvisibility:=vis_public;
  666. fields_allowed:=true;
  667. is_classdef:=false;
  668. hadgeneric:=false;
  669. classfields:=false;
  670. threadvarfields:=false;
  671. member_blocktype:=bt_general;
  672. rtti_attrs_def := nil;
  673. repeat
  674. case token of
  675. _TYPE :
  676. begin
  677. check_unbound_attributes;
  678. consume(_TYPE);
  679. member_blocktype:=bt_type;
  680. { local and anonymous records can not have inner types. skip top record symtable }
  681. if IsAnonOrLocal then
  682. Message(parser_e_no_types_in_local_anonymous_records);
  683. end;
  684. _VAR :
  685. begin
  686. check_unbound_attributes;
  687. consume(_VAR);
  688. fields_allowed:=true;
  689. member_blocktype:=bt_general;
  690. classfields:=is_classdef;
  691. threadvarfields:=false;
  692. is_classdef:=false;
  693. end;
  694. _THREADVAR :
  695. begin
  696. check_unbound_attributes;
  697. if not is_classdef then
  698. begin
  699. message(parser_e_threadvar_must_be_class);
  700. { for error recovery we enforce class fields }
  701. is_classdef:=true;
  702. end;
  703. consume(_THREADVAR);
  704. fields_allowed:=true;
  705. member_blocktype:=bt_general;
  706. classfields:=is_classdef;
  707. threadvarfields:=true;
  708. is_classdef:=false;
  709. end;
  710. _CONST:
  711. begin
  712. check_unbound_attributes;
  713. consume(_CONST);
  714. member_blocktype:=bt_const;
  715. { local and anonymous records can not have constants. skip top record symtable }
  716. if IsAnonOrLocal then
  717. Message(parser_e_no_consts_in_local_anonymous_records);
  718. end;
  719. _ID, _CASE, _OPERATOR :
  720. begin
  721. case idtoken of
  722. _PRIVATE :
  723. begin
  724. check_unbound_attributes;
  725. consume(_PRIVATE);
  726. current_structdef.symtable.currentvisibility:=vis_private;
  727. include(current_structdef.objectoptions,oo_has_private);
  728. fields_allowed:=true;
  729. is_classdef:=false;
  730. classfields:=false;
  731. threadvarfields:=false;
  732. member_blocktype:=bt_general;
  733. end;
  734. _PROTECTED :
  735. begin
  736. check_unbound_attributes;
  737. Message1(parser_e_not_allowed_in_record,tokeninfo^[_PROTECTED].str);
  738. consume(_PROTECTED);
  739. current_structdef.symtable.currentvisibility:=vis_protected;
  740. include(current_structdef.objectoptions,oo_has_protected);
  741. fields_allowed:=true;
  742. is_classdef:=false;
  743. classfields:=false;
  744. threadvarfields:=false;
  745. member_blocktype:=bt_general;
  746. end;
  747. _PUBLIC :
  748. begin
  749. check_unbound_attributes;
  750. consume(_PUBLIC);
  751. current_structdef.symtable.currentvisibility:=vis_public;
  752. fields_allowed:=true;
  753. is_classdef:=false;
  754. classfields:=false;
  755. threadvarfields:=false;
  756. member_blocktype:=bt_general;
  757. end;
  758. _PUBLISHED :
  759. begin
  760. check_unbound_attributes;
  761. Message(parser_e_no_record_published);
  762. consume(_PUBLISHED);
  763. current_structdef.symtable.currentvisibility:=vis_published;
  764. fields_allowed:=true;
  765. is_classdef:=false;
  766. classfields:=false;
  767. threadvarfields:=false;
  768. member_blocktype:=bt_general;
  769. end;
  770. _STRICT :
  771. begin
  772. consume(_STRICT);
  773. if token=_ID then
  774. begin
  775. case idtoken of
  776. _PRIVATE:
  777. begin
  778. consume(_PRIVATE);
  779. current_structdef.symtable.currentvisibility:=vis_strictprivate;
  780. include(current_structdef.objectoptions,oo_has_strictprivate);
  781. end;
  782. _PROTECTED:
  783. begin
  784. { "strict protected" is not allowed for records }
  785. Message1(parser_e_not_allowed_in_record,tokeninfo^[_STRICT].str+' '+tokeninfo^[_PROTECTED].str);
  786. consume(_PROTECTED);
  787. current_structdef.symtable.currentvisibility:=vis_strictprotected;
  788. include(current_structdef.objectoptions,oo_has_strictprotected);
  789. end;
  790. else
  791. message(parser_e_protected_or_private_expected);
  792. end;
  793. end
  794. else
  795. message(parser_e_protected_or_private_expected);
  796. fields_allowed:=true;
  797. is_classdef:=false;
  798. classfields:=false;
  799. threadvarfields:=false;
  800. member_blocktype:=bt_general;
  801. end
  802. else
  803. if is_classdef and (idtoken=_OPERATOR) then
  804. begin
  805. check_unbound_attributes;
  806. pd:=parse_record_method_dec(current_structdef,is_classdef,false);
  807. fields_allowed:=false;
  808. is_classdef:=false;
  809. end
  810. else
  811. begin
  812. if member_blocktype=bt_general then
  813. begin
  814. if (idtoken=_GENERIC) and
  815. not (m_delphi in current_settings.modeswitches) and
  816. not fields_allowed then
  817. begin
  818. if hadgeneric then
  819. Message(parser_e_procedure_or_function_expected);
  820. consume(_ID);
  821. hadgeneric:=true;
  822. if not (token in [_PROCEDURE,_FUNCTION,_CLASS]) then
  823. Message(parser_e_procedure_or_function_expected);
  824. end
  825. else
  826. begin
  827. if (not fields_allowed)and(idtoken<>_CASE) then
  828. Message(parser_e_field_not_allowed_here);
  829. vdoptions:=[vd_record];
  830. if classfields then
  831. include(vdoptions,vd_class);
  832. if not (m_delphi in current_settings.modeswitches) then
  833. include(vdoptions,vd_check_generic);
  834. if threadvarfields then
  835. include(vdoptions,vd_threadvar);
  836. fldCount:=current_structdef.symtable.SymList.Count;
  837. read_record_fields(vdoptions,nil,nil,hadgeneric,attr_element_count);
  838. {
  839. attr_element_count returns the number of fields to which the attribute must be applied.
  840. For
  841. [someattr]
  842. a : integer;
  843. b : integer;
  844. attr_element_count returns 1. For
  845. [someattr]
  846. a, b : integer;
  847. it returns 2.
  848. Basically the number of variables before the first colon.
  849. }
  850. if assigned(rtti_attrs_def) then
  851. begin
  852. While (attr_element_count>1) do
  853. begin
  854. trtti_attribute_list.copyandbind(rtti_attrs_def,(current_structdef.symtable.SymList[fldCount] as tfieldvarsym).rtti_attribute_list);
  855. inc(fldcount);
  856. dec(attr_element_count);
  857. end;
  858. if fldCount<current_structdef.symtable.SymList.Count then
  859. trtti_attribute_list.bind(rtti_attrs_def,(current_structdef.symtable.SymList[fldCount] as tfieldvarsym).rtti_attribute_list);
  860. end;
  861. end;
  862. end
  863. else if member_blocktype=bt_type then
  864. types_dec(true,hadgeneric, rtti_attrs_def)
  865. else if member_blocktype=bt_const then
  866. consts_dec(true,true,hadgeneric)
  867. else
  868. internalerror(201001110);
  869. end;
  870. end;
  871. end;
  872. _PROPERTY :
  873. begin
  874. if IsAnonOrLocal then
  875. Message(parser_e_no_properties_in_local_anonymous_records);
  876. struct_property_dec(is_classdef, rtti_attrs_def);
  877. fields_allowed:=false;
  878. is_classdef:=false;
  879. end;
  880. _CLASS:
  881. begin
  882. check_unbound_attributes;
  883. is_classdef:=false;
  884. { read class method/field/property }
  885. consume(_CLASS);
  886. { class modifier is only allowed for procedures, functions, }
  887. { constructors, destructors, fields and properties }
  888. if (hadgeneric and not (token in [_FUNCTION,_PROCEDURE])) or
  889. (not hadgeneric and (not ((token in [_FUNCTION,_PROCEDURE,_PROPERTY,_VAR,_DESTRUCTOR,_OPERATOR,_THREADVAR]) or (token=_CONSTRUCTOR)) and
  890. not((token=_ID) and (idtoken=_OPERATOR)))) then
  891. Message(parser_e_procedure_or_function_expected);
  892. if IsAnonOrLocal then
  893. Message(parser_e_no_class_in_local_anonymous_records);
  894. is_classdef:=true;
  895. end;
  896. _PROCEDURE,
  897. _FUNCTION:
  898. begin
  899. if IsAnonOrLocal then
  900. Message(parser_e_no_methods_in_local_anonymous_records);
  901. pd:=parse_record_method_dec(current_structdef,is_classdef,hadgeneric);
  902. if assigned(rtti_attrs_def) then
  903. begin
  904. trtti_attribute_list.bind(rtti_attrs_def,pd.rtti_attribute_list);
  905. rtti_attrs_def:=Nil;
  906. end;
  907. hadgeneric:=false;
  908. fields_allowed:=false;
  909. is_classdef:=false;
  910. end;
  911. _CONSTRUCTOR :
  912. begin
  913. check_unbound_attributes;
  914. if IsAnonOrLocal then
  915. Message(parser_e_no_methods_in_local_anonymous_records);
  916. if not is_classdef and (current_structdef.symtable.currentvisibility <> vis_public) then
  917. Message(parser_w_constructor_should_be_public);
  918. { only 1 class constructor is allowed }
  919. if is_classdef and (oo_has_class_constructor in current_structdef.objectoptions) then
  920. Message1(parser_e_only_one_class_constructor_allowed, current_structdef.objrealname^);
  921. oldparse_only:=parse_only;
  922. parse_only:=true;
  923. if is_classdef then
  924. pd:=class_constructor_head(current_structdef)
  925. else
  926. begin
  927. pd:=constructor_head;
  928. if pd.minparacount = 0 then
  929. MessagePos(pd.procsym.fileinfo,parser_e_no_parameterless_constructor_in_records);
  930. end;
  931. parse_only:=oldparse_only;
  932. fields_allowed:=false;
  933. is_classdef:=false;
  934. end;
  935. _DESTRUCTOR :
  936. begin
  937. check_unbound_attributes;
  938. if IsAnonOrLocal then
  939. Message(parser_e_no_methods_in_local_anonymous_records);
  940. if not is_classdef then
  941. Message(parser_e_no_destructor_in_records);
  942. { only 1 class destructor is allowed }
  943. if is_classdef and (oo_has_class_destructor in current_structdef.objectoptions) then
  944. Message1(parser_e_only_one_class_destructor_allowed, current_structdef.objrealname^);
  945. oldparse_only:=parse_only;
  946. parse_only:=true;
  947. if is_classdef then
  948. pd:=class_destructor_head(current_structdef)
  949. else
  950. pd:=destructor_head;
  951. parse_only:=oldparse_only;
  952. fields_allowed:=false;
  953. is_classdef:=false;
  954. end;
  955. _LECKKLAMMER:
  956. begin
  957. if m_prefixed_attributes in current_settings.modeswitches then
  958. parse_rttiattributes(rtti_attrs_def)
  959. else
  960. consume(_ID);
  961. end;
  962. _END :
  963. begin
  964. {$ifdef jvm}
  965. add_java_default_record_methods_intf(trecorddef(current_structdef));
  966. {$endif}
  967. if target_info.system in systems_typed_constants_node_init then
  968. add_typedconst_init_routine(current_structdef);
  969. consume(_END);
  970. break;
  971. end;
  972. else
  973. consume(_ID); { Give a ident expected message, like tp7 }
  974. end;
  975. until false;
  976. end;
  977. { reads a record declaration }
  978. function record_dec(const n:tidstring;recsym:tsym;genericdef:tstoreddef;genericlist:tfphashobjectlist):tdef;
  979. var
  980. olddef : tdef;
  981. procedure set_typesym;
  982. begin
  983. if not assigned(recsym) then
  984. exit;
  985. if ttypesym(recsym).typedef=current_structdef then
  986. exit;
  987. ttypesym(recsym).typedef:=current_structdef;
  988. current_structdef.typesym:=recsym;
  989. end;
  990. procedure reset_typesym;
  991. begin
  992. if not assigned(recsym) then
  993. exit;
  994. if ttypesym(recsym).typedef<>current_structdef then
  995. exit;
  996. ttypesym(recsym).typedef:=olddef;
  997. current_structdef.typesym:=nil;
  998. end;
  999. var
  1000. old_current_structdef: tabstractrecorddef;
  1001. old_current_genericdef,
  1002. old_current_specializedef: tstoreddef;
  1003. old_parse_generic: boolean;
  1004. recst: trecordsymtable;
  1005. hadgendummy : boolean;
  1006. alignment: Integer;
  1007. dummyattrelcount : Integer;
  1008. begin
  1009. old_current_structdef:=current_structdef;
  1010. old_current_genericdef:=current_genericdef;
  1011. old_current_specializedef:=current_specializedef;
  1012. old_parse_generic:=parse_generic;
  1013. current_genericdef:=nil;
  1014. current_specializedef:=nil;
  1015. { create recdef }
  1016. if (n<>'') or
  1017. not(target_info.system in systems_jvm) then
  1018. begin
  1019. recst:=trecordsymtable.create(n,current_settings.packrecords,current_settings.alignment.recordalignmin);
  1020. { can't use recst.realname^ instead of n, because recst.realname is
  1021. nil in case of an empty name }
  1022. current_structdef:=crecorddef.create(n,recst);
  1023. end
  1024. else
  1025. begin
  1026. { for the JVM target records always need a name, because they are
  1027. represented by a class }
  1028. recst:=trecordsymtable.create(current_module.realmodulename^+'__fpc_intern_recname_'+tostr(current_module.deflist.count),
  1029. current_settings.packrecords,current_settings.alignment.recordalignmin);
  1030. current_structdef:=crecorddef.create(recst.name^,recst);
  1031. end;
  1032. result:=current_structdef;
  1033. { insert in symtablestack }
  1034. symtablestack.push(recst);
  1035. { usage of specialized type inside its generic template }
  1036. if assigned(genericdef) then
  1037. current_specializedef:=current_structdef
  1038. { reject declaration of generic class inside generic class }
  1039. else if assigned(genericlist) then
  1040. current_genericdef:=current_structdef;
  1041. { nested types of specializations are specializations as well }
  1042. if assigned(old_current_structdef) and
  1043. (df_specialization in old_current_structdef.defoptions) then
  1044. include(current_structdef.defoptions,df_specialization);
  1045. if assigned(old_current_structdef) and
  1046. (df_generic in old_current_structdef.defoptions) then
  1047. include(current_structdef.defoptions,df_generic);
  1048. insert_generic_parameter_types(current_structdef,genericdef,genericlist,false);
  1049. { when we are parsing a generic already then this is a generic as
  1050. well }
  1051. if old_parse_generic then
  1052. include(current_structdef.defoptions, df_generic);
  1053. parse_generic:=(df_generic in current_structdef.defoptions);
  1054. if parse_generic and not assigned(current_genericdef) then
  1055. current_genericdef:=current_structdef;
  1056. { in non-Delphi modes we need a strict private symbol without type
  1057. count and type parameters in the name to simply resolving }
  1058. maybe_insert_generic_rename_symbol(n,genericlist);
  1059. { apply $RTTI directive to current object }
  1060. current_structdef.apply_rtti_directive(current_module.rtti_directive);
  1061. { the correct typesym<->def relationship is needed for example when
  1062. parsing parameters that are specializations of the record, when
  1063. using nested constants and such or when specializing the type
  1064. itself as a pointer type }
  1065. if assigned(recsym) then
  1066. olddef:=ttypesym(recsym).typedef
  1067. else
  1068. olddef:=nil;
  1069. set_typesym;
  1070. if m_advanced_records in current_settings.modeswitches then
  1071. begin
  1072. parse_record_members(recsym);
  1073. end
  1074. else
  1075. begin
  1076. read_record_fields([vd_record],nil,nil,hadgendummy,dummyattrelcount);
  1077. {$ifdef jvm}
  1078. { we need a constructor to create temps, a deep copy helper, ... }
  1079. add_java_default_record_methods_intf(trecorddef(current_structdef));
  1080. {$endif}
  1081. if target_info.system in systems_typed_constants_node_init then
  1082. add_typedconst_init_routine(current_structdef);
  1083. consume(_END);
  1084. end;
  1085. reset_typesym;
  1086. if (token=_ID) and (pattern='ALIGN') then
  1087. begin
  1088. consume(_ID);
  1089. alignment:=get_intconst.svalue;
  1090. { "(alignment and not $7F) = 0" means it's between 0 and 127, and
  1091. PopCnt = 1 for powers of 2 }
  1092. if ((alignment and not $7F) <> 0) or (PopCnt(Byte(alignment))<>1) then
  1093. message(scanner_e_illegal_alignment_directive)
  1094. else
  1095. recst.recordalignment:=shortint(alignment);
  1096. end;
  1097. { make the record size aligned (has to be done before inserting the
  1098. parameters, because that may depend on the record's size) }
  1099. recst.addalignmentpadding;
  1100. { don't keep track of procdefs in a separate list, because the
  1101. compiler may add additional procdefs (e.g. property wrappers for
  1102. the jvm backend) }
  1103. insert_struct_hidden_paras(trecorddef(current_structdef));
  1104. { restore symtable stack }
  1105. symtablestack.pop(recst);
  1106. if trecorddef(current_structdef).is_packed and is_managed_type(current_structdef) then
  1107. Message(type_e_no_packed_inittable);
  1108. { restore old state }
  1109. parse_generic:=old_parse_generic;
  1110. current_structdef:=old_current_structdef;
  1111. current_genericdef:=old_current_genericdef;
  1112. current_specializedef:=old_current_specializedef;
  1113. end;
  1114. { reads a type definition and returns a pointer to it }
  1115. procedure read_named_type(var def:tdef;const newsym:tsym;genericdef:tstoreddef;genericlist:tfphashobjectlist;parseprocvardir:boolean;var hadtypetoken:boolean);
  1116. const
  1117. SingleTypeOptionsInTypeBlock:array[Boolean] of TSingleTypeOptions = ([],[stoIsForwardDef]);
  1118. var
  1119. pt : tnode;
  1120. tt2 : tdef;
  1121. aktenumdef : tenumdef;
  1122. s : TIDString;
  1123. l,v : TConstExprInt;
  1124. oldpackrecords : longint;
  1125. defpos,storepos : tfileposinfo;
  1126. name: TIDString;
  1127. procedure expr_type;
  1128. var
  1129. pt1,pt2 : tnode;
  1130. lv,hv : TConstExprInt;
  1131. old_block_type : tblock_type;
  1132. dospecialize : boolean;
  1133. newdef : tdef;
  1134. sym : tsym;
  1135. genstr : string;
  1136. gencount : longint;
  1137. begin
  1138. old_block_type:=block_type;
  1139. dospecialize:=false;
  1140. { use of current parsed object:
  1141. classes, objects, records can be used also in themself }
  1142. if (token=_ID) then
  1143. if try_parse_structdef_nested_type(def,current_structdef,false) then
  1144. exit;
  1145. { we can't accept a equal in type }
  1146. pt1:=comp_expr([ef_type_only]);
  1147. if try_to_consume(_POINTPOINT) then
  1148. begin
  1149. { get high value of range }
  1150. pt2:=comp_expr([]);
  1151. { make both the same type or give an error. This is not
  1152. done when both are integer values, because typecasting
  1153. between -3200..3200 will result in a signed-unsigned
  1154. conflict and give a range check error (PFV) }
  1155. if not(is_integer(pt1.resultdef) and is_integer(pt2.resultdef)) then
  1156. inserttypeconv(pt1,pt2.resultdef);
  1157. { both must be evaluated to constants now }
  1158. if (pt1.nodetype=ordconstn) and
  1159. (pt2.nodetype=ordconstn) then
  1160. begin
  1161. lv:=tordconstnode(pt1).value;
  1162. hv:=tordconstnode(pt2).value;
  1163. { Check bounds }
  1164. if hv<lv then
  1165. message(parser_e_upper_lower_than_lower)
  1166. else if (lv.signed and (lv.svalue<0)) and (not hv.signed and (hv.uvalue>qword(high(int64)))) then
  1167. message(type_e_cant_eval_constant_expr)
  1168. else
  1169. begin
  1170. { All checks passed, create the new def }
  1171. case pt1.resultdef.typ of
  1172. enumdef :
  1173. def:=cenumdef.create_subrange(tenumdef(pt1.resultdef),lv.svalue,hv.svalue);
  1174. orddef :
  1175. begin
  1176. if is_char(pt1.resultdef) then
  1177. def:=corddef.create(uchar,lv,hv,true)
  1178. else
  1179. if is_boolean(pt1.resultdef) then
  1180. def:=corddef.create(pasbool1,lv,hv,true)
  1181. else if is_signed(pt1.resultdef) then
  1182. def:=corddef.create(range_to_basetype(lv,hv),lv,hv,true)
  1183. else
  1184. def:=corddef.create(range_to_basetype(lv,hv),lv,hv,true);
  1185. end;
  1186. else
  1187. internalerror(2019050527);
  1188. end;
  1189. end;
  1190. end
  1191. else
  1192. Message(sym_e_error_in_type_def);
  1193. pt2.free;
  1194. end
  1195. else
  1196. begin
  1197. { a simple type renaming or generic specialization }
  1198. if (pt1.nodetype=typen) then
  1199. begin
  1200. def:=ttypenode(pt1).resultdef;
  1201. { Delphi mode specialization? }
  1202. if (m_delphi in current_settings.modeswitches) then
  1203. dospecialize:=token=_LSHARPBRACKET
  1204. else
  1205. begin
  1206. dospecialize:=false;
  1207. { in non-Delphi modes we might get a inline specialization
  1208. without "specialize" or "<T>" of the same type we're
  1209. currently parsing, so we need to handle that special }
  1210. newdef:=nil;
  1211. end;
  1212. if not dospecialize and
  1213. assigned(ttypenode(pt1).typesym) and
  1214. (ttypenode(pt1).typesym.typ=typesym) and
  1215. (sp_generic_dummy in ttypenode(pt1).typesym.symoptions) and
  1216. assigned(current_structdef) and
  1217. (
  1218. (
  1219. not (m_delphi in current_settings.modeswitches) and
  1220. (ttypesym(ttypenode(pt1).typesym).typedef.typ=undefineddef) and
  1221. (df_generic in current_structdef.defoptions) and
  1222. (ttypesym(ttypenode(pt1).typesym).typedef.owner=current_structdef.owner) and
  1223. (upper(ttypenode(pt1).typesym.realname)=copy(current_structdef.objname^,1,pos('$',current_structdef.objname^)-1))
  1224. ) or (
  1225. { this could be a nested specialization which uses
  1226. the type name of a surrounding generic to
  1227. reference the specialization of said surrounding
  1228. class }
  1229. (df_specialization in current_structdef.defoptions) and
  1230. return_specialization_of_generic(current_structdef,ttypesym(ttypenode(pt1).typesym).typedef,newdef)
  1231. )
  1232. )
  1233. then
  1234. begin
  1235. if assigned(newdef) then
  1236. def:=newdef
  1237. else
  1238. def:=current_structdef;
  1239. if assigned(def) then
  1240. { handle nested types }
  1241. post_comp_expr_gendef(def)
  1242. else
  1243. def:=generrordef;
  1244. end;
  1245. if dospecialize then
  1246. begin
  1247. if not assigned(ttypenode(pt1).typesym) then
  1248. internalerror(2025103102);
  1249. generate_specialization(def,false,false,name,ttypenode(pt1).typesym.name,ttypenode(pt1).typesym.owner);
  1250. { handle nested types }
  1251. if assigned(def) then
  1252. post_comp_expr_gendef(def);
  1253. end
  1254. else
  1255. begin
  1256. if assigned(current_specializedef) and (def=current_specializedef.genericdef) then
  1257. begin
  1258. def:=current_specializedef
  1259. end
  1260. else if (def=current_genericdef) then
  1261. begin
  1262. def:=current_genericdef
  1263. end
  1264. else if tstoreddef(def).is_generic and
  1265. { TODO : check once nested generics are allowed }
  1266. not
  1267. (
  1268. parse_generic and
  1269. (current_genericdef.typ in [recorddef,objectdef]) and
  1270. (def.typ in [recorddef,objectdef]) and
  1271. (
  1272. { if both defs belong to the same generic (e.g. both are
  1273. subtypes) then we must allow the usage }
  1274. defs_belong_to_same_generic(def,current_genericdef) or
  1275. { this is needed to correctly resolve "type Foo=SomeGeneric<T>"
  1276. declarations inside a generic }
  1277. (
  1278. (ttypenode(pt1).typesym<>nil) and
  1279. sym_is_owned_by(ttypenode(pt1).typesym,tabstractrecorddef(current_genericdef).symtable)
  1280. )
  1281. )
  1282. )
  1283. then
  1284. begin
  1285. if assigned(def.typesym) then
  1286. begin
  1287. if ttypesym(def.typesym).typedef.typ<>undefineddef then
  1288. { non-Delphi modes... }
  1289. split_generic_name(def.typesym.name,genstr,gencount)
  1290. else
  1291. genstr:=def.typesym.name;
  1292. sym:=resolve_generic_dummysym(genstr);
  1293. end
  1294. else
  1295. sym:=nil;
  1296. if assigned(sym) and
  1297. not (sp_generic_dummy in sym.symoptions) and
  1298. (sym.typ=typesym) then
  1299. def:=ttypesym(sym).typedef
  1300. else
  1301. begin
  1302. Message(parser_e_no_generics_as_types);
  1303. def:=generrordef;
  1304. end;
  1305. end
  1306. else if is_classhelper(def) then
  1307. begin
  1308. Message(parser_e_no_category_as_types);
  1309. def:=generrordef
  1310. end
  1311. end;
  1312. end
  1313. else
  1314. Message(sym_e_error_in_type_def);
  1315. end;
  1316. pt1.free;
  1317. block_type:=old_block_type;
  1318. end;
  1319. procedure set_dec;
  1320. begin
  1321. consume(_SET);
  1322. consume(_OF);
  1323. read_anon_type(tt2,true,nil);
  1324. if assigned(tt2) then
  1325. begin
  1326. case tt2.typ of
  1327. { don't forget that min can be negativ PM }
  1328. enumdef :
  1329. if (tenumdef(tt2).min>=0) and
  1330. (tenumdef(tt2).max<=255) then
  1331. // !! def:=csetdef.create(tt2,tenumdef(tt2.def).min,tenumdef(tt2.def).max),true)
  1332. def:=csetdef.create(tt2,tenumdef(tt2).min,tenumdef(tt2).max,true)
  1333. else
  1334. Message(sym_e_ill_type_decl_set);
  1335. orddef :
  1336. begin
  1337. if (torddef(tt2).ordtype=uwidechar) then
  1338. begin
  1339. if (m_default_unicodestring in current_settings.modeswitches) then
  1340. begin
  1341. Message(parser_w_widechar_set_reduced);
  1342. def:=csetdef.create(cansichartype,torddef(cansichartype).low.svalue,torddef(cansichartype).high.svalue,true);
  1343. end
  1344. else
  1345. Message(sym_e_ill_type_decl_set);
  1346. end
  1347. else if (torddef(tt2).ordtype<>uvoid) and
  1348. (torddef(tt2).low>=0) then
  1349. // !! def:=csetdef.create(tt2,torddef(tt2.def).low,torddef(tt2.def).high),true)
  1350. if Torddef(tt2).high>int64(high(byte)) then
  1351. message(sym_e_ill_type_decl_set)
  1352. else
  1353. def:=csetdef.create(tt2,torddef(tt2).low.svalue,torddef(tt2).high.svalue,true)
  1354. else
  1355. Message(sym_e_ill_type_decl_set);
  1356. end;
  1357. { generic parameter? }
  1358. undefineddef:
  1359. ;
  1360. else
  1361. Message(sym_e_ill_type_decl_set);
  1362. end;
  1363. end
  1364. else
  1365. def:=generrordef;
  1366. end;
  1367. procedure pointer_dec;
  1368. var
  1369. sym: tsym;
  1370. begin
  1371. consume(_CARET);
  1372. single_type(tt2,
  1373. SingleTypeOptionsInTypeBlock[block_type=bt_type]+[stoAllowSpecialization]
  1374. );
  1375. { in case of e.g. var or const sections we need to especially
  1376. check that we don't use a generic dummy symbol }
  1377. if (block_type<>bt_type) and
  1378. (tt2.typ=undefineddef) and
  1379. assigned(tt2.typesym) and
  1380. (sp_generic_dummy in tt2.typesym.symoptions) then
  1381. begin
  1382. sym:=resolve_generic_dummysym(tt2.typesym.name);
  1383. if assigned(sym) and
  1384. not (sp_generic_dummy in sym.symoptions) and
  1385. (sym.typ=typesym) then
  1386. tt2:=ttypesym(sym).typedef
  1387. else
  1388. Message(parser_e_no_generics_as_types);
  1389. end;
  1390. { don't use cpointerdef.getreusable() here, since this is a type
  1391. declaration (-> must create new typedef) }
  1392. def:=cpointerdef.create(tt2);
  1393. if tt2.typ=forwarddef then
  1394. current_module.checkforwarddefs.add(def);
  1395. end;
  1396. procedure array_dec(is_packed:boolean;genericdef:tstoreddef;genericlist:tfphashobjectlist);
  1397. var
  1398. isgeneric : boolean;
  1399. lowval,
  1400. highval : TConstExprInt;
  1401. indexdef : tdef;
  1402. hdef : tdef;
  1403. arrdef : tarraydef;
  1404. procedure setdefdecl(def:tdef);
  1405. begin
  1406. case def.typ of
  1407. enumdef :
  1408. begin
  1409. lowval:=tenumdef(def).min;
  1410. highval:=tenumdef(def).max;
  1411. if (m_fpc in current_settings.modeswitches) and
  1412. (tenumdef(def).has_jumps) then
  1413. Message(type_e_array_index_enums_with_assign_not_possible);
  1414. indexdef:=def;
  1415. end;
  1416. orddef :
  1417. begin
  1418. if torddef(def).ordtype in [uchar,
  1419. u8bit,
  1420. s8bit,s16bit,
  1421. {$if defined(cpu32bitaddr) or defined(cpu64bitaddr)}
  1422. u16bit,s32bit,
  1423. {$endif defined(cpu32bitaddr) or defined(cpu64bitaddr)}
  1424. {$ifdef cpu64bitaddr}
  1425. u32bit,s64bit,
  1426. {$endif cpu64bitaddr}
  1427. pasbool1,pasbool8,pasbool16,pasbool32,pasbool64,
  1428. bool8bit,bool16bit,bool32bit,bool64bit,
  1429. uwidechar] then
  1430. begin
  1431. lowval:=torddef(def).low;
  1432. highval:=torddef(def).high;
  1433. indexdef:=def;
  1434. end
  1435. else
  1436. Message1(parser_e_type_cant_be_used_in_array_index,def.typename);
  1437. end;
  1438. { generic parameter? }
  1439. undefineddef:
  1440. begin
  1441. lowval:=0;
  1442. highval:=1;
  1443. indexdef:=def;
  1444. isgeneric:=true;
  1445. end;
  1446. else
  1447. Message(sym_e_error_in_type_def);
  1448. end;
  1449. end;
  1450. var
  1451. old_current_genericdef,
  1452. old_current_specializedef: tstoreddef;
  1453. first,
  1454. old_parse_generic: boolean;
  1455. begin
  1456. old_current_genericdef:=current_genericdef;
  1457. old_current_specializedef:=current_specializedef;
  1458. old_parse_generic:=parse_generic;
  1459. current_genericdef:=nil;
  1460. current_specializedef:=nil;
  1461. first:=true;
  1462. arrdef:=carraydef.create(0,0,s32inttype);
  1463. consume(_ARRAY);
  1464. { usage of specialized type inside its generic template }
  1465. if assigned(genericdef) then
  1466. current_specializedef:=arrdef
  1467. { reject declaration of generic class inside generic class }
  1468. else if assigned(genericlist) then
  1469. current_genericdef:=arrdef;
  1470. symtablestack.push(arrdef.symtable);
  1471. insert_generic_parameter_types(arrdef,genericdef,genericlist,false);
  1472. { there are two possibilties for the following to be true:
  1473. * the array declaration itself is generic
  1474. * the array is declared inside a generic
  1475. in both cases we need "parse_generic" and "current_genericdef"
  1476. so that e.g. specializations of another generic inside the
  1477. current generic can be used (either inline ones or "type" ones) }
  1478. if old_parse_generic then
  1479. include(arrdef.defoptions,df_generic);
  1480. parse_generic:=(df_generic in arrdef.defoptions);
  1481. if parse_generic and not assigned(current_genericdef) then
  1482. current_genericdef:=old_current_genericdef;
  1483. { open array? }
  1484. if try_to_consume(_LECKKLAMMER) then
  1485. begin
  1486. { defaults }
  1487. indexdef:=generrordef;
  1488. isgeneric:=false;
  1489. { use defaults which don't overflow the compiler }
  1490. lowval:=0;
  1491. highval:=0;
  1492. repeat
  1493. { read the expression and check it, check apart if the
  1494. declaration is an enum declaration because that needs to
  1495. be parsed by readtype (PFV) }
  1496. if token=_LKLAMMER then
  1497. begin
  1498. read_anon_type(hdef,true,nil);
  1499. setdefdecl(hdef);
  1500. end
  1501. else
  1502. begin
  1503. pt:=expr(true);
  1504. isgeneric:=false;
  1505. if pt.nodetype=typen then
  1506. setdefdecl(pt.resultdef)
  1507. else
  1508. begin
  1509. if pt.nodetype=rangen then
  1510. begin
  1511. if nf_generic_para in pt.flags then
  1512. isgeneric:=true;
  1513. { pure ordconstn expressions can be checked for
  1514. generics as well, but don't give an error in case
  1515. of parsing a generic if that isn't yet the case }
  1516. if (trangenode(pt).left.nodetype=ordconstn) and
  1517. (trangenode(pt).right.nodetype=ordconstn) then
  1518. begin
  1519. { make both the same type or give an error. This is not
  1520. done when both are integer values, because typecasting
  1521. between -3200..3200 will result in a signed-unsigned
  1522. conflict and give a range check error (PFV) }
  1523. if not(is_integer(trangenode(pt).left.resultdef) and is_integer(trangenode(pt).left.resultdef)) then
  1524. inserttypeconv(trangenode(pt).left,trangenode(pt).right.resultdef);
  1525. lowval:=tordconstnode(trangenode(pt).left).value;
  1526. highval:=tordconstnode(trangenode(pt).right).value;
  1527. if highval<lowval then
  1528. begin
  1529. { ignore error if node is generic param }
  1530. if not (nf_generic_para in pt.flags) then
  1531. Message(parser_e_array_lower_less_than_upper_bound);
  1532. highval:=lowval;
  1533. end
  1534. else if (lowval<int64(low(asizeint))) or
  1535. (highval>high(asizeint)) then
  1536. begin
  1537. Message(parser_e_array_range_out_of_bounds);
  1538. lowval :=0;
  1539. highval:=0;
  1540. end;
  1541. if is_integer(trangenode(pt).left.resultdef) then
  1542. range_to_type(lowval,highval,indexdef)
  1543. else
  1544. indexdef:=trangenode(pt).left.resultdef;
  1545. end
  1546. else
  1547. if not parse_generic then
  1548. Message(type_e_cant_eval_constant_expr)
  1549. else
  1550. { we need a valid range for debug information }
  1551. range_to_type(lowval,highval,indexdef);
  1552. end
  1553. else
  1554. Message(sym_e_error_in_type_def)
  1555. end;
  1556. pt.free;
  1557. end;
  1558. { if we are not at the first dimension, add the new arrray
  1559. as element of the existing array, otherwise modify the existing array }
  1560. if not(first) then
  1561. begin
  1562. arrdef.elementdef:=carraydef.create(lowval.svalue,highval.svalue,indexdef);
  1563. { push new symtable }
  1564. symtablestack.pop(arrdef.symtable);
  1565. arrdef:=tarraydef(arrdef.elementdef);
  1566. symtablestack.push(arrdef.symtable);
  1567. { correctly update the generic information of the new array def }
  1568. insert_generic_parameter_types(arrdef,genericdef,genericlist,false);
  1569. if old_parse_generic then
  1570. include(arrdef.defoptions,df_generic);
  1571. end
  1572. else
  1573. begin
  1574. arrdef.lowrange:=lowval.svalue;
  1575. arrdef.highrange:=highval.svalue;
  1576. arrdef.rangedef:=indexdef;
  1577. def:=arrdef;
  1578. first:=false;
  1579. end;
  1580. if is_packed then
  1581. include(arrdef.arrayoptions,ado_IsBitPacked);
  1582. if isgeneric then
  1583. include(arrdef.arrayoptions,ado_IsGeneric);
  1584. if token=_COMMA then
  1585. consume(_COMMA)
  1586. else
  1587. break;
  1588. until false;
  1589. consume(_RECKKLAMMER);
  1590. end
  1591. else
  1592. begin
  1593. if is_packed then
  1594. Message(parser_e_packed_dynamic_open_array);
  1595. arrdef.lowrange:=0;
  1596. arrdef.highrange:=-1;
  1597. arrdef.rangedef:=sizesinttype;
  1598. include(arrdef.arrayoptions,ado_IsDynamicArray);
  1599. def:=arrdef;
  1600. end;
  1601. consume(_OF);
  1602. read_anon_type(tt2,true,nil);
  1603. { set element type of the last array definition }
  1604. if assigned(arrdef) then
  1605. begin
  1606. symtablestack.pop(arrdef.symtable);
  1607. arrdef.elementdef:=tt2;
  1608. if is_packed and
  1609. is_managed_type(tt2) then
  1610. Message(type_e_no_packed_inittable);
  1611. end;
  1612. { restore old state }
  1613. parse_generic:=old_parse_generic;
  1614. current_genericdef:=old_current_genericdef;
  1615. current_specializedef:=old_current_specializedef;
  1616. end;
  1617. function procvar_dec(genericdef:tstoreddef;genericlist:tfphashobjectlist;sym:tsym;doregister:boolean):tdef;
  1618. var
  1619. is_func:boolean;
  1620. pd:tprocvardef;
  1621. old_current_genericdef,
  1622. old_current_specializedef: tstoreddef;
  1623. old_parse_generic: boolean;
  1624. olddef : tdef;
  1625. begin
  1626. old_current_genericdef:=current_genericdef;
  1627. old_current_specializedef:=current_specializedef;
  1628. old_parse_generic:=parse_generic;
  1629. current_genericdef:=nil;
  1630. current_specializedef:=nil;
  1631. olddef:=nil;
  1632. is_func:=(token=_FUNCTION);
  1633. if token in [_FUNCTION,_PROCEDURE] then
  1634. consume(token)
  1635. else
  1636. consume(_FUNCTION);
  1637. pd:=cprocvardef.create(normal_function_level,doregister);
  1638. if assigned(sym) then
  1639. begin
  1640. pd.typesym:=sym;
  1641. olddef:=ttypesym(sym).typedef;
  1642. ttypesym(sym).typedef:=pd;
  1643. end;
  1644. { usage of specialized type inside its generic template }
  1645. if assigned(genericdef) then
  1646. current_specializedef:=pd
  1647. { reject declaration of generic class inside generic class }
  1648. else if assigned(genericlist) then
  1649. current_genericdef:=pd;
  1650. symtablestack.push(pd.parast);
  1651. insert_generic_parameter_types(pd,genericdef,genericlist,false);
  1652. { there are two possibilties for the following to be true:
  1653. * the procvar declaration itself is generic
  1654. * the procvar is declared inside a generic
  1655. in both cases we need "parse_generic" and "current_genericdef"
  1656. so that e.g. specializations of another generic inside the
  1657. current generic can be used (either inline ones or "type" ones) }
  1658. if old_parse_generic then
  1659. include(pd.defoptions,df_generic);
  1660. parse_generic:=(df_generic in pd.defoptions);
  1661. if parse_generic and not assigned(current_genericdef) then
  1662. current_genericdef:=old_current_genericdef;
  1663. if token=_LKLAMMER then
  1664. parse_parameter_dec(pd);
  1665. if is_func then
  1666. begin
  1667. consume(_COLON);
  1668. pd.proctypeoption:=potype_function;
  1669. pd.returndef:=result_type([stoAllowSpecialization]);
  1670. end
  1671. else
  1672. pd.proctypeoption:=potype_procedure;
  1673. if try_to_consume(_OF) then
  1674. begin
  1675. consume(_OBJECT);
  1676. include(pd.procoptions,po_methodpointer);
  1677. end
  1678. else if (m_nested_procvars in current_settings.modeswitches) and
  1679. try_to_consume(_IS) then
  1680. begin
  1681. consume(_NESTED);
  1682. pd.parast.symtablelevel:=normal_function_level+1;
  1683. pd.check_mark_as_nested;
  1684. end;
  1685. symtablestack.pop(pd.parast);
  1686. { possible proc directives }
  1687. if parseprocvardir then
  1688. begin
  1689. if check_proc_directive(true) then
  1690. parse_proctype_directives(pd);
  1691. { Add implicit hidden parameters and function result }
  1692. handle_calling_convention(pd,hcc_default_actions_intf);
  1693. end;
  1694. { restore old state }
  1695. parse_generic:=old_parse_generic;
  1696. current_genericdef:=old_current_genericdef;
  1697. current_specializedef:=old_current_specializedef;
  1698. if assigned(sym) then
  1699. begin
  1700. pd.typesym:=nil;
  1701. ttypesym(sym).typedef:=olddef;
  1702. end;
  1703. result:=pd;
  1704. end;
  1705. var
  1706. p : tnode;
  1707. hdef : tdef;
  1708. enumdupmsg, first, is_specialize : boolean;
  1709. oldlocalswitches : tlocalswitches;
  1710. bitpacking: boolean;
  1711. stitem: psymtablestackitem;
  1712. sym: tsym;
  1713. st: tsymtable;
  1714. begin
  1715. def:=nil;
  1716. v:=0;
  1717. l:=0;
  1718. if assigned(newsym) then
  1719. name:=newsym.RealName
  1720. else
  1721. name:='';
  1722. { type a = type ..,; syntax is allowed only with type syms and apparently helpers, see below }
  1723. if hadtypetoken and
  1724. (
  1725. (token<>_ID) or
  1726. (
  1727. (m_function_references in current_settings.modeswitches) and
  1728. (idtoken=_REFERENCE)
  1729. )
  1730. ) and
  1731. (token<>_STRING) and (token<>_FILE) then
  1732. consume(_ID);
  1733. case token of
  1734. _STRING,_FILE:
  1735. begin
  1736. if hadtypetoken then
  1737. single_type(def,[])
  1738. else
  1739. single_type(def,[stoAllowTypeDef]);
  1740. end;
  1741. _LKLAMMER:
  1742. begin
  1743. consume(_LKLAMMER);
  1744. first:=true;
  1745. { allow negativ value_str }
  1746. l:=int64(-1);
  1747. enumdupmsg:=false;
  1748. { check that we are not adding an enum from specialization
  1749. we can't just use current_specializedef because of inner types
  1750. like specialize array of record }
  1751. is_specialize:=false;
  1752. stitem:=symtablestack.stack;
  1753. while assigned(stitem) do
  1754. begin
  1755. { check records, classes and arrays because they can be specialized }
  1756. if stitem^.symtable.symtabletype in [recordsymtable,ObjectSymtable,arraysymtable] then
  1757. begin
  1758. is_specialize:=is_specialize or (df_specialization in tstoreddef(stitem^.symtable.defowner).defoptions);
  1759. stitem:=stitem^.next;
  1760. end
  1761. else
  1762. break;
  1763. end;
  1764. if not is_specialize then
  1765. aktenumdef:=cenumdef.create
  1766. else
  1767. aktenumdef:=nil;
  1768. repeat
  1769. { if it is a specialization then search the first enum member
  1770. and get the member owner instead of just created enumdef }
  1771. if not assigned(aktenumdef) then
  1772. begin
  1773. if not searchsym(pattern,sym,st) then
  1774. internalerror(202504121)
  1775. else if sym.typ=enumsym then
  1776. aktenumdef:=tenumsym(sym).definition
  1777. else
  1778. internalerror(201101021);
  1779. end;
  1780. s:=orgpattern;
  1781. defpos:=current_tokenpos;
  1782. consume(_ID);
  1783. { only allow assigning of specific numbers under fpc mode }
  1784. if not(m_tp7 in current_settings.modeswitches) and
  1785. (
  1786. { in fpc mode also allow := to be compatible
  1787. with previous 1.0.x versions }
  1788. ((m_fpc in current_settings.modeswitches) and
  1789. try_to_consume(_ASSIGNMENT)) or
  1790. try_to_consume(_EQ)
  1791. ) then
  1792. begin
  1793. oldlocalswitches:=current_settings.localswitches;
  1794. include(current_settings.localswitches,cs_allow_enum_calc);
  1795. p:=comp_expr([ef_accept_equal]);
  1796. current_settings.localswitches:=oldlocalswitches;
  1797. if (p.nodetype=ordconstn) then
  1798. begin
  1799. { we expect an integer or an enum of the
  1800. same type }
  1801. if is_integer(p.resultdef) or
  1802. is_char(p.resultdef) or
  1803. equal_defs(p.resultdef,aktenumdef) then
  1804. v:=tordconstnode(p).value
  1805. else
  1806. IncompatibleTypes(p.resultdef,s32inttype);
  1807. end
  1808. else
  1809. Message(parser_e_illegal_expression);
  1810. p.free;
  1811. { please leave that a note, allows type save }
  1812. { declarations in the win32 units ! }
  1813. if (not first) and (v<=l) and (not enumdupmsg) then
  1814. begin
  1815. Message(parser_n_duplicate_enum);
  1816. enumdupmsg:=true;
  1817. end;
  1818. l:=v;
  1819. end
  1820. else
  1821. inc(l.svalue);
  1822. first:=false;
  1823. { don't generate enum members if this is a specialization because aktenumdef is copied from the generic type }
  1824. if not is_specialize then
  1825. begin
  1826. storepos:=current_tokenpos;
  1827. current_tokenpos:=defpos;
  1828. if (l.svalue<low(longint)) or (l.svalue>high(longint)) then
  1829. if m_delphi in current_settings.modeswitches then
  1830. Message(parser_w_enumeration_out_of_range)
  1831. else
  1832. Message(parser_e_enumeration_out_of_range);
  1833. tenumsymtable(aktenumdef.symtable).insertsym(cenumsym.create(s,aktenumdef,longint(l.svalue)));
  1834. if not (cs_scopedenums in current_settings.localswitches) or
  1835. { also provide the global symbol for anonymous enums }
  1836. not assigned(newsym) then
  1837. tstoredsymtable(aktenumdef.owner).insertsym(cenumsym.create(s,aktenumdef,longint(l.svalue)));
  1838. current_tokenpos:=storepos;
  1839. end;
  1840. until not try_to_consume(_COMMA);
  1841. def:=aktenumdef;
  1842. consume(_RKLAMMER);
  1843. {$ifdef jvm}
  1844. jvm_maybe_create_enum_class(name,def);
  1845. {$endif}
  1846. end;
  1847. _ARRAY:
  1848. array_dec(false,genericdef,genericlist);
  1849. _SET:
  1850. set_dec;
  1851. _CARET:
  1852. pointer_dec;
  1853. _RECORD:
  1854. begin
  1855. consume(token);
  1856. if (idtoken=_HELPER) and (m_advanced_records in current_settings.modeswitches) then
  1857. begin
  1858. consume(_HELPER);
  1859. def:=object_dec(odt_helper,name,newsym,genericdef,genericlist,nil,ht_record);
  1860. end
  1861. else
  1862. def:=record_dec(name,newsym,genericdef,genericlist);
  1863. end;
  1864. _PACKED,
  1865. _BITPACKED:
  1866. begin
  1867. bitpacking :=
  1868. (cs_bitpacking in current_settings.localswitches) or
  1869. (token = _BITPACKED);
  1870. consume(token);
  1871. if token=_ARRAY then
  1872. array_dec(bitpacking,genericdef,genericlist)
  1873. else if token=_SET then
  1874. set_dec
  1875. else if token=_FILE then
  1876. single_type(def,[stoAllowTypeDef])
  1877. else
  1878. begin
  1879. oldpackrecords:=current_settings.packrecords;
  1880. if (not bitpacking) or
  1881. (token in [_CLASS,_OBJECT]) then
  1882. current_settings.packrecords:=1
  1883. else
  1884. current_settings.packrecords:=bit_alignment;
  1885. case token of
  1886. _CLASS :
  1887. begin
  1888. consume(_CLASS);
  1889. def:=object_dec(odt_class,name,newsym,genericdef,genericlist,nil,ht_none);
  1890. end;
  1891. _OBJECT :
  1892. begin
  1893. consume(_OBJECT);
  1894. def:=object_dec(odt_object,name,newsym,genericdef,genericlist,nil,ht_none);
  1895. end;
  1896. else begin
  1897. consume(_RECORD);
  1898. def:=record_dec(name,newsym,genericdef,genericlist);
  1899. end;
  1900. end;
  1901. current_settings.packrecords:=oldpackrecords;
  1902. end;
  1903. end;
  1904. _DISPINTERFACE :
  1905. begin
  1906. { need extra check here since interface is a keyword
  1907. in all pascal modes }
  1908. if not(m_class in current_settings.modeswitches) then
  1909. Message(parser_f_need_objfpc_or_delphi_mode);
  1910. consume(token);
  1911. def:=object_dec(odt_dispinterface,name,newsym,genericdef,genericlist,nil,ht_none);
  1912. end;
  1913. _CLASS :
  1914. begin
  1915. consume(token);
  1916. { Delphi only allows class of in type blocks }
  1917. if (token=_OF) and
  1918. (
  1919. not(m_delphi in current_settings.modeswitches) or
  1920. (block_type=bt_type)
  1921. ) then
  1922. begin
  1923. consume(_OF);
  1924. single_type(hdef,SingleTypeOptionsInTypeBlock[block_type=bt_type]);
  1925. if is_class(hdef) or
  1926. is_objcclass(hdef) or
  1927. is_javaclass(hdef) then
  1928. def:=cclassrefdef.create(hdef)
  1929. else
  1930. if hdef.typ=forwarddef then
  1931. begin
  1932. def:=cclassrefdef.create(hdef);
  1933. current_module.checkforwarddefs.add(def);
  1934. end
  1935. else
  1936. Message1(type_e_class_or_objcclass_type_expected,hdef.typename);
  1937. end
  1938. else
  1939. if (idtoken=_HELPER) then
  1940. begin
  1941. consume(_HELPER);
  1942. def:=object_dec(odt_helper,name,newsym,genericdef,genericlist,nil,ht_class);
  1943. end
  1944. else
  1945. def:=object_dec(default_class_type,name,newsym,genericdef,genericlist,nil,ht_none);
  1946. end;
  1947. _CPPCLASS :
  1948. begin
  1949. consume(token);
  1950. def:=object_dec(odt_cppclass,name,newsym,genericdef,genericlist,nil,ht_none);
  1951. end;
  1952. _OBJCCLASS :
  1953. begin
  1954. if not(m_objectivec1 in current_settings.modeswitches) then
  1955. Message(parser_f_need_objc);
  1956. consume(token);
  1957. def:=object_dec(odt_objcclass,name,newsym,genericdef,genericlist,nil,ht_none);
  1958. end;
  1959. _INTERFACE :
  1960. begin
  1961. { need extra check here since interface is a keyword
  1962. in all pascal modes }
  1963. if not(m_class in current_settings.modeswitches) then
  1964. Message(parser_f_need_objfpc_or_delphi_mode);
  1965. consume(token);
  1966. case current_settings.interfacetype of
  1967. it_interfacecom:
  1968. def:=object_dec(odt_interfacecom,name,newsym,genericdef,genericlist,nil,ht_none);
  1969. it_interfacecorba:
  1970. def:=object_dec(odt_interfacecorba,name,newsym,genericdef,genericlist,nil,ht_none);
  1971. it_interfacejava:
  1972. def:=object_dec(odt_interfacejava,name,newsym,genericdef,genericlist,nil,ht_none);
  1973. end;
  1974. end;
  1975. _OBJCPROTOCOL :
  1976. begin
  1977. if not(m_objectivec1 in current_settings.modeswitches) then
  1978. Message(parser_f_need_objc);
  1979. consume(token);
  1980. def:=object_dec(odt_objcprotocol,name,newsym,genericdef,genericlist,nil,ht_none);
  1981. end;
  1982. _OBJCCATEGORY :
  1983. begin
  1984. if not(m_objectivec1 in current_settings.modeswitches) then
  1985. Message(parser_f_need_objc);
  1986. consume(token);
  1987. def:=object_dec(odt_objccategory,name,newsym,genericdef,genericlist,nil,ht_none);
  1988. end;
  1989. _OBJECT :
  1990. begin
  1991. consume(token);
  1992. def:=object_dec(odt_object,name,newsym,genericdef,genericlist,nil,ht_none);
  1993. end;
  1994. _PROCEDURE,
  1995. _FUNCTION:
  1996. begin
  1997. def:=procvar_dec(genericdef,genericlist,nil,true);
  1998. {$ifdef jvm}
  1999. jvm_create_procvar_class(name,def);
  2000. {$endif}
  2001. end;
  2002. _ID:
  2003. begin
  2004. case idtoken of
  2005. _HELPER:
  2006. begin
  2007. if hadtypetoken and
  2008. (m_type_helpers in current_settings.modeswitches) then
  2009. begin
  2010. { reset hadtypetoken, so that calling code knows that it should not be handled
  2011. as a "unique" type }
  2012. hadtypetoken:=false;
  2013. consume(_HELPER);
  2014. def:=object_dec(odt_helper,name,newsym,genericdef,genericlist,nil,ht_type);
  2015. end
  2016. else
  2017. expr_type
  2018. end;
  2019. _REFERENCE:
  2020. begin
  2021. if current_settings.modeswitches*[m_blocks,m_function_references]<>[] then
  2022. begin
  2023. consume(_REFERENCE);
  2024. consume(_TO);
  2025. { don't register the def as a non-cblock function
  2026. reference will be converted to an interface }
  2027. def:=procvar_dec(genericdef,genericlist,newsym,false);
  2028. { could be errordef in case of a syntax error }
  2029. if assigned(def) and
  2030. (def.typ=procvardef) then
  2031. begin
  2032. include(tprocvardef(def).procoptions,po_is_function_ref);
  2033. end;
  2034. end
  2035. else
  2036. expr_type;
  2037. end;
  2038. else
  2039. expr_type;
  2040. end;
  2041. end
  2042. else
  2043. if (token=_KLAMMERAFFE) and (([m_iso,m_extpas]*current_settings.modeswitches)<>[]) then
  2044. begin
  2045. consume(_KLAMMERAFFE);
  2046. single_type(tt2,SingleTypeOptionsInTypeBlock[block_type=bt_type]);
  2047. def:=cpointerdef.create(tt2);
  2048. if tt2.typ=forwarddef then
  2049. current_module.checkforwarddefs.add(def);
  2050. end
  2051. else
  2052. expr_type;
  2053. end;
  2054. if def=nil then
  2055. def:=generrordef;
  2056. end;
  2057. procedure read_anon_type(var def : tdef;parseprocvardir:boolean;genericdef:tstoreddef);
  2058. var
  2059. hadtypetoken : boolean;
  2060. begin
  2061. hadtypetoken:=false;
  2062. read_named_type(def,nil,genericdef,nil,parseprocvardir,hadtypetoken);
  2063. end;
  2064. procedure add_typedconst_init_routine(def: tabstractrecorddef);
  2065. var
  2066. sstate: tscannerstate;
  2067. pd: tprocdef;
  2068. begin
  2069. replace_scanner('tcinit_routine',sstate);
  2070. { the typed constant initialization code is called from the class
  2071. constructor by tnodeutils.wrap_proc_body; at this point, we don't
  2072. know yet whether that will be necessary, because there may be
  2073. typed constants inside method bodies -> always force the addition
  2074. of a class constructor.
  2075. We cannot directly add the typed constant initialisations to the
  2076. class constructor, because when it's parsed not all method bodies
  2077. are necessarily already parsed }
  2078. pd:=def.find_procdef_bytype(potype_class_constructor);
  2079. { the class constructor }
  2080. if not assigned(pd) then
  2081. begin
  2082. if str_parse_method_dec('constructor fpc_init_typed_consts_class_constructor;',potype_class_constructor,true,def,pd) then
  2083. pd.synthetickind:=tsk_empty
  2084. else
  2085. internalerror(2011040206);
  2086. end;
  2087. { the initialisation helper }
  2088. if str_parse_method_dec('procedure fpc_init_typed_consts_helper; static;',potype_procedure,true,def,pd) then
  2089. pd.synthetickind:=tsk_tcinit
  2090. else
  2091. internalerror(2011040207);
  2092. restore_scanner(sstate);
  2093. end;
  2094. end.