ptype.pas 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985
  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. else
  1256. Message(sym_e_error_in_type_def);
  1257. end;
  1258. end;
  1259. var
  1260. old_current_genericdef,
  1261. old_current_specializedef: tstoreddef;
  1262. first,
  1263. old_parse_generic: boolean;
  1264. begin
  1265. old_current_genericdef:=current_genericdef;
  1266. old_current_specializedef:=current_specializedef;
  1267. old_parse_generic:=parse_generic;
  1268. current_genericdef:=nil;
  1269. current_specializedef:=nil;
  1270. first:=true;
  1271. arrdef:=carraydef.create(0,0,s32inttype);
  1272. consume(_ARRAY);
  1273. { usage of specialized type inside its generic template }
  1274. if assigned(genericdef) then
  1275. current_specializedef:=arrdef
  1276. { reject declaration of generic class inside generic class }
  1277. else if assigned(genericlist) then
  1278. current_genericdef:=arrdef;
  1279. symtablestack.push(arrdef.symtable);
  1280. insert_generic_parameter_types(arrdef,genericdef,genericlist);
  1281. { there are two possibilties for the following to be true:
  1282. * the array declaration itself is generic
  1283. * the array is declared inside a generic
  1284. in both cases we need "parse_generic" and "current_genericdef"
  1285. so that e.g. specializations of another generic inside the
  1286. current generic can be used (either inline ones or "type" ones) }
  1287. if old_parse_generic then
  1288. include(arrdef.defoptions,df_generic);
  1289. parse_generic:=(df_generic in arrdef.defoptions);
  1290. if parse_generic and not assigned(current_genericdef) then
  1291. current_genericdef:=old_current_genericdef;
  1292. { open array? }
  1293. if try_to_consume(_LECKKLAMMER) then
  1294. begin
  1295. { defaults }
  1296. indexdef:=generrordef;
  1297. { use defaults which don't overflow the compiler }
  1298. lowval:=0;
  1299. highval:=0;
  1300. repeat
  1301. { read the expression and check it, check apart if the
  1302. declaration is an enum declaration because that needs to
  1303. be parsed by readtype (PFV) }
  1304. if token=_LKLAMMER then
  1305. begin
  1306. read_anon_type(hdef,true);
  1307. setdefdecl(hdef);
  1308. end
  1309. else
  1310. begin
  1311. pt:=expr(true);
  1312. if pt.nodetype=typen then
  1313. setdefdecl(pt.resultdef)
  1314. else
  1315. begin
  1316. if pt.nodetype=rangen then
  1317. begin
  1318. { pure ordconstn expressions can be checked for
  1319. generics as well, but don't give an error in case
  1320. of parsing a generic if that isn't yet the case }
  1321. if (trangenode(pt).left.nodetype=ordconstn) and
  1322. (trangenode(pt).right.nodetype=ordconstn) then
  1323. begin
  1324. { make both the same type or give an error. This is not
  1325. done when both are integer values, because typecasting
  1326. between -3200..3200 will result in a signed-unsigned
  1327. conflict and give a range check error (PFV) }
  1328. if not(is_integer(trangenode(pt).left.resultdef) and is_integer(trangenode(pt).left.resultdef)) then
  1329. inserttypeconv(trangenode(pt).left,trangenode(pt).right.resultdef);
  1330. lowval:=tordconstnode(trangenode(pt).left).value;
  1331. highval:=tordconstnode(trangenode(pt).right).value;
  1332. if highval<lowval then
  1333. begin
  1334. Message(parser_e_array_lower_less_than_upper_bound);
  1335. highval:=lowval;
  1336. end
  1337. else if (lowval<int64(low(asizeint))) or
  1338. (highval>high(asizeint)) then
  1339. begin
  1340. Message(parser_e_array_range_out_of_bounds);
  1341. lowval :=0;
  1342. highval:=0;
  1343. end;
  1344. if is_integer(trangenode(pt).left.resultdef) then
  1345. range_to_type(lowval,highval,indexdef)
  1346. else
  1347. indexdef:=trangenode(pt).left.resultdef;
  1348. end
  1349. else
  1350. if not parse_generic then
  1351. Message(type_e_cant_eval_constant_expr)
  1352. else
  1353. { we need a valid range for debug information }
  1354. range_to_type(lowval,highval,indexdef);
  1355. end
  1356. else
  1357. Message(sym_e_error_in_type_def)
  1358. end;
  1359. pt.free;
  1360. end;
  1361. { if we are not at the first dimension, add the new arrray
  1362. as element of the existing array, otherwise modify the existing array }
  1363. if not(first) then
  1364. begin
  1365. arrdef.elementdef:=carraydef.create(lowval.svalue,highval.svalue,indexdef);
  1366. { push new symtable }
  1367. symtablestack.pop(arrdef.symtable);
  1368. arrdef:=tarraydef(arrdef.elementdef);
  1369. symtablestack.push(arrdef.symtable);
  1370. end
  1371. else
  1372. begin
  1373. arrdef.lowrange:=lowval.svalue;
  1374. arrdef.highrange:=highval.svalue;
  1375. arrdef.rangedef:=indexdef;
  1376. def:=arrdef;
  1377. first:=false;
  1378. end;
  1379. if is_packed then
  1380. include(arrdef.arrayoptions,ado_IsBitPacked);
  1381. if token=_COMMA then
  1382. consume(_COMMA)
  1383. else
  1384. break;
  1385. until false;
  1386. consume(_RECKKLAMMER);
  1387. end
  1388. else
  1389. begin
  1390. if is_packed then
  1391. Message(parser_e_packed_dynamic_open_array);
  1392. arrdef.lowrange:=0;
  1393. arrdef.highrange:=-1;
  1394. arrdef.rangedef:=s32inttype;
  1395. include(arrdef.arrayoptions,ado_IsDynamicArray);
  1396. def:=arrdef;
  1397. end;
  1398. consume(_OF);
  1399. read_anon_type(tt2,true);
  1400. { set element type of the last array definition }
  1401. if assigned(arrdef) then
  1402. begin
  1403. symtablestack.pop(arrdef.symtable);
  1404. arrdef.elementdef:=tt2;
  1405. if is_packed and
  1406. is_managed_type(tt2) then
  1407. Message(type_e_no_packed_inittable);
  1408. end;
  1409. { restore old state }
  1410. parse_generic:=old_parse_generic;
  1411. current_genericdef:=old_current_genericdef;
  1412. current_specializedef:=old_current_specializedef;
  1413. end;
  1414. function procvar_dec(genericdef:tstoreddef;genericlist:tfphashobjectlist):tdef;
  1415. var
  1416. is_func:boolean;
  1417. pd:tabstractprocdef;
  1418. newtype:ttypesym;
  1419. old_current_genericdef,
  1420. old_current_specializedef: tstoreddef;
  1421. old_parse_generic: boolean;
  1422. begin
  1423. old_current_genericdef:=current_genericdef;
  1424. old_current_specializedef:=current_specializedef;
  1425. old_parse_generic:=parse_generic;
  1426. current_genericdef:=nil;
  1427. current_specializedef:=nil;
  1428. is_func:=(token=_FUNCTION);
  1429. consume(token);
  1430. pd:=cprocvardef.create(normal_function_level);
  1431. { usage of specialized type inside its generic template }
  1432. if assigned(genericdef) then
  1433. current_specializedef:=pd
  1434. { reject declaration of generic class inside generic class }
  1435. else if assigned(genericlist) then
  1436. current_genericdef:=pd;
  1437. symtablestack.push(pd.parast);
  1438. insert_generic_parameter_types(pd,genericdef,genericlist);
  1439. { there are two possibilties for the following to be true:
  1440. * the procvar declaration itself is generic
  1441. * the procvar is declared inside a generic
  1442. in both cases we need "parse_generic" and "current_genericdef"
  1443. so that e.g. specializations of another generic inside the
  1444. current generic can be used (either inline ones or "type" ones) }
  1445. if old_parse_generic then
  1446. include(pd.defoptions,df_generic);
  1447. parse_generic:=(df_generic in pd.defoptions);
  1448. if parse_generic and not assigned(current_genericdef) then
  1449. current_genericdef:=old_current_genericdef;
  1450. { don't allow to add defs to the symtable - use it for type param search only }
  1451. tparasymtable(pd.parast).readonly:=true;
  1452. if token=_LKLAMMER then
  1453. parse_parameter_dec(pd);
  1454. if is_func then
  1455. begin
  1456. consume(_COLON);
  1457. single_type(pd.returndef,[]);
  1458. end;
  1459. if try_to_consume(_OF) then
  1460. begin
  1461. consume(_OBJECT);
  1462. include(pd.procoptions,po_methodpointer);
  1463. end
  1464. else if (m_nested_procvars in current_settings.modeswitches) and
  1465. try_to_consume(_IS) then
  1466. begin
  1467. consume(_NESTED);
  1468. pd.parast.symtablelevel:=normal_function_level+1;
  1469. pd.check_mark_as_nested;
  1470. end;
  1471. symtablestack.pop(pd.parast);
  1472. tparasymtable(pd.parast).readonly:=false;
  1473. result:=pd;
  1474. { possible proc directives }
  1475. if parseprocvardir then
  1476. begin
  1477. if check_proc_directive(true) then
  1478. begin
  1479. newtype:=ctypesym.create('unnamed',result,true);
  1480. parse_var_proc_directives(tsym(newtype));
  1481. newtype.typedef:=nil;
  1482. result.typesym:=nil;
  1483. newtype.free;
  1484. end;
  1485. { Add implicit hidden parameters and function result }
  1486. handle_calling_convention(pd);
  1487. end;
  1488. { restore old state }
  1489. parse_generic:=old_parse_generic;
  1490. current_genericdef:=old_current_genericdef;
  1491. current_specializedef:=old_current_specializedef;
  1492. end;
  1493. const
  1494. SingleTypeOptionsInTypeBlock:array[Boolean] of TSingleTypeOptions = ([],[stoIsForwardDef]);
  1495. var
  1496. p : tnode;
  1497. hdef : tdef;
  1498. enumdupmsg, first, is_specialize : boolean;
  1499. oldlocalswitches : tlocalswitches;
  1500. bitpacking: boolean;
  1501. stitem: psymtablestackitem;
  1502. sym: tsym;
  1503. st: tsymtable;
  1504. begin
  1505. def:=nil;
  1506. v:=0;
  1507. l:=0;
  1508. if assigned(newsym) then
  1509. name:=newsym.RealName
  1510. else
  1511. name:='';
  1512. case token of
  1513. _STRING,_FILE:
  1514. begin
  1515. single_type(def,[stoAllowTypeDef]);
  1516. end;
  1517. _LKLAMMER:
  1518. begin
  1519. consume(_LKLAMMER);
  1520. first:=true;
  1521. { allow negativ value_str }
  1522. l:=int64(-1);
  1523. enumdupmsg:=false;
  1524. { check that we are not adding an enum from specialization
  1525. we can't just use current_specializedef because of inner types
  1526. like specialize array of record }
  1527. is_specialize:=false;
  1528. stitem:=symtablestack.stack;
  1529. while assigned(stitem) do
  1530. begin
  1531. { check records, classes and arrays because they can be specialized }
  1532. if stitem^.symtable.symtabletype in [recordsymtable,ObjectSymtable,arraysymtable] then
  1533. begin
  1534. is_specialize:=is_specialize or (df_specialization in tstoreddef(stitem^.symtable.defowner).defoptions);
  1535. stitem:=stitem^.next;
  1536. end
  1537. else
  1538. break;
  1539. end;
  1540. if not is_specialize then
  1541. aktenumdef:=cenumdef.create
  1542. else
  1543. aktenumdef:=nil;
  1544. repeat
  1545. { if it is a specialization then search the first enum member
  1546. and get the member owner instead of just created enumdef }
  1547. if not assigned(aktenumdef) then
  1548. begin
  1549. searchsym(pattern,sym,st);
  1550. if sym.typ=enumsym then
  1551. aktenumdef:=tenumsym(sym).definition
  1552. else
  1553. internalerror(201101021);
  1554. end;
  1555. s:=orgpattern;
  1556. defpos:=current_tokenpos;
  1557. consume(_ID);
  1558. { only allow assigning of specific numbers under fpc mode }
  1559. if not(m_tp7 in current_settings.modeswitches) and
  1560. (
  1561. { in fpc mode also allow := to be compatible
  1562. with previous 1.0.x versions }
  1563. ((m_fpc in current_settings.modeswitches) and
  1564. try_to_consume(_ASSIGNMENT)) or
  1565. try_to_consume(_EQ)
  1566. ) then
  1567. begin
  1568. oldlocalswitches:=current_settings.localswitches;
  1569. include(current_settings.localswitches,cs_allow_enum_calc);
  1570. p:=comp_expr([ef_accept_equal]);
  1571. current_settings.localswitches:=oldlocalswitches;
  1572. if (p.nodetype=ordconstn) then
  1573. begin
  1574. { we expect an integer or an enum of the
  1575. same type }
  1576. if is_integer(p.resultdef) or
  1577. is_char(p.resultdef) or
  1578. equal_defs(p.resultdef,aktenumdef) then
  1579. v:=tordconstnode(p).value
  1580. else
  1581. IncompatibleTypes(p.resultdef,s32inttype);
  1582. end
  1583. else
  1584. Message(parser_e_illegal_expression);
  1585. p.free;
  1586. { please leave that a note, allows type save }
  1587. { declarations in the win32 units ! }
  1588. if (not first) and (v<=l) and (not enumdupmsg) then
  1589. begin
  1590. Message(parser_n_duplicate_enum);
  1591. enumdupmsg:=true;
  1592. end;
  1593. l:=v;
  1594. end
  1595. else
  1596. inc(l.svalue);
  1597. first:=false;
  1598. { don't generate enum members is this is a specialization because aktenumdef is copied from the generic type }
  1599. if not is_specialize then
  1600. begin
  1601. storepos:=current_tokenpos;
  1602. current_tokenpos:=defpos;
  1603. tenumsymtable(aktenumdef.symtable).insert(cenumsym.create(s,aktenumdef,longint(l.svalue)));
  1604. if not (cs_scopedenums in current_settings.localswitches) then
  1605. tstoredsymtable(aktenumdef.owner).insert(cenumsym.create(s,aktenumdef,longint(l.svalue)));
  1606. current_tokenpos:=storepos;
  1607. end;
  1608. until not try_to_consume(_COMMA);
  1609. def:=aktenumdef;
  1610. consume(_RKLAMMER);
  1611. {$ifdef jvm}
  1612. jvm_maybe_create_enum_class(name,def);
  1613. {$endif}
  1614. end;
  1615. _ARRAY:
  1616. begin
  1617. array_dec(false,genericdef,genericlist);
  1618. end;
  1619. _SET:
  1620. begin
  1621. set_dec;
  1622. end;
  1623. _CARET:
  1624. begin
  1625. consume(_CARET);
  1626. single_type(tt2,
  1627. SingleTypeOptionsInTypeBlock[block_type=bt_type]+[stoAllowSpecialization]
  1628. );
  1629. { in case of e.g. var or const sections we need to especially
  1630. check that we don't use a generic dummy symbol }
  1631. if (block_type<>bt_type) and
  1632. (tt2.typ=undefineddef) and
  1633. assigned(tt2.typesym) and
  1634. (sp_generic_dummy in tt2.typesym.symoptions) then
  1635. begin
  1636. sym:=resolve_generic_dummysym(tt2.typesym.name);
  1637. if assigned(sym) and
  1638. not (sp_generic_dummy in sym.symoptions) and
  1639. (sym.typ=typesym) then
  1640. tt2:=ttypesym(sym).typedef
  1641. else
  1642. Message(parser_e_no_generics_as_types);
  1643. end;
  1644. { don't use cpointerdef.getreusable() here, since this is a type
  1645. declaration (-> must create new typedef) }
  1646. def:=cpointerdef.create(tt2);
  1647. if tt2.typ=forwarddef then
  1648. current_module.checkforwarddefs.add(def);
  1649. end;
  1650. _RECORD:
  1651. begin
  1652. consume(token);
  1653. if (idtoken=_HELPER) and (m_advanced_records in current_settings.modeswitches) then
  1654. begin
  1655. consume(_HELPER);
  1656. def:=object_dec(odt_helper,name,newsym,genericdef,genericlist,nil,ht_record);
  1657. end
  1658. else
  1659. def:=record_dec(name,newsym,genericdef,genericlist);
  1660. end;
  1661. _PACKED,
  1662. _BITPACKED:
  1663. begin
  1664. bitpacking :=
  1665. (cs_bitpacking in current_settings.localswitches) or
  1666. (token = _BITPACKED);
  1667. consume(token);
  1668. if token=_ARRAY then
  1669. array_dec(bitpacking,genericdef,genericlist)
  1670. else if token=_SET then
  1671. set_dec
  1672. else if token=_FILE then
  1673. single_type(def,[stoAllowTypeDef])
  1674. else
  1675. begin
  1676. oldpackrecords:=current_settings.packrecords;
  1677. if (not bitpacking) or
  1678. (token in [_CLASS,_OBJECT]) then
  1679. current_settings.packrecords:=1
  1680. else
  1681. current_settings.packrecords:=bit_alignment;
  1682. case token of
  1683. _CLASS :
  1684. begin
  1685. consume(_CLASS);
  1686. def:=object_dec(odt_class,name,newsym,genericdef,genericlist,nil,ht_none);
  1687. end;
  1688. _OBJECT :
  1689. begin
  1690. consume(_OBJECT);
  1691. def:=object_dec(odt_object,name,newsym,genericdef,genericlist,nil,ht_none);
  1692. end;
  1693. else begin
  1694. consume(_RECORD);
  1695. def:=record_dec(name,newsym,genericdef,genericlist);
  1696. end;
  1697. end;
  1698. current_settings.packrecords:=oldpackrecords;
  1699. end;
  1700. end;
  1701. _DISPINTERFACE :
  1702. begin
  1703. { need extra check here since interface is a keyword
  1704. in all pascal modes }
  1705. if not(m_class in current_settings.modeswitches) then
  1706. Message(parser_f_need_objfpc_or_delphi_mode);
  1707. consume(token);
  1708. def:=object_dec(odt_dispinterface,name,newsym,genericdef,genericlist,nil,ht_none);
  1709. end;
  1710. _CLASS :
  1711. begin
  1712. consume(token);
  1713. { Delphi only allows class of in type blocks }
  1714. if (token=_OF) and
  1715. (
  1716. not(m_delphi in current_settings.modeswitches) or
  1717. (block_type=bt_type)
  1718. ) then
  1719. begin
  1720. consume(_OF);
  1721. single_type(hdef,SingleTypeOptionsInTypeBlock[block_type=bt_type]);
  1722. if is_class(hdef) or
  1723. is_objcclass(hdef) or
  1724. is_javaclass(hdef) then
  1725. def:=cclassrefdef.create(hdef)
  1726. else
  1727. if hdef.typ=forwarddef then
  1728. begin
  1729. def:=cclassrefdef.create(hdef);
  1730. current_module.checkforwarddefs.add(def);
  1731. end
  1732. else
  1733. Message1(type_e_class_or_objcclass_type_expected,hdef.typename);
  1734. end
  1735. else
  1736. if (idtoken=_HELPER) then
  1737. begin
  1738. consume(_HELPER);
  1739. def:=object_dec(odt_helper,name,newsym,genericdef,genericlist,nil,ht_class);
  1740. end
  1741. else
  1742. def:=object_dec(default_class_type,name,newsym,genericdef,genericlist,nil,ht_none);
  1743. end;
  1744. _CPPCLASS :
  1745. begin
  1746. consume(token);
  1747. def:=object_dec(odt_cppclass,name,newsym,genericdef,genericlist,nil,ht_none);
  1748. end;
  1749. _OBJCCLASS :
  1750. begin
  1751. if not(m_objectivec1 in current_settings.modeswitches) then
  1752. Message(parser_f_need_objc);
  1753. consume(token);
  1754. def:=object_dec(odt_objcclass,name,newsym,genericdef,genericlist,nil,ht_none);
  1755. end;
  1756. _INTERFACE :
  1757. begin
  1758. { need extra check here since interface is a keyword
  1759. in all pascal modes }
  1760. if not(m_class in current_settings.modeswitches) then
  1761. Message(parser_f_need_objfpc_or_delphi_mode);
  1762. consume(token);
  1763. case current_settings.interfacetype of
  1764. it_interfacecom:
  1765. def:=object_dec(odt_interfacecom,name,newsym,genericdef,genericlist,nil,ht_none);
  1766. it_interfacecorba:
  1767. def:=object_dec(odt_interfacecorba,name,newsym,genericdef,genericlist,nil,ht_none);
  1768. it_interfacejava:
  1769. def:=object_dec(odt_interfacejava,name,newsym,genericdef,genericlist,nil,ht_none);
  1770. else
  1771. internalerror(2010122612);
  1772. end;
  1773. end;
  1774. _OBJCPROTOCOL :
  1775. begin
  1776. if not(m_objectivec1 in current_settings.modeswitches) then
  1777. Message(parser_f_need_objc);
  1778. consume(token);
  1779. def:=object_dec(odt_objcprotocol,name,newsym,genericdef,genericlist,nil,ht_none);
  1780. end;
  1781. _OBJCCATEGORY :
  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_objccategory,name,newsym,genericdef,genericlist,nil,ht_none);
  1787. end;
  1788. _OBJECT :
  1789. begin
  1790. consume(token);
  1791. def:=object_dec(odt_object,name,newsym,genericdef,genericlist,nil,ht_none);
  1792. end;
  1793. _PROCEDURE,
  1794. _FUNCTION:
  1795. begin
  1796. def:=procvar_dec(genericdef,genericlist);
  1797. {$ifdef jvm}
  1798. jvm_create_procvar_class(name,def);
  1799. {$endif}
  1800. end;
  1801. _ID:
  1802. begin
  1803. case idtoken of
  1804. _HELPER:
  1805. begin
  1806. if hadtypetoken and
  1807. (m_type_helpers in current_settings.modeswitches) then
  1808. begin
  1809. { reset hadtypetoken, so that calling code knows that it should not be handled
  1810. as a "unique" type }
  1811. hadtypetoken:=false;
  1812. consume(_HELPER);
  1813. def:=object_dec(odt_helper,name,newsym,genericdef,genericlist,nil,ht_type);
  1814. end
  1815. else
  1816. expr_type
  1817. end;
  1818. _REFERENCE:
  1819. begin
  1820. if m_blocks in current_settings.modeswitches then
  1821. begin
  1822. consume(_REFERENCE);
  1823. consume(_TO);
  1824. def:=procvar_dec(genericdef,genericlist);
  1825. { could be errordef in case of a syntax error }
  1826. if assigned(def) and
  1827. (def.typ=procvardef) then
  1828. include(tprocvardef(def).procoptions,po_is_function_ref);
  1829. end
  1830. else
  1831. expr_type;
  1832. end;
  1833. else
  1834. expr_type;
  1835. end;
  1836. end
  1837. else
  1838. if (token=_KLAMMERAFFE) and (([m_iso,m_extpas]*current_settings.modeswitches)<>[]) then
  1839. begin
  1840. consume(_KLAMMERAFFE);
  1841. single_type(tt2,SingleTypeOptionsInTypeBlock[block_type=bt_type]);
  1842. def:=cpointerdef.create(tt2);
  1843. if tt2.typ=forwarddef then
  1844. current_module.checkforwarddefs.add(def);
  1845. end
  1846. else
  1847. expr_type;
  1848. end;
  1849. if def=nil then
  1850. def:=generrordef;
  1851. end;
  1852. procedure read_anon_type(var def : tdef;parseprocvardir:boolean);
  1853. var
  1854. hadtypetoken : boolean;
  1855. begin
  1856. hadtypetoken:=false;
  1857. read_named_type(def,nil,nil,nil,parseprocvardir,hadtypetoken);
  1858. end;
  1859. procedure add_typedconst_init_routine(def: tabstractrecorddef);
  1860. var
  1861. sstate: tscannerstate;
  1862. pd: tprocdef;
  1863. begin
  1864. replace_scanner('tcinit_routine',sstate);
  1865. { the typed constant initialization code is called from the class
  1866. constructor by tnodeutils.wrap_proc_body; at this point, we don't
  1867. know yet whether that will be necessary, because there may be
  1868. typed constants inside method bodies -> always force the addition
  1869. of a class constructor.
  1870. We cannot directly add the typed constant initialisations to the
  1871. class constructor, because when it's parsed not all method bodies
  1872. are necessarily already parsed }
  1873. pd:=def.find_procdef_bytype(potype_class_constructor);
  1874. { the class constructor }
  1875. if not assigned(pd) then
  1876. begin
  1877. if str_parse_method_dec('constructor fpc_init_typed_consts_class_constructor;',potype_class_constructor,true,def,pd) then
  1878. pd.synthetickind:=tsk_empty
  1879. else
  1880. internalerror(2011040206);
  1881. end;
  1882. { the initialisation helper }
  1883. if str_parse_method_dec('procedure fpc_init_typed_consts_helper; static;',potype_procedure,true,def,pd) then
  1884. pd.synthetickind:=tsk_tcinit
  1885. else
  1886. internalerror(2011040207);
  1887. restore_scanner(sstate);
  1888. end;
  1889. end.