ptype.pas 72 KB

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