ptype.pas 74 KB

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