ptype.pas 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. Does parsing types for Free Pascal
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit ptype;
  19. interface
  20. uses
  21. globtype,symtable
  22. {$IFDEF NEWST}
  23. ,symbols,defs
  24. {$ENDIF NEWST};
  25. const
  26. { forward types should only be possible inside a TYPE statement }
  27. typecanbeforward : boolean = false;
  28. var
  29. { hack, which allows to use the current parsed }
  30. { object type as function argument type }
  31. testcurobject : byte;
  32. curobjectname : stringid;
  33. { parses a string declaration }
  34. function string_dec : pdef;
  35. { parses a object declaration }
  36. function object_dec(const n : stringid;fd : pobjectdef) : pdef;
  37. { reads a string, file type or a type id and returns a name and }
  38. { pdef }
  39. {$IFDEF NEWST}
  40. procedure single_type(var tt:Tdef;var s : string;isforwarddef:boolean);
  41. procedure read_type(var tt:Tdef;const name : stringid);
  42. {$ELSE}
  43. procedure single_type(var tt:ttype;var s : string;isforwarddef:boolean);
  44. procedure read_type(var tt:ttype;const name : stringid);
  45. {$ENDIF NEWST}
  46. implementation
  47. uses
  48. cobjects,globals,verbose,systems,tokens,
  49. aasm,symconst,types,
  50. {$ifdef GDB}
  51. gdb,
  52. {$endif}
  53. tree,hcodegen,hcgdata,
  54. scanner,pbase,pexpr,pdecl,psub,
  55. {$ifdef newcg}
  56. cgbase,
  57. {$else}
  58. tccnv,
  59. {$endif}
  60. pass_1;
  61. function string_dec : pdef;
  62. { reads a string type with optional length }
  63. { and returns a pointer to the string }
  64. { definition }
  65. var
  66. p : ptree;
  67. d : pdef;
  68. begin
  69. consume(_STRING);
  70. if token=_LECKKLAMMER then
  71. begin
  72. consume(_LECKKLAMMER);
  73. p:=comp_expr(true);
  74. do_firstpass(p);
  75. if not is_constintnode(p) then
  76. Message(cg_e_illegal_expression);
  77. if (p^.value<=0) then
  78. begin
  79. Message(parser_e_invalid_string_size);
  80. p^.value:=255;
  81. end;
  82. consume(_RECKKLAMMER);
  83. if p^.value>255 then
  84. d:=new(pstringdef,longinit(p^.value))
  85. else
  86. if p^.value<>255 then
  87. d:=new(pstringdef,shortinit(p^.value))
  88. else
  89. d:=cshortstringdef;
  90. disposetree(p);
  91. end
  92. else
  93. begin
  94. if cs_ansistrings in aktlocalswitches then
  95. d:=cansistringdef
  96. else
  97. d:=cshortstringdef;
  98. end;
  99. string_dec:=d;
  100. end;
  101. procedure id_type(var tt : ttype;var s : string;isforwarddef:boolean);
  102. { reads a type definition }
  103. { to a appropriating pdef, s gets the name of }
  104. { the type to allow name mangling }
  105. var
  106. is_unit_specific : boolean;
  107. pos : tfileposinfo;
  108. begin
  109. s:=pattern;
  110. pos:=tokenpos;
  111. { classes can be used also in classes }
  112. if (curobjectname=pattern) and aktobjectdef^.is_class then
  113. begin
  114. tt.setdef(aktobjectdef);
  115. consume(_ID);
  116. exit;
  117. end;
  118. { objects can be parameters }
  119. if (testcurobject=2) and (curobjectname=pattern) then
  120. begin
  121. tt.setdef(aktobjectdef);
  122. consume(_ID);
  123. exit;
  124. end;
  125. { try to load the symbol to see if it's a unitsym }
  126. is_unit_specific:=false;
  127. getsym(s,false);
  128. consume(_ID);
  129. if assigned(srsym) and
  130. (srsym^.typ=unitsym) then
  131. begin
  132. consume(_POINT);
  133. getsymonlyin(punitsym(srsym)^.unitsymtable,pattern);
  134. pos:=tokenpos;
  135. s:=pattern;
  136. consume(_ID);
  137. is_unit_specific:=true;
  138. end;
  139. { are we parsing a possible forward def ? }
  140. if isforwarddef and
  141. not(is_unit_specific) then
  142. begin
  143. tt.setdef(new(pforwarddef,init(s,pos)));
  144. exit;
  145. end;
  146. { unknown sym ? }
  147. if not assigned(srsym) then
  148. begin
  149. Message1(sym_e_id_not_found,s);
  150. tt.setdef(generrordef);
  151. exit;
  152. end;
  153. { type sym ? }
  154. if (srsym^.typ<>typesym) then
  155. begin
  156. Message(type_e_type_id_expected);
  157. tt.setdef(generrordef);
  158. exit;
  159. end;
  160. { Types are first defined with an error def before assigning
  161. the real type so check if it's an errordef. if so then
  162. give an error }
  163. if (ptypesym(srsym)^.restype.def=generrordef) then
  164. begin
  165. Message(sym_e_error_in_type_def);
  166. exit;
  167. end;
  168. { Only use the definitions for system/current unit, becuase
  169. they can be refered from the parameters and symbols are not
  170. loaded at that time. A symbol reference to an other unit
  171. is still possible, because it's already loaded (PFV)
  172. can't use in [] here, becuase unitid can be > 255 }
  173. if (ptypesym(srsym)^.owner^.unitid=0) or
  174. (ptypesym(srsym)^.owner^.unitid=1) then
  175. tt.setdef(ptypesym(srsym)^.restype.def)
  176. else
  177. tt.setsym(srsym);
  178. end;
  179. procedure single_type(var tt:ttype;var s : string;isforwarddef:boolean);
  180. { reads a string, file type or a type id and returns a name and }
  181. { pdef }
  182. var
  183. hs : string;
  184. t2 : ttype;
  185. begin
  186. case token of
  187. _STRING:
  188. begin
  189. tt.setdef(string_dec);
  190. s:='STRING';
  191. end;
  192. _FILE:
  193. begin
  194. consume(_FILE);
  195. if token=_OF then
  196. begin
  197. consume(_OF);
  198. single_type(t2,hs,false);
  199. tt.setdef(new(pfiledef,inittyped(t2)));
  200. s:='FILE$OF$'+hs;
  201. end
  202. else
  203. begin
  204. tt.setdef(cfiledef);
  205. s:='FILE';
  206. end;
  207. end;
  208. else
  209. begin
  210. id_type(tt,s,isforwarddef);
  211. end;
  212. end;
  213. end;
  214. function object_dec(const n : stringid;fd : pobjectdef) : pdef;
  215. { this function parses an object or class declaration }
  216. var
  217. actmembertype : tsymoptions;
  218. there_is_a_destructor : boolean;
  219. classtype : (ct_object,ct_class,ct_interface,ct_cppclass);
  220. childof : pobjectdef;
  221. aktclass : pobjectdef;
  222. procedure constructor_head;
  223. begin
  224. consume(_CONSTRUCTOR);
  225. { must be at same level as in implementation }
  226. inc(lexlevel);
  227. parse_proc_head(potype_constructor);
  228. dec(lexlevel);
  229. if (cs_constructor_name in aktglobalswitches) and (aktprocsym^.name<>'INIT') then
  230. Message(parser_e_constructorname_must_be_init);
  231. include(aktclass^.objectoptions,oo_has_constructor);
  232. consume(_SEMICOLON);
  233. begin
  234. if (aktclass^.is_class) then
  235. begin
  236. { CLASS constructors return the created instance }
  237. aktprocsym^.definition^.rettype.def:=aktclass;
  238. end
  239. else
  240. begin
  241. { OBJECT constructors return a boolean }
  242. aktprocsym^.definition^.rettype.setdef(booldef);
  243. end;
  244. end;
  245. end;
  246. procedure property_dec;
  247. var
  248. sym : psym;
  249. propertyparas : plinkedlist;
  250. { returns the matching procedure to access a property }
  251. function get_procdef : pprocdef;
  252. var
  253. p : pprocdef;
  254. begin
  255. p:=pprocsym(sym)^.definition;
  256. get_procdef:=nil;
  257. while assigned(p) do
  258. begin
  259. if equal_paras(p^.para,propertyparas,cp_value_equal_const) then
  260. break;
  261. p:=p^.nextoverloaded;
  262. end;
  263. get_procdef:=p;
  264. end;
  265. var
  266. hp2,datacoll : pparaitem;
  267. p : ppropertysym;
  268. overriden : psym;
  269. hs : string;
  270. varspez : tvarspez;
  271. sc : pstringcontainer;
  272. s : string;
  273. tt : ttype;
  274. declarepos : tfileposinfo;
  275. pp : pprocdef;
  276. pt : ptree;
  277. propname : stringid;
  278. begin
  279. { check for a class }
  280. if not(aktclass^.is_class) then
  281. Message(parser_e_syntax_error);
  282. consume(_PROPERTY);
  283. new(propertyparas,init);
  284. datacoll:=nil;
  285. if token=_ID then
  286. begin
  287. p:=new(ppropertysym,init(pattern));
  288. propname:=pattern;
  289. consume(_ID);
  290. { property parameters ? }
  291. if token=_LECKKLAMMER then
  292. begin
  293. if (sp_published in current_object_option) then
  294. Message(parser_e_cant_publish_that_property);
  295. { create a list of the parameters in propertyparas }
  296. consume(_LECKKLAMMER);
  297. inc(testcurobject);
  298. repeat
  299. if token=_VAR then
  300. begin
  301. consume(_VAR);
  302. varspez:=vs_var;
  303. end
  304. else if token=_CONST then
  305. begin
  306. consume(_CONST);
  307. varspez:=vs_const;
  308. end
  309. else if token=_OUT then
  310. begin
  311. consume(_OUT);
  312. varspez:=vs_out;
  313. end
  314. else varspez:=vs_value;
  315. sc:=idlist;
  316. {$ifdef fixLeaksOnError}
  317. strContStack.push(sc);
  318. {$endif fixLeaksOnError}
  319. if token=_COLON then
  320. begin
  321. consume(_COLON);
  322. if token=_ARRAY then
  323. begin
  324. {
  325. if (varspez<>vs_const) and
  326. (varspez<>vs_var) then
  327. begin
  328. varspez:=vs_const;
  329. Message(parser_e_illegal_open_parameter);
  330. end;
  331. }
  332. consume(_ARRAY);
  333. consume(_OF);
  334. { define range and type of range }
  335. tt.setdef(new(parraydef,init(0,-1,s32bitdef)));
  336. { define field type }
  337. single_type(parraydef(tt.def)^.elementtype,s,false);
  338. end
  339. else
  340. single_type(tt,s,false);
  341. end
  342. else
  343. tt.setdef(cformaldef);
  344. repeat
  345. s:=sc^.get_with_tokeninfo(declarepos);
  346. if s='' then
  347. break;
  348. new(hp2,init);
  349. hp2^.paratyp:=varspez;
  350. hp2^.paratype:=tt;
  351. propertyparas^.insert(hp2);
  352. until false;
  353. {$ifdef fixLeaksOnError}
  354. if strContStack.pop <> sc then
  355. writeln('problem with strContStack in ptype');
  356. {$endif fixLeaksOnError}
  357. dispose(sc,done);
  358. until not try_to_consume(_SEMICOLON);
  359. dec(testcurobject);
  360. consume(_RECKKLAMMER);
  361. end;
  362. { overriden property ? }
  363. { force property interface, if there is a property parameter }
  364. if (token=_COLON) or not(propertyparas^.empty) then
  365. begin
  366. consume(_COLON);
  367. single_type(p^.proptype,hs,false);
  368. if (idtoken=_INDEX) then
  369. begin
  370. consume(_INDEX);
  371. pt:=comp_expr(true);
  372. do_firstpass(pt);
  373. if not(is_ordinal(pt^.resulttype)) or
  374. is_64bitint(pt^.resulttype) then
  375. Message(parser_e_invalid_property_index_value);
  376. p^.index:=pt^.value;
  377. p^.indextype.setdef(pt^.resulttype);
  378. include(p^.propoptions,ppo_indexed);
  379. { concat a longint to the para template }
  380. new(hp2,init);
  381. hp2^.paratyp:=vs_value;
  382. hp2^.paratype:=p^.indextype;
  383. propertyparas^.insert(hp2);
  384. disposetree(pt);
  385. end;
  386. { the parser need to know if a property has parameters }
  387. if not(propertyparas^.empty) then
  388. p^.propoptions:=p^.propoptions+[ppo_hasparameters];
  389. end
  390. else
  391. begin
  392. { do an property override }
  393. overriden:=search_class_member(aktclass,propname);
  394. if assigned(overriden) and (overriden^.typ=propertysym) then
  395. begin
  396. p^.dooverride(ppropertysym(overriden));
  397. end
  398. else
  399. begin
  400. p^.proptype.setdef(generrordef);
  401. message(parser_e_no_property_found_to_override);
  402. end;
  403. end;
  404. if (sp_published in current_object_option) and
  405. not(p^.proptype.def^.is_publishable) then
  406. Message(parser_e_cant_publish_that_property);
  407. { create data defcoll to allow correct parameter checks }
  408. new(datacoll,init);
  409. datacoll^.paratyp:=vs_value;
  410. datacoll^.paratype:=p^.proptype;
  411. if (idtoken=_READ) then
  412. begin
  413. p^.readaccess^.clear;
  414. consume(_READ);
  415. sym:=search_class_member(aktclass,pattern);
  416. if not(assigned(sym)) then
  417. begin
  418. Message1(sym_e_unknown_id,pattern);
  419. consume(_ID);
  420. end
  421. else
  422. begin
  423. consume(_ID);
  424. while (token=_POINT) and
  425. ((sym^.typ=varsym) and
  426. (pvarsym(sym)^.vartype.def^.deftype=recorddef)) do
  427. begin
  428. p^.readaccess^.addsym(sym);
  429. consume(_POINT);
  430. getsymonlyin(precorddef(pvarsym(sym)^.vartype.def)^.symtable,pattern);
  431. if not assigned(srsym) then
  432. Message1(sym_e_illegal_field,pattern);
  433. sym:=srsym;
  434. consume(_ID);
  435. end;
  436. end;
  437. if assigned(sym) then
  438. begin
  439. { search the matching definition }
  440. case sym^.typ of
  441. procsym :
  442. begin
  443. pp:=get_procdef;
  444. if not(assigned(pp)) or
  445. not(is_equal(pp^.rettype.def,p^.proptype.def)) then
  446. Message(parser_e_ill_property_access_sym);
  447. p^.readaccess^.setdef(pp);
  448. end;
  449. varsym :
  450. begin
  451. if not(propertyparas^.empty) or
  452. not(is_equal(pvarsym(sym)^.vartype.def,p^.proptype.def)) then
  453. Message(parser_e_ill_property_access_sym);
  454. end;
  455. else
  456. Message(parser_e_ill_property_access_sym);
  457. end;
  458. p^.readaccess^.addsym(sym);
  459. end;
  460. end;
  461. if (idtoken=_WRITE) then
  462. begin
  463. p^.writeaccess^.clear;
  464. consume(_WRITE);
  465. sym:=search_class_member(aktclass,pattern);
  466. if not(assigned(sym)) then
  467. begin
  468. Message1(sym_e_unknown_id,pattern);
  469. consume(_ID);
  470. end
  471. else
  472. begin
  473. consume(_ID);
  474. while (token=_POINT) and
  475. ((sym^.typ=varsym) and
  476. (pvarsym(sym)^.vartype.def^.deftype=recorddef)) do
  477. begin
  478. p^.writeaccess^.addsym(sym);
  479. consume(_POINT);
  480. getsymonlyin(precorddef(pvarsym(sym)^.vartype.def)^.symtable,pattern);
  481. if not assigned(srsym) then
  482. Message1(sym_e_illegal_field,pattern);
  483. sym:=srsym;
  484. consume(_ID);
  485. end;
  486. end;
  487. if assigned(sym) then
  488. begin
  489. { search the matching definition }
  490. case sym^.typ of
  491. procsym :
  492. begin
  493. { insert data entry to check access method }
  494. propertyparas^.insert(datacoll);
  495. pp:=get_procdef;
  496. { ... and remove it }
  497. propertyparas^.remove(datacoll);
  498. if not(assigned(pp)) then
  499. Message(parser_e_ill_property_access_sym);
  500. p^.writeaccess^.setdef(pp);
  501. end;
  502. varsym :
  503. begin
  504. if not(propertyparas^.empty) or
  505. not(is_equal(pvarsym(sym)^.vartype.def,p^.proptype.def)) then
  506. Message(parser_e_ill_property_access_sym);
  507. end
  508. else
  509. Message(parser_e_ill_property_access_sym);
  510. end;
  511. p^.writeaccess^.addsym(sym);
  512. end;
  513. end;
  514. include(p^.propoptions,ppo_stored);
  515. if (idtoken=_STORED) then
  516. begin
  517. consume(_STORED);
  518. p^.storedaccess^.clear;
  519. case token of
  520. _ID:
  521. { in the case that idtoken=_DEFAULT }
  522. { we have to do nothing except }
  523. { setting ppo_stored, it's the same }
  524. { as stored true }
  525. if idtoken<>_DEFAULT then
  526. begin
  527. sym:=search_class_member(aktclass,pattern);
  528. if not(assigned(sym)) then
  529. begin
  530. Message1(sym_e_unknown_id,pattern);
  531. consume(_ID);
  532. end
  533. else
  534. begin
  535. consume(_ID);
  536. while (token=_POINT) and
  537. ((sym^.typ=varsym) and
  538. (pvarsym(sym)^.vartype.def^.deftype=recorddef)) do
  539. begin
  540. p^.storedaccess^.addsym(sym);
  541. consume(_POINT);
  542. getsymonlyin(precorddef(pvarsym(sym)^.vartype.def)^.symtable,pattern);
  543. if not assigned(srsym) then
  544. Message1(sym_e_illegal_field,pattern);
  545. sym:=srsym;
  546. consume(_ID);
  547. end;
  548. end;
  549. if assigned(sym) then
  550. begin
  551. { only non array properties can be stored }
  552. case sym^.typ of
  553. procsym :
  554. begin
  555. pp:=pprocsym(sym)^.definition;
  556. while assigned(pp) do
  557. begin
  558. { the stored function shouldn't have any parameters }
  559. if pp^.para^.empty then
  560. break;
  561. pp:=pp^.nextoverloaded;
  562. end;
  563. { found we a procedure and does it really return a bool? }
  564. if not(assigned(pp)) or
  565. not(is_equal(pp^.rettype.def,booldef)) then
  566. Message(parser_e_ill_property_storage_sym);
  567. p^.storedaccess^.setdef(pp);
  568. end;
  569. varsym :
  570. begin
  571. if not(propertyparas^.empty) or
  572. not(is_equal(pvarsym(sym)^.vartype.def,booldef)) then
  573. Message(parser_e_stored_property_must_be_boolean);
  574. end;
  575. else
  576. Message(parser_e_ill_property_storage_sym);
  577. end;
  578. p^.storedaccess^.addsym(sym);
  579. end;
  580. end;
  581. _FALSE:
  582. begin
  583. consume(_FALSE);
  584. exclude(p^.propoptions,ppo_stored);
  585. end;
  586. _TRUE:
  587. consume(_TRUE);
  588. end;
  589. end;
  590. if (idtoken=_DEFAULT) then
  591. begin
  592. consume(_DEFAULT);
  593. if not(is_ordinal(p^.proptype.def) or
  594. is_64bitint(p^.proptype.def) or
  595. ((p^.proptype.def^.deftype=setdef) and
  596. (psetdef(p^.proptype.def)^.settype=smallset))) or
  597. not(propertyparas^.empty) then
  598. Message(parser_e_property_cant_have_a_default_value);
  599. { Get the result of the default, the firstpass is
  600. needed to support values like -1 }
  601. pt:=comp_expr(true);
  602. do_firstpass(pt);
  603. if p^.proptype.def^.deftype=setdef then
  604. begin
  605. {$ifndef newcg}
  606. {!!!!!!!!!!}
  607. arrayconstructor_to_set(pt);
  608. {$endif newcg}
  609. do_firstpass(pt);
  610. end;
  611. pt:=gentypeconvnode(pt,p^.proptype.def);
  612. do_firstpass(pt);
  613. if not(is_constnode(pt)) then
  614. Message(parser_e_property_default_value_must_const);
  615. if pt^.treetype=setconstn then
  616. p^.default:=plongint(pt^.value_set)^
  617. else
  618. p^.default:=pt^.value;
  619. disposetree(pt);
  620. end
  621. else if (idtoken=_NODEFAULT) then
  622. begin
  623. consume(_NODEFAULT);
  624. p^.default:=0;
  625. end;
  626. symtablestack^.insert(p);
  627. { default property ? }
  628. consume(_SEMICOLON);
  629. if (idtoken=_DEFAULT) then
  630. begin
  631. consume(_DEFAULT);
  632. { overriding a default propertyp is allowed
  633. p2:=search_default_property(aktclass);
  634. if assigned(p2) then
  635. message1(parser_e_only_one_default_property,
  636. pobjectdef(p2^.owner^.defowner)^.objname^)
  637. else
  638. }
  639. begin
  640. include(p^.propoptions,ppo_defaultproperty);
  641. if propertyparas^.empty then
  642. message(parser_e_property_need_paras);
  643. end;
  644. consume(_SEMICOLON);
  645. end;
  646. { clean up }
  647. if assigned(datacoll) then
  648. dispose(datacoll,done);
  649. end
  650. else
  651. begin
  652. consume(_ID);
  653. consume(_SEMICOLON);
  654. end;
  655. dispose(propertyparas,done);
  656. end;
  657. procedure destructor_head;
  658. begin
  659. consume(_DESTRUCTOR);
  660. inc(lexlevel);
  661. parse_proc_head(potype_destructor);
  662. dec(lexlevel);
  663. if (cs_constructor_name in aktglobalswitches) and (aktprocsym^.name<>'DONE') then
  664. Message(parser_e_destructorname_must_be_done);
  665. include(aktclass^.objectoptions,oo_has_destructor);
  666. consume(_SEMICOLON);
  667. if not(aktprocsym^.definition^.para^.empty) then
  668. if not (m_tp in aktmodeswitches) then
  669. Message(parser_e_no_paras_for_destructor);
  670. { no return value }
  671. aktprocsym^.definition^.rettype.def:=voiddef;
  672. end;
  673. var
  674. hs : string;
  675. pcrd : pclassrefdef;
  676. tt : ttype;
  677. oldprocinfo : pprocinfo;
  678. oldprocsym : pprocsym;
  679. oldparse_only : boolean;
  680. methodnametable,intmessagetable,
  681. strmessagetable,classnamelabel,
  682. fieldtablelabel : pasmlabel;
  683. storetypecanbeforward : boolean;
  684. procedure setclassattributes;
  685. begin
  686. if classtype=ct_class then
  687. begin
  688. include(aktclass^.objectoptions,oo_is_class);
  689. if (cs_generate_rtti in aktlocalswitches) or
  690. (assigned(aktclass^.childof) and
  691. (oo_can_have_published in aktclass^.childof^.objectoptions)) then
  692. begin
  693. include(aktclass^.objectoptions,oo_can_have_published);
  694. { in "publishable" classes the default access type is published }
  695. actmembertype:=[sp_published];
  696. { don't know if this is necessary (FK) }
  697. current_object_option:=[sp_published];
  698. end;
  699. end;
  700. end;
  701. procedure setclassparent;
  702. begin
  703. { is the current class tobject? }
  704. { so you could define your own tobject }
  705. if (cs_compilesystem in aktmoduleswitches) and
  706. (n='TOBJECT') then
  707. begin
  708. if assigned(fd) then
  709. aktclass:=fd
  710. else
  711. aktclass:=new(pobjectdef,init(n,nil));
  712. class_tobject:=aktclass;
  713. end
  714. else
  715. begin
  716. childof:=class_tobject;
  717. if assigned(fd) then
  718. begin
  719. { the forward of the child must be resolved to get
  720. correct field addresses
  721. }
  722. if (oo_is_forward in childof^.objectoptions) then
  723. Message1(parser_e_forward_declaration_must_be_resolved,childof^.objname^);
  724. aktclass:=fd;
  725. aktclass^.set_parent(childof);
  726. end
  727. else
  728. begin
  729. aktclass:=new(pobjectdef,init(n,childof));
  730. aktclass^.set_parent(childof);
  731. end;
  732. end;
  733. end;
  734. { generates the vmt for classes as well as for objects }
  735. procedure writevmt;
  736. var
  737. vmtlist : taasmoutput;
  738. {$ifdef WITHDMT}
  739. dmtlabel : pasmlabel;
  740. {$endif WITHDMT}
  741. begin
  742. {$ifdef WITHDMT}
  743. dmtlabel:=gendmt(aktclass);
  744. {$endif WITHDMT}
  745. { this generates the entries }
  746. vmtlist.init;
  747. genvmt(@vmtlist,aktclass);
  748. { write tables for classes, this must be done before the actual
  749. class is written, because we need the labels defined }
  750. if classtype=ct_class then
  751. begin
  752. methodnametable:=genpublishedmethodstable(aktclass);
  753. fieldtablelabel:=aktclass^.generate_field_table;
  754. { rtti }
  755. if (oo_can_have_published in aktclass^.objectoptions) then
  756. aktclass^.generate_rtti;
  757. { write class name }
  758. getdatalabel(classnamelabel);
  759. datasegment^.concat(new(pai_label,init(classnamelabel)));
  760. datasegment^.concat(new(pai_const,init_8bit(length(aktclass^.objname^))));
  761. datasegment^.concat(new(pai_string,init(aktclass^.objname^)));
  762. { generate message and dynamic tables }
  763. if (oo_has_msgstr in aktclass^.objectoptions) then
  764. strmessagetable:=genstrmsgtab(aktclass);
  765. if (oo_has_msgint in aktclass^.objectoptions) then
  766. intmessagetable:=genintmsgtab(aktclass)
  767. else
  768. datasegment^.concat(new(pai_const,init_32bit(0)));
  769. end;
  770. { write debug info }
  771. {$ifdef GDB}
  772. if (cs_debuginfo in aktmoduleswitches) then
  773. begin
  774. do_count_dbx:=true;
  775. if assigned(aktclass^.owner) and assigned(aktclass^.owner^.name) then
  776. datasegment^.concat(new(pai_stabs,init(strpnew('"vmt_'+aktclass^.owner^.name^+n+':S'+
  777. typeglobalnumber('__vtbl_ptr_type')+'",'+tostr(N_STSYM)+',0,0,'+aktclass^.vmt_mangledname))));
  778. end;
  779. {$endif GDB}
  780. datasegment^.concat(new(pai_symbol,initdataname_global(aktclass^.vmt_mangledname,0)));
  781. { determine the size with symtable^.datasize, because }
  782. { size gives back 4 for classes }
  783. datasegment^.concat(new(pai_const,init_32bit(aktclass^.symtable^.datasize)));
  784. datasegment^.concat(new(pai_const,init_32bit(-aktclass^.symtable^.datasize)));
  785. {$ifdef WITHDMT}
  786. if classtype=ct_object then
  787. begin
  788. if assigned(dmtlabel) then
  789. datasegment^.concat(new(pai_const_symbol,init(dmtlabel)))
  790. else
  791. datasegment^.concat(new(pai_const,init_32bit(0)));
  792. end;
  793. {$endif WITHDMT}
  794. { write pointer to parent VMT, this isn't implemented in TP }
  795. { but this is not used in FPC ? (PM) }
  796. { it's not used yet, but the delphi-operators as and is need it (FK) }
  797. { it is not written for parents that don't have any vmt !! }
  798. if assigned(aktclass^.childof) and
  799. (oo_has_vmt in aktclass^.childof^.objectoptions) then
  800. datasegment^.concat(new(pai_const_symbol,initname(aktclass^.childof^.vmt_mangledname)))
  801. else
  802. datasegment^.concat(new(pai_const,init_32bit(0)));
  803. { write extended info for classes, for the order see rtl/inc/objpash.inc }
  804. if classtype=ct_class then
  805. begin
  806. { pointer to class name string }
  807. datasegment^.concat(new(pai_const_symbol,init(classnamelabel)));
  808. { pointer to dynamic table }
  809. if (oo_has_msgint in aktclass^.objectoptions) then
  810. datasegment^.concat(new(pai_const_symbol,init(intmessagetable)))
  811. else
  812. datasegment^.concat(new(pai_const,init_32bit(0)));
  813. { pointer to method table }
  814. if assigned(methodnametable) then
  815. datasegment^.concat(new(pai_const_symbol,init(methodnametable)))
  816. else
  817. datasegment^.concat(new(pai_const,init_32bit(0)));
  818. { pointer to field table }
  819. datasegment^.concat(new(pai_const_symbol,init(fieldtablelabel)));
  820. { pointer to type info of published section }
  821. if (oo_can_have_published in aktclass^.objectoptions) then
  822. datasegment^.concat(new(pai_const_symbol,initname(aktclass^.rtti_name)))
  823. else
  824. datasegment^.concat(new(pai_const,init_32bit(0)));
  825. { inittable for con-/destruction }
  826. {
  827. if aktclass^.needs_inittable then
  828. }
  829. { we generate the init table for classes always, because needs_inittable }
  830. { for classes is always false, it applies only for objects }
  831. datasegment^.concat(new(pai_const_symbol,init(aktclass^.get_inittable_label)));
  832. {
  833. else
  834. datasegment^.concat(new(pai_const,init_32bit(0)));
  835. }
  836. { auto table }
  837. datasegment^.concat(new(pai_const,init_32bit(0)));
  838. { interface table }
  839. datasegment^.concat(new(pai_const,init_32bit(0)));
  840. { table for string messages }
  841. if (oo_has_msgstr in aktclass^.objectoptions) then
  842. datasegment^.concat(new(pai_const_symbol,init(strmessagetable)))
  843. else
  844. datasegment^.concat(new(pai_const,init_32bit(0)));
  845. end;
  846. datasegment^.concatlist(@vmtlist);
  847. vmtlist.done;
  848. { write the size of the VMT }
  849. datasegment^.concat(new(pai_symbol_end,initname(aktclass^.vmt_mangledname)));
  850. end;
  851. procedure readparentclasses;
  852. begin
  853. { reads the parent class }
  854. if token=_LKLAMMER then
  855. begin
  856. consume(_LKLAMMER);
  857. id_type(tt,pattern,false);
  858. childof:=pobjectdef(tt.def);
  859. if (childof^.deftype<>objectdef) then
  860. begin
  861. Message1(type_e_class_type_expected,childof^.typename);
  862. childof:=nil;
  863. aktclass:=new(pobjectdef,init(n,nil));
  864. end
  865. else
  866. begin
  867. { a mix of class, interfaces, objects and cppclasses
  868. isn't allowed }
  869. case classtype of
  870. ct_class:
  871. if not(childof^.is_class) and
  872. not(childof^.is_interface) then
  873. Message(parser_e_mix_of_classes_and_objects);
  874. ct_interface:
  875. if not(childof^.is_interface) then
  876. Message(parser_e_mix_of_classes_and_objects);
  877. ct_cppclass:
  878. if not(childof^.is_cppclass) then
  879. Message(parser_e_mix_of_classes_and_objects);
  880. ct_object:
  881. if not(childof^.is_object) then
  882. Message(parser_e_mix_of_classes_and_objects);
  883. end;
  884. { the forward of the child must be resolved to get
  885. correct field addresses }
  886. if assigned(fd) then
  887. begin
  888. if (oo_is_forward in childof^.objectoptions) then
  889. Message1(parser_e_forward_declaration_must_be_resolved,childof^.objname^);
  890. aktclass:=fd;
  891. { we must inherit several options !!
  892. this was missing !!
  893. all is now done in set_parent
  894. including symtable datasize setting PM }
  895. fd^.set_parent(childof);
  896. end
  897. else
  898. aktclass:=new(pobjectdef,init(n,childof));
  899. end;
  900. consume(_RKLAMMER);
  901. end
  902. { if no parent class, then a class get tobject as parent }
  903. else if classtype=ct_class then
  904. setclassparent
  905. else
  906. aktclass:=new(pobjectdef,init(n,nil));
  907. end;
  908. var
  909. temppd : pprocdef;
  910. begin
  911. {Nowadays aktprocsym may already have a value, so we need to save
  912. it.}
  913. oldprocsym:=aktprocsym;
  914. { forward is resolved }
  915. if assigned(fd) then
  916. exclude(fd^.objectoptions,oo_is_forward);
  917. there_is_a_destructor:=false;
  918. actmembertype:=[sp_public];
  919. { objects and class types can't be declared local }
  920. if (symtablestack^.symtabletype<>globalsymtable) and
  921. (symtablestack^.symtabletype<>staticsymtable) then
  922. Message(parser_e_no_local_objects);
  923. storetypecanbeforward:=typecanbeforward;
  924. { for tp mode don't allow forward types }
  925. if (m_tp in aktmodeswitches) and
  926. not (m_delphi in aktmodeswitches) then
  927. typecanbeforward:=false;
  928. { distinguish classes and objects }
  929. case token of
  930. _OBJECT:
  931. begin
  932. classtype:=ct_object;
  933. consume(_OBJECT)
  934. end;
  935. _CPPCLASS:
  936. begin
  937. classtype:=ct_cppclass;
  938. internalerror(2003001);
  939. end;
  940. _INTERFACE:
  941. begin
  942. classtype:=ct_interface;
  943. internalerror(2003002);
  944. end;
  945. _CLASS:
  946. begin
  947. classtype:=ct_class;
  948. consume(_CLASS);
  949. if not(assigned(fd)) and (token=_OF) then
  950. begin
  951. { a hack, but it's easy to handle }
  952. { class reference type }
  953. consume(_OF);
  954. single_type(tt,hs,typecanbeforward);
  955. { accept hp1, if is a forward def or a class }
  956. if (tt.def^.deftype=forwarddef) or
  957. ((tt.def^.deftype=objectdef) and pobjectdef(tt.def)^.is_class) then
  958. begin
  959. pcrd:=new(pclassrefdef,init(tt.def));
  960. object_dec:=pcrd;
  961. end
  962. else
  963. begin
  964. object_dec:=generrordef;
  965. Message1(type_e_class_type_expected,generrordef^.typename);
  966. end;
  967. typecanbeforward:=storetypecanbeforward;
  968. exit;
  969. end
  970. { forward class }
  971. else if not(assigned(fd)) and (token=_SEMICOLON) then
  972. begin
  973. { also anonym objects aren't allow (o : object a : longint; end;) }
  974. if n='' then
  975. begin
  976. Message(parser_f_no_anonym_objects)
  977. end;
  978. if (cs_compilesystem in aktmoduleswitches) and
  979. (n='TOBJECT') then
  980. begin
  981. aktclass:=new(pobjectdef,init(n,nil));
  982. class_tobject:=aktclass;
  983. end
  984. else
  985. aktclass:=new(pobjectdef,init(n,nil));
  986. aktclass^.objectoptions:=aktclass^.objectoptions+[oo_is_class,oo_is_forward];
  987. { all classes must have a vmt !! at offset zero }
  988. if not(oo_has_vmt in aktclass^.objectoptions) then
  989. aktclass^.insertvmt;
  990. object_dec:=aktclass;
  991. typecanbeforward:=storetypecanbeforward;
  992. exit;
  993. end;
  994. end;
  995. else
  996. consume(_OBJECT);
  997. end;
  998. { also anonym objects aren't allow (o : object a : longint; end;) }
  999. if n='' then
  1000. Message(parser_f_no_anonym_objects);
  1001. readparentclasses;
  1002. { default access is public }
  1003. actmembertype:=[sp_public];
  1004. { set class flags and inherits published, if necessary? }
  1005. setclassattributes;
  1006. aktobjectdef:=aktclass;
  1007. aktclass^.symtable^.next:=symtablestack;
  1008. symtablestack:=aktclass^.symtable;
  1009. testcurobject:=1;
  1010. curobjectname:=n;
  1011. { new procinfo }
  1012. oldprocinfo:=procinfo;
  1013. new(procinfo,init);
  1014. procinfo^._class:=aktclass;
  1015. { short class declaration ? }
  1016. if (classtype<>ct_class) or (token<>_SEMICOLON) then
  1017. begin
  1018. { Parse componenten }
  1019. repeat
  1020. if (sp_private in actmembertype) then
  1021. include(aktclass^.objectoptions,oo_has_private);
  1022. if (sp_protected in actmembertype) then
  1023. include(aktclass^.objectoptions,oo_has_protected);
  1024. case token of
  1025. _ID : begin
  1026. case idtoken of
  1027. _PRIVATE : begin
  1028. consume(_PRIVATE);
  1029. actmembertype:=[sp_private];
  1030. current_object_option:=[sp_private];
  1031. end;
  1032. _PROTECTED : begin
  1033. consume(_PROTECTED);
  1034. current_object_option:=[sp_protected];
  1035. actmembertype:=[sp_protected];
  1036. end;
  1037. _PUBLIC : begin
  1038. consume(_PUBLIC);
  1039. current_object_option:=[sp_public];
  1040. actmembertype:=[sp_public];
  1041. end;
  1042. _PUBLISHED : begin
  1043. if not(oo_can_have_published in aktclass^.objectoptions) then
  1044. Message(parser_e_cant_have_published);
  1045. consume(_PUBLISHED);
  1046. current_object_option:=[sp_published];
  1047. actmembertype:=[sp_published];
  1048. end;
  1049. else
  1050. read_var_decs(false,true,false);
  1051. end;
  1052. end;
  1053. _PROPERTY : property_dec;
  1054. _PROCEDURE,
  1055. _FUNCTION,
  1056. _CLASS : begin
  1057. oldparse_only:=parse_only;
  1058. parse_only:=true;
  1059. parse_proc_dec;
  1060. {$ifndef newcg}
  1061. parse_object_proc_directives(aktprocsym);
  1062. {$endif newcg}
  1063. { check if there are duplicates }
  1064. check_identical_proc(temppd);
  1065. if (po_msgint in aktprocsym^.definition^.procoptions) then
  1066. include(aktclass^.objectoptions,oo_has_msgint);
  1067. if (po_msgstr in aktprocsym^.definition^.procoptions) then
  1068. include(aktclass^.objectoptions,oo_has_msgstr);
  1069. if (po_virtualmethod in aktprocsym^.definition^.procoptions) then
  1070. include(aktclass^.objectoptions,oo_has_virtual);
  1071. { C++ classes use always C calling conventions }
  1072. if aktclass^.is_cppclass then
  1073. include(aktprocsym^.definition^.proccalloptions,pocall_cdecl);
  1074. parse_only:=oldparse_only;
  1075. end;
  1076. _CONSTRUCTOR : begin
  1077. if not(sp_public in actmembertype) then
  1078. Message(parser_w_constructor_should_be_public);
  1079. oldparse_only:=parse_only;
  1080. parse_only:=true;
  1081. constructor_head;
  1082. {$ifndef newcg}
  1083. parse_object_proc_directives(aktprocsym);
  1084. {$endif newcg}
  1085. if (po_virtualmethod in aktprocsym^.definition^.procoptions) then
  1086. include(aktclass^.objectoptions,oo_has_virtual);
  1087. { C++ classes use always C calling conventions }
  1088. if aktclass^.is_cppclass then
  1089. include(aktprocsym^.definition^.proccalloptions,pocall_cdecl);
  1090. parse_only:=oldparse_only;
  1091. end;
  1092. _DESTRUCTOR : begin
  1093. if there_is_a_destructor then
  1094. Message(parser_n_only_one_destructor);
  1095. there_is_a_destructor:=true;
  1096. if not(sp_public in actmembertype) then
  1097. Message(parser_w_destructor_should_be_public);
  1098. oldparse_only:=parse_only;
  1099. parse_only:=true;
  1100. destructor_head;
  1101. {$ifndef newcg}
  1102. parse_object_proc_directives(aktprocsym);
  1103. {$endif newcg}
  1104. if (po_virtualmethod in aktprocsym^.definition^.procoptions) then
  1105. include(aktclass^.objectoptions,oo_has_virtual);
  1106. { C++ classes use always C calling conventions }
  1107. if aktclass^.is_cppclass then
  1108. include(aktprocsym^.definition^.proccalloptions,pocall_cdecl);
  1109. parse_only:=oldparse_only;
  1110. end;
  1111. _END : begin
  1112. consume(_END);
  1113. break;
  1114. end;
  1115. else
  1116. consume(_ID); { Give a ident expected message, like tp7 }
  1117. end;
  1118. until false;
  1119. current_object_option:=[sp_public];
  1120. end;
  1121. testcurobject:=0;
  1122. curobjectname:='';
  1123. typecanbeforward:=storetypecanbeforward;
  1124. { generate vmt space if needed }
  1125. if not(oo_has_vmt in aktclass^.objectoptions) and
  1126. ([oo_has_virtual,oo_has_constructor,oo_has_destructor,oo_is_class]*aktclass^.objectoptions<>[]) then
  1127. aktclass^.insertvmt;
  1128. if (cs_create_smart in aktmoduleswitches) then
  1129. datasegment^.concat(new(pai_cut,init));
  1130. if (oo_has_vmt in aktclass^.objectoptions) then
  1131. writevmt;
  1132. { restore old state }
  1133. symtablestack:=symtablestack^.next;
  1134. aktobjectdef:=nil;
  1135. {Restore procinfo}
  1136. dispose(procinfo,done);
  1137. procinfo:=oldprocinfo;
  1138. {Restore the aktprocsym.}
  1139. aktprocsym:=oldprocsym;
  1140. object_dec:=aktclass;
  1141. end;
  1142. { reads a record declaration }
  1143. function record_dec : pdef;
  1144. var
  1145. symtable : psymtable;
  1146. storetypecanbeforward : boolean;
  1147. begin
  1148. { create recdef }
  1149. symtable:=new(psymtable,init(recordsymtable));
  1150. record_dec:=new(precorddef,init(symtable));
  1151. { update symtable stack }
  1152. symtable^.next:=symtablestack;
  1153. symtablestack:=symtable;
  1154. { parse record }
  1155. consume(_RECORD);
  1156. storetypecanbeforward:=typecanbeforward;
  1157. { for tp mode don't allow forward types }
  1158. if m_tp in aktmodeswitches then
  1159. typecanbeforward:=false;
  1160. read_var_decs(true,false,false);
  1161. consume(_END);
  1162. typecanbeforward:=storetypecanbeforward;
  1163. { may be scale record size to a size of n*4 ? }
  1164. symtablestack^.datasize:=align(symtablestack^.datasize,symtablestack^.dataalignment);
  1165. { restore symtable stack }
  1166. symtablestack:=symtable^.next;
  1167. end;
  1168. { reads a type definition and returns a pointer to it }
  1169. procedure read_type(var tt : ttype;const name : stringid);
  1170. var
  1171. pt : ptree;
  1172. tt2 : ttype;
  1173. aktenumdef : penumdef;
  1174. ap : parraydef;
  1175. s : stringid;
  1176. l,v : longint;
  1177. oldaktpackrecords : tpackrecords;
  1178. hs : string;
  1179. defpos,storepos : tfileposinfo;
  1180. procedure expr_type;
  1181. var
  1182. pt1,pt2 : ptree;
  1183. begin
  1184. { use of current parsed object ? }
  1185. if (token=_ID) and (testcurobject=2) and (curobjectname=pattern) then
  1186. begin
  1187. consume(_ID);
  1188. tt.setdef(aktobjectdef);
  1189. exit;
  1190. end;
  1191. { classes can be used also in classes }
  1192. if (curobjectname=pattern) and aktobjectdef^.is_class then
  1193. begin
  1194. tt.setdef(aktobjectdef);
  1195. consume(_ID);
  1196. exit;
  1197. end;
  1198. { we can't accept a equal in type }
  1199. pt1:=comp_expr(not(ignore_equal));
  1200. do_firstpass(pt1);
  1201. if (token=_POINTPOINT) then
  1202. begin
  1203. consume(_POINTPOINT);
  1204. { get high value of range }
  1205. pt2:=comp_expr(not(ignore_equal));
  1206. do_firstpass(pt2);
  1207. { both must be evaluated to constants now }
  1208. if (pt1^.treetype<>ordconstn) or (pt2^.treetype<>ordconstn) then
  1209. Message(sym_e_error_in_type_def)
  1210. else
  1211. begin
  1212. { check types }
  1213. if CheckTypes(pt1^.resulttype,pt2^.resulttype) then
  1214. begin
  1215. { Check bounds }
  1216. if pt2^.value<pt1^.value then
  1217. Message(cg_e_upper_lower_than_lower)
  1218. else
  1219. begin
  1220. { All checks passed, create the new def }
  1221. case pt1^.resulttype^.deftype of
  1222. enumdef : tt.setdef(new(penumdef,init_subrange(penumdef(pt1^.resulttype),pt1^.value,pt2^.value)));
  1223. orddef : begin
  1224. if is_char(pt1^.resulttype) then
  1225. tt.setdef(new(porddef,init(uchar,pt1^.value,pt2^.value)))
  1226. else
  1227. if is_boolean(pt1^.resulttype) then
  1228. tt.setdef(new(porddef,init(bool8bit,pt1^.value,pt2^.value)))
  1229. else
  1230. tt.setdef(new(porddef,init(uauto,pt1^.value,pt2^.value)));
  1231. end;
  1232. end;
  1233. end;
  1234. end;
  1235. end;
  1236. disposetree(pt2);
  1237. end
  1238. else
  1239. begin
  1240. { a simple type renaming }
  1241. if (pt1^.treetype=typen) then
  1242. begin
  1243. if assigned(pt1^.typenodesym) then
  1244. tt.setsym(pt1^.typenodesym)
  1245. else
  1246. tt.setdef(pt1^.resulttype);
  1247. end
  1248. else
  1249. Message(sym_e_error_in_type_def);
  1250. end;
  1251. disposetree(pt1);
  1252. end;
  1253. procedure array_dec;
  1254. var
  1255. lowval,
  1256. highval : longint;
  1257. arraytype : pdef;
  1258. ht : ttype;
  1259. procedure setdefdecl(p:pdef);
  1260. begin
  1261. case p^.deftype of
  1262. enumdef :
  1263. begin
  1264. lowval:=penumdef(p)^.min;
  1265. highval:=penumdef(p)^.max;
  1266. arraytype:=p;
  1267. end;
  1268. orddef :
  1269. begin
  1270. lowval:=porddef(p)^.low;
  1271. highval:=porddef(p)^.high;
  1272. arraytype:=p;
  1273. end;
  1274. else
  1275. Message(sym_e_error_in_type_def);
  1276. end;
  1277. end;
  1278. begin
  1279. consume(_ARRAY);
  1280. consume(_LECKKLAMMER);
  1281. { defaults }
  1282. arraytype:=generrordef;
  1283. lowval:=$80000000;
  1284. highval:=$7fffffff;
  1285. tt.reset;
  1286. repeat
  1287. { read the expression and check it, check apart if the
  1288. declaration is an enum declaration because that needs to
  1289. be parsed by readtype (PFV) }
  1290. if token=_LKLAMMER then
  1291. begin
  1292. read_type(ht,'');
  1293. setdefdecl(ht.def);
  1294. end
  1295. else
  1296. begin
  1297. pt:=expr;
  1298. if pt^.treetype=typen then
  1299. setdefdecl(pt^.resulttype)
  1300. else
  1301. begin
  1302. do_firstpass(pt);
  1303. if (pt^.treetype=rangen) then
  1304. begin
  1305. if (pt^.left^.treetype=ordconstn) and
  1306. (pt^.right^.treetype=ordconstn) then
  1307. begin
  1308. lowval:=pt^.left^.value;
  1309. highval:=pt^.right^.value;
  1310. if highval<lowval then
  1311. begin
  1312. Message(parser_e_array_lower_less_than_upper_bound);
  1313. highval:=lowval;
  1314. end;
  1315. arraytype:=pt^.right^.resulttype;
  1316. end
  1317. else
  1318. Message(type_e_cant_eval_constant_expr);
  1319. end
  1320. else
  1321. Message(sym_e_error_in_type_def)
  1322. end;
  1323. disposetree(pt);
  1324. end;
  1325. { create arraydef }
  1326. if not assigned(tt.def) then
  1327. begin
  1328. ap:=new(parraydef,init(lowval,highval,arraytype));
  1329. tt.setdef(ap);
  1330. end
  1331. else
  1332. begin
  1333. ap^.elementtype.setdef(new(parraydef,init(lowval,highval,arraytype)));
  1334. ap:=parraydef(ap^.elementtype.def);
  1335. end;
  1336. if token=_COMMA then
  1337. consume(_COMMA)
  1338. else
  1339. break;
  1340. until false;
  1341. consume(_RECKKLAMMER);
  1342. consume(_OF);
  1343. read_type(tt2,'');
  1344. { if no error, set element type }
  1345. if assigned(ap) then
  1346. ap^.elementtype:=tt2;
  1347. end;
  1348. begin
  1349. tt.reset;
  1350. case token of
  1351. _STRING,_FILE:
  1352. begin
  1353. single_type(tt,hs,false);
  1354. end;
  1355. _LKLAMMER:
  1356. begin
  1357. consume(_LKLAMMER);
  1358. { allow negativ value_str }
  1359. l:=-1;
  1360. aktenumdef:=new(penumdef,init);
  1361. repeat
  1362. s:=pattern;
  1363. defpos:=tokenpos;
  1364. consume(_ID);
  1365. { only allow assigning of specific numbers under fpc mode }
  1366. if (m_fpc in aktmodeswitches) and
  1367. (token=_ASSIGNMENT) then
  1368. begin
  1369. consume(_ASSIGNMENT);
  1370. v:=get_intconst;
  1371. { please leave that a note, allows type save }
  1372. { declarations in the win32 units ! }
  1373. if v<=l then
  1374. Message(parser_n_duplicate_enum);
  1375. l:=v;
  1376. end
  1377. else
  1378. inc(l);
  1379. storepos:=tokenpos;
  1380. tokenpos:=defpos;
  1381. constsymtable^.insert(new(penumsym,init(s,aktenumdef,l)));
  1382. tokenpos:=storepos;
  1383. if token=_COMMA then
  1384. consume(_COMMA)
  1385. else
  1386. break;
  1387. until false;
  1388. tt.setdef(aktenumdef);
  1389. consume(_RKLAMMER);
  1390. end;
  1391. _ARRAY:
  1392. begin
  1393. array_dec;
  1394. end;
  1395. _SET:
  1396. begin
  1397. consume(_SET);
  1398. consume(_OF);
  1399. read_type(tt2,'');
  1400. if assigned(tt2.def) then
  1401. begin
  1402. case tt2.def^.deftype of
  1403. { don't forget that min can be negativ PM }
  1404. enumdef :
  1405. if penumdef(tt2.def)^.min>=0 then
  1406. tt.setdef(new(psetdef,init(tt2.def,penumdef(tt2.def)^.max)))
  1407. else
  1408. Message(sym_e_ill_type_decl_set);
  1409. orddef :
  1410. begin
  1411. case porddef(tt2.def)^.typ of
  1412. uchar :
  1413. tt.setdef(new(psetdef,init(tt2.def,255)));
  1414. u8bit,u16bit,u32bit,
  1415. s8bit,s16bit,s32bit :
  1416. begin
  1417. if (porddef(tt2.def)^.low>=0) then
  1418. tt.setdef(new(psetdef,init(tt2.def,porddef(tt2.def)^.high)))
  1419. else
  1420. Message(sym_e_ill_type_decl_set);
  1421. end;
  1422. else
  1423. Message(sym_e_ill_type_decl_set);
  1424. end;
  1425. end;
  1426. else
  1427. Message(sym_e_ill_type_decl_set);
  1428. end;
  1429. end
  1430. else
  1431. tt.setdef(generrordef);
  1432. end;
  1433. _CARET:
  1434. begin
  1435. consume(_CARET);
  1436. single_type(tt2,hs,typecanbeforward);
  1437. tt.setdef(new(ppointerdef,init(tt2)));
  1438. end;
  1439. _RECORD:
  1440. begin
  1441. tt.setdef(record_dec);
  1442. end;
  1443. _PACKED:
  1444. begin
  1445. consume(_PACKED);
  1446. if token=_ARRAY then
  1447. array_dec
  1448. else
  1449. begin
  1450. oldaktpackrecords:=aktpackrecords;
  1451. aktpackrecords:=packrecord_1;
  1452. if token in [_CLASS,_OBJECT] then
  1453. tt.setdef(object_dec(name,nil))
  1454. else
  1455. tt.setdef(record_dec);
  1456. aktpackrecords:=oldaktpackrecords;
  1457. end;
  1458. end;
  1459. _CLASS,
  1460. {$ifdef SUPPORTCPPCLASS}
  1461. _CPPCLASS,
  1462. {$endif SUPPORTCPPCLASS}
  1463. {$ifdef SUPPORTINTERFACES}
  1464. _INTERFACE,
  1465. {$endif SUPPORTINTERFACES}
  1466. _OBJECT:
  1467. begin
  1468. tt.setdef(object_dec(name,nil));
  1469. end;
  1470. _PROCEDURE:
  1471. begin
  1472. consume(_PROCEDURE);
  1473. tt.setdef(new(pprocvardef,init));
  1474. if token=_LKLAMMER then
  1475. parameter_dec(pprocvardef(tt.def));
  1476. if token=_OF then
  1477. begin
  1478. consume(_OF);
  1479. consume(_OBJECT);
  1480. include(pprocvardef(tt.def)^.procoptions,po_methodpointer);
  1481. end;
  1482. end;
  1483. _FUNCTION:
  1484. begin
  1485. consume(_FUNCTION);
  1486. tt.def:=new(pprocvardef,init);
  1487. if token=_LKLAMMER then
  1488. parameter_dec(pprocvardef(tt.def));
  1489. consume(_COLON);
  1490. single_type(pprocvardef(tt.def)^.rettype,hs,false);
  1491. if token=_OF then
  1492. begin
  1493. consume(_OF);
  1494. consume(_OBJECT);
  1495. include(pprocvardef(tt.def)^.procoptions,po_methodpointer);
  1496. end;
  1497. end;
  1498. else
  1499. expr_type;
  1500. end;
  1501. if tt.def=nil then
  1502. tt.setdef(generrordef);
  1503. end;
  1504. end.
  1505. {
  1506. $Log$
  1507. Revision 1.6 2000-08-16 18:33:54 peter
  1508. * splitted namedobjectitem.next into indexnext and listnext so it
  1509. can be used in both lists
  1510. * don't allow "word = word" type definitions (merged)
  1511. Revision 1.5 2000/08/06 14:17:15 peter
  1512. * overload fixes (merged)
  1513. Revision 1.4 2000/07/30 17:04:43 peter
  1514. * merged fixes
  1515. Revision 1.3 2000/07/13 12:08:27 michael
  1516. + patched to 1.1.0 with former 1.09patch from peter
  1517. Revision 1.2 2000/07/13 11:32:47 michael
  1518. + removed logs
  1519. }