ptype.pas 86 KB

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