ptype.pas 83 KB

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