ptype.pas 36 KB

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