pexpr.pas 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788
  1. {
  2. $Id$
  3. Copyright (c) 1998 by Florian Klaempfl
  4. Does parsing of expression 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 pexpr;
  19. interface
  20. uses symtable,tree;
  21. { reads a whole expression }
  22. function expr : ptree;
  23. { reads an expression without assignements and .. }
  24. function comp_expr(accept_equal : boolean):Ptree;
  25. { reads a single factor }
  26. function factor(getaddr : boolean) : ptree;
  27. { the ID token has to be consumed before calling this function }
  28. procedure do_member_read(const sym : psym;var p1 : ptree;
  29. var pd : pdef;var again : boolean);
  30. function get_intconst:longint;
  31. function get_stringconst:string;
  32. implementation
  33. uses
  34. cobjects,globals,scanner,aasm,pass_1,systems,
  35. hcodegen,types,verbose
  36. { parser specific stuff }
  37. ,pbase,pdecl
  38. { processor specific stuff }
  39. {$ifdef i386}
  40. ,i386
  41. {$endif}
  42. {$ifdef m68k}
  43. ,m68k
  44. {$endif}
  45. ;
  46. function parse_paras(_colon,in_prop_paras : boolean) : ptree;
  47. var
  48. p1,p2 : ptree;
  49. end_of_paras : ttoken;
  50. begin
  51. if in_prop_paras then
  52. end_of_paras:=RECKKLAMMER
  53. else
  54. end_of_paras:=RKLAMMER;
  55. if token=end_of_paras then
  56. begin
  57. parse_paras:=nil;
  58. exit;
  59. end;
  60. p2:=nil;
  61. inc(parsing_para_level);
  62. while true do
  63. begin
  64. p1:=comp_expr(true);
  65. p2:=gencallparanode(p1,p2);
  66. { it's for the str(l:5,s); }
  67. if _colon and (token=COLON) then
  68. begin
  69. consume(COLON);
  70. p1:=comp_expr(true);
  71. p2:=gencallparanode(p1,p2);
  72. p2^.is_colon_para:=true;
  73. if token=COLON then
  74. begin
  75. consume(COLON);
  76. p1:=comp_expr(true);
  77. p2:=gencallparanode(p1,p2);
  78. p2^.is_colon_para:=true;
  79. end
  80. end;
  81. if token=COMMA then
  82. consume(COMMA)
  83. else
  84. break;
  85. end;
  86. dec(parsing_para_level);
  87. parse_paras:=p2;
  88. end;
  89. function statement_syssym(l : longint;var pd : pdef) : ptree;
  90. { const regnames:array[R_EAX..R_EDI] of string[3]=
  91. ('EAX','ECX','EDX','EBX','','','ESI','EDI'); }
  92. var
  93. p1,p2 : ptree;
  94. paras : ptree;
  95. prev_in_args : boolean;
  96. Store_valid : boolean;
  97. begin
  98. prev_in_args:=in_args;
  99. Store_valid:=Must_be_valid;
  100. case l of
  101. in_ord_x:
  102. begin
  103. consume(LKLAMMER);
  104. in_args:=true;
  105. Must_be_valid:=true;
  106. p1:=comp_expr(true);
  107. consume(RKLAMMER);
  108. do_firstpass(p1);
  109. p1:=geninlinenode(in_ord_x,p1);
  110. do_firstpass(p1);
  111. statement_syssym := p1;
  112. pd:=p1^.resulttype;
  113. end;
  114. in_break:
  115. begin
  116. statement_syssym:=genzeronode(breakn);
  117. pd:=voiddef;
  118. end;
  119. in_continue:
  120. begin
  121. statement_syssym:=genzeronode(continuen);
  122. pd:=voiddef;
  123. end;
  124. in_typeof_x : begin
  125. consume(LKLAMMER);
  126. in_args:=true;
  127. p1:=comp_expr(true);
  128. consume(RKLAMMER);
  129. pd:=voidpointerdef;
  130. if p1^.treetype=typen then
  131. begin
  132. if (p1^.resulttype=nil) then
  133. begin
  134. Message(sym_e_type_mismatch);
  135. statement_syssym:=genzeronode(errorn);
  136. end
  137. else
  138. if p1^.resulttype^.deftype=objectdef then
  139. statement_syssym:=geninlinenode(in_typeof_x,p1)
  140. else
  141. begin
  142. Message(sym_e_type_mismatch);
  143. statement_syssym:=genzeronode(errorn);
  144. end;
  145. end
  146. else
  147. begin
  148. Must_be_valid:=false;
  149. do_firstpass(p1);
  150. if (p1^.resulttype=nil) then
  151. begin
  152. Message(sym_e_type_mismatch);
  153. statement_syssym:=genzeronode(errorn)
  154. end
  155. else
  156. if p1^.resulttype^.deftype=objectdef then
  157. statement_syssym:=geninlinenode(in_typeof_x,p1)
  158. else
  159. begin
  160. Message(sym_e_type_mismatch);
  161. statement_syssym:=genzeronode(errorn)
  162. end;
  163. end;
  164. end;
  165. in_sizeof_x : begin
  166. consume(LKLAMMER);
  167. in_args:=true;
  168. p1:=comp_expr(true);
  169. consume(RKLAMMER);
  170. pd:=s32bitdef;
  171. if p1^.treetype=typen then
  172. begin
  173. statement_syssym:=genordinalconstnode(
  174. p1^.resulttype^.size,pd);
  175. { p1 not needed !}
  176. disposetree(p1);
  177. end
  178. else
  179. begin
  180. Must_be_valid:=false;
  181. do_firstpass(p1);
  182. if p1^.resulttype^.deftype<>objectdef then
  183. begin
  184. statement_syssym:=genordinalconstnode(
  185. p1^.resulttype^.size,pd);
  186. { p1 not needed !}
  187. disposetree(p1);
  188. end
  189. else
  190. begin
  191. statement_syssym:=geninlinenode(in_sizeof_x,p1);
  192. end;
  193. end;
  194. end;
  195. in_assigned_x : begin
  196. consume(LKLAMMER);
  197. in_args:=true;
  198. p1:=comp_expr(true);
  199. Must_be_valid:=true;
  200. do_firstpass(p1);
  201. case p1^.resulttype^.deftype of
  202. pointerdef,procvardef,
  203. classrefdef:
  204. ;
  205. objectdef:
  206. if not(pobjectdef(p1^.resulttype)^.isclass) then
  207. Message(parser_e_illegal_parameter_list);
  208. else Message(parser_e_illegal_parameter_list);
  209. end;
  210. p2:=gencallparanode(p1,nil);
  211. p2:=geninlinenode(in_assigned_x,p2);
  212. consume(RKLAMMER);
  213. pd:=booldef;
  214. statement_syssym:=p2;
  215. end;
  216. in_ofs_x : begin
  217. consume(LKLAMMER);
  218. in_args:=true;
  219. p1:=comp_expr(true);
  220. p1:=gensinglenode(addrn,p1);
  221. Must_be_valid:=false;
  222. do_firstpass(p1);
  223. { Ofs() returns a longint, not a pointer }
  224. p1^.resulttype:=u32bitdef;
  225. pd:=p1^.resulttype;
  226. consume(RKLAMMER);
  227. statement_syssym:=p1;
  228. end;
  229. in_seg_x : begin
  230. consume(LKLAMMER);
  231. in_args:=true;
  232. p1:=comp_expr(true);
  233. do_firstpass(p1);
  234. if p1^.location.loc<>LOC_REFERENCE then
  235. Message(cg_e_illegal_expression);
  236. p1:=genordinalconstnode(0,s32bitdef);
  237. Must_be_valid:=false;
  238. pd:=s32bitdef;
  239. consume(RKLAMMER);
  240. statement_syssym:=p1;
  241. end;
  242. in_high_x,
  243. in_low_x : begin
  244. consume(LKLAMMER);
  245. in_args:=true;
  246. p1:=comp_expr(true);
  247. do_firstpass(p1);
  248. Must_be_valid:=false;
  249. p2:=geninlinenode(l,p1);
  250. consume(RKLAMMER);
  251. pd:=s32bitdef;
  252. statement_syssym:=p2;
  253. end;
  254. in_succ_x,
  255. in_pred_x : begin
  256. consume(LKLAMMER);
  257. in_args:=true;
  258. p1:=comp_expr(true);
  259. do_firstpass(p1);
  260. Must_be_valid:=false;
  261. p2:=geninlinenode(l,p1);
  262. consume(RKLAMMER);
  263. pd:=p1^.resulttype;
  264. statement_syssym:=p2;
  265. end;
  266. in_inc_x,
  267. in_dec_x : begin
  268. consume(LKLAMMER);
  269. in_args:=true;
  270. p1:=comp_expr(true);
  271. p2:=gencallparanode(p1,nil);
  272. Must_be_valid:=false;
  273. if token=COMMA then
  274. begin
  275. consume(COMMA);
  276. p1:=comp_expr(true);
  277. p2:=gencallparanode(p1,p2);
  278. end;
  279. statement_syssym:=geninlinenode(l,p2);
  280. consume(RKLAMMER);
  281. pd:=voiddef;
  282. end;
  283. in_concat_x : begin
  284. consume(LKLAMMER);
  285. in_args:=true;
  286. p2:=nil;
  287. while true do
  288. begin
  289. p1:=comp_expr(true);
  290. Must_be_valid:=true;
  291. do_firstpass(p1);
  292. if not((p1^.resulttype^.deftype=stringdef) or
  293. ((p1^.resulttype^.deftype=orddef) and
  294. (porddef(p1^.resulttype)^.typ=uchar)
  295. )
  296. ) then Message(parser_e_illegal_parameter_list);
  297. if p2<>nil then
  298. p2:=gennode(addn,p2,p1)
  299. else p2:=p1;
  300. if token=COMMA then
  301. consume(COMMA)
  302. else break;
  303. end;
  304. consume(RKLAMMER);
  305. pd:=cstringdef;
  306. statement_syssym:=p2;
  307. end;
  308. in_read_x,
  309. in_readln_x : begin
  310. if token=LKLAMMER then
  311. begin
  312. consume(LKLAMMER);
  313. in_args:=true;
  314. Must_be_valid:=false;
  315. paras:=parse_paras(false,false);
  316. consume(RKLAMMER);
  317. end
  318. else
  319. paras:=nil;
  320. pd:=voiddef;
  321. p1:=geninlinenode(l,paras);
  322. do_firstpass(p1);
  323. statement_syssym := p1;
  324. end;
  325. in_write_x,
  326. in_writeln_x : begin
  327. if token=LKLAMMER then
  328. begin
  329. consume(LKLAMMER);
  330. in_args:=true;
  331. Must_be_valid:=true;
  332. paras:=parse_paras(true,false);
  333. consume(RKLAMMER);
  334. end
  335. else
  336. paras:=nil;
  337. pd:=voiddef;
  338. p1 := geninlinenode(l,paras);
  339. do_firstpass(p1);
  340. statement_syssym := p1;
  341. end;
  342. in_str_x_string : begin
  343. consume(LKLAMMER);
  344. in_args:=true;
  345. paras:=parse_paras(true,false);
  346. consume(RKLAMMER);
  347. p1 := geninlinenode(l,paras);
  348. do_firstpass(p1);
  349. statement_syssym := p1;
  350. pd:=voiddef;
  351. end;
  352. in_include_x_y,
  353. in_exclude_x_y:
  354. begin
  355. consume(LKLAMMER);
  356. in_args:=true;
  357. p1:=comp_expr(true);
  358. Must_be_valid:=false;
  359. consume(COMMA);
  360. p2:=comp_expr(true);
  361. { just a bit lisp feeling }
  362. statement_syssym:=geninlinenode(l,
  363. gencallparanode(p1,gencallparanode(p2,nil)));
  364. consume(RKLAMMER);
  365. pd:=voiddef;
  366. end;
  367. {in_val_x : begin
  368. consume(LKLAMMER);
  369. paras:=parse_paras(false);
  370. consume(RKLAMMER);
  371. p1 := geninlinenode(l,paras);
  372. do_firstpass(p1);
  373. statement_syssym := p1;
  374. pd:=voiddef;
  375. end; }
  376. else internalerror(15);
  377. end;
  378. in_args:=prev_in_args;
  379. Must_be_valid:=Store_valid;
  380. end;
  381. { reads the parameter for a subroutine call }
  382. procedure do_proc_call(getaddr : boolean;var again : boolean;var p1:Ptree;var pd:Pdef);
  383. var
  384. prev_in_args : boolean;
  385. prevafterassn : boolean;
  386. begin
  387. prev_in_args:=in_args;
  388. prevafterassn:=afterassignment;
  389. afterassignment:=false;
  390. { want we only determine the address of }
  391. { a subroutine }
  392. if not(getaddr) then
  393. begin
  394. if token=LKLAMMER then
  395. begin
  396. consume(LKLAMMER);
  397. in_args:=true;
  398. p1^.left:=parse_paras(false,false);
  399. consume(RKLAMMER);
  400. end
  401. else p1^.left:=nil;
  402. { do firstpass because we need the }
  403. { result type }
  404. do_firstpass(p1);
  405. end
  406. else
  407. begin
  408. { address operator @: }
  409. p1^.left:=nil;
  410. { forget pd }
  411. pd:=nil;
  412. { no postfix operators }
  413. again:=false;
  414. end;
  415. pd:=p1^.resulttype;
  416. in_args:=prev_in_args;
  417. afterassignment:=prevafterassn;
  418. end;
  419. { the following procedure handles the access to a property symbol }
  420. procedure handle_propertysym(sym : psym;var p1 : ptree;
  421. var pd : pdef);
  422. var
  423. paras : ptree;
  424. p2 : ptree;
  425. begin
  426. paras:=nil;
  427. { property parameters? }
  428. if token=LECKKLAMMER then
  429. begin
  430. consume(LECKKLAMMER);
  431. paras:=parse_paras(false,true);
  432. consume(RECKKLAMMER);
  433. end;
  434. { indexed property }
  435. if (ppropertysym(sym)^.options and ppo_indexed)<>0 then
  436. begin
  437. p2:=genordinalconstnode(ppropertysym(sym)^.index,s32bitdef);
  438. paras:=gencallparanode(p2,paras);
  439. end;
  440. if not(afterassignment) and not(in_args) then
  441. begin
  442. { write property: }
  443. { no result }
  444. pd:=voiddef;
  445. if assigned(ppropertysym(sym)^.writeaccesssym) then
  446. begin
  447. if ppropertysym(sym)^.writeaccesssym^.typ=procsym then
  448. begin
  449. { generate the method call }
  450. p1:=genmethodcallnode(pprocsym(
  451. ppropertysym(sym)^.writeaccesssym),
  452. ppropertysym(sym)^.writeaccesssym^.owner,p1);
  453. { we know the procedure to call, so
  454. force the usage of that procedure }
  455. p1^.procdefinition:=pprocdef(ppropertysym(sym)^.writeaccessdef);
  456. p1^.left:=paras;
  457. consume(ASSIGNMENT);
  458. { read the expression }
  459. p2:=comp_expr(true);
  460. p1^.left:=gencallparanode(p2,p1^.left);
  461. end
  462. else if ppropertysym(sym)^.writeaccesssym^.typ=varsym then
  463. begin
  464. if assigned(paras) then
  465. message(parser_e_no_paras_allowed);
  466. { subscribed access? }
  467. if p1=nil then
  468. begin
  469. p1:=genloadnode(pvarsym(ppropertysym(sym)^.readaccesssym),
  470. ppropertysym(sym)^.readaccesssym^.owner);
  471. end
  472. else
  473. p1:=gensubscriptnode(pvarsym(
  474. ppropertysym(sym)^.readaccesssym),p1);
  475. consume(ASSIGNMENT);
  476. { read the expression }
  477. p2:=comp_expr(true);
  478. p1:=gennode(assignn,p1,p2);
  479. end
  480. else
  481. begin
  482. p1:=genzeronode(errorn);
  483. Message(parser_e_no_procedure_to_access_property);
  484. end;
  485. end
  486. else
  487. begin
  488. p1:=genzeronode(errorn);
  489. Message(parser_e_no_procedure_to_access_property);
  490. end;
  491. end
  492. else
  493. begin
  494. { read property: }
  495. pd:=ppropertysym(sym)^.proptype;
  496. if assigned(ppropertysym(sym)^.readaccesssym) then
  497. begin
  498. if ppropertysym(sym)^.readaccesssym^.typ=varsym then
  499. begin
  500. if assigned(paras) then
  501. message(parser_e_no_paras_allowed);
  502. { subscribed access? }
  503. if p1=nil then
  504. begin
  505. p1:=genloadnode(pvarsym(ppropertysym(sym)^.readaccesssym),
  506. ppropertysym(sym)^.readaccesssym^.owner);
  507. end
  508. else
  509. p1:=gensubscriptnode(pvarsym(
  510. ppropertysym(sym)^.readaccesssym),p1);
  511. end
  512. else if ppropertysym(sym)^.readaccesssym^.typ=procsym then
  513. begin
  514. { generate the method call }
  515. p1:=genmethodcallnode(pprocsym(
  516. ppropertysym(sym)^.readaccesssym),
  517. ppropertysym(sym)^.readaccesssym^.owner,p1);
  518. { we know the procedure to call, so
  519. force the usage of that procedure }
  520. p1^.procdefinition:=pprocdef(ppropertysym(sym)^.readaccessdef);
  521. { insert paras }
  522. p1^.left:=paras;
  523. { if we should be delphi compatible }
  524. { then force type conversion }
  525. { }
  526. { isn't neccessary, the result types }
  527. { have to match excatly }
  528. {if cs_delphi2_compatible in aktswitches then
  529. p1:=gentypeconvnode(p1,pd);
  530. }
  531. end
  532. else
  533. begin
  534. p1:=genzeronode(errorn);
  535. Message(sym_e_type_mismatch);
  536. end;
  537. end
  538. else
  539. begin
  540. { error, no function to read property }
  541. p1:=genzeronode(errorn);
  542. Message(parser_e_no_procedure_to_access_property);
  543. end;
  544. end;
  545. end;
  546. { the ID token has to be consumed before calling this function }
  547. procedure do_member_read(const sym : psym;var p1 : ptree;
  548. var pd : pdef;var again : boolean);
  549. var
  550. static_name : string;
  551. isclassref : boolean;
  552. begin
  553. if sym=nil then
  554. begin
  555. Message(sym_e_id_no_member);
  556. disposetree(p1);
  557. p1:=genzeronode(errorn);
  558. { try to clean up }
  559. pd:=generrordef;
  560. again:=false;
  561. end
  562. else
  563. begin
  564. isclassref:=pd^.deftype=classrefdef;
  565. { check protected and private members }
  566. if ((sym^.properties and sp_private)<>0) and
  567. (pobjectdef(sym^.owner^.defowner)^.owner^.symtabletype=unitsymtable) then
  568. Message(parser_e_cant_access_private_member);
  569. if ((sym^.properties and sp_protected)<>0) and
  570. (pobjectdef(pd)^.owner^.symtabletype=unitsymtable) then
  571. Message(parser_e_cant_access_protected_member);
  572. { we assume, that only procsyms and varsyms are in an object }
  573. { symbol table, for classes, properties are allowed }
  574. case sym^.typ of
  575. procsym:
  576. begin
  577. p1:=genmethodcallnode(pprocsym(sym),srsymtable,p1);
  578. do_proc_call(false,again,p1,pd);
  579. { now we know the real method e.g. we can check for }
  580. { a class method }
  581. if isclassref and ((p1^.procdefinition^.options and (poclassmethod or poconstructor))=0) then
  582. Message(parser_e_only_class_methods_via_class_ref);
  583. end;
  584. varsym:
  585. begin
  586. if isclassref then
  587. Message(parser_e_only_class_methods_via_class_ref);
  588. if (sym^.properties and sp_static)<>0 then
  589. begin
  590. static_name:=lowercase(srsymtable^.name^)+'_'+sym^.name;
  591. getsym(static_name,true);
  592. disposetree(p1);
  593. p1:=genloadnode(pvarsym(srsym),srsymtable);
  594. end
  595. else
  596. p1:=gensubscriptnode(pvarsym(sym),p1);
  597. pd:=pvarsym(sym)^.definition;
  598. end;
  599. propertysym:
  600. begin
  601. if isclassref then
  602. Message(parser_e_only_class_methods_via_class_ref);
  603. handle_propertysym(sym,p1,pd);
  604. end;
  605. else internalerror(16);
  606. end;
  607. end;
  608. end;
  609. function factor(getaddr : boolean) : ptree;
  610. var
  611. l : longint;
  612. p1,p2,p3 : ptree;
  613. code : word;
  614. pd,pd2 : pdef;
  615. unit_specific, again : boolean;
  616. static_name : string;
  617. sym : pvarsym;
  618. classh : pobjectdef;
  619. d : bestreal;
  620. constset : pconstset;
  621. propsym : ppropertysym;
  622. oldp1 : ptree;
  623. filepos : tfileposinfo;
  624. procedure check_tokenpos;
  625. begin
  626. if (p1<>oldp1) then
  627. begin
  628. if assigned(p1) then
  629. set_tree_filepos(p1,filepos);
  630. oldp1:=p1;
  631. filepos:=tokenpos;
  632. end;
  633. end;
  634. { p1 and p2 must contain valid values }
  635. procedure postfixoperators;
  636. begin
  637. check_tokenpos;
  638. while again do
  639. begin
  640. case token of
  641. CARET:
  642. begin
  643. consume(CARET);
  644. if pd^.deftype<>pointerdef then
  645. begin
  646. { ^ as binary operator is a problem!!!! (FK) }
  647. again:=false;
  648. Message(cg_e_invalid_qualifier);
  649. disposetree(p1);
  650. p1:=genzeronode(errorn);
  651. end
  652. else
  653. begin
  654. p1:=gensinglenode(derefn,p1);
  655. pd:=ppointerdef(pd)^.definition;
  656. end;
  657. end;
  658. LECKKLAMMER:
  659. begin
  660. if (pd^.deftype=objectdef) and
  661. pobjectdef(pd)^.isclass then
  662. begin
  663. { default property }
  664. propsym:=search_default_property(pobjectdef(pd));
  665. if not(assigned(propsym)) then
  666. begin
  667. disposetree(p1);
  668. p1:=genzeronode(errorn);
  669. again:=false;
  670. message(parser_e_no_default_property_available);
  671. end
  672. else
  673. begin
  674. p1:=nil;
  675. handle_propertysym(propsym,p1,pd);
  676. end;
  677. end
  678. else
  679. begin
  680. consume(LECKKLAMMER);
  681. repeat
  682. if (pd^.deftype<>arraydef) and
  683. (pd^.deftype<>stringdef) and
  684. (pd^.deftype<>pointerdef) then
  685. begin
  686. Message(cg_e_invalid_qualifier);
  687. disposetree(p1);
  688. p1:=genzeronode(errorn);
  689. again:=false;
  690. end
  691. else if (pd^.deftype=pointerdef) then
  692. begin
  693. p2:=comp_expr(true);
  694. p1:=gennode(vecn,p1,p2);
  695. pd:=ppointerdef(pd)^.definition;
  696. end
  697. else
  698. begin
  699. p2:=comp_expr(true);
  700. { support SEG:OFS for go32v2 Mem[] }
  701. if (target_info.target=target_GO32V2) and
  702. (p1^.treetype=loadn) and
  703. assigned(p1^.symtableentry) and
  704. assigned(p1^.symtableentry^.owner^.name) and
  705. (p1^.symtableentry^.owner^.name^='SYSTEM') and
  706. ((p1^.symtableentry^.name='MEM') or
  707. (p1^.symtableentry^.name='MEMW') or
  708. (p1^.symtableentry^.name='MEML')) then
  709. begin
  710. if (token=COLON) then
  711. begin
  712. consume(COLON);
  713. p3:=gennode(muln,genordinalconstnode($10,s32bitdef),p2);
  714. p2:=comp_expr(true);
  715. p2:=gennode(addn,p2,p3);
  716. p1:=gennode(vecn,p1,p2);
  717. p1^.memseg:=true;
  718. p1^.memindex:=true;
  719. end
  720. else
  721. begin
  722. p1:=gennode(vecn,p1,p2);
  723. p1^.memindex:=true;
  724. end;
  725. end
  726. { else
  727. if (target_info.target=target_GO32V2) and
  728. assigned(p1^.symtableentry) and
  729. assigned(p1^.symtableentry^.owner^.name) and
  730. (p1^.symtableentry^.owner^.name^='SYSTEM') and
  731. ((p1^.symtableentry^.name='PORT') or
  732. (p1^.symtableentry^.name='PORTW') or
  733. (p1^.symtableentry^.name='PORTL')) then
  734. begin
  735. p1:=gennode(vecn,p1,p2);
  736. p1^.portindex:=true;
  737. p
  738. end;
  739. end }
  740. else
  741. p1:=gennode(vecn,p1,p2);
  742. if pd^.deftype=stringdef then
  743. pd:=cchardef
  744. else
  745. pd:=parraydef(pd)^.definition;
  746. end;
  747. if token=COMMA then consume(COMMA)
  748. else break;
  749. until false;
  750. consume(RECKKLAMMER);
  751. end;
  752. end;
  753. POINT:
  754. begin
  755. consume(POINT);
  756. case pd^.deftype of
  757. recorddef:
  758. begin
  759. sym:=pvarsym(precdef(pd)^.symtable^.search(pattern));
  760. consume(ID);
  761. if sym=nil then
  762. begin
  763. Message(sym_e_illegal_field);
  764. disposetree(p1);
  765. p1:=genzeronode(errorn);
  766. end
  767. else
  768. begin
  769. p1:=gensubscriptnode(sym,p1);
  770. pd:=sym^.definition;
  771. end;
  772. end;
  773. classrefdef:
  774. begin
  775. classh:=pobjectdef(pclassrefdef(pd)^.definition);
  776. sym:=nil;
  777. while assigned(classh) do
  778. begin
  779. sym:=pvarsym(classh^.publicsyms^.search(pattern));
  780. srsymtable:=classh^.publicsyms;
  781. if assigned(sym) then
  782. break;
  783. classh:=classh^.childof;
  784. end;
  785. consume(ID);
  786. do_member_read(sym,p1,pd,again);
  787. end;
  788. objectdef:
  789. begin
  790. classh:=pobjectdef(pd);
  791. sym:=nil;
  792. while assigned(classh) do
  793. begin
  794. sym:=pvarsym(classh^.publicsyms^.search(pattern));
  795. srsymtable:=classh^.publicsyms;
  796. if assigned(sym) then
  797. break;
  798. classh:=classh^.childof;
  799. end;
  800. consume(ID);
  801. do_member_read(sym,p1,pd,again);
  802. end;
  803. pointerdef:
  804. begin
  805. if ppointerdef(pd)^.definition^.deftype
  806. in [recorddef,objectdef,classrefdef] then
  807. begin
  808. Message(cg_e_invalid_qualifier);
  809. { exterror:=strpnew(' may be pointer deref ^ is missing');
  810. error(invalid_qualifizier); }
  811. Comment(V_hint,' may be pointer deref ^ is missing');
  812. end
  813. else
  814. Message(cg_e_invalid_qualifier);
  815. end
  816. else
  817. begin
  818. Message(cg_e_invalid_qualifier);
  819. disposetree(p1);
  820. p1:=genzeronode(errorn);
  821. end;
  822. end;
  823. end;
  824. else
  825. begin
  826. { is this a procedure variable ? }
  827. if assigned(pd) then
  828. begin
  829. if (pd^.deftype=procvardef) then
  830. begin
  831. if getprocvar then
  832. again:=false
  833. else
  834. if (token=LKLAMMER) or
  835. ((pprocvardef(pd)^.para1=nil) and
  836. (token<>ASSIGNMENT) and (not in_args)) then
  837. begin
  838. { do this in a strange way }
  839. { it's not a clean solution }
  840. p2:=p1;
  841. p1:=gencallnode(nil,
  842. nil);
  843. p1^.right:=p2;
  844. p1^.unit_specific:=unit_specific;
  845. if token=LKLAMMER then
  846. begin
  847. consume(LKLAMMER);
  848. p1^.left:=parse_paras(false,false);
  849. consume(RKLAMMER);
  850. end;
  851. pd:=pprocvardef(pd)^.retdef;
  852. p1^.resulttype:=pd;
  853. end
  854. else again:=false;
  855. p1^.resulttype:=pd;
  856. end
  857. else again:=false;
  858. end
  859. else again:=false;
  860. end;
  861. end;
  862. check_tokenpos;
  863. end;
  864. end;
  865. procedure do_set(p : pconstset;pos : longint);
  866. var
  867. l : longint;
  868. begin
  869. if (pos>255) or
  870. (pos<0) then
  871. Message(parser_e_illegal_set_expr);
  872. l:=pos div 8;
  873. { do we allow the same twice }
  874. if (p^[l] and (1 shl (pos mod 8)))<>0 then
  875. Message(parser_e_illegal_set_expr);
  876. p^[l]:=p^[l] or (1 shl (pos mod 8));
  877. end;
  878. var
  879. possible_error : boolean;
  880. begin
  881. oldp1:=nil;
  882. filepos:=tokenpos;
  883. case token of
  884. ID:
  885. begin
  886. { allow post fix operators }
  887. again:=true;
  888. if (cs_delphi2_compatible in aktswitches) and
  889. (pattern='RESULT') and
  890. assigned(aktprocsym) and
  891. (procinfo.retdef<>pdef(voiddef)) then
  892. begin
  893. consume(ID);
  894. p1:=genzeronode(funcretn);
  895. pd:=procinfo.retdef;
  896. {$ifdef TEST_FUNCRET}
  897. p1^.funcretprocinfo:=pointer(@procinfo);
  898. p1^.retdef:=pd;
  899. {$endif TEST_FUNCRET}
  900. end
  901. else
  902. begin
  903. if lastsymknown then
  904. begin
  905. srsym:=lastsrsym;
  906. srsymtable:=lastsrsymtable;
  907. lastsymknown:=false;
  908. end
  909. else
  910. getsym(pattern,true);
  911. consume(ID);
  912. { is this an access to a function result ? }
  913. if assigned(aktprocsym) and
  914. ((srsym^.name=aktprocsym^.name) or
  915. ((pvarsym(srsym)=opsym) and
  916. ((procinfo.flags and pi_operator)<>0))) and
  917. (procinfo.retdef<>pdef(voiddef)) and
  918. (token<>LKLAMMER) and
  919. (not ((cs_tp_compatible in aktswitches) and
  920. (afterassignment or in_args))) then
  921. begin
  922. p1:=genzeronode(funcretn);
  923. pd:=procinfo.retdef;
  924. {$ifdef TEST_FUNCRET}
  925. p1^.funcretprocinfo:=pointer(@procinfo);
  926. p1^.retdef:=pd;
  927. {$endif TEST_FUNCRET}
  928. end
  929. else
  930. { else it's a normal symbol }
  931. begin
  932. if srsym^.typ=unitsym then
  933. begin
  934. consume(POINT);
  935. getsymonlyin(punitsym(srsym)^.unitsymtable,pattern);
  936. unit_specific:=true;
  937. consume(ID);
  938. end
  939. else
  940. unit_specific:=false;
  941. { check semantics of private }
  942. if (srsym^.typ in [propertysym,procsym,varsym]) and
  943. (srsymtable^.symtabletype=objectsymtable) then
  944. begin
  945. if ((srsym^.properties and sp_private)<>0) and
  946. (pobjectdef(srsym^.owner^.defowner)^.
  947. owner^.symtabletype=unitsymtable) then
  948. Message(parser_e_cant_access_private_member);
  949. end;
  950. case srsym^.typ of
  951. absolutesym:
  952. begin
  953. p1:=genloadnode(pvarsym(srsym),srsymtable);
  954. pd:=pabsolutesym(srsym)^.definition;
  955. end;
  956. varsym:
  957. begin
  958. { are we in a class method ? }
  959. if (srsymtable^.symtabletype=objectsymtable) and
  960. assigned(aktprocsym) and
  961. ((aktprocsym^.definition^.options and poclassmethod)<>0) then
  962. Message(parser_e_only_class_methods);
  963. if (srsym^.properties and sp_static)<>0 then
  964. begin
  965. static_name:=lowercase(srsymtable^.name^)+'_'+srsym^.name;
  966. getsym(static_name,true);
  967. end;
  968. p1:=genloadnode(pvarsym(srsym),srsymtable);
  969. if pvarsym(srsym)^.is_valid=0 then
  970. begin
  971. p1^.is_first := true;
  972. { set special between first loaded
  973. until checked in firstpass }
  974. pvarsym(srsym)^.is_valid:=2;
  975. end;
  976. pd:=pvarsym(srsym)^.definition;
  977. end;
  978. typedconstsym:
  979. begin
  980. p1:=gentypedconstloadnode(ptypedconstsym(srsym),srsymtable);
  981. pd:=ptypedconstsym(srsym)^.definition;
  982. end;
  983. syssym:
  984. p1:=statement_syssym(psyssym(srsym)^.number,pd);
  985. typesym:
  986. begin
  987. pd:=ptypesym(srsym)^.definition;
  988. { if we read a type declaration }
  989. { we have to return the type and }
  990. { nothing else }
  991. if block_type=bt_type then
  992. begin
  993. p1:=genzeronode(typen);
  994. p1^.resulttype:=pd;
  995. pd:=voiddef;
  996. end
  997. else
  998. begin
  999. if token=LKLAMMER then
  1000. begin
  1001. consume(LKLAMMER);
  1002. p1:=comp_expr(true);
  1003. consume(RKLAMMER);
  1004. p1:=gentypeconvnode(p1,pd);
  1005. p1^.explizit:=true;
  1006. end
  1007. else if (token=POINT) and
  1008. (pd^.deftype=objectdef) and
  1009. ((pobjectdef(pd)^.options and oois_class)=0) then
  1010. begin
  1011. consume(POINT);
  1012. if assigned(procinfo._class) then
  1013. begin
  1014. if procinfo._class^.isrelated(pobjectdef(pd)) then
  1015. begin
  1016. p1:=genzeronode(typen);
  1017. p1^.resulttype:=pd;
  1018. srsymtable:=pobjectdef(pd)^.publicsyms;
  1019. sym:=pvarsym(srsymtable^.search(pattern));
  1020. consume(ID);
  1021. do_member_read(sym,p1,pd,again);
  1022. end
  1023. else
  1024. begin
  1025. Message(parser_e_no_super_class);
  1026. pd:=generrordef;
  1027. again:=false;
  1028. end;
  1029. end
  1030. else
  1031. begin
  1032. { allows @TObject.Load }
  1033. { also allows static methods and variables }
  1034. p1:=genzeronode(typen);
  1035. p1^.resulttype:=pd;
  1036. srsymtable:=pobjectdef(pd)^.publicsyms;
  1037. sym:=pvarsym(srsymtable^.search(pattern));
  1038. if not(getaddr) and
  1039. ((sym^.properties and sp_static)=0) then
  1040. Message(sym_e_only_static_in_static)
  1041. else
  1042. begin
  1043. consume(ID);
  1044. do_member_read(sym,p1,pd,again);
  1045. end;
  1046. end
  1047. end
  1048. else
  1049. begin
  1050. { class reference ? }
  1051. if (pd^.deftype=objectdef)
  1052. and ((pobjectdef(pd)^.options and oois_class)<>0) then
  1053. begin
  1054. p1:=genzeronode(typen);
  1055. p1^.resulttype:=pd;
  1056. pd:=new(pclassrefdef,init(pd));
  1057. p1:=gensinglenode(loadvmtn,p1);
  1058. p1^.resulttype:=pd;
  1059. end
  1060. else
  1061. begin
  1062. { generate a type node }
  1063. { (for typeof etc) }
  1064. p1:=genzeronode(typen);
  1065. p1^.resulttype:=pd;
  1066. pd:=voiddef;
  1067. end;
  1068. end;
  1069. end;
  1070. end;
  1071. enumsym:
  1072. begin
  1073. p1:=genenumnode(penumsym(srsym));
  1074. pd:=p1^.resulttype;
  1075. end;
  1076. constsym:
  1077. begin
  1078. case pconstsym(srsym)^.consttype of
  1079. constint:
  1080. p1:=genordinalconstnode(pconstsym(srsym)^.value,s32bitdef);
  1081. conststring:
  1082. p1:=genstringconstnode(pstring(pconstsym(srsym)^.value)^);
  1083. constchar:
  1084. p1:=genordinalconstnode(pconstsym(srsym)^.value,cchardef);
  1085. constreal:
  1086. p1:=genrealconstnode(pdouble(pconstsym(srsym)^.value)^);
  1087. constbool:
  1088. p1:=genordinalconstnode(pconstsym(srsym)^.value,booldef);
  1089. constseta:
  1090. p1:=gensetconstruktnode(pconstset(pconstsym(srsym)^.value),
  1091. psetdef(pconstsym(srsym)^.definition));
  1092. constord:
  1093. p1:=genordinalconstnode(pconstsym(srsym)^.value,
  1094. pconstsym(srsym)^.definition);
  1095. end;
  1096. pd:=p1^.resulttype;
  1097. end;
  1098. procsym:
  1099. begin
  1100. { are we in a class method ? }
  1101. possible_error:=(srsymtable^.symtabletype=objectsymtable) and
  1102. assigned(aktprocsym) and
  1103. ((aktprocsym^.definition^.options and poclassmethod)<>0);
  1104. p1:=gencallnode(pprocsym(srsym),srsymtable);
  1105. p1^.unit_specific:=unit_specific;
  1106. do_proc_call(getaddr,again,p1,pd);
  1107. if possible_error and
  1108. ((p1^.procdefinition^.options and poclassmethod)=0) then
  1109. Message(parser_e_only_class_methods);
  1110. end;
  1111. propertysym:
  1112. begin
  1113. { access to property in a method }
  1114. { are we in a class method ? }
  1115. if (srsymtable^.symtabletype=objectsymtable) and
  1116. assigned(aktprocsym) and
  1117. ((aktprocsym^.definition^.options and poclassmethod)<>0) then
  1118. Message(parser_e_only_class_methods);
  1119. { no method pointer }
  1120. p1:=nil;
  1121. handle_propertysym(srsym,p1,pd);
  1122. end;
  1123. errorsym:
  1124. begin
  1125. p1:=genzeronode(errorn);
  1126. pd:=generrordef;
  1127. if token=LKLAMMER then
  1128. begin
  1129. consume(LKLAMMER);
  1130. parse_paras(false,false);
  1131. consume(RKLAMMER);
  1132. end;
  1133. end;
  1134. else
  1135. begin
  1136. p1:=genzeronode(errorn);
  1137. pd:=generrordef;
  1138. Message(cg_e_illegal_expression);
  1139. end;
  1140. end;
  1141. end;
  1142. end;
  1143. { handle post fix operators }
  1144. postfixoperators;
  1145. end;
  1146. _NEW : begin
  1147. consume(_NEW);
  1148. consume(LKLAMMER);
  1149. p1:=factor(false);
  1150. if p1^.treetype<>typen then
  1151. Message(sym_e_type_id_expected);
  1152. pd:=p1^.resulttype;
  1153. pd2:=pd;
  1154. if (pd^.deftype<>pointerdef) or
  1155. (ppointerdef(pd)^.definition^.deftype<>objectdef) then
  1156. begin
  1157. Message(parser_e_pointer_to_class_expected);
  1158. { if an error occurs, read til the end of the new }
  1159. { statement }
  1160. p1:=genzeronode(errorn);
  1161. l:=1;
  1162. while true do
  1163. begin
  1164. case token of
  1165. LKLAMMER : inc(l);
  1166. RKLAMMER : dec(l);
  1167. end;
  1168. consume(token);
  1169. if l=0 then
  1170. break;
  1171. end;
  1172. end
  1173. else
  1174. begin
  1175. disposetree(p1);
  1176. p1:=genzeronode(hnewn);
  1177. p1^.resulttype:=ppointerdef(pd)^.definition;
  1178. consume(COMMA);
  1179. afterassignment:=false;
  1180. { determines the current object defintion }
  1181. classh:=pobjectdef(ppointerdef(pd)^.definition);
  1182. { check for an abstract class }
  1183. if (classh^.options and oois_abstract)<>0 then
  1184. Message(sym_e_no_instance_of_abstract_object);
  1185. { search the constructor also in the symbol tables of }
  1186. { the parents }
  1187. { no constructor found }
  1188. sym:=nil;
  1189. while assigned(classh) do
  1190. begin
  1191. sym:=pvarsym(classh^.publicsyms^.search(pattern));
  1192. srsymtable:=classh^.publicsyms;
  1193. if assigned(sym) then
  1194. break;
  1195. classh:=classh^.childof;
  1196. end;
  1197. consume(ID);
  1198. do_member_read(sym,p1,pd,again);
  1199. if (p1^.treetype<>calln) or
  1200. (assigned(p1^.procdefinition) and
  1201. ((p1^.procdefinition^.options and poconstructor)=0)) then
  1202. Message(parser_e_expr_have_to_be_constructor_call);
  1203. p1:=gensinglenode(newn,p1);
  1204. { set the resulttype }
  1205. p1^.resulttype:=pd2;
  1206. consume(RKLAMMER);
  1207. end;
  1208. end;
  1209. _SELF:
  1210. begin
  1211. again:=true;
  1212. consume(_SELF);
  1213. if not assigned(procinfo._class) then
  1214. begin
  1215. p1:=genzeronode(errorn);
  1216. pd:=generrordef;
  1217. again:=false;
  1218. Message(parser_e_self_not_in_method);
  1219. end
  1220. else
  1221. begin
  1222. if (aktprocsym^.definition^.options and poclassmethod)<>0 then
  1223. begin
  1224. { self in class methods is a class reference type }
  1225. pd:=new(pclassrefdef,init(procinfo._class));
  1226. p1:=genselfnode(pd);
  1227. p1^.resulttype:=pd;
  1228. end
  1229. else
  1230. begin
  1231. p1:=genselfnode(procinfo._class);
  1232. p1^.resulttype:=procinfo._class;
  1233. end;
  1234. pd:=p1^.resulttype;
  1235. postfixoperators;
  1236. end;
  1237. end;
  1238. _INHERITED : begin
  1239. again:=true;
  1240. consume(_INHERITED);
  1241. if assigned(procinfo._class) then
  1242. begin
  1243. classh:=procinfo._class^.childof;
  1244. while assigned(classh) do
  1245. begin
  1246. srsymtable:=pobjectdef(classh)^.publicsyms;
  1247. sym:=pvarsym(srsymtable^.search(pattern));
  1248. if assigned(sym) then
  1249. begin
  1250. p1:=genzeronode(typen);
  1251. p1^.resulttype:=classh;
  1252. pd:=p1^.resulttype;
  1253. consume(ID);
  1254. do_member_read(sym,p1,pd,again);
  1255. break;
  1256. end;
  1257. classh:=classh^.childof;
  1258. end;
  1259. if classh=nil then
  1260. begin
  1261. Message1(sym_e_id_no_member,pattern);
  1262. again:=false;
  1263. pd:=generrordef;
  1264. p1:=genzeronode(errorn);
  1265. end;
  1266. end
  1267. else
  1268. Message(parser_e_generic_methods_only_in_methods);
  1269. postfixoperators;
  1270. end;
  1271. INTCONST : begin
  1272. valint(pattern,l,code);
  1273. if code<>0 then
  1274. begin
  1275. val(pattern,d,code);
  1276. if code<>0 then
  1277. begin
  1278. Message(cg_e_invalid_integer);
  1279. l:=1;
  1280. consume(INTCONST);
  1281. p1:=genordinalconstnode(l,s32bitdef);
  1282. end
  1283. else
  1284. begin
  1285. consume(INTCONST);
  1286. p1:=genrealconstnode(d);
  1287. end;
  1288. end
  1289. else
  1290. begin
  1291. consume(INTCONST);
  1292. p1:=genordinalconstnode(l,s32bitdef);
  1293. end;
  1294. end;
  1295. REALNUMBER : begin
  1296. val(pattern,d,code);
  1297. if code<>0 then
  1298. begin
  1299. Message(parser_e_error_in_real);
  1300. d:=1.0;
  1301. end;
  1302. consume(REALNUMBER);
  1303. p1:=genrealconstnode(d);
  1304. end;
  1305. { FILE and STRING can be also a type cast }
  1306. _STRING:
  1307. begin
  1308. pd:=stringtype;
  1309. consume(LKLAMMER);
  1310. p1:=comp_expr(true);
  1311. consume(RKLAMMER);
  1312. p1:=gentypeconvnode(p1,pd);
  1313. p1^.explizit:=true;
  1314. { handle postfix operators here e.g. string(a)[10] }
  1315. again:=true;
  1316. postfixoperators;
  1317. end;
  1318. _FILE:
  1319. begin
  1320. pd:=cfiledef;
  1321. consume(_FILE);
  1322. consume(LKLAMMER);
  1323. p1:=comp_expr(true);
  1324. consume(RKLAMMER);
  1325. p1:=gentypeconvnode(p1,pd);
  1326. p1^.explizit:=true;
  1327. { handle postfix operators here e.g. string(a)[10] }
  1328. again:=true;
  1329. postfixoperators;
  1330. end;
  1331. CSTRING:
  1332. begin
  1333. p1:=genstringconstnode(pattern);
  1334. consume(CSTRING);
  1335. end;
  1336. CCHAR:
  1337. begin
  1338. p1:=genordinalconstnode(ord(pattern[1]),cchardef);
  1339. consume(CCHAR);
  1340. end;
  1341. KLAMMERAFFE : begin
  1342. consume(KLAMMERAFFE);
  1343. p1:=factor(true);
  1344. p1:=gensinglenode(addrn,p1);
  1345. end;
  1346. LKLAMMER : begin
  1347. consume(LKLAMMER);
  1348. p1:=comp_expr(true);
  1349. consume(RKLAMMER);
  1350. { it's not a good solution }
  1351. { but (a+b)^ makes some problems }
  1352. case token of
  1353. CARET,POINT,LECKKLAMMER:
  1354. begin
  1355. { we need the resulttype }
  1356. { of the expression in pd }
  1357. do_firstpass(p1);
  1358. pd:=p1^.resulttype;
  1359. again:=true;
  1360. postfixoperators;
  1361. end;
  1362. end;
  1363. end;
  1364. LECKKLAMMER : begin
  1365. consume(LECKKLAMMER);
  1366. new(constset);
  1367. for l:=0 to 31 do
  1368. constset^[l]:=0;
  1369. p2:=nil;
  1370. pd:=nil;
  1371. if token<>RECKKLAMMER then
  1372. while true do
  1373. begin
  1374. p1:=comp_expr(true);
  1375. do_firstpass(p1);
  1376. case p1^.treetype of
  1377. ordconstn : begin
  1378. if pd=nil then
  1379. pd:=p1^.resulttype;
  1380. if not(is_equal(pd,p1^.resulttype)) then
  1381. Message(parser_e_typeconflict_in_set)
  1382. else
  1383. if token=POINTPOINT then
  1384. begin
  1385. consume(POINTPOINT);
  1386. p3:=comp_expr(true);
  1387. do_firstpass(p3);
  1388. if not(is_equal(pd,p3^.resulttype)) then
  1389. Message(parser_e_typeconflict_in_set)
  1390. else
  1391. for l:=p1^.value to p3^.value do
  1392. do_set(constset,l);
  1393. disposetree(p3);
  1394. end
  1395. else
  1396. do_set(constset,p1^.value);
  1397. disposetree(p1);
  1398. end;
  1399. stringconstn : begin
  1400. if pd=nil then
  1401. pd:=cchardef;
  1402. if not(is_equal(pd,cchardef)) then
  1403. Message(parser_e_typeconflict_in_set)
  1404. else
  1405. for l:=1 to length(pstring(p1^.values)^) do
  1406. do_set(constset,ord(pstring(p1^.values)^[l]));
  1407. disposetree(p1);
  1408. end;
  1409. else
  1410. begin
  1411. if pd=nil then
  1412. pd:=p1^.resulttype;
  1413. if not(is_equal(pd,p1^.resulttype)) then
  1414. Message(parser_e_typeconflict_in_set);
  1415. p2:=gennode(setelen,p1,p2);
  1416. end;
  1417. end;
  1418. if token=COMMA then
  1419. consume(COMMA)
  1420. else break;
  1421. end;
  1422. consume(RECKKLAMMER);
  1423. p1:=gensinglenode(setconstrn,p2);
  1424. p1^.resulttype:=new(psetdef,init(pd,255));
  1425. p1^.constset:=constset;
  1426. end;
  1427. PLUS : begin
  1428. consume(PLUS);
  1429. p1:=factor(false);
  1430. end;
  1431. MINUS : begin
  1432. consume(MINUS);
  1433. p1:=factor(false);
  1434. p1:=gensinglenode(umminusn,p1);
  1435. end;
  1436. _NOT : begin
  1437. consume(_NOT);
  1438. p1:=factor(false);
  1439. p1:=gensinglenode(notn,p1);
  1440. end;
  1441. _TRUE : begin
  1442. consume(_TRUE);
  1443. p1:=genordinalconstnode(1,booldef);
  1444. end;
  1445. _FALSE : begin
  1446. consume(_FALSE);
  1447. p1:=genordinalconstnode(0,booldef);
  1448. end;
  1449. _NIL : begin
  1450. consume(_NIL);
  1451. p1:=genzeronode(niln);
  1452. end;
  1453. else
  1454. begin
  1455. p1:=genzeronode(errorn);
  1456. consume(token);
  1457. Message(cg_e_illegal_expression);
  1458. end;
  1459. end;
  1460. factor:=p1;
  1461. check_tokenpos;
  1462. end;
  1463. type Toperator_precedence=(opcompare,opaddition,opmultiply);
  1464. const tok2node:array[PLUS.._XOR] of Ttreetyp=
  1465. (addn,subn,muln,slashn,equaln,gtn,ltn,gten,lten,
  1466. isn,asn,inn,
  1467. symdifn,starstarn,nothingn,caretn,unequaln,nothingn,
  1468. nothingn,nothingn,nothingn,nothingn,nothingn,
  1469. nothingn,nothingn,nothingn,nothingn,nothingn,
  1470. nothingn,nothingn,nothingn,nothingn,nothingn,
  1471. nothingn,andn,nothingn,nothingn,nothingn,
  1472. nothingn,nothingn,nothingn,nothingn,nothingn,
  1473. nothingn,nothingn,divn,nothingn,nothingn,
  1474. nothingn,nothingn,nothingn,nothingn,nothingn,
  1475. nothingn,nothingn,nothingn,nothingn,nothingn,
  1476. nothingn,nothingn,nothingn,nothingn,nothingn,
  1477. modn,nothingn,nothingn,nothingn,nothingn,
  1478. nothingn,nothingn,orn,
  1479. nothingn,nothingn,nothingn,nothingn,nothingn,
  1480. nothingn,nothingn,shln,shrn,
  1481. nothingn,nothingn,nothingn,nothingn,nothingn,
  1482. nothingn,nothingn,nothingn,nothingn,nothingn,
  1483. nothingn,xorn);
  1484. operator_levels:array[Toperator_precedence] of set of Ttoken=
  1485. ([LT,LTE,GT,GTE,EQUAL,UNEQUAL,_IN,_IS],
  1486. [PLUS,MINUS,_OR,_XOR],
  1487. [CARET,SYMDIF,STARSTAR,STAR,SLASH,_DIV,_MOD,_AND,_SHL,_SHR,_AS]);
  1488. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):Ptree;
  1489. {Reads a subexpression while the operators are of the current precedence
  1490. level, or any higher level. Replaces the old term, simpl_expr and
  1491. simpl2_expr.}
  1492. var p1,p2:Ptree;
  1493. oldt:Ttoken;
  1494. filepos : tfileposinfo;
  1495. begin
  1496. { if pred_level=high(Toperator_precedence) then }
  1497. if pred_level=opmultiply then
  1498. p1:=factor(getprocvar)
  1499. else
  1500. p1:=sub_expr(succ(pred_level),true);
  1501. repeat
  1502. { aweful hack to support const a : 1..2=1; }
  1503. { disadvantage of tables :) FK }
  1504. if (token in operator_levels[pred_level]) and
  1505. ((token<>EQUAL) or accept_equal) then
  1506. begin
  1507. oldt:=token;
  1508. filepos:=tokenpos;
  1509. consume(token);
  1510. { if pred_level=high(Toperator_precedence) then }
  1511. if pred_level=opmultiply then
  1512. p2:=factor(getprocvar)
  1513. else
  1514. p2:=sub_expr(succ(pred_level),true);
  1515. p1:=gennode(tok2node[oldt],p1,p2);
  1516. set_tree_filepos(p1,filepos);
  1517. end
  1518. else
  1519. break;
  1520. until false;
  1521. sub_expr:=p1;
  1522. end;
  1523. function comp_expr(accept_equal : boolean):Ptree;
  1524. var
  1525. oldafterassignment : boolean;
  1526. begin
  1527. oldafterassignment:=afterassignment;
  1528. afterassignment:=true;
  1529. comp_expr:=sub_expr(opcompare,accept_equal);
  1530. afterassignment:=oldafterassignment;
  1531. end;
  1532. function expr : ptree;
  1533. var
  1534. p1,p2 : ptree;
  1535. oldafterassignment : boolean;
  1536. oldp1 : ptree;
  1537. filepos : tfileposinfo;
  1538. begin
  1539. oldafterassignment:=afterassignment;
  1540. p1:=sub_expr(opcompare,true);
  1541. if token in [ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  1542. afterassignment:=true;
  1543. filepos:=tokenpos;
  1544. oldp1:=p1;
  1545. case token of
  1546. POINTPOINT : begin
  1547. consume(POINTPOINT);
  1548. p2:=sub_expr(opcompare,true);
  1549. p1:=gennode(rangen,p1,p2);
  1550. end;
  1551. ASSIGNMENT : begin
  1552. consume(ASSIGNMENT);
  1553. { avoid a firstpass of a procedure if
  1554. it must be assigned to a procvar }
  1555. { should be recursive for a:=b:=c !!! }
  1556. if (p1^.resulttype<>nil) and (p1^.resulttype^.deftype=procvardef) then
  1557. getprocvar:=true;
  1558. p2:=sub_expr(opcompare,true);
  1559. if getprocvar and (p2^.treetype=calln) then
  1560. begin
  1561. p2^.treetype:=loadn;
  1562. p2^.resulttype:=pprocsym(p2^.symtableprocentry)^.definition;
  1563. p2^.symtableentry:=p2^.symtableprocentry;
  1564. end;
  1565. getprocvar:=false;
  1566. p1:=gennode(assignn,p1,p2);
  1567. end;
  1568. { this is the code for C like assignements }
  1569. { from an improvement of Peter Schaefer }
  1570. _PLUSASN : begin
  1571. consume(_PLUSASN );
  1572. p2:=sub_expr(opcompare,true);
  1573. p1:=gennode(assignn,p1,gennode(addn,getcopy(p1),p2));
  1574. { was first
  1575. p1:=gennode(assignn,p1,gennode(addn,p1,p2));
  1576. but disposetree assumes that we have a real
  1577. *** tree *** }
  1578. end;
  1579. _MINUSASN : begin
  1580. consume(_MINUSASN );
  1581. p2:=sub_expr(opcompare,true);
  1582. p1:=gennode(assignn,p1,gennode(subn,getcopy(p1),p2));
  1583. end;
  1584. _STARASN : begin
  1585. consume(_STARASN );
  1586. p2:=sub_expr(opcompare,true);
  1587. p1:=gennode(assignn,p1,gennode(muln,getcopy(p1),p2));
  1588. end;
  1589. _SLASHASN : begin
  1590. consume(_SLASHASN );
  1591. p2:=sub_expr(opcompare,true);
  1592. p1:=gennode(assignn,p1,gennode(slashn,getcopy(p1),p2));
  1593. end;
  1594. end;
  1595. afterassignment:=oldafterassignment;
  1596. if p1<>oldp1 then
  1597. set_tree_filepos(p1,filepos);
  1598. expr:=p1;
  1599. end;
  1600. function get_intconst:longint;
  1601. {Reads an expression, tries to evalute it and check if it is an integer
  1602. constant. Then the constant is returned.}
  1603. var p:Ptree;
  1604. begin
  1605. p:=comp_expr(true);
  1606. do_firstpass(p);
  1607. if (p^.treetype<>ordconstn) and
  1608. (p^.resulttype^.deftype=orddef) and
  1609. not (Porddef(p^.resulttype)^.typ in
  1610. [uvoid,uchar,bool8bit]) then
  1611. Message(cg_e_illegal_expression)
  1612. else
  1613. get_intconst:=p^.value;
  1614. disposetree(p);
  1615. end;
  1616. function get_stringconst:string;
  1617. {Reads an expression, tries to evaluate it and checks if it is a string
  1618. constant. Then the constant is returned.}
  1619. var p:Ptree;
  1620. begin
  1621. get_stringconst:='';
  1622. p:=comp_expr(true);
  1623. do_firstpass(p);
  1624. if p^.treetype<>stringconstn then
  1625. if (p^.treetype=ordconstn) and
  1626. (p^.resulttype^.deftype=orddef) and
  1627. (Porddef(p^.resulttype)^.typ=uchar) then
  1628. get_stringconst:=char(p^.value)
  1629. else
  1630. Message(cg_e_illegal_expression)
  1631. else
  1632. get_stringconst:=p^.values^;
  1633. disposetree(p);
  1634. end;
  1635. end.
  1636. {
  1637. $Log$
  1638. Revision 1.15 1998-05-20 09:42:35 pierre
  1639. + UseTokenInfo now default
  1640. * unit in interface uses and implementation uses gives error now
  1641. * only one error for unknown symbol (uses lastsymknown boolean)
  1642. the problem came from the label code !
  1643. + first inlined procedures and function work
  1644. (warning there might be allowed cases were the result is still wrong !!)
  1645. * UseBrower updated gives a global list of all position of all used symbols
  1646. with switch -gb
  1647. Revision 1.14 1998/05/11 13:07:56 peter
  1648. + $ifdef NEWPPU for the new ppuformat
  1649. + $define GDB not longer required
  1650. * removed all warnings and stripped some log comments
  1651. * no findfirst/findnext anymore to remove smartlink *.o files
  1652. Revision 1.13 1998/05/06 08:38:45 pierre
  1653. * better position info with UseTokenInfo
  1654. UseTokenInfo greatly simplified
  1655. + added check for changed tree after first time firstpass
  1656. (if we could remove all the cases were it happen
  1657. we could skip all firstpass if firstpasscount > 1)
  1658. Only with ExtDebug
  1659. Revision 1.12 1998/05/05 12:05:42 florian
  1660. * problems with properties fixed
  1661. * crash fixed: i:=l when i and l are undefined, was a problem with
  1662. implementation of private/protected
  1663. Revision 1.11 1998/05/04 11:22:26 florian
  1664. * problem with DOM solved: it crashes when accessing a property in a method
  1665. Revision 1.10 1998/05/01 16:38:45 florian
  1666. * handling of private and protected fixed
  1667. + change_keywords_to_tp implemented to remove
  1668. keywords which aren't supported by tp
  1669. * break and continue are now symbols of the system unit
  1670. + widestring, longstring and ansistring type released
  1671. Revision 1.9 1998/04/29 10:33:58 pierre
  1672. + added some code for ansistring (not complete nor working yet)
  1673. * corrected operator overloading
  1674. * corrected nasm output
  1675. + started inline procedures
  1676. + added starstarn : use ** for exponentiation (^ gave problems)
  1677. + started UseTokenInfo cond to get accurate positions
  1678. Revision 1.8 1998/04/14 23:27:03 florian
  1679. + exclude/include with constant second parameter added
  1680. Revision 1.7 1998/04/09 23:02:15 florian
  1681. * small problems solved to get remake3 work
  1682. Revision 1.6 1998/04/09 22:16:35 florian
  1683. * problem with previous REGALLOC solved
  1684. * improved property support
  1685. Revision 1.5 1998/04/08 10:26:09 florian
  1686. * correct error handling of virtual constructors
  1687. * problem with new type declaration handling fixed
  1688. Revision 1.4 1998/04/07 22:45:05 florian
  1689. * bug0092, bug0115 and bug0121 fixed
  1690. + packed object/class/array
  1691. Revision 1.3 1998/04/07 13:19:46 pierre
  1692. * bugfixes for reset_gdb_info
  1693. in MEM parsing for go32v2
  1694. better external symbol creation
  1695. support for rhgdb.exe (lowercase file names)
  1696. }