ptype.pas 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091
  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);
  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,'');
  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. begin
  454. dospecialize:=false;
  455. isunitspecific:=false;
  456. srsym:=nil;
  457. repeat
  458. again:=false;
  459. case token of
  460. _STRING:
  461. begin
  462. string_dec(def,stoAllowTypeDef in options);
  463. end;
  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. generate_specialization(def,isunitspecific,stoParseClassParent in options,'');
  539. parse_nested_types(def,stoIsForwardDef in options,[stoAllowSpecialization,stoAllowTypeDef]*options<>[],nil);
  540. end
  541. else
  542. begin
  543. if assigned(current_specializedef) and (def=current_specializedef.genericdef) then
  544. begin
  545. def:=current_specializedef
  546. end
  547. else if (def=current_genericdef) then
  548. begin
  549. def:=current_genericdef
  550. end
  551. { when parsing a nested specialization in non-Delphi mode it might
  552. use the name of the topmost generic without type paramaters, thus
  553. def will contain the generic definition, but we need a reference
  554. to the specialization of that generic }
  555. { TODO : only in non-Delphi modes? }
  556. else if assigned(current_structdef) and
  557. (df_specialization in current_structdef.defoptions) and
  558. return_specialization_of_generic(current_structdef,def,t2) then
  559. begin
  560. def:=t2
  561. end
  562. else if tstoreddef(def).is_generic and
  563. not
  564. (
  565. parse_generic and
  566. (
  567. { if this is a generic parameter than it has already been checked that this is
  568. a valid usage of a generic }
  569. (sp_generic_para in srsym.symoptions) or
  570. (
  571. (current_genericdef.typ in [recorddef,objectdef]) and
  572. (
  573. { if both defs belong to the same generic (e.g. both are
  574. subtypes) then we must allow the usage }
  575. defs_belong_to_same_generic(def,current_genericdef) or
  576. { this is needed to correctly resolve "type Foo=SomeGeneric<T>"
  577. declarations inside a generic }
  578. sym_is_owned_by(srsym,tabstractrecorddef(current_genericdef).symtable)
  579. )
  580. )
  581. )
  582. )
  583. then
  584. begin
  585. def:=handle_dummysym(srsym);
  586. end
  587. else if (def.typ=undefineddef) and
  588. (sp_generic_dummy in srsym.symoptions) then
  589. begin
  590. if parse_generic and
  591. (current_genericdef.typ in [recorddef,objectdef]) and
  592. (Pos(upper(srsym.realname),tabstractrecorddef(current_genericdef).objname^)=1) then
  593. begin
  594. if m_delphi in current_settings.modeswitches then
  595. begin
  596. def:=handle_dummysym(srsym);
  597. end
  598. else
  599. def:=current_genericdef;
  600. end
  601. else
  602. begin
  603. def:=handle_dummysym(srsym);
  604. end;
  605. end
  606. else if is_classhelper(def) and
  607. not (stoParseClassParent in options) then
  608. begin
  609. Message(parser_e_no_category_as_types);
  610. def:=generrordef
  611. end
  612. end;
  613. end;
  614. function result_type(options:TSingleTypeOptions):tdef;
  615. begin
  616. single_type(result,options);
  617. { file types cannot be function results }
  618. if result.typ=filedef then
  619. message(parser_e_illegal_function_result);
  620. end;
  621. procedure parse_record_members(recsym:tsym);
  622. function IsAnonOrLocal: Boolean;
  623. begin
  624. result:=(current_structdef.objname^='') or
  625. not(symtablestack.stack^.next^.symtable.symtabletype in [globalsymtable,staticsymtable,objectsymtable,recordsymtable]);
  626. end;
  627. var
  628. olddef : tdef;
  629. procedure set_typesym;
  630. begin
  631. if not assigned(recsym) then
  632. exit;
  633. if ttypesym(recsym).typedef=current_structdef then
  634. exit;
  635. ttypesym(recsym).typedef:=current_structdef;
  636. current_structdef.typesym:=recsym;
  637. end;
  638. procedure reset_typesym;
  639. begin
  640. if not assigned(recsym) then
  641. exit;
  642. if ttypesym(recsym).typedef<>current_structdef then
  643. exit;
  644. ttypesym(recsym).typedef:=olddef;
  645. current_structdef.typesym:=nil;
  646. end;
  647. var
  648. pd : tprocdef;
  649. oldparse_only: boolean;
  650. member_blocktype : tblock_type;
  651. hadgeneric,
  652. fields_allowed, is_classdef, classfields, threadvarfields: boolean;
  653. vdoptions: tvar_dec_options;
  654. rtti_attrs_def: trtti_attribute_list;
  655. begin
  656. { empty record declaration ? }
  657. if (token=_SEMICOLON) then
  658. Exit;
  659. { the correct typesym<->def relationship is needed for example when
  660. parsing parameters that are specializations of the record or when
  661. using nested constants and such }
  662. if assigned(recsym) then
  663. olddef:=ttypesym(recsym).typedef
  664. else
  665. olddef:=nil;
  666. set_typesym;
  667. current_structdef.symtable.currentvisibility:=vis_public;
  668. fields_allowed:=true;
  669. is_classdef:=false;
  670. hadgeneric:=false;
  671. classfields:=false;
  672. threadvarfields:=false;
  673. member_blocktype:=bt_general;
  674. rtti_attrs_def := nil;
  675. repeat
  676. case token of
  677. _TYPE :
  678. begin
  679. consume(_TYPE);
  680. member_blocktype:=bt_type;
  681. { local and anonymous records can not have inner types. skip top record symtable }
  682. if IsAnonOrLocal then
  683. Message(parser_e_no_types_in_local_anonymous_records);
  684. end;
  685. _VAR :
  686. begin
  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. if not is_classdef then
  697. begin
  698. message(parser_e_threadvar_must_be_class);
  699. { for error recovery we enforce class fields }
  700. is_classdef:=true;
  701. end;
  702. consume(_THREADVAR);
  703. fields_allowed:=true;
  704. member_blocktype:=bt_general;
  705. classfields:=is_classdef;
  706. threadvarfields:=true;
  707. is_classdef:=false;
  708. end;
  709. _CONST:
  710. begin
  711. consume(_CONST);
  712. member_blocktype:=bt_const;
  713. { local and anonymous records can not have constants. skip top record symtable }
  714. if IsAnonOrLocal then
  715. Message(parser_e_no_consts_in_local_anonymous_records);
  716. end;
  717. _ID, _CASE, _OPERATOR :
  718. begin
  719. case idtoken of
  720. _PRIVATE :
  721. begin
  722. consume(_PRIVATE);
  723. current_structdef.symtable.currentvisibility:=vis_private;
  724. include(current_structdef.objectoptions,oo_has_private);
  725. fields_allowed:=true;
  726. is_classdef:=false;
  727. classfields:=false;
  728. threadvarfields:=false;
  729. member_blocktype:=bt_general;
  730. end;
  731. _PROTECTED :
  732. begin
  733. Message1(parser_e_not_allowed_in_record,tokeninfo^[_PROTECTED].str);
  734. consume(_PROTECTED);
  735. current_structdef.symtable.currentvisibility:=vis_protected;
  736. include(current_structdef.objectoptions,oo_has_protected);
  737. fields_allowed:=true;
  738. is_classdef:=false;
  739. classfields:=false;
  740. threadvarfields:=false;
  741. member_blocktype:=bt_general;
  742. end;
  743. _PUBLIC :
  744. begin
  745. consume(_PUBLIC);
  746. current_structdef.symtable.currentvisibility:=vis_public;
  747. fields_allowed:=true;
  748. is_classdef:=false;
  749. classfields:=false;
  750. threadvarfields:=false;
  751. member_blocktype:=bt_general;
  752. end;
  753. _PUBLISHED :
  754. begin
  755. Message(parser_e_no_record_published);
  756. consume(_PUBLISHED);
  757. current_structdef.symtable.currentvisibility:=vis_published;
  758. fields_allowed:=true;
  759. is_classdef:=false;
  760. classfields:=false;
  761. threadvarfields:=false;
  762. member_blocktype:=bt_general;
  763. end;
  764. _STRICT :
  765. begin
  766. consume(_STRICT);
  767. if token=_ID then
  768. begin
  769. case idtoken of
  770. _PRIVATE:
  771. begin
  772. consume(_PRIVATE);
  773. current_structdef.symtable.currentvisibility:=vis_strictprivate;
  774. include(current_structdef.objectoptions,oo_has_strictprivate);
  775. end;
  776. _PROTECTED:
  777. begin
  778. { "strict protected" is not allowed for records }
  779. Message1(parser_e_not_allowed_in_record,tokeninfo^[_STRICT].str+' '+tokeninfo^[_PROTECTED].str);
  780. consume(_PROTECTED);
  781. current_structdef.symtable.currentvisibility:=vis_strictprotected;
  782. include(current_structdef.objectoptions,oo_has_strictprotected);
  783. end;
  784. else
  785. message(parser_e_protected_or_private_expected);
  786. end;
  787. end
  788. else
  789. message(parser_e_protected_or_private_expected);
  790. fields_allowed:=true;
  791. is_classdef:=false;
  792. classfields:=false;
  793. threadvarfields:=false;
  794. member_blocktype:=bt_general;
  795. end
  796. else
  797. if is_classdef and (idtoken=_OPERATOR) then
  798. begin
  799. pd:=parse_record_method_dec(current_structdef,is_classdef,false);
  800. fields_allowed:=false;
  801. is_classdef:=false;
  802. end
  803. else
  804. begin
  805. if member_blocktype=bt_general then
  806. begin
  807. if (idtoken=_GENERIC) and
  808. not (m_delphi in current_settings.modeswitches) and
  809. not fields_allowed then
  810. begin
  811. if hadgeneric then
  812. Message(parser_e_procedure_or_function_expected);
  813. consume(_ID);
  814. hadgeneric:=true;
  815. if not (token in [_PROCEDURE,_FUNCTION,_CLASS]) then
  816. Message(parser_e_procedure_or_function_expected);
  817. end
  818. else
  819. begin
  820. if (not fields_allowed)and(idtoken<>_CASE) then
  821. Message(parser_e_field_not_allowed_here);
  822. vdoptions:=[vd_record];
  823. if classfields then
  824. include(vdoptions,vd_class);
  825. if not (m_delphi in current_settings.modeswitches) then
  826. include(vdoptions,vd_check_generic);
  827. if threadvarfields then
  828. include(vdoptions,vd_threadvar);
  829. read_record_fields(vdoptions,nil,nil,hadgeneric);
  830. end;
  831. end
  832. else if member_blocktype=bt_type then
  833. types_dec(true,hadgeneric, rtti_attrs_def)
  834. else if member_blocktype=bt_const then
  835. consts_dec(true,true,hadgeneric)
  836. else
  837. internalerror(201001110);
  838. end;
  839. end;
  840. end;
  841. _PROPERTY :
  842. begin
  843. if IsAnonOrLocal then
  844. Message(parser_e_no_properties_in_local_anonymous_records);
  845. struct_property_dec(is_classdef, rtti_attrs_def);
  846. fields_allowed:=false;
  847. is_classdef:=false;
  848. end;
  849. _CLASS:
  850. begin
  851. is_classdef:=false;
  852. { read class method/field/property }
  853. consume(_CLASS);
  854. { class modifier is only allowed for procedures, functions, }
  855. { constructors, destructors, fields and properties }
  856. if (hadgeneric and not (token in [_FUNCTION,_PROCEDURE])) or
  857. (not hadgeneric and (not ((token in [_FUNCTION,_PROCEDURE,_PROPERTY,_VAR,_DESTRUCTOR,_OPERATOR,_THREADVAR]) or (token=_CONSTRUCTOR)) and
  858. not((token=_ID) and (idtoken=_OPERATOR)))) then
  859. Message(parser_e_procedure_or_function_expected);
  860. if IsAnonOrLocal then
  861. Message(parser_e_no_class_in_local_anonymous_records);
  862. is_classdef:=true;
  863. end;
  864. _PROCEDURE,
  865. _FUNCTION:
  866. begin
  867. if IsAnonOrLocal then
  868. Message(parser_e_no_methods_in_local_anonymous_records);
  869. pd:=parse_record_method_dec(current_structdef,is_classdef,hadgeneric);
  870. hadgeneric:=false;
  871. fields_allowed:=false;
  872. is_classdef:=false;
  873. end;
  874. _CONSTRUCTOR :
  875. begin
  876. if IsAnonOrLocal then
  877. Message(parser_e_no_methods_in_local_anonymous_records);
  878. if not is_classdef and (current_structdef.symtable.currentvisibility <> vis_public) then
  879. Message(parser_w_constructor_should_be_public);
  880. { only 1 class constructor is allowed }
  881. if is_classdef and (oo_has_class_constructor in current_structdef.objectoptions) then
  882. Message1(parser_e_only_one_class_constructor_allowed, current_structdef.objrealname^);
  883. oldparse_only:=parse_only;
  884. parse_only:=true;
  885. if is_classdef then
  886. pd:=class_constructor_head(current_structdef)
  887. else
  888. begin
  889. pd:=constructor_head;
  890. if pd.minparacount = 0 then
  891. MessagePos(pd.procsym.fileinfo,parser_e_no_parameterless_constructor_in_records);
  892. end;
  893. parse_only:=oldparse_only;
  894. fields_allowed:=false;
  895. is_classdef:=false;
  896. end;
  897. _DESTRUCTOR :
  898. begin
  899. if IsAnonOrLocal then
  900. Message(parser_e_no_methods_in_local_anonymous_records);
  901. if not is_classdef then
  902. Message(parser_e_no_destructor_in_records);
  903. { only 1 class destructor is allowed }
  904. if is_classdef and (oo_has_class_destructor in current_structdef.objectoptions) then
  905. Message1(parser_e_only_one_class_destructor_allowed, current_structdef.objrealname^);
  906. oldparse_only:=parse_only;
  907. parse_only:=true;
  908. if is_classdef then
  909. pd:=class_destructor_head(current_structdef)
  910. else
  911. pd:=destructor_head;
  912. parse_only:=oldparse_only;
  913. fields_allowed:=false;
  914. is_classdef:=false;
  915. end;
  916. _END :
  917. begin
  918. {$ifdef jvm}
  919. add_java_default_record_methods_intf(trecorddef(current_structdef));
  920. {$endif}
  921. if target_info.system in systems_typed_constants_node_init then
  922. add_typedconst_init_routine(current_structdef);
  923. consume(_END);
  924. break;
  925. end;
  926. else
  927. consume(_ID); { Give a ident expected message, like tp7 }
  928. end;
  929. until false;
  930. reset_typesym;
  931. end;
  932. { reads a record declaration }
  933. function record_dec(const n:tidstring;recsym:tsym;genericdef:tstoreddef;genericlist:tfphashobjectlist):tdef;
  934. var
  935. old_current_structdef: tabstractrecorddef;
  936. old_current_genericdef,
  937. old_current_specializedef: tstoreddef;
  938. old_parse_generic: boolean;
  939. recst: trecordsymtable;
  940. hadgendummy : boolean;
  941. alignment: Integer;
  942. begin
  943. old_current_structdef:=current_structdef;
  944. old_current_genericdef:=current_genericdef;
  945. old_current_specializedef:=current_specializedef;
  946. old_parse_generic:=parse_generic;
  947. current_genericdef:=nil;
  948. current_specializedef:=nil;
  949. { create recdef }
  950. if (n<>'') or
  951. not(target_info.system in systems_jvm) then
  952. begin
  953. recst:=trecordsymtable.create(n,current_settings.packrecords,current_settings.alignment.recordalignmin);
  954. { can't use recst.realname^ instead of n, because recst.realname is
  955. nil in case of an empty name }
  956. current_structdef:=crecorddef.create(n,recst);
  957. end
  958. else
  959. begin
  960. { for the JVM target records always need a name, because they are
  961. represented by a class }
  962. recst:=trecordsymtable.create(current_module.realmodulename^+'__fpc_intern_recname_'+tostr(current_module.deflist.count),
  963. current_settings.packrecords,current_settings.alignment.recordalignmin);
  964. current_structdef:=crecorddef.create(recst.name^,recst);
  965. end;
  966. result:=current_structdef;
  967. { insert in symtablestack }
  968. symtablestack.push(recst);
  969. { usage of specialized type inside its generic template }
  970. if assigned(genericdef) then
  971. current_specializedef:=current_structdef
  972. { reject declaration of generic class inside generic class }
  973. else if assigned(genericlist) then
  974. current_genericdef:=current_structdef;
  975. { nested types of specializations are specializations as well }
  976. if assigned(old_current_structdef) and
  977. (df_specialization in old_current_structdef.defoptions) then
  978. include(current_structdef.defoptions,df_specialization);
  979. if assigned(old_current_structdef) and
  980. (df_generic in old_current_structdef.defoptions) then
  981. include(current_structdef.defoptions,df_generic);
  982. insert_generic_parameter_types(current_structdef,genericdef,genericlist,false);
  983. { when we are parsing a generic already then this is a generic as
  984. well }
  985. if old_parse_generic then
  986. include(current_structdef.defoptions, df_generic);
  987. parse_generic:=(df_generic in current_structdef.defoptions);
  988. if parse_generic and not assigned(current_genericdef) then
  989. current_genericdef:=current_structdef;
  990. { in non-Delphi modes we need a strict private symbol without type
  991. count and type parameters in the name to simply resolving }
  992. maybe_insert_generic_rename_symbol(n,genericlist);
  993. if m_advanced_records in current_settings.modeswitches then
  994. begin
  995. parse_record_members(recsym);
  996. end
  997. else
  998. begin
  999. read_record_fields([vd_record],nil,nil,hadgendummy);
  1000. {$ifdef jvm}
  1001. { we need a constructor to create temps, a deep copy helper, ... }
  1002. add_java_default_record_methods_intf(trecorddef(current_structdef));
  1003. {$endif}
  1004. if target_info.system in systems_typed_constants_node_init then
  1005. add_typedconst_init_routine(current_structdef);
  1006. consume(_END);
  1007. end;
  1008. if (token=_ID) and (pattern='ALIGN') then
  1009. begin
  1010. consume(_ID);
  1011. alignment:=get_intconst.svalue;
  1012. { "(alignment and not $7F) = 0" means it's between 0 and 127, and
  1013. PopCnt = 1 for powers of 2 }
  1014. if ((alignment and not $7F) <> 0) or (PopCnt(Byte(alignment))<>1) then
  1015. message(scanner_e_illegal_alignment_directive)
  1016. else
  1017. recst.recordalignment:=shortint(alignment);
  1018. end;
  1019. { make the record size aligned (has to be done before inserting the
  1020. parameters, because that may depend on the record's size) }
  1021. recst.addalignmentpadding;
  1022. { don't keep track of procdefs in a separate list, because the
  1023. compiler may add additional procdefs (e.g. property wrappers for
  1024. the jvm backend) }
  1025. insert_struct_hidden_paras(trecorddef(current_structdef));
  1026. { restore symtable stack }
  1027. symtablestack.pop(recst);
  1028. if trecorddef(current_structdef).is_packed and is_managed_type(current_structdef) then
  1029. Message(type_e_no_packed_inittable);
  1030. { restore old state }
  1031. parse_generic:=old_parse_generic;
  1032. current_structdef:=old_current_structdef;
  1033. current_genericdef:=old_current_genericdef;
  1034. current_specializedef:=old_current_specializedef;
  1035. end;
  1036. { reads a type definition and returns a pointer to it }
  1037. procedure read_named_type(var def:tdef;const newsym:tsym;genericdef:tstoreddef;genericlist:tfphashobjectlist;parseprocvardir:boolean;var hadtypetoken:boolean);
  1038. var
  1039. pt : tnode;
  1040. tt2 : tdef;
  1041. aktenumdef : tenumdef;
  1042. s : TIDString;
  1043. l,v : TConstExprInt;
  1044. oldpackrecords : longint;
  1045. defpos,storepos : tfileposinfo;
  1046. name: TIDString;
  1047. procedure expr_type;
  1048. var
  1049. pt1,pt2 : tnode;
  1050. lv,hv : TConstExprInt;
  1051. old_block_type : tblock_type;
  1052. dospecialize : boolean;
  1053. newdef : tdef;
  1054. sym : tsym;
  1055. genstr : string;
  1056. gencount : longint;
  1057. begin
  1058. old_block_type:=block_type;
  1059. dospecialize:=false;
  1060. { use of current parsed object:
  1061. classes, objects, records can be used also in themself }
  1062. if (token=_ID) then
  1063. if try_parse_structdef_nested_type(def,current_structdef,false) then
  1064. exit;
  1065. { we can't accept a equal in type }
  1066. pt1:=comp_expr([ef_type_only]);
  1067. if try_to_consume(_POINTPOINT) then
  1068. begin
  1069. { get high value of range }
  1070. pt2:=comp_expr([]);
  1071. { make both the same type or give an error. This is not
  1072. done when both are integer values, because typecasting
  1073. between -3200..3200 will result in a signed-unsigned
  1074. conflict and give a range check error (PFV) }
  1075. if not(is_integer(pt1.resultdef) and is_integer(pt2.resultdef)) then
  1076. inserttypeconv(pt1,pt2.resultdef);
  1077. { both must be evaluated to constants now }
  1078. if (pt1.nodetype=ordconstn) and
  1079. (pt2.nodetype=ordconstn) then
  1080. begin
  1081. lv:=tordconstnode(pt1).value;
  1082. hv:=tordconstnode(pt2).value;
  1083. { Check bounds }
  1084. if hv<lv then
  1085. message(parser_e_upper_lower_than_lower)
  1086. else if (lv.signed and (lv.svalue<0)) and (not hv.signed and (hv.uvalue>qword(high(int64)))) then
  1087. message(type_e_cant_eval_constant_expr)
  1088. else
  1089. begin
  1090. { All checks passed, create the new def }
  1091. case pt1.resultdef.typ of
  1092. enumdef :
  1093. def:=cenumdef.create_subrange(tenumdef(pt1.resultdef),lv.svalue,hv.svalue);
  1094. orddef :
  1095. begin
  1096. if is_char(pt1.resultdef) then
  1097. def:=corddef.create(uchar,lv,hv,true)
  1098. else
  1099. if is_boolean(pt1.resultdef) then
  1100. def:=corddef.create(pasbool1,lv,hv,true)
  1101. else if is_signed(pt1.resultdef) then
  1102. def:=corddef.create(range_to_basetype(lv,hv),lv,hv,true)
  1103. else
  1104. def:=corddef.create(range_to_basetype(lv,hv),lv,hv,true);
  1105. end;
  1106. else
  1107. internalerror(2019050527);
  1108. end;
  1109. end;
  1110. end
  1111. else
  1112. Message(sym_e_error_in_type_def);
  1113. pt2.free;
  1114. end
  1115. else
  1116. begin
  1117. { a simple type renaming or generic specialization }
  1118. if (pt1.nodetype=typen) then
  1119. begin
  1120. def:=ttypenode(pt1).resultdef;
  1121. { Delphi mode specialization? }
  1122. if (m_delphi in current_settings.modeswitches) then
  1123. dospecialize:=token=_LSHARPBRACKET
  1124. else
  1125. begin
  1126. dospecialize:=false;
  1127. { in non-Delphi modes we might get a inline specialization
  1128. without "specialize" or "<T>" of the same type we're
  1129. currently parsing, so we need to handle that special }
  1130. newdef:=nil;
  1131. end;
  1132. if not dospecialize and
  1133. assigned(ttypenode(pt1).typesym) and
  1134. (ttypenode(pt1).typesym.typ=typesym) and
  1135. (sp_generic_dummy in ttypenode(pt1).typesym.symoptions) and
  1136. assigned(current_structdef) and
  1137. (
  1138. (
  1139. not (m_delphi in current_settings.modeswitches) and
  1140. (ttypesym(ttypenode(pt1).typesym).typedef.typ=undefineddef) and
  1141. (df_generic in current_structdef.defoptions) and
  1142. (ttypesym(ttypenode(pt1).typesym).typedef.owner=current_structdef.owner) and
  1143. (upper(ttypenode(pt1).typesym.realname)=copy(current_structdef.objname^,1,pos('$',current_structdef.objname^)-1))
  1144. ) or (
  1145. { this could be a nested specialization which uses
  1146. the type name of a surrounding generic to
  1147. reference the specialization of said surrounding
  1148. class }
  1149. (df_specialization in current_structdef.defoptions) and
  1150. return_specialization_of_generic(current_structdef,ttypesym(ttypenode(pt1).typesym).typedef,newdef)
  1151. )
  1152. )
  1153. then
  1154. begin
  1155. if assigned(newdef) then
  1156. def:=newdef
  1157. else
  1158. def:=current_structdef;
  1159. if assigned(def) then
  1160. { handle nested types }
  1161. post_comp_expr_gendef(def)
  1162. else
  1163. def:=generrordef;
  1164. end;
  1165. if dospecialize then
  1166. begin
  1167. generate_specialization(def,false,false,name);
  1168. { handle nested types }
  1169. if assigned(def) then
  1170. post_comp_expr_gendef(def);
  1171. end
  1172. else
  1173. begin
  1174. if assigned(current_specializedef) and (def=current_specializedef.genericdef) then
  1175. begin
  1176. def:=current_specializedef
  1177. end
  1178. else if (def=current_genericdef) then
  1179. begin
  1180. def:=current_genericdef
  1181. end
  1182. else if tstoreddef(def).is_generic and
  1183. { TODO : check once nested generics are allowed }
  1184. not
  1185. (
  1186. parse_generic and
  1187. (current_genericdef.typ in [recorddef,objectdef]) and
  1188. (def.typ in [recorddef,objectdef]) and
  1189. (
  1190. { if both defs belong to the same generic (e.g. both are
  1191. subtypes) then we must allow the usage }
  1192. defs_belong_to_same_generic(def,current_genericdef) or
  1193. { this is needed to correctly resolve "type Foo=SomeGeneric<T>"
  1194. declarations inside a generic }
  1195. (
  1196. (ttypenode(pt1).typesym<>nil) and
  1197. sym_is_owned_by(ttypenode(pt1).typesym,tabstractrecorddef(current_genericdef).symtable)
  1198. )
  1199. )
  1200. )
  1201. then
  1202. begin
  1203. if assigned(def.typesym) then
  1204. begin
  1205. if ttypesym(def.typesym).typedef.typ<>undefineddef then
  1206. { non-Delphi modes... }
  1207. split_generic_name(def.typesym.name,genstr,gencount)
  1208. else
  1209. genstr:=def.typesym.name;
  1210. sym:=resolve_generic_dummysym(genstr);
  1211. end
  1212. else
  1213. sym:=nil;
  1214. if assigned(sym) and
  1215. not (sp_generic_dummy in sym.symoptions) and
  1216. (sym.typ=typesym) then
  1217. def:=ttypesym(sym).typedef
  1218. else
  1219. begin
  1220. Message(parser_e_no_generics_as_types);
  1221. def:=generrordef;
  1222. end;
  1223. end
  1224. else if is_classhelper(def) then
  1225. begin
  1226. Message(parser_e_no_category_as_types);
  1227. def:=generrordef
  1228. end
  1229. end;
  1230. end
  1231. else
  1232. Message(sym_e_error_in_type_def);
  1233. end;
  1234. pt1.free;
  1235. block_type:=old_block_type;
  1236. end;
  1237. procedure set_dec;
  1238. begin
  1239. consume(_SET);
  1240. consume(_OF);
  1241. read_anon_type(tt2,true);
  1242. if assigned(tt2) then
  1243. begin
  1244. case tt2.typ of
  1245. { don't forget that min can be negativ PM }
  1246. enumdef :
  1247. if (tenumdef(tt2).min>=0) and
  1248. (tenumdef(tt2).max<=255) then
  1249. // !! def:=csetdef.create(tt2,tenumdef(tt2.def).min,tenumdef(tt2.def).max),true)
  1250. def:=csetdef.create(tt2,tenumdef(tt2).min,tenumdef(tt2).max,true)
  1251. else
  1252. Message(sym_e_ill_type_decl_set);
  1253. orddef :
  1254. begin
  1255. if (torddef(tt2).ordtype=uwidechar) then
  1256. begin
  1257. if (m_default_unicodestring in current_settings.modeswitches) then
  1258. begin
  1259. Message(parser_w_widechar_set_reduced);
  1260. def:=csetdef.create(cansichartype,torddef(cansichartype).low.svalue,torddef(cansichartype).high.svalue,true);
  1261. end
  1262. else
  1263. Message(sym_e_ill_type_decl_set);
  1264. end
  1265. else if (torddef(tt2).ordtype<>uvoid) and
  1266. (torddef(tt2).low>=0) then
  1267. // !! def:=csetdef.create(tt2,torddef(tt2.def).low,torddef(tt2.def).high),true)
  1268. if Torddef(tt2).high>int64(high(byte)) then
  1269. message(sym_e_ill_type_decl_set)
  1270. else
  1271. def:=csetdef.create(tt2,torddef(tt2).low.svalue,torddef(tt2).high.svalue,true)
  1272. else
  1273. Message(sym_e_ill_type_decl_set);
  1274. end;
  1275. else
  1276. Message(sym_e_ill_type_decl_set);
  1277. end;
  1278. end
  1279. else
  1280. def:=generrordef;
  1281. end;
  1282. procedure array_dec(is_packed:boolean;genericdef:tstoreddef;genericlist:tfphashobjectlist);
  1283. var
  1284. isgeneric : boolean;
  1285. lowval,
  1286. highval : TConstExprInt;
  1287. indexdef : tdef;
  1288. hdef : tdef;
  1289. arrdef : tarraydef;
  1290. procedure setdefdecl(def:tdef);
  1291. begin
  1292. case def.typ of
  1293. enumdef :
  1294. begin
  1295. lowval:=tenumdef(def).min;
  1296. highval:=tenumdef(def).max;
  1297. if (m_fpc in current_settings.modeswitches) and
  1298. (tenumdef(def).has_jumps) then
  1299. Message(type_e_array_index_enums_with_assign_not_possible);
  1300. indexdef:=def;
  1301. end;
  1302. orddef :
  1303. begin
  1304. if torddef(def).ordtype in [uchar,
  1305. u8bit,
  1306. s8bit,s16bit,
  1307. {$if defined(cpu32bitaddr) or defined(cpu64bitaddr)}
  1308. u16bit,s32bit,
  1309. {$endif defined(cpu32bitaddr) or defined(cpu64bitaddr)}
  1310. {$ifdef cpu64bitaddr}
  1311. u32bit,s64bit,
  1312. {$endif cpu64bitaddr}
  1313. pasbool1,pasbool8,pasbool16,pasbool32,pasbool64,
  1314. bool8bit,bool16bit,bool32bit,bool64bit,
  1315. uwidechar] then
  1316. begin
  1317. lowval:=torddef(def).low;
  1318. highval:=torddef(def).high;
  1319. indexdef:=def;
  1320. end
  1321. else
  1322. Message1(parser_e_type_cant_be_used_in_array_index,def.typename);
  1323. end;
  1324. { generic parameter? }
  1325. undefineddef:
  1326. begin
  1327. lowval:=0;
  1328. highval:=1;
  1329. indexdef:=def;
  1330. isgeneric:=true;
  1331. end;
  1332. else
  1333. Message(sym_e_error_in_type_def);
  1334. end;
  1335. end;
  1336. var
  1337. old_current_genericdef,
  1338. old_current_specializedef: tstoreddef;
  1339. first,
  1340. old_parse_generic: boolean;
  1341. begin
  1342. old_current_genericdef:=current_genericdef;
  1343. old_current_specializedef:=current_specializedef;
  1344. old_parse_generic:=parse_generic;
  1345. current_genericdef:=nil;
  1346. current_specializedef:=nil;
  1347. first:=true;
  1348. arrdef:=carraydef.create(0,0,s32inttype);
  1349. consume(_ARRAY);
  1350. { usage of specialized type inside its generic template }
  1351. if assigned(genericdef) then
  1352. current_specializedef:=arrdef
  1353. { reject declaration of generic class inside generic class }
  1354. else if assigned(genericlist) then
  1355. current_genericdef:=arrdef;
  1356. symtablestack.push(arrdef.symtable);
  1357. insert_generic_parameter_types(arrdef,genericdef,genericlist,false);
  1358. { there are two possibilties for the following to be true:
  1359. * the array declaration itself is generic
  1360. * the array is declared inside a generic
  1361. in both cases we need "parse_generic" and "current_genericdef"
  1362. so that e.g. specializations of another generic inside the
  1363. current generic can be used (either inline ones or "type" ones) }
  1364. if old_parse_generic then
  1365. include(arrdef.defoptions,df_generic);
  1366. parse_generic:=(df_generic in arrdef.defoptions);
  1367. if parse_generic and not assigned(current_genericdef) then
  1368. current_genericdef:=old_current_genericdef;
  1369. { open array? }
  1370. if try_to_consume(_LECKKLAMMER) then
  1371. begin
  1372. { defaults }
  1373. indexdef:=generrordef;
  1374. isgeneric:=false;
  1375. { use defaults which don't overflow the compiler }
  1376. lowval:=0;
  1377. highval:=0;
  1378. repeat
  1379. { read the expression and check it, check apart if the
  1380. declaration is an enum declaration because that needs to
  1381. be parsed by readtype (PFV) }
  1382. if token=_LKLAMMER then
  1383. begin
  1384. read_anon_type(hdef,true);
  1385. setdefdecl(hdef);
  1386. end
  1387. else
  1388. begin
  1389. pt:=expr(true);
  1390. isgeneric:=false;
  1391. if pt.nodetype=typen then
  1392. setdefdecl(pt.resultdef)
  1393. else
  1394. begin
  1395. if pt.nodetype=rangen then
  1396. begin
  1397. if nf_generic_para in pt.flags then
  1398. isgeneric:=true;
  1399. { pure ordconstn expressions can be checked for
  1400. generics as well, but don't give an error in case
  1401. of parsing a generic if that isn't yet the case }
  1402. if (trangenode(pt).left.nodetype=ordconstn) and
  1403. (trangenode(pt).right.nodetype=ordconstn) then
  1404. begin
  1405. { make both the same type or give an error. This is not
  1406. done when both are integer values, because typecasting
  1407. between -3200..3200 will result in a signed-unsigned
  1408. conflict and give a range check error (PFV) }
  1409. if not(is_integer(trangenode(pt).left.resultdef) and is_integer(trangenode(pt).left.resultdef)) then
  1410. inserttypeconv(trangenode(pt).left,trangenode(pt).right.resultdef);
  1411. lowval:=tordconstnode(trangenode(pt).left).value;
  1412. highval:=tordconstnode(trangenode(pt).right).value;
  1413. if highval<lowval then
  1414. begin
  1415. { ignore error if node is generic param }
  1416. if not (nf_generic_para in pt.flags) then
  1417. Message(parser_e_array_lower_less_than_upper_bound);
  1418. highval:=lowval;
  1419. end
  1420. else if (lowval<int64(low(asizeint))) or
  1421. (highval>high(asizeint)) then
  1422. begin
  1423. Message(parser_e_array_range_out_of_bounds);
  1424. lowval :=0;
  1425. highval:=0;
  1426. end;
  1427. if is_integer(trangenode(pt).left.resultdef) then
  1428. range_to_type(lowval,highval,indexdef)
  1429. else
  1430. indexdef:=trangenode(pt).left.resultdef;
  1431. end
  1432. else
  1433. if not parse_generic then
  1434. Message(type_e_cant_eval_constant_expr)
  1435. else
  1436. { we need a valid range for debug information }
  1437. range_to_type(lowval,highval,indexdef);
  1438. end
  1439. else
  1440. Message(sym_e_error_in_type_def)
  1441. end;
  1442. pt.free;
  1443. end;
  1444. { if we are not at the first dimension, add the new arrray
  1445. as element of the existing array, otherwise modify the existing array }
  1446. if not(first) then
  1447. begin
  1448. arrdef.elementdef:=carraydef.create(lowval.svalue,highval.svalue,indexdef);
  1449. { push new symtable }
  1450. symtablestack.pop(arrdef.symtable);
  1451. arrdef:=tarraydef(arrdef.elementdef);
  1452. symtablestack.push(arrdef.symtable);
  1453. end
  1454. else
  1455. begin
  1456. arrdef.lowrange:=lowval.svalue;
  1457. arrdef.highrange:=highval.svalue;
  1458. arrdef.rangedef:=indexdef;
  1459. def:=arrdef;
  1460. first:=false;
  1461. end;
  1462. if is_packed then
  1463. include(arrdef.arrayoptions,ado_IsBitPacked);
  1464. if isgeneric then
  1465. include(arrdef.arrayoptions,ado_IsGeneric);
  1466. if token=_COMMA then
  1467. consume(_COMMA)
  1468. else
  1469. break;
  1470. until false;
  1471. consume(_RECKKLAMMER);
  1472. end
  1473. else
  1474. begin
  1475. if is_packed then
  1476. Message(parser_e_packed_dynamic_open_array);
  1477. arrdef.lowrange:=0;
  1478. arrdef.highrange:=-1;
  1479. arrdef.rangedef:=sizesinttype;
  1480. include(arrdef.arrayoptions,ado_IsDynamicArray);
  1481. def:=arrdef;
  1482. end;
  1483. consume(_OF);
  1484. read_anon_type(tt2,true);
  1485. { set element type of the last array definition }
  1486. if assigned(arrdef) then
  1487. begin
  1488. symtablestack.pop(arrdef.symtable);
  1489. arrdef.elementdef:=tt2;
  1490. if is_packed and
  1491. is_managed_type(tt2) then
  1492. Message(type_e_no_packed_inittable);
  1493. end;
  1494. { restore old state }
  1495. parse_generic:=old_parse_generic;
  1496. current_genericdef:=old_current_genericdef;
  1497. current_specializedef:=old_current_specializedef;
  1498. end;
  1499. function procvar_dec(genericdef:tstoreddef;genericlist:tfphashobjectlist;sym:tsym;doregister:boolean):tdef;
  1500. var
  1501. is_func:boolean;
  1502. pd:tprocvardef;
  1503. old_current_genericdef,
  1504. old_current_specializedef: tstoreddef;
  1505. old_parse_generic: boolean;
  1506. olddef : tdef;
  1507. begin
  1508. old_current_genericdef:=current_genericdef;
  1509. old_current_specializedef:=current_specializedef;
  1510. old_parse_generic:=parse_generic;
  1511. current_genericdef:=nil;
  1512. current_specializedef:=nil;
  1513. olddef:=nil;
  1514. is_func:=(token=_FUNCTION);
  1515. if token in [_FUNCTION,_PROCEDURE] then
  1516. consume(token)
  1517. else
  1518. consume(_FUNCTION);
  1519. pd:=cprocvardef.create(normal_function_level,doregister);
  1520. if assigned(sym) then
  1521. begin
  1522. pd.typesym:=sym;
  1523. olddef:=ttypesym(sym).typedef;
  1524. ttypesym(sym).typedef:=pd;
  1525. end;
  1526. { usage of specialized type inside its generic template }
  1527. if assigned(genericdef) then
  1528. current_specializedef:=pd
  1529. { reject declaration of generic class inside generic class }
  1530. else if assigned(genericlist) then
  1531. current_genericdef:=pd;
  1532. symtablestack.push(pd.parast);
  1533. insert_generic_parameter_types(pd,genericdef,genericlist,false);
  1534. { there are two possibilties for the following to be true:
  1535. * the procvar declaration itself is generic
  1536. * the procvar is declared inside a generic
  1537. in both cases we need "parse_generic" and "current_genericdef"
  1538. so that e.g. specializations of another generic inside the
  1539. current generic can be used (either inline ones or "type" ones) }
  1540. if old_parse_generic then
  1541. include(pd.defoptions,df_generic);
  1542. parse_generic:=(df_generic in pd.defoptions);
  1543. if parse_generic and not assigned(current_genericdef) then
  1544. current_genericdef:=old_current_genericdef;
  1545. if token=_LKLAMMER then
  1546. parse_parameter_dec(pd);
  1547. if is_func then
  1548. begin
  1549. consume(_COLON);
  1550. pd.proctypeoption:=potype_function;
  1551. pd.returndef:=result_type([stoAllowSpecialization]);
  1552. end
  1553. else
  1554. pd.proctypeoption:=potype_procedure;
  1555. if try_to_consume(_OF) then
  1556. begin
  1557. consume(_OBJECT);
  1558. include(pd.procoptions,po_methodpointer);
  1559. end
  1560. else if (m_nested_procvars in current_settings.modeswitches) and
  1561. try_to_consume(_IS) then
  1562. begin
  1563. consume(_NESTED);
  1564. pd.parast.symtablelevel:=normal_function_level+1;
  1565. pd.check_mark_as_nested;
  1566. end;
  1567. symtablestack.pop(pd.parast);
  1568. { possible proc directives }
  1569. if parseprocvardir then
  1570. begin
  1571. if check_proc_directive(true) then
  1572. parse_proctype_directives(pd);
  1573. { Add implicit hidden parameters and function result }
  1574. handle_calling_convention(pd,hcc_default_actions_intf);
  1575. end;
  1576. { restore old state }
  1577. parse_generic:=old_parse_generic;
  1578. current_genericdef:=old_current_genericdef;
  1579. current_specializedef:=old_current_specializedef;
  1580. if assigned(sym) then
  1581. begin
  1582. pd.typesym:=nil;
  1583. ttypesym(sym).typedef:=olddef;
  1584. end;
  1585. result:=pd;
  1586. end;
  1587. const
  1588. SingleTypeOptionsInTypeBlock:array[Boolean] of TSingleTypeOptions = ([],[stoIsForwardDef]);
  1589. var
  1590. p : tnode;
  1591. hdef : tdef;
  1592. enumdupmsg, first, is_specialize : boolean;
  1593. oldlocalswitches : tlocalswitches;
  1594. bitpacking: boolean;
  1595. stitem: psymtablestackitem;
  1596. sym: tsym;
  1597. st: tsymtable;
  1598. begin
  1599. def:=nil;
  1600. v:=0;
  1601. l:=0;
  1602. if assigned(newsym) then
  1603. name:=newsym.RealName
  1604. else
  1605. name:='';
  1606. case token of
  1607. _STRING,_FILE:
  1608. begin
  1609. single_type(def,[stoAllowTypeDef]);
  1610. end;
  1611. _LKLAMMER:
  1612. begin
  1613. consume(_LKLAMMER);
  1614. first:=true;
  1615. { allow negativ value_str }
  1616. l:=int64(-1);
  1617. enumdupmsg:=false;
  1618. { check that we are not adding an enum from specialization
  1619. we can't just use current_specializedef because of inner types
  1620. like specialize array of record }
  1621. is_specialize:=false;
  1622. stitem:=symtablestack.stack;
  1623. while assigned(stitem) do
  1624. begin
  1625. { check records, classes and arrays because they can be specialized }
  1626. if stitem^.symtable.symtabletype in [recordsymtable,ObjectSymtable,arraysymtable] then
  1627. begin
  1628. is_specialize:=is_specialize or (df_specialization in tstoreddef(stitem^.symtable.defowner).defoptions);
  1629. stitem:=stitem^.next;
  1630. end
  1631. else
  1632. break;
  1633. end;
  1634. if not is_specialize then
  1635. aktenumdef:=cenumdef.create
  1636. else
  1637. aktenumdef:=nil;
  1638. repeat
  1639. { if it is a specialization then search the first enum member
  1640. and get the member owner instead of just created enumdef }
  1641. if not assigned(aktenumdef) then
  1642. begin
  1643. searchsym(pattern,sym,st);
  1644. if sym.typ=enumsym then
  1645. aktenumdef:=tenumsym(sym).definition
  1646. else
  1647. internalerror(201101021);
  1648. end;
  1649. s:=orgpattern;
  1650. defpos:=current_tokenpos;
  1651. consume(_ID);
  1652. { only allow assigning of specific numbers under fpc mode }
  1653. if not(m_tp7 in current_settings.modeswitches) and
  1654. (
  1655. { in fpc mode also allow := to be compatible
  1656. with previous 1.0.x versions }
  1657. ((m_fpc in current_settings.modeswitches) and
  1658. try_to_consume(_ASSIGNMENT)) or
  1659. try_to_consume(_EQ)
  1660. ) then
  1661. begin
  1662. oldlocalswitches:=current_settings.localswitches;
  1663. include(current_settings.localswitches,cs_allow_enum_calc);
  1664. p:=comp_expr([ef_accept_equal]);
  1665. current_settings.localswitches:=oldlocalswitches;
  1666. if (p.nodetype=ordconstn) then
  1667. begin
  1668. { we expect an integer or an enum of the
  1669. same type }
  1670. if is_integer(p.resultdef) or
  1671. is_char(p.resultdef) or
  1672. equal_defs(p.resultdef,aktenumdef) then
  1673. v:=tordconstnode(p).value
  1674. else
  1675. IncompatibleTypes(p.resultdef,s32inttype);
  1676. end
  1677. else
  1678. Message(parser_e_illegal_expression);
  1679. p.free;
  1680. { please leave that a note, allows type save }
  1681. { declarations in the win32 units ! }
  1682. if (not first) and (v<=l) and (not enumdupmsg) then
  1683. begin
  1684. Message(parser_n_duplicate_enum);
  1685. enumdupmsg:=true;
  1686. end;
  1687. l:=v;
  1688. end
  1689. else
  1690. inc(l.svalue);
  1691. first:=false;
  1692. { don't generate enum members is this is a specialization because aktenumdef is copied from the generic type }
  1693. if not is_specialize then
  1694. begin
  1695. storepos:=current_tokenpos;
  1696. current_tokenpos:=defpos;
  1697. if (l.svalue<low(longint)) or (l.svalue>high(longint)) then
  1698. if m_delphi in current_settings.modeswitches then
  1699. Message(parser_w_enumeration_out_of_range)
  1700. else
  1701. Message(parser_e_enumeration_out_of_range);
  1702. tenumsymtable(aktenumdef.symtable).insertsym(cenumsym.create(s,aktenumdef,longint(l.svalue)));
  1703. if not (cs_scopedenums in current_settings.localswitches) then
  1704. tstoredsymtable(aktenumdef.owner).insertsym(cenumsym.create(s,aktenumdef,longint(l.svalue)));
  1705. current_tokenpos:=storepos;
  1706. end;
  1707. until not try_to_consume(_COMMA);
  1708. def:=aktenumdef;
  1709. consume(_RKLAMMER);
  1710. {$ifdef jvm}
  1711. jvm_maybe_create_enum_class(name,def);
  1712. {$endif}
  1713. end;
  1714. _ARRAY:
  1715. begin
  1716. array_dec(false,genericdef,genericlist);
  1717. end;
  1718. _SET:
  1719. begin
  1720. set_dec;
  1721. end;
  1722. _CARET:
  1723. begin
  1724. consume(_CARET);
  1725. single_type(tt2,
  1726. SingleTypeOptionsInTypeBlock[block_type=bt_type]+[stoAllowSpecialization]
  1727. );
  1728. { in case of e.g. var or const sections we need to especially
  1729. check that we don't use a generic dummy symbol }
  1730. if (block_type<>bt_type) and
  1731. (tt2.typ=undefineddef) and
  1732. assigned(tt2.typesym) and
  1733. (sp_generic_dummy in tt2.typesym.symoptions) then
  1734. begin
  1735. sym:=resolve_generic_dummysym(tt2.typesym.name);
  1736. if assigned(sym) and
  1737. not (sp_generic_dummy in sym.symoptions) and
  1738. (sym.typ=typesym) then
  1739. tt2:=ttypesym(sym).typedef
  1740. else
  1741. Message(parser_e_no_generics_as_types);
  1742. end;
  1743. { don't use cpointerdef.getreusable() here, since this is a type
  1744. declaration (-> must create new typedef) }
  1745. def:=cpointerdef.create(tt2);
  1746. if tt2.typ=forwarddef then
  1747. current_module.checkforwarddefs.add(def);
  1748. end;
  1749. _RECORD:
  1750. begin
  1751. consume(token);
  1752. if (idtoken=_HELPER) and (m_advanced_records in current_settings.modeswitches) then
  1753. begin
  1754. consume(_HELPER);
  1755. def:=object_dec(odt_helper,name,newsym,genericdef,genericlist,nil,ht_record);
  1756. end
  1757. else
  1758. def:=record_dec(name,newsym,genericdef,genericlist);
  1759. end;
  1760. _PACKED,
  1761. _BITPACKED:
  1762. begin
  1763. bitpacking :=
  1764. (cs_bitpacking in current_settings.localswitches) or
  1765. (token = _BITPACKED);
  1766. consume(token);
  1767. if token=_ARRAY then
  1768. array_dec(bitpacking,genericdef,genericlist)
  1769. else if token=_SET then
  1770. set_dec
  1771. else if token=_FILE then
  1772. single_type(def,[stoAllowTypeDef])
  1773. else
  1774. begin
  1775. oldpackrecords:=current_settings.packrecords;
  1776. if (not bitpacking) or
  1777. (token in [_CLASS,_OBJECT]) then
  1778. current_settings.packrecords:=1
  1779. else
  1780. current_settings.packrecords:=bit_alignment;
  1781. case token of
  1782. _CLASS :
  1783. begin
  1784. consume(_CLASS);
  1785. def:=object_dec(odt_class,name,newsym,genericdef,genericlist,nil,ht_none);
  1786. end;
  1787. _OBJECT :
  1788. begin
  1789. consume(_OBJECT);
  1790. def:=object_dec(odt_object,name,newsym,genericdef,genericlist,nil,ht_none);
  1791. end;
  1792. else begin
  1793. consume(_RECORD);
  1794. def:=record_dec(name,newsym,genericdef,genericlist);
  1795. end;
  1796. end;
  1797. current_settings.packrecords:=oldpackrecords;
  1798. end;
  1799. end;
  1800. _DISPINTERFACE :
  1801. begin
  1802. { need extra check here since interface is a keyword
  1803. in all pascal modes }
  1804. if not(m_class in current_settings.modeswitches) then
  1805. Message(parser_f_need_objfpc_or_delphi_mode);
  1806. consume(token);
  1807. def:=object_dec(odt_dispinterface,name,newsym,genericdef,genericlist,nil,ht_none);
  1808. end;
  1809. _CLASS :
  1810. begin
  1811. consume(token);
  1812. { Delphi only allows class of in type blocks }
  1813. if (token=_OF) and
  1814. (
  1815. not(m_delphi in current_settings.modeswitches) or
  1816. (block_type=bt_type)
  1817. ) then
  1818. begin
  1819. consume(_OF);
  1820. single_type(hdef,SingleTypeOptionsInTypeBlock[block_type=bt_type]);
  1821. if is_class(hdef) or
  1822. is_objcclass(hdef) or
  1823. is_javaclass(hdef) then
  1824. def:=cclassrefdef.create(hdef)
  1825. else
  1826. if hdef.typ=forwarddef then
  1827. begin
  1828. def:=cclassrefdef.create(hdef);
  1829. current_module.checkforwarddefs.add(def);
  1830. end
  1831. else
  1832. Message1(type_e_class_or_objcclass_type_expected,hdef.typename);
  1833. end
  1834. else
  1835. if (idtoken=_HELPER) then
  1836. begin
  1837. consume(_HELPER);
  1838. def:=object_dec(odt_helper,name,newsym,genericdef,genericlist,nil,ht_class);
  1839. end
  1840. else
  1841. def:=object_dec(default_class_type,name,newsym,genericdef,genericlist,nil,ht_none);
  1842. end;
  1843. _CPPCLASS :
  1844. begin
  1845. consume(token);
  1846. def:=object_dec(odt_cppclass,name,newsym,genericdef,genericlist,nil,ht_none);
  1847. end;
  1848. _OBJCCLASS :
  1849. begin
  1850. if not(m_objectivec1 in current_settings.modeswitches) then
  1851. Message(parser_f_need_objc);
  1852. consume(token);
  1853. def:=object_dec(odt_objcclass,name,newsym,genericdef,genericlist,nil,ht_none);
  1854. end;
  1855. _INTERFACE :
  1856. begin
  1857. { need extra check here since interface is a keyword
  1858. in all pascal modes }
  1859. if not(m_class in current_settings.modeswitches) then
  1860. Message(parser_f_need_objfpc_or_delphi_mode);
  1861. consume(token);
  1862. case current_settings.interfacetype of
  1863. it_interfacecom:
  1864. def:=object_dec(odt_interfacecom,name,newsym,genericdef,genericlist,nil,ht_none);
  1865. it_interfacecorba:
  1866. def:=object_dec(odt_interfacecorba,name,newsym,genericdef,genericlist,nil,ht_none);
  1867. it_interfacejava:
  1868. def:=object_dec(odt_interfacejava,name,newsym,genericdef,genericlist,nil,ht_none);
  1869. end;
  1870. end;
  1871. _OBJCPROTOCOL :
  1872. begin
  1873. if not(m_objectivec1 in current_settings.modeswitches) then
  1874. Message(parser_f_need_objc);
  1875. consume(token);
  1876. def:=object_dec(odt_objcprotocol,name,newsym,genericdef,genericlist,nil,ht_none);
  1877. end;
  1878. _OBJCCATEGORY :
  1879. begin
  1880. if not(m_objectivec1 in current_settings.modeswitches) then
  1881. Message(parser_f_need_objc);
  1882. consume(token);
  1883. def:=object_dec(odt_objccategory,name,newsym,genericdef,genericlist,nil,ht_none);
  1884. end;
  1885. _OBJECT :
  1886. begin
  1887. consume(token);
  1888. def:=object_dec(odt_object,name,newsym,genericdef,genericlist,nil,ht_none);
  1889. end;
  1890. _PROCEDURE,
  1891. _FUNCTION:
  1892. begin
  1893. def:=procvar_dec(genericdef,genericlist,nil,true);
  1894. {$ifdef jvm}
  1895. jvm_create_procvar_class(name,def);
  1896. {$endif}
  1897. end;
  1898. _ID:
  1899. begin
  1900. case idtoken of
  1901. _HELPER:
  1902. begin
  1903. if hadtypetoken and
  1904. (m_type_helpers in current_settings.modeswitches) then
  1905. begin
  1906. { reset hadtypetoken, so that calling code knows that it should not be handled
  1907. as a "unique" type }
  1908. hadtypetoken:=false;
  1909. consume(_HELPER);
  1910. def:=object_dec(odt_helper,name,newsym,genericdef,genericlist,nil,ht_type);
  1911. end
  1912. else
  1913. expr_type
  1914. end;
  1915. _REFERENCE:
  1916. begin
  1917. if current_settings.modeswitches*[m_blocks,m_function_references]<>[] then
  1918. begin
  1919. consume(_REFERENCE);
  1920. consume(_TO);
  1921. { don't register the def as a non-cblock function
  1922. reference will be converted to an interface }
  1923. def:=procvar_dec(genericdef,genericlist,newsym,false);
  1924. { could be errordef in case of a syntax error }
  1925. if assigned(def) and
  1926. (def.typ=procvardef) then
  1927. begin
  1928. include(tprocvardef(def).procoptions,po_is_function_ref);
  1929. end;
  1930. end
  1931. else
  1932. expr_type;
  1933. end;
  1934. else
  1935. expr_type;
  1936. end;
  1937. end
  1938. else
  1939. if (token=_KLAMMERAFFE) and (([m_iso,m_extpas]*current_settings.modeswitches)<>[]) then
  1940. begin
  1941. consume(_KLAMMERAFFE);
  1942. single_type(tt2,SingleTypeOptionsInTypeBlock[block_type=bt_type]);
  1943. def:=cpointerdef.create(tt2);
  1944. if tt2.typ=forwarddef then
  1945. current_module.checkforwarddefs.add(def);
  1946. end
  1947. else
  1948. expr_type;
  1949. end;
  1950. if def=nil then
  1951. def:=generrordef;
  1952. end;
  1953. procedure read_anon_type(var def : tdef;parseprocvardir:boolean);
  1954. var
  1955. hadtypetoken : boolean;
  1956. begin
  1957. hadtypetoken:=false;
  1958. read_named_type(def,nil,nil,nil,parseprocvardir,hadtypetoken);
  1959. end;
  1960. procedure add_typedconst_init_routine(def: tabstractrecorddef);
  1961. var
  1962. sstate: tscannerstate;
  1963. pd: tprocdef;
  1964. begin
  1965. replace_scanner('tcinit_routine',sstate);
  1966. { the typed constant initialization code is called from the class
  1967. constructor by tnodeutils.wrap_proc_body; at this point, we don't
  1968. know yet whether that will be necessary, because there may be
  1969. typed constants inside method bodies -> always force the addition
  1970. of a class constructor.
  1971. We cannot directly add the typed constant initialisations to the
  1972. class constructor, because when it's parsed not all method bodies
  1973. are necessarily already parsed }
  1974. pd:=def.find_procdef_bytype(potype_class_constructor);
  1975. { the class constructor }
  1976. if not assigned(pd) then
  1977. begin
  1978. if str_parse_method_dec('constructor fpc_init_typed_consts_class_constructor;',potype_class_constructor,true,def,pd) then
  1979. pd.synthetickind:=tsk_empty
  1980. else
  1981. internalerror(2011040206);
  1982. end;
  1983. { the initialisation helper }
  1984. if str_parse_method_dec('procedure fpc_init_typed_consts_helper; static;',potype_procedure,true,def,pd) then
  1985. pd.synthetickind:=tsk_tcinit
  1986. else
  1987. internalerror(2011040207);
  1988. restore_scanner(sstate);
  1989. end;
  1990. end.