ptype.pas 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  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. var
  24. { hack, which allows to use the current parsed }
  25. { object type as function argument type }
  26. testcurobject : byte;
  27. procedure resolve_forward_types;
  28. { reads a type identifier }
  29. procedure id_type(var def : tdef;isforwarddef:boolean);
  30. { reads a string, file type or a type identifier }
  31. procedure single_type(var def:tdef;isforwarddef,allowtypedef:boolean);
  32. { reads any type declaration, where the resulting type will get name as type identifier }
  33. procedure read_named_type(var def:tdef;const name : TIDString;genericdef:tstoreddef;genericlist:TFPObjectList;parseprocvardir:boolean);
  34. { reads any type declaration }
  35. procedure read_anon_type(var def : tdef;parseprocvardir:boolean);
  36. { generate persistent type information like VMT, RTTI and inittables }
  37. procedure write_persistent_type_info(st:tsymtable);
  38. implementation
  39. uses
  40. { common }
  41. cutils,
  42. { global }
  43. globals,tokens,verbose,constexp,
  44. systems,
  45. { target }
  46. paramgr,procinfo,
  47. { symtable }
  48. symconst,symsym,symtable,
  49. defutil,defcmp,
  50. { modules }
  51. fmodule,
  52. { pass 1 }
  53. node,ncgrtti,nobj,
  54. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,
  55. { parser }
  56. scanner,
  57. pbase,pexpr,pdecsub,pdecvar,pdecobj;
  58. procedure resolve_forward_types;
  59. var
  60. i: longint;
  61. hpd,
  62. def : tdef;
  63. srsym : tsym;
  64. srsymtable : TSymtable;
  65. hs : string;
  66. begin
  67. for i:=0 to current_module.checkforwarddefs.Count-1 do
  68. begin
  69. def:=tdef(current_module.checkforwarddefs[i]);
  70. case def.typ of
  71. pointerdef,
  72. classrefdef :
  73. begin
  74. { classrefdef inherits from pointerdef }
  75. hpd:=tabstractpointerdef(def).pointeddef;
  76. { still a forward def ? }
  77. if hpd.typ=forwarddef then
  78. begin
  79. { try to resolve the forward }
  80. if not assigned(tforwarddef(hpd).tosymname) then
  81. internalerror(200211201);
  82. hs:=tforwarddef(hpd).tosymname^;
  83. searchsym(upper(hs),srsym,srsymtable);
  84. { we don't need the forwarddef anymore, dispose it }
  85. hpd.free;
  86. tabstractpointerdef(def).pointeddef:=nil; { if error occurs }
  87. { was a type sym found ? }
  88. if assigned(srsym) and
  89. (srsym.typ=typesym) then
  90. begin
  91. tabstractpointerdef(def).pointeddef:=ttypesym(srsym).typedef;
  92. { avoid wrong unused warnings web bug 801 PM }
  93. inc(ttypesym(srsym).refs);
  94. { we need a class type for classrefdef }
  95. if (def.typ=classrefdef) and
  96. not(is_class(ttypesym(srsym).typedef)) and
  97. not(is_objcclass(ttypesym(srsym).typedef)) then
  98. MessagePos1(def.typesym.fileinfo,type_e_class_type_expected,ttypesym(srsym).typedef.typename);
  99. end
  100. else
  101. begin
  102. Message1(sym_e_forward_type_not_resolved,hs);
  103. { try to recover }
  104. tabstractpointerdef(def).pointeddef:=generrordef;
  105. end;
  106. end;
  107. end;
  108. objectdef :
  109. begin
  110. { give an error as the implementation may follow in an
  111. other type block which is allowed by FPC modes }
  112. if not(m_fpc in current_settings.modeswitches) and
  113. (oo_is_forward in tobjectdef(def).objectoptions) then
  114. MessagePos1(def.typesym.fileinfo,type_e_type_is_not_completly_defined,def.typename);
  115. end;
  116. else
  117. internalerror(200811071);
  118. end;
  119. end;
  120. current_module.checkforwarddefs.clear;
  121. end;
  122. procedure generate_specialization(var tt:tdef);
  123. var
  124. st : TSymtable;
  125. srsym : tsym;
  126. pt2 : tnode;
  127. first,
  128. err : boolean;
  129. i : longint;
  130. sym : tsym;
  131. genericdef : tstoreddef;
  132. generictype : ttypesym;
  133. generictypelist : TFPObjectList;
  134. oldsymtablestack : tsymtablestack;
  135. hmodule : tmodule;
  136. pu : tused_unit;
  137. uspecializename,
  138. specializename : string;
  139. vmtbuilder : TVMTBuilder;
  140. onlyparsepara : boolean;
  141. specializest : tsymtable;
  142. begin
  143. { retrieve generic def that we are going to replace }
  144. genericdef:=tstoreddef(tt);
  145. tt:=nil;
  146. onlyparsepara:=false;
  147. if not(df_generic in genericdef.defoptions) then
  148. begin
  149. Message(parser_e_special_onlygenerics);
  150. tt:=generrordef;
  151. onlyparsepara:=true;
  152. end;
  153. { Only need to record the tokens, then we don't know the type yet }
  154. if parse_generic then
  155. begin
  156. tt:=cundefinedtype;
  157. onlyparsepara:=true;
  158. end;
  159. { Only parse the parameters for recovery or
  160. for recording in genericbuf }
  161. if onlyparsepara then
  162. begin
  163. consume(_LSHARPBRACKET);
  164. repeat
  165. pt2:=factor(false);
  166. pt2.free;
  167. until not try_to_consume(_COMMA);
  168. consume(_RSHARPBRACKET);
  169. exit;
  170. end;
  171. consume(_LSHARPBRACKET);
  172. { Parse generic parameters, for each undefineddef in the symtable of
  173. the genericdef we need to have a new def }
  174. err:=false;
  175. first:=true;
  176. generictypelist:=TFPObjectList.create(false);
  177. case genericdef.typ of
  178. procdef :
  179. st:=genericdef.GetSymtable(gs_para);
  180. objectdef,
  181. recorddef :
  182. st:=genericdef.GetSymtable(gs_record);
  183. end;
  184. if not assigned(st) then
  185. internalerror(200511182);
  186. { Parse type parameters }
  187. if not assigned(genericdef.typesym) then
  188. internalerror(200710173);
  189. specializename:=genericdef.typesym.realname;
  190. for i:=0 to st.SymList.Count-1 do
  191. begin
  192. sym:=tsym(st.SymList[i]);
  193. if (sp_generic_para in sym.symoptions) then
  194. begin
  195. if not first then
  196. consume(_COMMA)
  197. else
  198. first:=false;
  199. pt2:=factor(false);
  200. if pt2.nodetype=typen then
  201. begin
  202. if df_generic in pt2.resultdef.defoptions then
  203. Message(parser_e_no_generics_as_params);
  204. generictype:=ttypesym.create(sym.realname,pt2.resultdef);
  205. generictypelist.add(generictype);
  206. if not assigned(pt2.resultdef.typesym) then
  207. internalerror(200710172);
  208. specializename:=specializename+'$'+pt2.resultdef.typesym.realname;
  209. end
  210. else
  211. begin
  212. Message(type_e_type_id_expected);
  213. err:=true;
  214. end;
  215. pt2.free;
  216. end;
  217. end;
  218. uspecializename:=upper(specializename);
  219. { force correct error location if too much type parameters are passed }
  220. if token<>_RSHARPBRACKET then
  221. consume(_RSHARPBRACKET);
  222. { Special case if we are referencing the current defined object }
  223. if assigned(current_objectdef) and
  224. (current_objectdef.objname^=uspecializename) then
  225. tt:=current_objectdef;
  226. { for units specializations can already be needed in the interface, therefor we
  227. will use the global symtable. Programs don't have a globalsymtable and there we
  228. use the localsymtable }
  229. if current_module.is_unit then
  230. specializest:=current_module.globalsymtable
  231. else
  232. specializest:=current_module.localsymtable;
  233. { Can we reuse an already specialized type? }
  234. if not assigned(tt) then
  235. begin
  236. srsym:=tsym(specializest.find(uspecializename));
  237. if assigned(srsym) then
  238. begin
  239. if srsym.typ<>typesym then
  240. internalerror(200710171);
  241. tt:=ttypesym(srsym).typedef;
  242. end;
  243. end;
  244. if not assigned(tt) then
  245. begin
  246. { Setup symtablestack at definition time
  247. to get types right, however this is not perfect, we should probably record
  248. the resolved symbols }
  249. oldsymtablestack:=symtablestack;
  250. symtablestack:=tsymtablestack.create;
  251. if not assigned(genericdef) then
  252. internalerror(200705151);
  253. hmodule:=find_module_from_symtable(genericdef.owner);
  254. if hmodule=nil then
  255. internalerror(200705152);
  256. pu:=tused_unit(hmodule.used_units.first);
  257. while assigned(pu) do
  258. begin
  259. if not assigned(pu.u.globalsymtable) then
  260. internalerror(200705153);
  261. symtablestack.push(pu.u.globalsymtable);
  262. pu:=tused_unit(pu.next);
  263. end;
  264. if assigned(hmodule.globalsymtable) then
  265. symtablestack.push(hmodule.globalsymtable);
  266. { hacky, but necessary to insert the newly generated class properly }
  267. symtablestack.push(oldsymtablestack.top);
  268. { Reparse the original type definition }
  269. if not err then
  270. begin
  271. { First a new typesym so we can reuse this specialization and
  272. references to this specialization can be handled }
  273. srsym:=ttypesym.create(specializename,generrordef);
  274. specializest.insert(srsym);
  275. if not assigned(genericdef.generictokenbuf) then
  276. internalerror(200511171);
  277. current_scanner.startreplaytokens(genericdef.generictokenbuf);
  278. read_named_type(tt,specializename,genericdef,generictypelist,false);
  279. ttypesym(srsym).typedef:=tt;
  280. tt.typesym:=srsym;
  281. { Consume the semicolon if it is also recorded }
  282. try_to_consume(_SEMICOLON);
  283. { Build VMT indexes for classes }
  284. if (tt.typ=objectdef) then
  285. begin
  286. vmtbuilder:=TVMTBuilder.Create(tobjectdef(tt));
  287. vmtbuilder.generate_vmt;
  288. vmtbuilder.free;
  289. end;
  290. end;
  291. { Restore symtablestack }
  292. symtablestack.free;
  293. symtablestack:=oldsymtablestack;
  294. end;
  295. generictypelist.free;
  296. consume(_RSHARPBRACKET);
  297. end;
  298. procedure id_type(var def : tdef;isforwarddef:boolean);
  299. { reads a type definition }
  300. { to a appropriating tdef, s gets the name of }
  301. { the type to allow name mangling }
  302. var
  303. is_unit_specific : boolean;
  304. pos : tfileposinfo;
  305. srsym : tsym;
  306. srsymtable : TSymtable;
  307. s,sorg : TIDString;
  308. t : ttoken;
  309. begin
  310. s:=pattern;
  311. sorg:=orgpattern;
  312. pos:=current_tokenpos;
  313. { use of current parsed object:
  314. - classes can be used also in classes
  315. - objects can be parameters }
  316. if assigned(current_objectdef) and
  317. (current_objectdef.objname^=pattern) and
  318. (
  319. (testcurobject=2) or
  320. is_class_or_interface_or_objc(current_objectdef)
  321. )then
  322. begin
  323. consume(_ID);
  324. def:=current_objectdef;
  325. exit;
  326. end;
  327. { Use the special searchsym_type that ignores records,objects and
  328. parameters }
  329. searchsym_type(s,srsym,srsymtable);
  330. { handle unit specification like System.Writeln }
  331. is_unit_specific:=try_consume_unitsym(srsym,srsymtable,t);
  332. consume(t);
  333. { Types are first defined with an error def before assigning
  334. the real type so check if it's an errordef. if so then
  335. give an error. Only check for typesyms in the current symbol
  336. table as forwarddef are not resolved directly }
  337. if assigned(srsym) and
  338. (srsym.typ=typesym) and
  339. (ttypesym(srsym).typedef.typ=errordef) then
  340. begin
  341. Message1(type_e_type_is_not_completly_defined,ttypesym(srsym).realname);
  342. def:=generrordef;
  343. exit;
  344. end;
  345. { are we parsing a possible forward def ? }
  346. if isforwarddef and
  347. not(is_unit_specific) then
  348. begin
  349. def:=tforwarddef.create(sorg,pos);
  350. exit;
  351. end;
  352. { unknown sym ? }
  353. if not assigned(srsym) then
  354. begin
  355. Message1(sym_e_id_not_found,sorg);
  356. def:=generrordef;
  357. exit;
  358. end;
  359. { type sym ? }
  360. if (srsym.typ<>typesym) then
  361. begin
  362. Message(type_e_type_id_expected);
  363. def:=generrordef;
  364. exit;
  365. end;
  366. { Give an error when referring to an errordef }
  367. if (ttypesym(srsym).typedef.typ=errordef) then
  368. begin
  369. Message(sym_e_error_in_type_def);
  370. def:=generrordef;
  371. exit;
  372. end;
  373. def:=ttypesym(srsym).typedef;
  374. end;
  375. procedure single_type(var def:tdef;isforwarddef,allowtypedef:boolean);
  376. var
  377. t2 : tdef;
  378. dospecialize,
  379. again : boolean;
  380. begin
  381. dospecialize:=false;
  382. repeat
  383. again:=false;
  384. case token of
  385. _STRING:
  386. string_dec(def,allowtypedef);
  387. _FILE:
  388. begin
  389. consume(_FILE);
  390. if (token=_OF) then
  391. begin
  392. if not(allowtypedef) then
  393. Message(parser_e_no_local_para_def);
  394. consume(_OF);
  395. single_type(t2,false,false);
  396. def:=tfiledef.createtyped(t2);
  397. end
  398. else
  399. def:=cfiletype;
  400. end;
  401. _ID:
  402. begin
  403. if try_to_consume(_SPECIALIZE) then
  404. begin
  405. dospecialize:=true;
  406. again:=true;
  407. end
  408. else
  409. begin
  410. id_type(def,isforwarddef);
  411. { handle types inside classes for generics, e.g. TNode.TLongint }
  412. while (token=_POINT) do
  413. begin
  414. if parse_generic then
  415. begin
  416. consume(_POINT);
  417. consume(_ID);
  418. end
  419. else if ((def.typ=objectdef) and (df_specialization in def.defoptions)) then
  420. begin
  421. symtablestack.push(tobjectdef(def).symtable);
  422. consume(_POINT);
  423. id_type(t2,isforwarddef);
  424. symtablestack.pop(tobjectdef(def).symtable);
  425. def:=t2;
  426. end
  427. else
  428. break;
  429. end;
  430. end;
  431. end;
  432. else
  433. begin
  434. message(type_e_type_id_expected);
  435. def:=generrordef;
  436. end;
  437. end;
  438. until not again;
  439. if dospecialize then
  440. generate_specialization(def)
  441. else
  442. begin
  443. if (df_generic in def.defoptions) then
  444. begin
  445. Message(parser_e_no_generics_as_types);
  446. def:=generrordef;
  447. end;
  448. end;
  449. end;
  450. { reads a record declaration }
  451. function record_dec : tdef;
  452. var
  453. recst : trecordsymtable;
  454. begin
  455. { create recdef }
  456. recst:=trecordsymtable.create(current_settings.packrecords);
  457. record_dec:=trecorddef.create(recst);
  458. { insert in symtablestack }
  459. symtablestack.push(recst);
  460. { parse record }
  461. consume(_RECORD);
  462. read_record_fields([vd_record]);
  463. consume(_END);
  464. { make the record size aligned }
  465. recst.addalignmentpadding;
  466. { restore symtable stack }
  467. symtablestack.pop(recst);
  468. if trecorddef(record_dec).is_packed and
  469. record_dec.needs_inittable then
  470. Message(type_e_no_packed_inittable);
  471. end;
  472. { reads a type definition and returns a pointer to it }
  473. procedure read_named_type(var def : tdef;const name : TIDString;genericdef:tstoreddef;genericlist:TFPObjectList;parseprocvardir:boolean);
  474. var
  475. pt : tnode;
  476. tt2 : tdef;
  477. aktenumdef : tenumdef;
  478. s : TIDString;
  479. l,v : TConstExprInt;
  480. oldpackrecords : longint;
  481. defpos,storepos : tfileposinfo;
  482. procedure expr_type;
  483. var
  484. pt1,pt2 : tnode;
  485. lv,hv : TConstExprInt;
  486. old_block_type : tblock_type;
  487. dospecialize : boolean;
  488. begin
  489. old_block_type:=block_type;
  490. dospecialize:=false;
  491. { use of current parsed object:
  492. - classes can be used also in classes
  493. - objects can be parameters }
  494. if (token=_ID) and
  495. assigned(current_objectdef) and
  496. (current_objectdef.objname^=pattern) and
  497. (
  498. (testcurobject=2) or
  499. is_class_or_interface_or_objc(current_objectdef)
  500. )then
  501. begin
  502. consume(_ID);
  503. def:=current_objectdef;
  504. exit;
  505. end;
  506. { Generate a specialization? }
  507. if try_to_consume(_SPECIALIZE) then
  508. dospecialize:=true;
  509. { we can't accept a equal in type }
  510. pt1:=comp_expr(false);
  511. if not dospecialize and
  512. try_to_consume(_POINTPOINT) then
  513. begin
  514. { get high value of range }
  515. pt2:=comp_expr(false);
  516. { make both the same type or give an error. This is not
  517. done when both are integer values, because typecasting
  518. between -3200..3200 will result in a signed-unsigned
  519. conflict and give a range check error (PFV) }
  520. if not(is_integer(pt1.resultdef) and is_integer(pt2.resultdef)) then
  521. inserttypeconv(pt1,pt2.resultdef);
  522. { both must be evaluated to constants now }
  523. if (pt1.nodetype=ordconstn) and
  524. (pt2.nodetype=ordconstn) then
  525. begin
  526. lv:=tordconstnode(pt1).value;
  527. hv:=tordconstnode(pt2).value;
  528. { Check bounds }
  529. if hv<lv then
  530. message(parser_e_upper_lower_than_lower)
  531. else if (lv.signed and (lv.svalue<0)) and (not hv.signed and (hv.uvalue>qword(high(int64)))) then
  532. message(type_e_cant_eval_constant_expr)
  533. else
  534. begin
  535. { All checks passed, create the new def }
  536. case pt1.resultdef.typ of
  537. enumdef :
  538. def:=tenumdef.create_subrange(tenumdef(pt1.resultdef),lv.svalue,hv.svalue);
  539. orddef :
  540. begin
  541. if is_char(pt1.resultdef) then
  542. def:=torddef.create(uchar,lv,hv)
  543. else
  544. if is_boolean(pt1.resultdef) then
  545. def:=torddef.create(pasbool,lv,hv)
  546. else if is_signed(pt1.resultdef) then
  547. def:=torddef.create(range_to_basetype(lv,hv),lv,hv)
  548. else
  549. def:=torddef.create(range_to_basetype(lv,hv),lv,hv);
  550. end;
  551. end;
  552. end;
  553. end
  554. else
  555. Message(sym_e_error_in_type_def);
  556. pt2.free;
  557. end
  558. else
  559. begin
  560. { a simple type renaming or generic specialization }
  561. if (pt1.nodetype=typen) then
  562. begin
  563. def:=ttypenode(pt1).resultdef;
  564. if dospecialize then
  565. generate_specialization(def)
  566. else
  567. begin
  568. if (df_generic in def.defoptions) then
  569. begin
  570. Message(parser_e_no_generics_as_types);
  571. def:=generrordef;
  572. end;
  573. end;
  574. end
  575. else
  576. Message(sym_e_error_in_type_def);
  577. end;
  578. pt1.free;
  579. block_type:=old_block_type;
  580. end;
  581. procedure set_dec;
  582. begin
  583. consume(_SET);
  584. consume(_OF);
  585. read_anon_type(tt2,true);
  586. if assigned(tt2) then
  587. begin
  588. case tt2.typ of
  589. { don't forget that min can be negativ PM }
  590. enumdef :
  591. if (tenumdef(tt2).min>=0) and
  592. (tenumdef(tt2).max<=255) then
  593. // !! def:=tsetdef.create(tt2,tenumdef(tt2.def).min,tenumdef(tt2.def).max))
  594. def:=tsetdef.create(tt2,tenumdef(tt2).min,tenumdef(tt2).max)
  595. else
  596. Message(sym_e_ill_type_decl_set);
  597. orddef :
  598. begin
  599. if (torddef(tt2).ordtype<>uvoid) and
  600. (torddef(tt2).ordtype<>uwidechar) and
  601. (torddef(tt2).low>=0) then
  602. // !! def:=tsetdef.create(tt2,torddef(tt2.def).low,torddef(tt2.def).high))
  603. if Torddef(tt2).high>int64(high(byte)) then
  604. message(sym_e_ill_type_decl_set)
  605. else
  606. def:=tsetdef.create(tt2,torddef(tt2).low.svalue,torddef(tt2).high.svalue)
  607. else
  608. Message(sym_e_ill_type_decl_set);
  609. end;
  610. else
  611. Message(sym_e_ill_type_decl_set);
  612. end;
  613. end
  614. else
  615. def:=generrordef;
  616. end;
  617. procedure array_dec(is_packed: boolean);
  618. var
  619. lowval,
  620. highval : TConstExprInt;
  621. indexdef : tdef;
  622. hdef : tdef;
  623. arrdef : tarraydef;
  624. procedure setdefdecl(def:tdef);
  625. begin
  626. case def.typ of
  627. enumdef :
  628. begin
  629. lowval:=tenumdef(def).min;
  630. highval:=tenumdef(def).max;
  631. if (m_fpc in current_settings.modeswitches) and
  632. (tenumdef(def).has_jumps) then
  633. Message(type_e_array_index_enums_with_assign_not_possible);
  634. indexdef:=def;
  635. end;
  636. orddef :
  637. begin
  638. if torddef(def).ordtype in [uchar,
  639. u8bit,u16bit,
  640. s8bit,s16bit,s32bit,
  641. {$ifdef cpu64bitaddr}
  642. u32bit,s64bit,
  643. {$endif cpu64bitaddr}
  644. pasbool,bool8bit,bool16bit,bool32bit,bool64bit,
  645. uwidechar] then
  646. begin
  647. lowval:=torddef(def).low;
  648. highval:=torddef(def).high;
  649. indexdef:=def;
  650. end
  651. else
  652. Message1(parser_e_type_cant_be_used_in_array_index,def.GetTypeName);
  653. end;
  654. else
  655. Message(sym_e_error_in_type_def);
  656. end;
  657. end;
  658. begin
  659. arrdef:=nil;
  660. consume(_ARRAY);
  661. { open array? }
  662. if try_to_consume(_LECKKLAMMER) then
  663. begin
  664. { defaults }
  665. indexdef:=generrordef;
  666. { use defaults which don't overflow the compiler }
  667. lowval:=0;
  668. highval:=0;
  669. repeat
  670. { read the expression and check it, check apart if the
  671. declaration is an enum declaration because that needs to
  672. be parsed by readtype (PFV) }
  673. if token=_LKLAMMER then
  674. begin
  675. read_anon_type(hdef,true);
  676. setdefdecl(hdef);
  677. end
  678. else
  679. begin
  680. pt:=expr;
  681. if pt.nodetype=typen then
  682. setdefdecl(pt.resultdef)
  683. else
  684. begin
  685. if (pt.nodetype=rangen) then
  686. begin
  687. if (trangenode(pt).left.nodetype=ordconstn) and
  688. (trangenode(pt).right.nodetype=ordconstn) then
  689. begin
  690. { make both the same type or give an error. This is not
  691. done when both are integer values, because typecasting
  692. between -3200..3200 will result in a signed-unsigned
  693. conflict and give a range check error (PFV) }
  694. if not(is_integer(trangenode(pt).left.resultdef) and is_integer(trangenode(pt).left.resultdef)) then
  695. inserttypeconv(trangenode(pt).left,trangenode(pt).right.resultdef);
  696. lowval:=tordconstnode(trangenode(pt).left).value;
  697. highval:=tordconstnode(trangenode(pt).right).value;
  698. if highval<lowval then
  699. begin
  700. Message(parser_e_array_lower_less_than_upper_bound);
  701. highval:=lowval;
  702. end
  703. else if (lowval<int64(low(aint))) or
  704. (highval > high(aint)) then
  705. begin
  706. Message(parser_e_array_range_out_of_bounds);
  707. lowval :=0;
  708. highval:=0;
  709. end;
  710. if is_integer(trangenode(pt).left.resultdef) then
  711. range_to_type(lowval,highval,indexdef)
  712. else
  713. indexdef:=trangenode(pt).left.resultdef;
  714. end
  715. else
  716. Message(type_e_cant_eval_constant_expr);
  717. end
  718. else
  719. Message(sym_e_error_in_type_def)
  720. end;
  721. pt.free;
  722. end;
  723. { if the array is already created add the new arrray
  724. as element of the existing array, otherwise create a new array }
  725. if assigned(arrdef) then
  726. begin
  727. arrdef.elementdef:=tarraydef.create(lowval.svalue,highval.svalue,indexdef);
  728. arrdef:=tarraydef(arrdef.elementdef);
  729. end
  730. else
  731. begin
  732. arrdef:=tarraydef.create(lowval.svalue,highval.svalue,indexdef);
  733. def:=arrdef;
  734. end;
  735. if is_packed then
  736. include(arrdef.arrayoptions,ado_IsBitPacked);
  737. if token=_COMMA then
  738. consume(_COMMA)
  739. else
  740. break;
  741. until false;
  742. consume(_RECKKLAMMER);
  743. end
  744. else
  745. begin
  746. if is_packed then
  747. Message(parser_e_packed_dynamic_open_array);
  748. arrdef:=tarraydef.create(0,-1,s32inttype);
  749. include(arrdef.arrayoptions,ado_IsDynamicArray);
  750. def:=arrdef;
  751. end;
  752. consume(_OF);
  753. read_anon_type(tt2,true);
  754. { set element type of the last array definition }
  755. if assigned(arrdef) then
  756. begin
  757. arrdef.elementdef:=tt2;
  758. if is_packed and
  759. tt2.needs_inittable then
  760. Message(type_e_no_packed_inittable);
  761. end;
  762. end;
  763. var
  764. p : tnode;
  765. hdef : tdef;
  766. pd : tabstractprocdef;
  767. is_func,
  768. enumdupmsg, first : boolean;
  769. newtype : ttypesym;
  770. oldlocalswitches : tlocalswitches;
  771. bitpacking: boolean;
  772. begin
  773. def:=nil;
  774. case token of
  775. _STRING,_FILE:
  776. begin
  777. single_type(def,false,true);
  778. end;
  779. _LKLAMMER:
  780. begin
  781. consume(_LKLAMMER);
  782. first := true;
  783. { allow negativ value_str }
  784. l:=int64(-1);
  785. enumdupmsg:=false;
  786. aktenumdef:=tenumdef.create;
  787. repeat
  788. s:=orgpattern;
  789. defpos:=current_tokenpos;
  790. consume(_ID);
  791. { only allow assigning of specific numbers under fpc mode }
  792. if not(m_tp7 in current_settings.modeswitches) and
  793. (
  794. { in fpc mode also allow := to be compatible
  795. with previous 1.0.x versions }
  796. ((m_fpc in current_settings.modeswitches) and
  797. try_to_consume(_ASSIGNMENT)) or
  798. try_to_consume(_EQUAL)
  799. ) then
  800. begin
  801. oldlocalswitches:=current_settings.localswitches;
  802. include(current_settings.localswitches,cs_allow_enum_calc);
  803. p:=comp_expr(true);
  804. current_settings.localswitches:=oldlocalswitches;
  805. if (p.nodetype=ordconstn) then
  806. begin
  807. { we expect an integer or an enum of the
  808. same type }
  809. if is_integer(p.resultdef) or
  810. is_char(p.resultdef) or
  811. equal_defs(p.resultdef,aktenumdef) then
  812. v:=tordconstnode(p).value
  813. else
  814. IncompatibleTypes(p.resultdef,s32inttype);
  815. end
  816. else
  817. Message(parser_e_illegal_expression);
  818. p.free;
  819. { please leave that a note, allows type save }
  820. { declarations in the win32 units ! }
  821. if (not first) and (v<=l) and (not enumdupmsg) then
  822. begin
  823. Message(parser_n_duplicate_enum);
  824. enumdupmsg:=true;
  825. end;
  826. l:=v;
  827. end
  828. else
  829. inc(l.svalue);
  830. first := false;
  831. storepos:=current_tokenpos;
  832. current_tokenpos:=defpos;
  833. tstoredsymtable(aktenumdef.owner).insert(tenumsym.create(s,aktenumdef,longint(l.svalue)));
  834. current_tokenpos:=storepos;
  835. until not try_to_consume(_COMMA);
  836. def:=aktenumdef;
  837. consume(_RKLAMMER);
  838. end;
  839. _ARRAY:
  840. begin
  841. array_dec(false);
  842. end;
  843. _SET:
  844. begin
  845. set_dec;
  846. end;
  847. _CARET:
  848. begin
  849. consume(_CARET);
  850. single_type(tt2,(block_type=bt_type),false);
  851. def:=tpointerdef.create(tt2);
  852. if tt2.typ=forwarddef then
  853. current_module.checkforwarddefs.add(def);
  854. end;
  855. _RECORD:
  856. begin
  857. def:=record_dec;
  858. end;
  859. _PACKED,
  860. _BITPACKED:
  861. begin
  862. bitpacking :=
  863. (cs_bitpacking in current_settings.localswitches) or
  864. (token = _BITPACKED);
  865. consume(token);
  866. if token=_ARRAY then
  867. array_dec(bitpacking)
  868. else if token=_SET then
  869. set_dec
  870. else
  871. begin
  872. oldpackrecords:=current_settings.packrecords;
  873. if (not bitpacking) or
  874. (token in [_CLASS,_OBJECT]) then
  875. current_settings.packrecords:=1
  876. else
  877. current_settings.packrecords:=bit_alignment;
  878. case token of
  879. _CLASS :
  880. begin
  881. consume(_CLASS);
  882. def:=object_dec(odt_class,name,genericdef,genericlist,nil);
  883. end;
  884. _OBJECT :
  885. begin
  886. consume(_OBJECT);
  887. def:=object_dec(odt_object,name,genericdef,genericlist,nil);
  888. end;
  889. else
  890. def:=record_dec;
  891. end;
  892. current_settings.packrecords:=oldpackrecords;
  893. end;
  894. end;
  895. _DISPINTERFACE :
  896. begin
  897. { need extra check here since interface is a keyword
  898. in all pascal modes }
  899. if not(m_class in current_settings.modeswitches) then
  900. Message(parser_f_need_objfpc_or_delphi_mode);
  901. consume(token);
  902. def:=object_dec(odt_dispinterface,name,genericdef,genericlist,nil);
  903. end;
  904. _CLASS :
  905. begin
  906. consume(token);
  907. { Delphi only allows class of in type blocks }
  908. if (token=_OF) and
  909. (
  910. not(m_delphi in current_settings.modeswitches) or
  911. (block_type=bt_type)
  912. ) then
  913. begin
  914. consume(_OF);
  915. single_type(hdef,(block_type=bt_type),false);
  916. if is_class(hdef) or
  917. is_objcclass(hdef) then
  918. def:=tclassrefdef.create(hdef)
  919. else
  920. if hdef.typ=forwarddef then
  921. begin
  922. def:=tclassrefdef.create(hdef);
  923. current_module.checkforwarddefs.add(def);
  924. end
  925. else
  926. Message1(type_e_class_or_objcclass_type_expected,hdef.typename);
  927. end
  928. else
  929. def:=object_dec(odt_class,name,genericdef,genericlist,nil);
  930. end;
  931. _CPPCLASS :
  932. begin
  933. consume(token);
  934. def:=object_dec(odt_cppclass,name,genericdef,genericlist,nil);
  935. end;
  936. _OBJCCLASS :
  937. begin
  938. if not(m_objectivec1 in current_settings.modeswitches) then
  939. Message(parser_f_need_objc);
  940. consume(token);
  941. def:=object_dec(odt_objcclass,name,genericdef,genericlist,nil);
  942. end;
  943. _INTERFACE :
  944. begin
  945. { need extra check here since interface is a keyword
  946. in all pascal modes }
  947. if not(m_class in current_settings.modeswitches) then
  948. Message(parser_f_need_objfpc_or_delphi_mode);
  949. consume(token);
  950. if current_settings.interfacetype=it_interfacecom then
  951. def:=object_dec(odt_interfacecom,name,genericdef,genericlist,nil)
  952. else {it_interfacecorba}
  953. def:=object_dec(odt_interfacecorba,name,genericdef,genericlist,nil);
  954. end;
  955. _OBJCPROTOCOL :
  956. begin
  957. if not(m_objectivec1 in current_settings.modeswitches) then
  958. Message(parser_f_need_objc);
  959. consume(token);
  960. def:=object_dec(odt_objcprotocol,name,genericdef,genericlist,nil);
  961. end;
  962. _OBJECT :
  963. begin
  964. consume(token);
  965. def:=object_dec(odt_object,name,genericdef,genericlist,nil);
  966. end;
  967. _PROCEDURE,
  968. _FUNCTION:
  969. begin
  970. is_func:=(token=_FUNCTION);
  971. consume(token);
  972. pd:=tprocvardef.create(normal_function_level);
  973. if token=_LKLAMMER then
  974. parse_parameter_dec(pd);
  975. if is_func then
  976. begin
  977. consume(_COLON);
  978. single_type(pd.returndef,false,false);
  979. end;
  980. if token=_OF then
  981. begin
  982. consume(_OF);
  983. consume(_OBJECT);
  984. include(pd.procoptions,po_methodpointer);
  985. end;
  986. def:=pd;
  987. { possible proc directives }
  988. if parseprocvardir then
  989. begin
  990. if check_proc_directive(true) then
  991. begin
  992. newtype:=ttypesym.create('unnamed',def);
  993. parse_var_proc_directives(tsym(newtype));
  994. newtype.typedef:=nil;
  995. def.typesym:=nil;
  996. newtype.free;
  997. end;
  998. { Add implicit hidden parameters and function result }
  999. handle_calling_convention(pd);
  1000. end;
  1001. end;
  1002. else
  1003. expr_type;
  1004. end;
  1005. if def=nil then
  1006. def:=generrordef;
  1007. end;
  1008. procedure read_anon_type(var def : tdef;parseprocvardir:boolean);
  1009. begin
  1010. read_named_type(def,'',nil,nil,parseprocvardir);
  1011. end;
  1012. procedure write_persistent_type_info(st:tsymtable);
  1013. var
  1014. i : longint;
  1015. def : tdef;
  1016. vmtwriter : TVMTWriter;
  1017. begin
  1018. for i:=0 to st.DefList.Count-1 do
  1019. begin
  1020. def:=tdef(st.DefList[i]);
  1021. case def.typ of
  1022. recorddef :
  1023. write_persistent_type_info(trecorddef(def).symtable);
  1024. objectdef :
  1025. begin
  1026. { Skip generics and forward defs }
  1027. if (df_generic in def.defoptions) or
  1028. (oo_is_forward in tobjectdef(def).objectoptions) then
  1029. continue;
  1030. write_persistent_type_info(tobjectdef(def).symtable);
  1031. { Write also VMT if not done yet }
  1032. if not(ds_vmt_written in def.defstates) then
  1033. begin
  1034. vmtwriter:=TVMTWriter.create(tobjectdef(def));
  1035. if is_interface(tobjectdef(def)) then
  1036. vmtwriter.writeinterfaceids;
  1037. if (oo_has_vmt in tobjectdef(def).objectoptions) then
  1038. vmtwriter.writevmt;
  1039. vmtwriter.free;
  1040. include(def.defstates,ds_vmt_written);
  1041. end;
  1042. end;
  1043. procdef :
  1044. begin
  1045. if assigned(tprocdef(def).localst) and
  1046. (tprocdef(def).localst.symtabletype=localsymtable) then
  1047. write_persistent_type_info(tprocdef(def).localst);
  1048. if assigned(tprocdef(def).parast) then
  1049. write_persistent_type_info(tprocdef(def).parast);
  1050. end;
  1051. end;
  1052. { generate always persistent tables for types in the interface so it can
  1053. be reused in other units and give always the same pointer location. }
  1054. { Init }
  1055. if (
  1056. assigned(def.typesym) and
  1057. (st.symtabletype=globalsymtable) and
  1058. not is_objc_class_or_protocol(def)
  1059. ) or
  1060. def.needs_inittable or
  1061. (ds_init_table_used in def.defstates) then
  1062. RTTIWriter.write_rtti(def,initrtti);
  1063. { RTTI }
  1064. if (
  1065. assigned(def.typesym) and
  1066. (st.symtabletype=globalsymtable) and
  1067. not is_objc_class_or_protocol(def)
  1068. ) or
  1069. (ds_rtti_table_used in def.defstates) then
  1070. RTTIWriter.write_rtti(def,fullrtti);
  1071. end;
  1072. end;
  1073. end.