pexpr.pas 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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. {$i defines.inc}
  20. interface
  21. uses
  22. symtype,
  23. node;
  24. { reads a whole expression }
  25. function expr : tnode;
  26. { reads an expression without assignements and .. }
  27. function comp_expr(accept_equal : boolean):tnode;
  28. { reads a single factor }
  29. function factor(getaddr : boolean) : tnode;
  30. function string_dec : pdef;
  31. { the ID token has to be consumed before calling this function }
  32. procedure do_member_read(getaddr : boolean;const sym : psym;var p1 : tnode;
  33. var pd : pdef;var again : boolean);
  34. function get_intconst:longint;
  35. function get_stringconst:string;
  36. implementation
  37. uses
  38. { common }
  39. cutils,cobjects,
  40. { global }
  41. globtype,globals,tokens,verbose,
  42. systems,cpuinfo,
  43. { aasm }
  44. aasm,
  45. { symtable }
  46. symconst,symbase,symdef,symsym,symtable,types,
  47. {$ifdef GDB}
  48. gdb,
  49. {$endif}
  50. { pass 1 }
  51. pass_1,htypechk,
  52. nmat,nadd,ncal,nmem,nset,ncnv,ninl,ncon,nld,nflw,nbas,
  53. { parser }
  54. scanner,
  55. pbase,
  56. { codegen }
  57. {$ifdef newcg}
  58. cgbase
  59. {$else}
  60. hcodegen
  61. {$endif}
  62. ;
  63. { sub_expr(opmultiply) is need to get -1 ** 4 to be
  64. read as - (1**4) and not (-1)**4 PM }
  65. type
  66. Toperator_precedence=(opcompare,opaddition,opmultiply,oppower);
  67. const
  68. highest_precedence = oppower;
  69. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):tnode;forward;
  70. const
  71. allow_type : boolean = true;
  72. got_addrn : boolean = false;
  73. auto_inherited : boolean = false;
  74. function string_dec : pdef;
  75. { reads a string type with optional length }
  76. { and returns a pointer to the string }
  77. { definition }
  78. var
  79. p : tnode;
  80. d : pdef;
  81. begin
  82. consume(_STRING);
  83. if token=_LECKKLAMMER then
  84. begin
  85. consume(_LECKKLAMMER);
  86. p:=comp_expr(true);
  87. do_firstpass(p);
  88. if not is_constintnode(p) then
  89. begin
  90. Message(cg_e_illegal_expression);
  91. { error recovery }
  92. consume(_RECKKLAMMER);
  93. d:=cshortstringdef;
  94. end
  95. else
  96. begin
  97. if (tordconstnode(p).value<=0) then
  98. begin
  99. Message(parser_e_invalid_string_size);
  100. tordconstnode(p).value:=255;
  101. end;
  102. consume(_RECKKLAMMER);
  103. if tordconstnode(p).value>255 then
  104. d:=new(pstringdef,longinit(tordconstnode(p).value))
  105. else
  106. if tordconstnode(p).value<>255 then
  107. d:=new(pstringdef,shortinit(tordconstnode(p).value))
  108. else
  109. d:=cshortstringdef;
  110. end;
  111. p.free;
  112. end
  113. else
  114. begin
  115. if cs_ansistrings in aktlocalswitches then
  116. d:=cansistringdef
  117. else
  118. d:=cshortstringdef;
  119. end;
  120. string_dec:=d;
  121. end;
  122. function parse_paras(__colon,in_prop_paras : boolean) : tnode;
  123. var
  124. p1,p2 : tnode;
  125. end_of_paras : ttoken;
  126. begin
  127. if in_prop_paras then
  128. end_of_paras:=_RECKKLAMMER
  129. else
  130. end_of_paras:=_RKLAMMER;
  131. if token=end_of_paras then
  132. begin
  133. parse_paras:=nil;
  134. exit;
  135. end;
  136. p2:=nil;
  137. inc(parsing_para_level);
  138. while true do
  139. begin
  140. p1:=comp_expr(true);
  141. p2:=gencallparanode(p1,p2);
  142. { it's for the str(l:5,s); }
  143. if __colon and (token=_COLON) then
  144. begin
  145. consume(_COLON);
  146. p1:=comp_expr(true);
  147. p2:=gencallparanode(p1,p2);
  148. include(tcallparanode(p2).callparaflags,cpf_is_colon_para);
  149. if token=_COLON then
  150. begin
  151. consume(_COLON);
  152. p1:=comp_expr(true);
  153. p2:=gencallparanode(p1,p2);
  154. include(tcallparanode(p2).callparaflags,cpf_is_colon_para);
  155. end
  156. end;
  157. if token=_COMMA then
  158. consume(_COMMA)
  159. else
  160. break;
  161. end;
  162. dec(parsing_para_level);
  163. parse_paras:=p2;
  164. end;
  165. procedure check_tp_procvar(var p : tnode);
  166. var
  167. p1 : tnode;
  168. begin
  169. if (m_tp_procvar in aktmodeswitches) and
  170. (not got_addrn) and
  171. (not in_args) and
  172. (p.nodetype=loadn) then
  173. begin
  174. { support if procvar then for tp7 and many other expression like this }
  175. do_firstpass(p);
  176. set_varstate(p,false);
  177. { reset varstateset to maybe set used state later web bug769 PM }
  178. unset_varstate(p);
  179. if not(getprocvar) and (p.resulttype^.deftype=procvardef) then
  180. begin
  181. p1:=gencallnode(nil,nil);
  182. tcallnode(p1).right:=p;
  183. p1.resulttype:=pprocvardef(p.resulttype)^.rettype.def;
  184. firstpass(p1);
  185. p:=p1;
  186. end;
  187. end;
  188. end;
  189. function statement_syssym(l : longint;var pd : pdef) : tnode;
  190. var
  191. p1,p2,paras : tnode;
  192. prev_in_args : boolean;
  193. begin
  194. prev_in_args:=in_args;
  195. case l of
  196. in_ord_x :
  197. begin
  198. consume(_LKLAMMER);
  199. in_args:=true;
  200. p1:=comp_expr(true);
  201. consume(_RKLAMMER);
  202. do_firstpass(p1);
  203. p1:=geninlinenode(in_ord_x,false,p1);
  204. do_firstpass(p1);
  205. statement_syssym := p1;
  206. pd:=p1.resulttype;
  207. end;
  208. in_break :
  209. begin
  210. statement_syssym:=cbreaknode.create;
  211. pd:=voiddef;
  212. end;
  213. in_continue :
  214. begin
  215. statement_syssym:=ccontinuenode.create;
  216. pd:=voiddef;
  217. end;
  218. in_typeof_x :
  219. begin
  220. consume(_LKLAMMER);
  221. in_args:=true;
  222. {allow_type:=true;}
  223. p1:=comp_expr(true);
  224. {allow_type:=false;}
  225. consume(_RKLAMMER);
  226. pd:=voidpointerdef;
  227. if p1.nodetype=typen then
  228. begin
  229. if (ttypenode(p1).typenodetype=nil) then
  230. begin
  231. Message(type_e_mismatch);
  232. statement_syssym:=cerrornode.create;
  233. end
  234. else
  235. if ttypenode(p1).typenodetype^.deftype=objectdef then
  236. begin
  237. { we can use resulttype in pass_2 (PM) }
  238. p1.resulttype:=ttypenode(p1).typenodetype;
  239. statement_syssym:=geninlinenode(in_typeof_x,false,p1);
  240. end
  241. else
  242. begin
  243. Message(type_e_mismatch);
  244. p1.destroy;
  245. statement_syssym:=cerrornode.create;
  246. end;
  247. end
  248. else { not a type node }
  249. begin
  250. do_firstpass(p1);
  251. set_varstate(p1,false);
  252. if (p1.resulttype=nil) then
  253. begin
  254. Message(type_e_mismatch);
  255. p1.destroy;
  256. statement_syssym:=cerrornode.create;
  257. end
  258. else
  259. if p1.resulttype^.deftype=objectdef then
  260. statement_syssym:=geninlinenode(in_typeof_x,false,p1)
  261. else
  262. begin
  263. Message(type_e_mismatch);
  264. statement_syssym:=cerrornode.create;
  265. p1.destroy;
  266. end;
  267. end;
  268. end;
  269. in_sizeof_x :
  270. begin
  271. consume(_LKLAMMER);
  272. in_args:=true;
  273. {allow_type:=true;}
  274. p1:=comp_expr(true);
  275. {allow_type:=false; }
  276. consume(_RKLAMMER);
  277. pd:=s32bitdef;
  278. if p1.nodetype=typen then
  279. begin
  280. statement_syssym:=genordinalconstnode(ttypenode(p1).typenodetype^.size,pd);
  281. { p1 not needed !}
  282. p1.destroy;
  283. end
  284. else
  285. begin
  286. do_firstpass(p1);
  287. if ((p1.resulttype^.deftype=objectdef) and
  288. (oo_has_constructor in pobjectdef(p1.resulttype)^.objectoptions)) or
  289. is_open_array(p1.resulttype) or
  290. is_open_string(p1.resulttype) then
  291. statement_syssym:=geninlinenode(in_sizeof_x,false,p1)
  292. else
  293. begin
  294. statement_syssym:=genordinalconstnode(p1.resulttype^.size,pd);
  295. { p1 not needed !}
  296. p1.destroy;
  297. end;
  298. end;
  299. end;
  300. in_typeinfo_x :
  301. begin
  302. consume(_LKLAMMER);
  303. in_args:=true;
  304. {allow_type:=true;}
  305. p1:=comp_expr(true);
  306. do_firstpass(p1);
  307. {allow_type:=false; }
  308. if p1.nodetype<>typen then
  309. begin
  310. p1.destroy;
  311. p1:=cerrornode.create;
  312. Message(parser_e_illegal_parameter_list);
  313. end;
  314. consume(_RKLAMMER);
  315. p2:=gencallparanode(p1,nil);
  316. p2:=geninlinenode(in_typeinfo_x,false,p2);
  317. pd:=voidpointerdef;
  318. statement_syssym:=p2;
  319. end;
  320. in_assigned_x :
  321. begin
  322. consume(_LKLAMMER);
  323. in_args:=true;
  324. p1:=comp_expr(true);
  325. do_firstpass(p1);
  326. if not codegenerror then
  327. begin
  328. case p1.resulttype^.deftype of
  329. pointerdef,
  330. procvardef,
  331. classrefdef : ;
  332. objectdef :
  333. if not is_class_or_interface(p1.resulttype) then
  334. Message(parser_e_illegal_parameter_list);
  335. else
  336. Message(parser_e_illegal_parameter_list);
  337. end;
  338. end;
  339. p2:=gencallparanode(p1,nil);
  340. p2:=geninlinenode(in_assigned_x,false,p2);
  341. consume(_RKLAMMER);
  342. pd:=booldef;
  343. statement_syssym:=p2;
  344. end;
  345. in_addr_x :
  346. begin
  347. consume(_LKLAMMER);
  348. in_args:=true;
  349. p1:=comp_expr(true);
  350. p1:=caddrnode.create(p1);
  351. do_firstpass(p1);
  352. pd:=p1.resulttype;
  353. consume(_RKLAMMER);
  354. statement_syssym:=p1;
  355. end;
  356. in_ofs_x :
  357. begin
  358. consume(_LKLAMMER);
  359. in_args:=true;
  360. p1:=comp_expr(true);
  361. p1:=caddrnode.create(p1);
  362. do_firstpass(p1);
  363. { Ofs() returns a longint, not a pointer }
  364. p1.resulttype:=u32bitdef;
  365. pd:=p1.resulttype;
  366. consume(_RKLAMMER);
  367. statement_syssym:=p1;
  368. end;
  369. in_seg_x :
  370. begin
  371. consume(_LKLAMMER);
  372. in_args:=true;
  373. { the expression must be allowed to be assigned to }
  374. p1:=comp_expr(true);
  375. p1:=geninlinenode(in_assigned_x,false,p1);
  376. do_firstpass(p1);
  377. consume(_RKLAMMER);
  378. statement_syssym:=p1;
  379. end;
  380. in_high_x,
  381. in_low_x :
  382. begin
  383. consume(_LKLAMMER);
  384. in_args:=true;
  385. {allow_type:=true;}
  386. p1:=comp_expr(true);
  387. {allow_type:=false;}
  388. do_firstpass(p1);
  389. if p1.nodetype=typen then
  390. p1.resulttype:=ttypenode(p1).typenodetype;
  391. p2:=geninlinenode(l,false,p1);
  392. consume(_RKLAMMER);
  393. pd:=s32bitdef;
  394. statement_syssym:=p2;
  395. end;
  396. in_succ_x,
  397. in_pred_x :
  398. begin
  399. consume(_LKLAMMER);
  400. in_args:=true;
  401. p1:=comp_expr(true);
  402. do_firstpass(p1);
  403. p2:=geninlinenode(l,false,p1);
  404. consume(_RKLAMMER);
  405. pd:=p1.resulttype;
  406. statement_syssym:=p2;
  407. end;
  408. in_inc_x,
  409. in_dec_x :
  410. begin
  411. consume(_LKLAMMER);
  412. in_args:=true;
  413. p1:=comp_expr(true);
  414. if token=_COMMA then
  415. begin
  416. consume(_COMMA);
  417. p2:=gencallparanode(comp_expr(true),nil);
  418. end
  419. else
  420. p2:=nil;
  421. p2:=gencallparanode(p1,p2);
  422. statement_syssym:=geninlinenode(l,false,p2);
  423. consume(_RKLAMMER);
  424. pd:=voiddef;
  425. end;
  426. in_finalize_x:
  427. begin
  428. consume(_LKLAMMER);
  429. in_args:=true;
  430. p1:=comp_expr(true);
  431. if token=_COMMA then
  432. begin
  433. consume(_COMMA);
  434. p2:=gencallparanode(comp_expr(true),nil);
  435. end
  436. else
  437. p2:=nil;
  438. p2:=gencallparanode(p1,p2);
  439. statement_syssym:=geninlinenode(in_finalize_x,false,p2);
  440. consume(_RKLAMMER);
  441. pd:=voiddef;
  442. end;
  443. in_concat_x :
  444. begin
  445. consume(_LKLAMMER);
  446. in_args:=true;
  447. p2:=nil;
  448. while true do
  449. begin
  450. p1:=comp_expr(true);
  451. do_firstpass(p1);
  452. set_varstate(p1,true);
  453. if not((p1.resulttype^.deftype=stringdef) or
  454. ((p1.resulttype^.deftype=orddef) and
  455. (porddef(p1.resulttype)^.typ=uchar))) then
  456. Message(parser_e_illegal_parameter_list);
  457. if p2<>nil then
  458. p2:=caddnode.create(addn,p2,p1)
  459. else
  460. p2:=p1;
  461. if token=_COMMA then
  462. consume(_COMMA)
  463. else
  464. break;
  465. end;
  466. consume(_RKLAMMER);
  467. pd:=cshortstringdef;
  468. statement_syssym:=p2;
  469. end;
  470. in_read_x,
  471. in_readln_x :
  472. begin
  473. if token=_LKLAMMER then
  474. begin
  475. consume(_LKLAMMER);
  476. in_args:=true;
  477. paras:=parse_paras(false,false);
  478. consume(_RKLAMMER);
  479. end
  480. else
  481. paras:=nil;
  482. pd:=voiddef;
  483. p1:=geninlinenode(l,false,paras);
  484. do_firstpass(p1);
  485. statement_syssym := p1;
  486. end;
  487. in_setlength_x:
  488. begin
  489. if token=_LKLAMMER then
  490. begin
  491. consume(_LKLAMMER);
  492. in_args:=true;
  493. paras:=parse_paras(false,false);
  494. consume(_RKLAMMER);
  495. end
  496. else
  497. paras:=nil;
  498. pd:=voiddef;
  499. p1:=geninlinenode(l,false,paras);
  500. do_firstpass(p1);
  501. statement_syssym := p1;
  502. end;
  503. in_write_x,
  504. in_writeln_x :
  505. begin
  506. if token=_LKLAMMER then
  507. begin
  508. consume(_LKLAMMER);
  509. in_args:=true;
  510. paras:=parse_paras(true,false);
  511. consume(_RKLAMMER);
  512. end
  513. else
  514. paras:=nil;
  515. pd:=voiddef;
  516. p1 := geninlinenode(l,false,paras);
  517. do_firstpass(p1);
  518. statement_syssym := p1;
  519. end;
  520. in_str_x_string :
  521. begin
  522. consume(_LKLAMMER);
  523. in_args:=true;
  524. paras:=parse_paras(true,false);
  525. consume(_RKLAMMER);
  526. p1 := geninlinenode(l,false,paras);
  527. do_firstpass(p1);
  528. statement_syssym := p1;
  529. pd:=voiddef;
  530. end;
  531. in_val_x:
  532. Begin
  533. consume(_LKLAMMER);
  534. in_args := true;
  535. p1:= gencallparanode(comp_expr(true), nil);
  536. consume(_COMMA);
  537. p2 := gencallparanode(comp_expr(true),p1);
  538. if (token = _COMMA) then
  539. Begin
  540. consume(_COMMA);
  541. p2 := gencallparanode(comp_expr(true),p2)
  542. End;
  543. consume(_RKLAMMER);
  544. p2 := geninlinenode(l,false,p2);
  545. do_firstpass(p2);
  546. statement_syssym := p2;
  547. pd := voiddef;
  548. End;
  549. in_include_x_y,
  550. in_exclude_x_y :
  551. begin
  552. consume(_LKLAMMER);
  553. in_args:=true;
  554. p1:=comp_expr(true);
  555. consume(_COMMA);
  556. p2:=comp_expr(true);
  557. statement_syssym:=geninlinenode(l,false,gencallparanode(p1,gencallparanode(p2,nil)));
  558. consume(_RKLAMMER);
  559. pd:=voiddef;
  560. end;
  561. in_assert_x_y :
  562. begin
  563. consume(_LKLAMMER);
  564. in_args:=true;
  565. p1:=comp_expr(true);
  566. if token=_COMMA then
  567. begin
  568. consume(_COMMA);
  569. p2:=comp_expr(true);
  570. end
  571. else
  572. begin
  573. { then insert an empty string }
  574. p2:=genstringconstnode('',st_default);
  575. end;
  576. statement_syssym:=geninlinenode(l,false,gencallparanode(p1,gencallparanode(p2,nil)));
  577. consume(_RKLAMMER);
  578. pd:=voiddef;
  579. end;
  580. else
  581. internalerror(15);
  582. end;
  583. in_args:=prev_in_args;
  584. end;
  585. { reads the parameter for a subroutine call }
  586. procedure do_proc_call(getaddr : boolean;var again : boolean;var p1:tcallnode;var pd:Pdef);
  587. var
  588. prev_in_args : boolean;
  589. prevafterassn : boolean;
  590. hs,hs1 : pvarsym;
  591. st : psymtable;
  592. p2 : tnode;
  593. begin
  594. prev_in_args:=in_args;
  595. prevafterassn:=afterassignment;
  596. afterassignment:=false;
  597. { want we only determine the address of }
  598. { a subroutine ? }
  599. if not(getaddr) then
  600. begin
  601. if auto_inherited then
  602. begin
  603. st:=symtablestack;
  604. while assigned(st) and (st^.symtabletype<>parasymtable) do
  605. st:=st^.next;
  606. p2:=nil;
  607. if assigned(st) then
  608. begin
  609. hs:=pvarsym(st^.symindex^.first);
  610. while assigned(hs) do
  611. begin
  612. if hs^.typ<>varsym then
  613. internalerror(54382953);
  614. { if there is a localcopy then use that }
  615. if assigned(hs^.localvarsym) then
  616. hs1:=hs^.localvarsym
  617. else
  618. hs1:=hs;
  619. p2:=gencallparanode(genloadnode(hs1,hs1^.owner),p2);
  620. hs:=pvarsym(hs^.indexnext);
  621. end;
  622. end
  623. else
  624. internalerror(54382954);
  625. p1.left:=p2;
  626. end
  627. else
  628. begin
  629. if token=_LKLAMMER then
  630. begin
  631. consume(_LKLAMMER);
  632. in_args:=true;
  633. p1.left:=parse_paras(false,false);
  634. consume(_RKLAMMER);
  635. end
  636. else
  637. p1.left:=nil;
  638. end;
  639. { do firstpass because we need the }
  640. { result type }
  641. do_firstpass(p1);
  642. {set_var_state is handled inside firstcalln }
  643. end
  644. else
  645. begin
  646. { address operator @: }
  647. p1.left:=nil;
  648. { forget pd }
  649. pd:=nil;
  650. if (p1.symtableproc^.symtabletype=withsymtable) and
  651. (p1.symtableproc^.defowner^.deftype=objectdef) then
  652. begin
  653. p1.methodpointer:=tnode(pwithsymtable(p1.symtableproc)^.withrefnode).getcopy;
  654. end
  655. else if not(assigned(p1.methodpointer)) then
  656. begin
  657. { we must provide a method pointer, if it isn't given, }
  658. { it is self }
  659. if assigned(procinfo) then
  660. begin
  661. p1.methodpointer:=genselfnode(procinfo^._class);
  662. p1.methodpointer.resulttype:=procinfo^._class;
  663. end
  664. else
  665. begin
  666. p1.methodpointer:=genselfnode(nil);
  667. p1.methodpointer.resulttype:=nil;
  668. end;
  669. end;
  670. { no postfix operators }
  671. again:=false;
  672. end;
  673. pd:=p1.resulttype;
  674. in_args:=prev_in_args;
  675. afterassignment:=prevafterassn;
  676. end;
  677. procedure handle_procvar(pv : pprocvardef;var p2 : tnode);
  678. procedure doconv(procvar : pprocvardef;var t : tnode);
  679. var
  680. hp : tnode;
  681. begin
  682. hp:=nil;
  683. if (proc_to_procvar_equal(pprocsym(tcallnode(t).symtableprocentry)^.definition,procvar)) then
  684. begin
  685. if (po_methodpointer in procvar^.procoptions) then
  686. hp:=genloadmethodcallnode(pprocsym(tcallnode(t).symtableprocentry),tcallnode(t).symtableproc,tnode(tcallnode(t).methodpointer).getcopy)
  687. else
  688. hp:=genloadcallnode(pprocsym(tcallnode(t).symtableprocentry),tcallnode(t).symtableproc);
  689. end;
  690. if assigned(hp) then
  691. begin
  692. t.destroy;
  693. t:=hp;
  694. end;
  695. end;
  696. begin
  697. if (p2.nodetype=calln) then
  698. doconv(pv,tcallnode(p2))
  699. else
  700. if (p2.nodetype=typeconvn) and
  701. (ttypeconvnode(p2).left.nodetype=calln) then
  702. doconv(pv,tcallnode(ttypeconvnode(p2).left));
  703. end;
  704. { the following procedure handles the access to a property symbol }
  705. procedure handle_propertysym(sym : psym;st : psymtable;var p1 : tnode;
  706. var pd : pdef);
  707. var
  708. paras : tnode;
  709. p2 : tnode;
  710. plist : psymlistitem;
  711. begin
  712. paras:=nil;
  713. { property parameters? read them only if the property really }
  714. { has parameters }
  715. if ppo_hasparameters in ppropertysym(sym)^.propoptions then
  716. begin
  717. if token=_LECKKLAMMER then
  718. begin
  719. consume(_LECKKLAMMER);
  720. paras:=parse_paras(false,true);
  721. consume(_RECKKLAMMER);
  722. end;
  723. { indexed property }
  724. if (ppo_indexed in ppropertysym(sym)^.propoptions) then
  725. begin
  726. p2:=genordinalconstnode(ppropertysym(sym)^.index,ppropertysym(sym)^.indextype.def);
  727. paras:=gencallparanode(p2,paras);
  728. end;
  729. end;
  730. { we need only a write property if a := follows }
  731. { if not(afterassignment) and not(in_args) then }
  732. if token=_ASSIGNMENT then
  733. begin
  734. { write property: }
  735. { no result }
  736. pd:=voiddef;
  737. if not ppropertysym(sym)^.writeaccess^.empty then
  738. begin
  739. case ppropertysym(sym)^.writeaccess^.firstsym^.sym^.typ of
  740. procsym :
  741. begin
  742. { generate the method call }
  743. p1:=ccallnode.create(pprocsym(ppropertysym(sym)^.writeaccess^.firstsym^.sym),st,p1);
  744. { we know the procedure to call, so
  745. force the usage of that procedure }
  746. tcallnode(p1).procdefinition:=pprocdef(ppropertysym(sym)^.writeaccess^.def);
  747. tcallnode(p1).left:=paras;
  748. consume(_ASSIGNMENT);
  749. { read the expression }
  750. getprocvar:=ppropertysym(sym)^.proptype.def^.deftype=procvardef;
  751. p2:=comp_expr(true);
  752. if getprocvar then
  753. handle_procvar(pprocvardef(ppropertysym(sym)^.proptype.def),p2);
  754. tcallnode(p1).left:=gencallparanode(p2,tcallnode(p1).left);
  755. include(tcallnode(p1).flags,nf_isproperty);
  756. getprocvar:=false;
  757. end;
  758. varsym :
  759. begin
  760. if assigned(paras) then
  761. message(parser_e_no_paras_allowed);
  762. { subscribed access? }
  763. plist:=ppropertysym(sym)^.writeaccess^.firstsym;
  764. while assigned(plist) do
  765. begin
  766. if p1=nil then
  767. p1:=genloadnode(pvarsym(plist^.sym),st)
  768. else
  769. p1:=gensubscriptnode(pvarsym(plist^.sym),p1);
  770. plist:=plist^.next;
  771. end;
  772. include(tcallnode(p1).flags,nf_isproperty);
  773. consume(_ASSIGNMENT);
  774. { read the expression }
  775. p2:=comp_expr(true);
  776. p1:=cassignmentnode.create(p1,p2);
  777. end
  778. else
  779. begin
  780. p1:=cerrornode.create;
  781. Message(parser_e_no_procedure_to_access_property);
  782. end;
  783. end;
  784. end
  785. else
  786. begin
  787. p1:=cerrornode.create;
  788. Message(parser_e_no_procedure_to_access_property);
  789. end;
  790. end
  791. else
  792. begin
  793. { read property: }
  794. pd:=ppropertysym(sym)^.proptype.def;
  795. if not ppropertysym(sym)^.readaccess^.empty then
  796. begin
  797. case ppropertysym(sym)^.readaccess^.firstsym^.sym^.typ of
  798. varsym :
  799. begin
  800. if assigned(paras) then
  801. message(parser_e_no_paras_allowed);
  802. { subscribed access? }
  803. plist:=ppropertysym(sym)^.readaccess^.firstsym;
  804. while assigned(plist) do
  805. begin
  806. if p1=nil then
  807. p1:=genloadnode(pvarsym(plist^.sym),st)
  808. else
  809. p1:=gensubscriptnode(pvarsym(plist^.sym),p1);
  810. plist:=plist^.next;
  811. end;
  812. include(p1.flags,nf_isproperty);
  813. end;
  814. procsym :
  815. begin
  816. { generate the method call }
  817. p1:=ccallnode.create(pprocsym(ppropertysym(sym)^.readaccess^.firstsym^.sym),st,p1);
  818. { we know the procedure to call, so
  819. force the usage of that procedure }
  820. tcallnode(p1).procdefinition:=pprocdef(ppropertysym(sym)^.readaccess^.def);
  821. { insert paras }
  822. tcallnode(p1).left:=paras;
  823. include(p1.flags,nf_isproperty);
  824. end
  825. else
  826. begin
  827. p1:=cerrornode.create;
  828. Message(type_e_mismatch);
  829. end;
  830. end;
  831. end
  832. else
  833. begin
  834. { error, no function to read property }
  835. p1:=cerrornode.create;
  836. Message(parser_e_no_procedure_to_access_property);
  837. end;
  838. end;
  839. end;
  840. { the ID token has to be consumed before calling this function }
  841. procedure do_member_read(getaddr : boolean;const sym : psym;var p1 : tnode;
  842. var pd : pdef;var again : boolean);
  843. var
  844. static_name : string;
  845. isclassref : boolean;
  846. objdef : pobjectdef;
  847. begin
  848. if sym=nil then
  849. begin
  850. { pattern is still valid unless
  851. there is another ID just after the ID of sym }
  852. Message1(sym_e_id_no_member,pattern);
  853. p1.destroy;
  854. p1:=cerrornode.create;
  855. { try to clean up }
  856. pd:=generrordef;
  857. again:=false;
  858. end
  859. else
  860. begin
  861. objdef:=pobjectdef(sym^.owner^.defowner);
  862. isclassref:=(pd^.deftype=classrefdef);
  863. { check protected and private members }
  864. { please leave this code as it is, }
  865. { it has now the same behaviaor as TP/Delphi }
  866. if (sp_private in sym^.symoptions) and
  867. (objdef^.owner^.symtabletype=unitsymtable) then
  868. Message(parser_e_cant_access_private_member);
  869. if (sp_protected in sym^.symoptions) and
  870. (objdef^.owner^.symtabletype=unitsymtable) then
  871. begin
  872. if assigned(aktprocsym^.definition^._class) then
  873. begin
  874. if not aktprocsym^.definition^._class^.is_related(objdef) then
  875. Message(parser_e_cant_access_protected_member);
  876. end
  877. else
  878. Message(parser_e_cant_access_protected_member);
  879. end;
  880. { we assume, that only procsyms and varsyms are in an object }
  881. { symbol table, for classes, properties are allowed }
  882. case sym^.typ of
  883. procsym:
  884. begin
  885. p1:=ccallnode.create(pprocsym(sym),sym^.owner,p1);
  886. do_proc_call(getaddr or
  887. (getprocvar and
  888. ((block_type=bt_const) or
  889. ((m_tp_procvar in aktmodeswitches) and
  890. proc_to_procvar_equal(pprocsym(sym)^.definition,getprocvardef)
  891. )
  892. )
  893. ),again,tcallnode(p1),pd);
  894. if (block_type=bt_const) and
  895. getprocvar then
  896. handle_procvar(getprocvardef,p1);
  897. { now we know the real method e.g. we can check for a class method }
  898. if isclassref and
  899. assigned(tcallnode(p1).procdefinition) and
  900. not(po_classmethod in tcallnode(p1).procdefinition^.procoptions) and
  901. not(tcallnode(p1).procdefinition^.proctypeoption=potype_constructor) then
  902. Message(parser_e_only_class_methods_via_class_ref);
  903. end;
  904. varsym:
  905. begin
  906. if isclassref then
  907. Message(parser_e_only_class_methods_via_class_ref);
  908. if (sp_static in sym^.symoptions) then
  909. begin
  910. { static_name:=lower(srsymtable^.name^)+'_'+sym^.name;
  911. this is wrong for static field in with symtable (PM) }
  912. static_name:=lower(srsym^.owner^.name^)+'_'+sym^.name;
  913. getsym(static_name,true);
  914. p1.destroy;
  915. p1:=genloadnode(pvarsym(srsym),srsymtable);
  916. end
  917. else
  918. p1:=gensubscriptnode(pvarsym(sym),p1);
  919. pd:=pvarsym(sym)^.vartype.def;
  920. end;
  921. propertysym:
  922. begin
  923. if isclassref then
  924. Message(parser_e_only_class_methods_via_class_ref);
  925. handle_propertysym(sym,srsymtable,p1,pd);
  926. end;
  927. else internalerror(16);
  928. end;
  929. end;
  930. end;
  931. {****************************************************************************
  932. Factor
  933. ****************************************************************************}
  934. {$ifdef fpc}
  935. {$maxfpuregisters 0}
  936. {$endif fpc}
  937. function factor(getaddr : boolean) : tnode;
  938. var
  939. l : longint;
  940. ic : TConstExprInt;
  941. oldp1,
  942. p1,p2,p3 : tnode;
  943. code : integer;
  944. pd,pd2 : pdef;
  945. possible_error,
  946. {$ifdef TEST_PROCSYMS}
  947. unit_specific,
  948. {$endif TEST_PROCSYMS}
  949. again : boolean;
  950. sym : psym;
  951. classh : pobjectdef;
  952. d : bestreal;
  953. hs,
  954. static_name : string;
  955. propsym : ppropertysym;
  956. filepos : tfileposinfo;
  957. {---------------------------------------------
  958. Is_func_ret
  959. ---------------------------------------------}
  960. function is_func_ret(sym : psym) : boolean;
  961. var
  962. p : pprocinfo;
  963. storesymtablestack : psymtable;
  964. begin
  965. is_func_ret:=false;
  966. if not assigned(procinfo) or
  967. ((sym^.typ<>funcretsym) and ((procinfo^.flags and pi_operator)=0)) then
  968. exit;
  969. p:=procinfo;
  970. while assigned(p) do
  971. begin
  972. { is this an access to a function result? Accessing _RESULT is
  973. always allowed and funcretn is generated }
  974. if assigned(p^.funcretsym) and
  975. ((pfuncretsym(sym)=p^.resultfuncretsym) or
  976. ((pfuncretsym(sym)=p^.funcretsym) or
  977. ((pvarsym(sym)=opsym) and ((p^.flags and pi_operator)<>0))) and
  978. (p^.returntype.def<>pdef(voiddef)) and
  979. (token<>_LKLAMMER) and
  980. (not ((m_tp in aktmodeswitches) and (afterassignment or in_args)))
  981. ) then
  982. begin
  983. if ((pvarsym(sym)=opsym) and
  984. ((p^.flags and pi_operator)<>0)) then
  985. inc(opsym^.refs);
  986. p1:=cfuncretnode.create;
  987. pd:=p^.returntype.def;
  988. tfuncretnode(p1).funcretprocinfo:=p;
  989. tfuncretnode(p1).rettype.def:=pd;
  990. is_func_ret:=true;
  991. if p^.funcret_state=vs_declared then
  992. begin
  993. p^.funcret_state:=vs_declared_and_first_found;
  994. include(p1.flags,nf_is_first_funcret);
  995. end;
  996. exit;
  997. end;
  998. p:=p^.parent;
  999. end;
  1000. { we must use the function call }
  1001. if (sym^.typ=funcretsym) then
  1002. begin
  1003. storesymtablestack:=symtablestack;
  1004. symtablestack:=srsymtable^.next;
  1005. getsym(sym^.name,true);
  1006. if srsym^.typ<>procsym then
  1007. Message(cg_e_illegal_expression);
  1008. symtablestack:=storesymtablestack;
  1009. end;
  1010. end;
  1011. {---------------------------------------------
  1012. Factor_read_id
  1013. ---------------------------------------------}
  1014. procedure factor_read_id;
  1015. var
  1016. pc : pchar;
  1017. len : longint;
  1018. begin
  1019. { allow post fix operators }
  1020. again:=true;
  1021. begin
  1022. if lastsymknown then
  1023. begin
  1024. srsym:=lastsrsym;
  1025. srsymtable:=lastsrsymtable;
  1026. lastsymknown:=false;
  1027. end
  1028. else
  1029. getsym(pattern,true);
  1030. consume(_ID);
  1031. if not is_func_ret(srsym) then
  1032. { else it's a normal symbol }
  1033. begin
  1034. { is it defined like UNIT.SYMBOL ? }
  1035. if srsym^.typ=unitsym then
  1036. begin
  1037. consume(_POINT);
  1038. getsymonlyin(punitsym(srsym)^.unitsymtable,pattern);
  1039. {$ifdef TEST_PROCSYMS}
  1040. unit_specific:=true;
  1041. {$endif TEST_PROCSYMS}
  1042. consume(_ID);
  1043. {$ifdef TEST_PROCSYMS}
  1044. end
  1045. else
  1046. unit_specific:=false;
  1047. {$else TEST_PROCSYMS}
  1048. end;
  1049. {$endif TEST_PROCSYMS}
  1050. if not assigned(srsym) then
  1051. Begin
  1052. p1:=cerrornode.create;
  1053. { try to clean up }
  1054. pd:=generrordef;
  1055. end
  1056. else
  1057. Begin
  1058. { check semantics of private }
  1059. if (srsym^.typ in [propertysym,procsym,varsym]) and
  1060. (srsymtable^.symtabletype=objectsymtable) then
  1061. begin
  1062. if (sp_private in srsym^.symoptions) and
  1063. (pobjectdef(srsym^.owner^.defowner)^.owner^.symtabletype=unitsymtable) then
  1064. Message(parser_e_cant_access_private_member);
  1065. end;
  1066. case srsym^.typ of
  1067. absolutesym : begin
  1068. p1:=genloadnode(pvarsym(srsym),srsymtable);
  1069. pd:=pabsolutesym(srsym)^.vartype.def;
  1070. end;
  1071. varsym : begin
  1072. { are we in a class method ? }
  1073. if (srsymtable^.symtabletype=objectsymtable) and
  1074. assigned(aktprocsym) and
  1075. (po_classmethod in aktprocsym^.definition^.procoptions) then
  1076. Message(parser_e_only_class_methods);
  1077. if (sp_static in srsym^.symoptions) then
  1078. begin
  1079. static_name:=lower(srsym^.owner^.name^)+'_'+srsym^.name;
  1080. getsym(static_name,true);
  1081. end;
  1082. p1:=genloadnode(pvarsym(srsym),srsymtable);
  1083. if pvarsym(srsym)^.varstate=vs_declared then
  1084. begin
  1085. include(p1.flags,nf_first);
  1086. { set special between first loaded until checked in firstpass }
  1087. pvarsym(srsym)^.varstate:=vs_declared_and_first_found;
  1088. end;
  1089. pd:=pvarsym(srsym)^.vartype.def;
  1090. end;
  1091. typedconstsym : begin
  1092. p1:=gentypedconstloadnode(ptypedconstsym(srsym),srsymtable);
  1093. pd:=ptypedconstsym(srsym)^.typedconsttype.def;
  1094. end;
  1095. syssym : p1:=statement_syssym(psyssym(srsym)^.number,pd);
  1096. typesym : begin
  1097. pd:=ptypesym(srsym)^.restype.def;
  1098. if not assigned(pd) then
  1099. begin
  1100. pd:=generrordef;
  1101. again:=false;
  1102. end
  1103. else
  1104. begin
  1105. { if we read a type declaration }
  1106. { we have to return the type and }
  1107. { nothing else }
  1108. if block_type=bt_type then
  1109. begin
  1110. { we don't need sym reference when it's in the
  1111. current unit or system unit, because those
  1112. units are always loaded (PFV) }
  1113. if not(assigned(pd^.owner)) or
  1114. (pd^.owner^.unitid=0) or
  1115. (pd^.owner^.unitid=1) then
  1116. p1:=gentypenode(pd,nil)
  1117. else
  1118. p1:=gentypenode(pd,ptypesym(srsym));
  1119. { here we can also set resulttype !! }
  1120. p1.resulttype:=pd;
  1121. pd:=voiddef;
  1122. end
  1123. else { not type block }
  1124. begin
  1125. if token=_LKLAMMER then
  1126. begin
  1127. consume(_LKLAMMER);
  1128. p1:=comp_expr(true);
  1129. consume(_RKLAMMER);
  1130. p1:=gentypeconvnode(p1,pd);
  1131. include(p1.flags,nf_explizit);
  1132. end
  1133. else { not LKLAMMER }
  1134. if (token=_POINT) and
  1135. is_object(pd) then
  1136. begin
  1137. consume(_POINT);
  1138. if assigned(procinfo) and
  1139. assigned(procinfo^._class) and
  1140. not(getaddr) then
  1141. begin
  1142. if procinfo^._class^.is_related(pobjectdef(pd)) then
  1143. begin
  1144. p1:=gentypenode(pd,ptypesym(srsym));
  1145. p1.resulttype:=pd;
  1146. { search also in inherited methods }
  1147. repeat
  1148. srsymtable:=pobjectdef(pd)^.symtable;
  1149. sym:=pvarsym(srsymtable^.search(pattern));
  1150. if assigned(sym) then
  1151. break;
  1152. pd:=pobjectdef(pd)^.childof;
  1153. until not assigned(pd);
  1154. consume(_ID);
  1155. do_member_read(false,sym,p1,pd,again);
  1156. end
  1157. else
  1158. begin
  1159. Message(parser_e_no_super_class);
  1160. pd:=generrordef;
  1161. again:=false;
  1162. end;
  1163. end
  1164. else
  1165. begin
  1166. { allows @TObject.Load }
  1167. { also allows static methods and variables }
  1168. p1:=ctypenode.create(nil,nil);
  1169. p1.resulttype:=pd;
  1170. { TP allows also @TMenu.Load if Load is only }
  1171. { defined in an anchestor class }
  1172. sym:=pvarsym(search_class_member(pobjectdef(pd),pattern));
  1173. if not assigned(sym) then
  1174. Message1(sym_e_id_no_member,pattern)
  1175. else if not(getaddr) and not(sp_static in sym^.symoptions) then
  1176. Message(sym_e_only_static_in_static)
  1177. else
  1178. begin
  1179. consume(_ID);
  1180. do_member_read(getaddr,sym,p1,pd,again);
  1181. end;
  1182. end;
  1183. end
  1184. else
  1185. begin
  1186. { class reference ? }
  1187. if is_class(pd) then
  1188. begin
  1189. if getaddr and (token=_POINT) then
  1190. begin
  1191. consume(_POINT);
  1192. { allows @Object.Method }
  1193. { also allows static methods and variables }
  1194. p1:=gentypenode(nil,nil);
  1195. p1.resulttype:=pd;
  1196. { TP allows also @TMenu.Load if Load is only }
  1197. { defined in an anchestor class }
  1198. sym:=pvarsym(search_class_member(pobjectdef(pd),pattern));
  1199. if not assigned(sym) then
  1200. Message1(sym_e_id_no_member,pattern)
  1201. else
  1202. begin
  1203. consume(_ID);
  1204. do_member_read(getaddr,sym,p1,pd,again);
  1205. end;
  1206. end
  1207. else
  1208. begin
  1209. p1:=gentypenode(pd,nil);
  1210. p1.resulttype:=pd;
  1211. pd:=new(pclassrefdef,init(pd));
  1212. p1:=cloadvmtnode.create(p1);
  1213. p1.resulttype:=pd;
  1214. end;
  1215. end
  1216. else
  1217. begin
  1218. { generate a type node }
  1219. { (for typeof etc) }
  1220. if allow_type then
  1221. begin
  1222. p1:=gentypenode(pd,nil);
  1223. { here we must use typenodetype explicitly !! PM
  1224. p1.resulttype:=pd; }
  1225. pd:=voiddef;
  1226. end
  1227. else
  1228. Message(parser_e_no_type_not_allowed_here);
  1229. end;
  1230. end;
  1231. end;
  1232. end;
  1233. end;
  1234. enumsym : begin
  1235. p1:=genenumnode(penumsym(srsym));
  1236. pd:=p1.resulttype;
  1237. end;
  1238. constsym : begin
  1239. case pconstsym(srsym)^.consttyp of
  1240. constint :
  1241. { do a very dirty trick to bootstrap this code }
  1242. if (pconstsym(srsym)^.value>=-(int64(2147483647)+int64(1))) and (pconstsym(srsym)^.value<=2147483647) then
  1243. p1:=genordinalconstnode(pconstsym(srsym)^.value,s32bitdef)
  1244. else
  1245. p1:=genordinalconstnode(pconstsym(srsym)^.value,cs64bitdef);
  1246. conststring :
  1247. begin
  1248. len:=pconstsym(srsym)^.len;
  1249. if not(cs_ansistrings in aktlocalswitches) and (len>255) then
  1250. len:=255;
  1251. getmem(pc,len+1);
  1252. move(pchar(tpointerord(pconstsym(srsym)^.value))^,pc^,len);
  1253. pc[len]:=#0;
  1254. p1:=genpcharconstnode(pc,len);
  1255. end;
  1256. constchar :
  1257. p1:=genordinalconstnode(pconstsym(srsym)^.value,cchardef);
  1258. constreal :
  1259. p1:=genrealconstnode(pbestreal(tpointerord(pconstsym(srsym)^.value))^,bestrealdef^);
  1260. constbool :
  1261. p1:=genordinalconstnode(pconstsym(srsym)^.value,booldef);
  1262. constset :
  1263. p1:=gensetconstnode(pconstset(tpointerord(pconstsym(srsym)^.value)),
  1264. psetdef(pconstsym(srsym)^.consttype.def));
  1265. constord :
  1266. p1:=genordinalconstnode(pconstsym(srsym)^.value,
  1267. pconstsym(srsym)^.consttype.def);
  1268. constpointer :
  1269. p1:=genpointerconstnode(pconstsym(srsym)^.value,
  1270. pconstsym(srsym)^.consttype.def);
  1271. constnil :
  1272. p1:=cnilnode.create;
  1273. constresourcestring:
  1274. begin
  1275. p1:=genloadnode(pvarsym(srsym),srsymtable);
  1276. p1.resulttype:=cansistringdef;
  1277. end;
  1278. end;
  1279. pd:=p1.resulttype;
  1280. end;
  1281. procsym : begin
  1282. { are we in a class method ? }
  1283. possible_error:=(srsymtable^.symtabletype=objectsymtable) and
  1284. assigned(aktprocsym) and
  1285. (po_classmethod in aktprocsym^.definition^.procoptions);
  1286. p1:=gencallnode(pprocsym(srsym),srsymtable);
  1287. {$ifdef TEST_PROCSYMS}
  1288. p1.unit_specific:=unit_specific;
  1289. {$endif TEST_PROCSYMS}
  1290. do_proc_call(getaddr or
  1291. (getprocvar and
  1292. ((block_type=bt_const) or
  1293. ((m_tp_procvar in aktmodeswitches) and
  1294. proc_to_procvar_equal(pprocsym(srsym)^.definition,getprocvardef)
  1295. )
  1296. )
  1297. ),again,tcallnode(p1),pd);
  1298. if (block_type=bt_const) and
  1299. getprocvar then
  1300. handle_procvar(getprocvardef,p1);
  1301. if possible_error and
  1302. not(po_classmethod in tcallnode(p1).procdefinition^.procoptions) then
  1303. Message(parser_e_only_class_methods);
  1304. end;
  1305. propertysym : begin
  1306. { access to property in a method }
  1307. { are we in a class method ? }
  1308. if (srsymtable^.symtabletype=objectsymtable) and
  1309. assigned(aktprocsym) and
  1310. (po_classmethod in aktprocsym^.definition^.procoptions) then
  1311. Message(parser_e_only_class_methods);
  1312. { no method pointer }
  1313. p1:=nil;
  1314. handle_propertysym(srsym,srsymtable,p1,pd);
  1315. end;
  1316. errorsym : begin
  1317. p1:=cerrornode.create;
  1318. p1.resulttype:=generrordef;
  1319. pd:=generrordef;
  1320. if token=_LKLAMMER then
  1321. begin
  1322. consume(_LKLAMMER);
  1323. parse_paras(false,false);
  1324. consume(_RKLAMMER);
  1325. end;
  1326. end;
  1327. else
  1328. begin
  1329. p1:=cerrornode.create;
  1330. pd:=generrordef;
  1331. Message(cg_e_illegal_expression);
  1332. end;
  1333. end; { end case }
  1334. end;
  1335. end;
  1336. end;
  1337. end;
  1338. {---------------------------------------------
  1339. Factor_Read_Set
  1340. ---------------------------------------------}
  1341. { Read a set between [] }
  1342. function factor_read_set:tnode;
  1343. var
  1344. p1 : tnode;
  1345. lastp,
  1346. buildp : tarrayconstructornode;
  1347. begin
  1348. buildp:=nil;
  1349. { be sure that a least one arrayconstructn is used, also for an
  1350. empty [] }
  1351. if token=_RECKKLAMMER then
  1352. buildp:=carrayconstructornode.create(nil,buildp)
  1353. else
  1354. begin
  1355. while true do
  1356. begin
  1357. p1:=comp_expr(true);
  1358. if token=_POINTPOINT then
  1359. begin
  1360. consume(_POINTPOINT);
  1361. p2:=comp_expr(true);
  1362. p1:=carrayconstructorrangenode.create(p1,p2);
  1363. end;
  1364. { insert at the end of the tree, to get the correct order }
  1365. if not assigned(buildp) then
  1366. begin
  1367. buildp:=carrayconstructornode.create(p1,nil);
  1368. lastp:=buildp;
  1369. end
  1370. else
  1371. begin
  1372. lastp.right:=carrayconstructornode.create(p1,nil);
  1373. lastp:=tarrayconstructornode(lastp.right);
  1374. end;
  1375. { there could be more elements }
  1376. if token=_COMMA then
  1377. consume(_COMMA)
  1378. else
  1379. break;
  1380. end;
  1381. end;
  1382. factor_read_set:=buildp;
  1383. end;
  1384. {---------------------------------------------
  1385. Helpers
  1386. ---------------------------------------------}
  1387. procedure check_tokenpos;
  1388. begin
  1389. if (p1<>oldp1) then
  1390. begin
  1391. if assigned(p1) then
  1392. p1.set_tree_filepos(filepos);
  1393. oldp1:=p1;
  1394. filepos:=akttokenpos;
  1395. end;
  1396. end;
  1397. {---------------------------------------------
  1398. PostFixOperators
  1399. ---------------------------------------------}
  1400. procedure postfixoperators;
  1401. { tries to avoid syntax errors after invalid qualifiers }
  1402. procedure recoverconsume_postfixops;
  1403. begin
  1404. while true do
  1405. begin
  1406. case token of
  1407. _CARET:
  1408. consume(_CARET);
  1409. _POINT:
  1410. begin
  1411. consume(_POINT);
  1412. if token=_ID then
  1413. consume(_ID);
  1414. end;
  1415. _LECKKLAMMER:
  1416. begin
  1417. consume(_LECKKLAMMER);
  1418. repeat
  1419. comp_expr(true);
  1420. if token=_COMMA then
  1421. consume(_COMMA)
  1422. else
  1423. break;
  1424. until false;
  1425. consume(_RECKKLAMMER);
  1426. end
  1427. else
  1428. break;
  1429. end;
  1430. end;
  1431. end;
  1432. var
  1433. store_static : boolean;
  1434. { p1 and p2 must contain valid value_str }
  1435. begin
  1436. check_tokenpos;
  1437. while again do
  1438. begin
  1439. { prevent crashes with unknown types }
  1440. if not assigned(pd) then
  1441. begin
  1442. recoverconsume_postfixops;
  1443. exit;
  1444. end;
  1445. { handle token }
  1446. case token of
  1447. _CARET:
  1448. begin
  1449. consume(_CARET);
  1450. if (pd^.deftype<>pointerdef) then
  1451. begin
  1452. { ^ as binary operator is a problem!!!! (FK) }
  1453. again:=false;
  1454. Message(cg_e_invalid_qualifier);
  1455. recoverconsume_postfixops;
  1456. p1.destroy;
  1457. p1:=cerrornode.create;
  1458. end
  1459. else
  1460. begin
  1461. p1:=cderefnode.create(p1);
  1462. pd:=ppointerdef(pd)^.pointertype.def;
  1463. end;
  1464. end;
  1465. _LECKKLAMMER:
  1466. begin
  1467. if is_class_or_interface(pd) then
  1468. begin
  1469. { default property }
  1470. propsym:=search_default_property(pobjectdef(pd));
  1471. if not(assigned(propsym)) then
  1472. begin
  1473. p1.destroy;
  1474. p1:=cerrornode.create;
  1475. again:=false;
  1476. message(parser_e_no_default_property_available);
  1477. end
  1478. else
  1479. handle_propertysym(propsym,propsym^.owner,p1,pd);
  1480. end
  1481. else
  1482. begin
  1483. consume(_LECKKLAMMER);
  1484. repeat
  1485. case pd^.deftype of
  1486. pointerdef:
  1487. begin
  1488. pd:=ppointerdef(pd)^.pointertype.def;
  1489. { support delphi autoderef }
  1490. if (pd^.deftype=arraydef) and
  1491. (m_autoderef in aktmodeswitches) then
  1492. begin
  1493. p1:=cderefnode.create(p1);
  1494. pd:=parraydef(pd)^.elementtype.def;
  1495. end;
  1496. p2:=comp_expr(true);
  1497. p1:=cvecnode.create(p1,p2);
  1498. end;
  1499. stringdef : begin
  1500. p2:=comp_expr(true);
  1501. p1:=cvecnode.create(p1,p2);
  1502. pd:=cchardef
  1503. end;
  1504. arraydef : begin
  1505. p2:=comp_expr(true);
  1506. { support SEG:OFS for go32v2 Mem[] }
  1507. if (target_info.target=target_i386_go32v2) and
  1508. (p1.nodetype=loadn) and
  1509. assigned(tloadnode(p1).symtableentry) and
  1510. assigned(tloadnode(p1).symtableentry^.owner^.name) and
  1511. (tloadnode(p1).symtableentry^.owner^.name^='SYSTEM') and
  1512. ((tloadnode(p1).symtableentry^.name='MEM') or
  1513. (tloadnode(p1).symtableentry^.name='MEMW') or
  1514. (tloadnode(p1).symtableentry^.name='MEML')) then
  1515. begin
  1516. if (token=_COLON) then
  1517. begin
  1518. consume(_COLON);
  1519. p3:=caddnode.create(muln,genordinalconstnode($10,s32bitdef),p2);
  1520. p2:=comp_expr(true);
  1521. p2:=caddnode.create(addn,p2,p3);
  1522. p1:=cvecnode.create(p1,p2);
  1523. include(tvecnode(p1).flags,nf_memseg);
  1524. include(tvecnode(p1).flags,nf_memindex);
  1525. end
  1526. else
  1527. begin
  1528. p1:=cvecnode.create(p1,p2);
  1529. include(tvecnode(p1).flags,nf_memindex);
  1530. end;
  1531. end
  1532. else
  1533. p1:=cvecnode.create(p1,p2);
  1534. pd:=parraydef(pd)^.elementtype.def;
  1535. end;
  1536. else
  1537. begin
  1538. Message(cg_e_invalid_qualifier);
  1539. p1.destroy;
  1540. p1:=cerrornode.create;
  1541. comp_expr(true);
  1542. again:=false;
  1543. end;
  1544. end;
  1545. if token=_COMMA then
  1546. consume(_COMMA)
  1547. else
  1548. break;
  1549. until false;
  1550. consume(_RECKKLAMMER);
  1551. end;
  1552. end;
  1553. _POINT : begin
  1554. consume(_POINT);
  1555. if (pd^.deftype=pointerdef) and
  1556. (m_autoderef in aktmodeswitches) then
  1557. begin
  1558. p1:=cderefnode.create(p1);
  1559. pd:=ppointerdef(pd)^.pointertype.def;
  1560. end;
  1561. case pd^.deftype of
  1562. recorddef:
  1563. begin
  1564. sym:=psym(precorddef(pd)^.symtable^.search(pattern));
  1565. if assigned(sym) and
  1566. (sym^.typ=varsym) then
  1567. begin
  1568. p1:=gensubscriptnode(pvarsym(sym),p1);
  1569. pd:=pvarsym(sym)^.vartype.def;
  1570. end
  1571. else
  1572. begin
  1573. Message1(sym_e_illegal_field,pattern);
  1574. p1.destroy;
  1575. p1:=cerrornode.create;
  1576. end;
  1577. consume(_ID);
  1578. end;
  1579. classrefdef:
  1580. begin
  1581. classh:=pobjectdef(pclassrefdef(pd)^.pointertype.def);
  1582. sym:=nil;
  1583. while assigned(classh) do
  1584. begin
  1585. sym:=psym(classh^.symtable^.search(pattern));
  1586. srsymtable:=classh^.symtable;
  1587. if assigned(sym) then
  1588. break;
  1589. classh:=classh^.childof;
  1590. end;
  1591. if sym=nil then
  1592. begin
  1593. Message1(sym_e_id_no_member,pattern);
  1594. p1.destroy;
  1595. p1:=cerrornode.create;
  1596. { try to clean up }
  1597. pd:=generrordef;
  1598. consume(_ID);
  1599. end
  1600. else
  1601. begin
  1602. consume(_ID);
  1603. do_member_read(getaddr,sym,p1,pd,again);
  1604. end;
  1605. end;
  1606. objectdef:
  1607. begin
  1608. classh:=pobjectdef(pd);
  1609. sym:=nil;
  1610. store_static:=allow_only_static;
  1611. allow_only_static:=false;
  1612. while assigned(classh) do
  1613. begin
  1614. sym:=psym(classh^.symtable^.search(pattern));
  1615. srsymtable:=classh^.symtable;
  1616. if assigned(sym) then
  1617. break;
  1618. classh:=classh^.childof;
  1619. end;
  1620. allow_only_static:=store_static;
  1621. if sym=nil then
  1622. begin
  1623. Message1(sym_e_id_no_member,pattern);
  1624. p1.destroy;
  1625. p1:=cerrornode.create;
  1626. { try to clean up }
  1627. pd:=generrordef;
  1628. consume(_ID);
  1629. end
  1630. else
  1631. begin
  1632. consume(_ID);
  1633. do_member_read(getaddr,sym,p1,pd,again);
  1634. end;
  1635. end;
  1636. pointerdef:
  1637. begin
  1638. Message(cg_e_invalid_qualifier);
  1639. if ppointerdef(pd)^.pointertype.def^.deftype in [recorddef,objectdef,classrefdef] then
  1640. Message(parser_h_maybe_deref_caret_missing);
  1641. end;
  1642. else
  1643. begin
  1644. Message(cg_e_invalid_qualifier);
  1645. p1.destroy;
  1646. p1:=cerrornode.create;
  1647. consume(_ID);
  1648. end;
  1649. end;
  1650. end;
  1651. else
  1652. begin
  1653. { is this a procedure variable ? }
  1654. if assigned(pd) then
  1655. begin
  1656. if (pd^.deftype=procvardef) then
  1657. begin
  1658. if getprocvar and is_equal(pd,getprocvardef) then
  1659. again:=false
  1660. else
  1661. if (token=_LKLAMMER) or
  1662. ((pprocvardef(pd)^.para^.empty) and
  1663. (not((token in [_ASSIGNMENT,_UNEQUAL,_EQUAL]))) and
  1664. (not afterassignment) and
  1665. (not in_args)) then
  1666. begin
  1667. { do this in a strange way }
  1668. { it's not a clean solution }
  1669. p2:=p1;
  1670. p1:=gencallnode(nil,nil);
  1671. tcallnode(p1).right:=p2;
  1672. {$ifdef TEST_PROCSYMS}
  1673. p1.unit_specific:=unit_specific;
  1674. {$endif TEST_PROCSYMS}
  1675. tcallnode(p1).symtableprocentry:=pprocsym(sym);
  1676. if token=_LKLAMMER then
  1677. begin
  1678. consume(_LKLAMMER);
  1679. tcallnode(p1).left:=parse_paras(false,false);
  1680. consume(_RKLAMMER);
  1681. end;
  1682. pd:=pprocvardef(pd)^.rettype.def;
  1683. { proc():= is never possible }
  1684. if token=_ASSIGNMENT then
  1685. begin
  1686. Message(cg_e_illegal_expression);
  1687. p1:=cerrornode.create;
  1688. again:=false;
  1689. end;
  1690. p1.resulttype:=pd;
  1691. end
  1692. else
  1693. again:=false;
  1694. p1.resulttype:=pd;
  1695. end
  1696. else
  1697. again:=false;
  1698. end
  1699. else
  1700. again:=false;
  1701. end;
  1702. end;
  1703. check_tokenpos;
  1704. end; { while again }
  1705. end;
  1706. {---------------------------------------------
  1707. Factor (Main)
  1708. ---------------------------------------------}
  1709. begin
  1710. oldp1:=nil;
  1711. p1:=nil;
  1712. filepos:=akttokenpos;
  1713. if token=_ID then
  1714. begin
  1715. factor_read_id;
  1716. { handle post fix operators }
  1717. postfixoperators;
  1718. end
  1719. else
  1720. case token of
  1721. _NEW : begin
  1722. consume(_NEW);
  1723. consume(_LKLAMMER);
  1724. {allow_type:=true;}
  1725. p1:=factor(false);
  1726. {allow_type:=false;}
  1727. if p1.nodetype<>typen then
  1728. begin
  1729. Message(type_e_type_id_expected);
  1730. p1.destroy;
  1731. pd:=generrordef;
  1732. end
  1733. else
  1734. pd:=ttypenode(p1).typenodetype;
  1735. pd2:=pd;
  1736. if (pd^.deftype<>pointerdef) then
  1737. Message1(type_e_pointer_type_expected,pd^.typename)
  1738. else
  1739. if token=_RKLAMMER then
  1740. begin
  1741. if (ppointerdef(pd)^.pointertype.def^.deftype=objectdef) and
  1742. (oo_has_vmt in pobjectdef(ppointerdef(pd)^.pointertype.def)^.objectoptions) then
  1743. Message(parser_w_use_extended_syntax_for_objects);
  1744. p1:=cnewnode.create(nil);
  1745. p1.resulttype:=pd2;
  1746. consume(_RKLAMMER);
  1747. end
  1748. else
  1749. begin
  1750. p1.destroy;
  1751. p1:=chnewnode.create;
  1752. p1.resulttype:=ppointerdef(pd)^.pointertype.def;
  1753. consume(_COMMA);
  1754. afterassignment:=false;
  1755. { determines the current object defintion }
  1756. classh:=pobjectdef(ppointerdef(pd)^.pointertype.def);
  1757. if classh^.deftype<>objectdef then
  1758. Message(parser_e_pointer_to_class_expected)
  1759. else
  1760. begin
  1761. { check for an abstract class }
  1762. if (oo_has_abstract in classh^.objectoptions) then
  1763. Message(sym_e_no_instance_of_abstract_object);
  1764. { search the constructor also in the symbol tables of
  1765. the parents }
  1766. sym:=nil;
  1767. while assigned(classh) do
  1768. begin
  1769. sym:=psym(classh^.symtable^.search(pattern));
  1770. srsymtable:=classh^.symtable;
  1771. if assigned(sym) then
  1772. break;
  1773. classh:=classh^.childof;
  1774. end;
  1775. consume(_ID);
  1776. do_member_read(false,sym,p1,pd,again);
  1777. if (p1.nodetype<>calln) or
  1778. (assigned(tcallnode(p1).procdefinition) and
  1779. (tcallnode(p1).procdefinition^.proctypeoption<>potype_constructor)) then
  1780. Message(parser_e_expr_have_to_be_constructor_call);
  1781. end;
  1782. p1:=cnewnode.create(p1);
  1783. { set the resulttype }
  1784. p1.resulttype:=pd2;
  1785. consume(_RKLAMMER);
  1786. end;
  1787. postfixoperators;
  1788. end;
  1789. _SELF : begin
  1790. again:=true;
  1791. consume(_SELF);
  1792. if not assigned(procinfo^._class) then
  1793. begin
  1794. p1:=cerrornode.create;
  1795. pd:=generrordef;
  1796. again:=false;
  1797. Message(parser_e_self_not_in_method);
  1798. end
  1799. else
  1800. begin
  1801. if (po_classmethod in aktprocsym^.definition^.procoptions) then
  1802. begin
  1803. { self in class methods is a class reference type }
  1804. pd:=new(pclassrefdef,init(procinfo^._class));
  1805. p1:=genselfnode(pd);
  1806. p1.resulttype:=pd;
  1807. end
  1808. else
  1809. begin
  1810. p1:=genselfnode(procinfo^._class);
  1811. p1.resulttype:=procinfo^._class;
  1812. end;
  1813. pd:=p1.resulttype;
  1814. postfixoperators;
  1815. end;
  1816. end;
  1817. _INHERITED : begin
  1818. again:=true;
  1819. consume(_INHERITED);
  1820. if assigned(procinfo^._class) then
  1821. begin
  1822. { if inherited; only then we need the method with
  1823. the same name }
  1824. if token=_SEMICOLON then
  1825. begin
  1826. hs:=aktprocsym^.name;
  1827. auto_inherited:=true
  1828. end
  1829. else
  1830. begin
  1831. hs:=pattern;
  1832. consume(_ID);
  1833. auto_inherited:=false;
  1834. end;
  1835. classh:=procinfo^._class^.childof;
  1836. while assigned(classh) do
  1837. begin
  1838. srsymtable:=pobjectdef(classh)^.symtable;
  1839. sym:=psym(srsymtable^.search(hs));
  1840. if assigned(sym) then
  1841. begin
  1842. { only for procsyms we need to set the type (PFV) }
  1843. case sym^.typ of
  1844. procsym :
  1845. begin
  1846. p1:=ctypenode.create(nil,nil);
  1847. p1.resulttype:=classh;
  1848. pd:=p1.resulttype;
  1849. end;
  1850. varsym :
  1851. begin
  1852. p1:=nil;
  1853. pd:=pvarsym(sym)^.vartype.def;
  1854. end;
  1855. propertysym :
  1856. begin
  1857. p1:=nil;
  1858. pd:=ppropertysym(sym)^.proptype.def;
  1859. end;
  1860. else
  1861. internalerror(83251763);
  1862. end;
  1863. do_member_read(false,sym,p1,pd,again);
  1864. break;
  1865. end;
  1866. classh:=classh^.childof;
  1867. end;
  1868. if classh=nil then
  1869. begin
  1870. Message1(sym_e_id_no_member,hs);
  1871. again:=false;
  1872. pd:=generrordef;
  1873. p1:=cerrornode.create;
  1874. end;
  1875. { turn auto inheriting off }
  1876. auto_inherited:=false;
  1877. end
  1878. else
  1879. begin
  1880. Message(parser_e_generic_methods_only_in_methods);
  1881. again:=false;
  1882. pd:=generrordef;
  1883. p1:=cerrornode.create;
  1884. end;
  1885. postfixoperators;
  1886. end;
  1887. _INTCONST : begin
  1888. valint(pattern,l,code);
  1889. if code<>0 then
  1890. begin
  1891. { try int64 if available }
  1892. { if no int64 available longint is tried a second }
  1893. { time which doesn't hurt }
  1894. val(pattern,ic,code);
  1895. if code<>0 then
  1896. begin
  1897. val(pattern,d,code);
  1898. if code<>0 then
  1899. begin
  1900. Message(cg_e_invalid_integer);
  1901. consume(_INTCONST);
  1902. l:=1;
  1903. p1:=genordinalconstnode(l,s32bitdef);
  1904. end
  1905. else
  1906. begin
  1907. consume(_INTCONST);
  1908. p1:=genrealconstnode(d,bestrealdef^);
  1909. end;
  1910. end
  1911. else
  1912. begin
  1913. consume(_INTCONST);
  1914. p1:=genordinalconstnode(ic,cs64bitdef);
  1915. end;
  1916. end
  1917. else
  1918. begin
  1919. consume(_INTCONST);
  1920. p1:=genordinalconstnode(l,s32bitdef)
  1921. end;
  1922. end;
  1923. _REALNUMBER : begin
  1924. val(pattern,d,code);
  1925. if code<>0 then
  1926. begin
  1927. Message(parser_e_error_in_real);
  1928. d:=1.0;
  1929. end;
  1930. consume(_REALNUMBER);
  1931. p1:=genrealconstnode(d,bestrealdef^);
  1932. end;
  1933. _STRING : begin
  1934. pd:=string_dec;
  1935. { STRING can be also a type cast }
  1936. if token=_LKLAMMER then
  1937. begin
  1938. consume(_LKLAMMER);
  1939. p1:=comp_expr(true);
  1940. consume(_RKLAMMER);
  1941. p1:=gentypeconvnode(p1,pd);
  1942. include(p1.flags,nf_explizit);
  1943. { handle postfix operators here e.g. string(a)[10] }
  1944. again:=true;
  1945. postfixoperators;
  1946. end
  1947. else
  1948. p1:=gentypenode(pd,nil);
  1949. end;
  1950. _FILE : begin
  1951. pd:=cfiledef;
  1952. consume(_FILE);
  1953. { FILE can be also a type cast }
  1954. if token=_LKLAMMER then
  1955. begin
  1956. consume(_LKLAMMER);
  1957. p1:=comp_expr(true);
  1958. consume(_RKLAMMER);
  1959. p1:=gentypeconvnode(p1,pd);
  1960. include(p1.flags,nf_explizit);
  1961. { handle postfix operators here e.g. string(a)[10] }
  1962. again:=true;
  1963. postfixoperators;
  1964. end
  1965. else
  1966. p1:=gentypenode(pd,nil);
  1967. end;
  1968. _CSTRING : begin
  1969. p1:=genstringconstnode(pattern,st_default);
  1970. consume(_CSTRING);
  1971. end;
  1972. _CCHAR : begin
  1973. p1:=genordinalconstnode(ord(pattern[1]),cchardef);
  1974. consume(_CCHAR);
  1975. end;
  1976. _KLAMMERAFFE : begin
  1977. consume(_KLAMMERAFFE);
  1978. got_addrn:=true;
  1979. { support both @<x> and @(<x>) }
  1980. if token=_LKLAMMER then
  1981. begin
  1982. consume(_LKLAMMER);
  1983. p1:=factor(true);
  1984. consume(_RKLAMMER);
  1985. if token in [_CARET,_POINT,_LECKKLAMMER] then
  1986. begin
  1987. { we need the resulttype }
  1988. { of the expression in pd }
  1989. do_firstpass(p1);
  1990. pd:=p1.resulttype;
  1991. again:=true;
  1992. postfixoperators;
  1993. end;
  1994. end
  1995. else
  1996. p1:=factor(true);
  1997. got_addrn:=false;
  1998. p1:=caddrnode.create(p1);
  1999. end;
  2000. _LKLAMMER : begin
  2001. consume(_LKLAMMER);
  2002. p1:=comp_expr(true);
  2003. consume(_RKLAMMER);
  2004. { it's not a good solution }
  2005. { but (a+b)^ makes some problems }
  2006. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2007. begin
  2008. { we need the resulttype }
  2009. { of the expression in pd }
  2010. do_firstpass(p1);
  2011. pd:=p1.resulttype;
  2012. again:=true;
  2013. postfixoperators;
  2014. end;
  2015. end;
  2016. _LECKKLAMMER : begin
  2017. consume(_LECKKLAMMER);
  2018. p1:=factor_read_set;
  2019. consume(_RECKKLAMMER);
  2020. end;
  2021. _PLUS : begin
  2022. consume(_PLUS);
  2023. p1:=factor(false);
  2024. end;
  2025. _MINUS : begin
  2026. consume(_MINUS);
  2027. p1:=sub_expr(oppower,false);
  2028. p1:=cunaryminusnode.create(p1);
  2029. end;
  2030. _OP_NOT : begin
  2031. consume(_OP_NOT);
  2032. p1:=factor(false);
  2033. p1:=cnotnode.create(p1);
  2034. end;
  2035. _TRUE : begin
  2036. consume(_TRUE);
  2037. p1:=genordinalconstnode(1,booldef);
  2038. end;
  2039. _FALSE : begin
  2040. consume(_FALSE);
  2041. p1:=genordinalconstnode(0,booldef);
  2042. end;
  2043. _NIL : begin
  2044. consume(_NIL);
  2045. p1:=cnilnode.create;
  2046. end;
  2047. else
  2048. begin
  2049. p1:=cerrornode.create;
  2050. consume(token);
  2051. Message(cg_e_illegal_expression);
  2052. end;
  2053. end;
  2054. { generate error node if no node is created }
  2055. if not assigned(p1) then
  2056. p1:=cerrornode.create;
  2057. { tp7 procvar handling, but not if the next token
  2058. will be a := }
  2059. if (m_tp_procvar in aktmodeswitches) and
  2060. (token<>_ASSIGNMENT) then
  2061. check_tp_procvar(p1);
  2062. factor:=p1;
  2063. check_tokenpos;
  2064. end;
  2065. {$ifdef fpc}
  2066. {$maxfpuregisters default}
  2067. {$endif fpc}
  2068. {****************************************************************************
  2069. Sub_Expr
  2070. ****************************************************************************}
  2071. const
  2072. { Warning these stay be ordered !! }
  2073. operator_levels:array[Toperator_precedence] of set of Ttoken=
  2074. ([_LT,_LTE,_GT,_GTE,_EQUAL,_UNEQUAL,_OP_IN,_OP_IS],
  2075. [_PLUS,_MINUS,_OP_OR,_OP_XOR],
  2076. [_CARET,_SYMDIF,_STARSTAR,_STAR,_SLASH,
  2077. _OP_AS,_OP_AND,_OP_DIV,_OP_MOD,_OP_SHL,_OP_SHR],
  2078. [_STARSTAR] );
  2079. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):tnode;
  2080. {Reads a subexpression while the operators are of the current precedence
  2081. level, or any higher level. Replaces the old term, simpl_expr and
  2082. simpl2_expr.}
  2083. var
  2084. p1,p2 : tnode;
  2085. oldt : Ttoken;
  2086. filepos : tfileposinfo;
  2087. begin
  2088. if pred_level=highest_precedence then
  2089. p1:=factor(false)
  2090. else
  2091. p1:=sub_expr(succ(pred_level),true);
  2092. repeat
  2093. if (token in operator_levels[pred_level]) and
  2094. ((token<>_EQUAL) or accept_equal) then
  2095. begin
  2096. oldt:=token;
  2097. filepos:=akttokenpos;
  2098. consume(token);
  2099. if pred_level=highest_precedence then
  2100. p2:=factor(false)
  2101. else
  2102. p2:=sub_expr(succ(pred_level),true);
  2103. case oldt of
  2104. _PLUS :
  2105. p1:=caddnode.create(addn,p1,p2);
  2106. _MINUS :
  2107. p1:=caddnode.create(subn,p1,p2);
  2108. _STAR :
  2109. p1:=caddnode.create(muln,p1,p2);
  2110. _SLASH :
  2111. p1:=caddnode.create(slashn,p1,p2);
  2112. _EQUAL :
  2113. p1:=caddnode.create(equaln,p1,p2);
  2114. _GT :
  2115. p1:=caddnode.create(gtn,p1,p2);
  2116. _LT :
  2117. p1:=caddnode.create(ltn,p1,p2);
  2118. _GTE :
  2119. p1:=caddnode.create(gten,p1,p2);
  2120. _LTE :
  2121. p1:=caddnode.create(lten,p1,p2);
  2122. _SYMDIF :
  2123. p1:=caddnode.create(symdifn,p1,p2);
  2124. _STARSTAR :
  2125. p1:=caddnode.create(starstarn,p1,p2);
  2126. _OP_AS :
  2127. p1:=casnode.create(p1,p2);
  2128. _OP_IN :
  2129. p1:=cinnode.create(p1,p2);
  2130. _OP_IS :
  2131. p1:=cisnode.create(p1,p2);
  2132. _OP_OR :
  2133. p1:=caddnode.create(orn,p1,p2);
  2134. _OP_AND :
  2135. p1:=caddnode.create(andn,p1,p2);
  2136. _OP_DIV :
  2137. p1:=cmoddivnode.create(divn,p1,p2);
  2138. _OP_NOT :
  2139. p1:=cnotnode.create(p1);
  2140. _OP_MOD :
  2141. p1:=cmoddivnode.create(modn,p1,p2);
  2142. _OP_SHL :
  2143. p1:=cshlshrnode.create(shln,p1,p2);
  2144. _OP_SHR :
  2145. p1:=cshlshrnode.create(shrn,p1,p2);
  2146. _OP_XOR :
  2147. p1:=caddnode.create(xorn,p1,p2);
  2148. _ASSIGNMENT :
  2149. p1:=cassignmentnode.create(p1,p2);
  2150. _CARET :
  2151. p1:=caddnode.create(caretn,p1,p2);
  2152. _UNEQUAL :
  2153. p1:=caddnode.create(unequaln,p1,p2);
  2154. end;
  2155. p1.set_tree_filepos(filepos);
  2156. end
  2157. else
  2158. break;
  2159. until false;
  2160. sub_expr:=p1;
  2161. end;
  2162. function comp_expr(accept_equal : boolean):tnode;
  2163. var
  2164. oldafterassignment : boolean;
  2165. p1 : tnode;
  2166. begin
  2167. oldafterassignment:=afterassignment;
  2168. afterassignment:=true;
  2169. p1:=sub_expr(opcompare,accept_equal);
  2170. afterassignment:=oldafterassignment;
  2171. comp_expr:=p1;
  2172. end;
  2173. function expr : tnode;
  2174. var
  2175. p1,p2 : tnode;
  2176. oldafterassignment : boolean;
  2177. oldp1 : tnode;
  2178. filepos : tfileposinfo;
  2179. begin
  2180. oldafterassignment:=afterassignment;
  2181. p1:=sub_expr(opcompare,true);
  2182. filepos:=akttokenpos;
  2183. if (m_tp_procvar in aktmodeswitches) and
  2184. (token<>_ASSIGNMENT) then
  2185. check_tp_procvar(p1);
  2186. if token in [_ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  2187. afterassignment:=true;
  2188. oldp1:=p1;
  2189. case token of
  2190. _POINTPOINT :
  2191. begin
  2192. consume(_POINTPOINT);
  2193. p2:=sub_expr(opcompare,true);
  2194. p1:=crangenode.create(p1,p2);
  2195. end;
  2196. _ASSIGNMENT :
  2197. begin
  2198. consume(_ASSIGNMENT);
  2199. if (p1.resulttype<>nil) and (p1.resulttype^.deftype=procvardef) then
  2200. begin
  2201. getprocvar:=true;
  2202. getprocvardef:=pprocvardef(p1.resulttype);
  2203. end;
  2204. p2:=sub_expr(opcompare,true);
  2205. if getprocvar then
  2206. handle_procvar(getprocvardef,p2);
  2207. getprocvar:=false;
  2208. p1:=cassignmentnode.create(p1,p2);
  2209. end;
  2210. _PLUSASN :
  2211. begin
  2212. consume(_PLUSASN);
  2213. p2:=sub_expr(opcompare,true);
  2214. p1:=cassignmentnode.create(p1,caddnode.create(addn,p1.getcopy,p2));
  2215. end;
  2216. _MINUSASN :
  2217. begin
  2218. consume(_MINUSASN);
  2219. p2:=sub_expr(opcompare,true);
  2220. p1:=cassignmentnode.create(p1,caddnode.create(subn,p1.getcopy,p2));
  2221. end;
  2222. _STARASN :
  2223. begin
  2224. consume(_STARASN );
  2225. p2:=sub_expr(opcompare,true);
  2226. p1:=cassignmentnode.create(p1,caddnode.create(muln,p1.getcopy,p2));
  2227. end;
  2228. _SLASHASN :
  2229. begin
  2230. consume(_SLASHASN );
  2231. p2:=sub_expr(opcompare,true);
  2232. p1:=cassignmentnode.create(p1,caddnode.create(slashn,p1.getcopy,p2));
  2233. end;
  2234. end;
  2235. afterassignment:=oldafterassignment;
  2236. if p1<>oldp1 then
  2237. p1.set_tree_filepos(filepos);
  2238. expr:=p1;
  2239. end;
  2240. function get_intconst:longint;
  2241. {Reads an expression, tries to evalute it and check if it is an integer
  2242. constant. Then the constant is returned.}
  2243. var
  2244. p:tnode;
  2245. begin
  2246. p:=comp_expr(true);
  2247. do_firstpass(p);
  2248. if not codegenerror then
  2249. begin
  2250. if (p.nodetype<>ordconstn) and
  2251. (p.resulttype^.deftype=orddef) and
  2252. not(Porddef(p.resulttype)^.typ in [uvoid,uchar,bool8bit,bool16bit,bool32bit]) then
  2253. Message(cg_e_illegal_expression)
  2254. else
  2255. get_intconst:=tordconstnode(p).value;
  2256. end;
  2257. p.free;
  2258. end;
  2259. function get_stringconst:string;
  2260. {Reads an expression, tries to evaluate it and checks if it is a string
  2261. constant. Then the constant is returned.}
  2262. var
  2263. p:tnode;
  2264. begin
  2265. get_stringconst:='';
  2266. p:=comp_expr(true);
  2267. do_firstpass(p);
  2268. if p.nodetype<>stringconstn then
  2269. begin
  2270. if (p.nodetype=ordconstn) and is_char(p.resulttype) then
  2271. get_stringconst:=char(tordconstnode(p).value)
  2272. else
  2273. Message(cg_e_illegal_expression);
  2274. end
  2275. else
  2276. get_stringconst:=strpas(tstringconstnode(p).value_str);
  2277. p.free;
  2278. end;
  2279. end.
  2280. {
  2281. $Log$
  2282. Revision 1.17 2000-11-09 17:46:55 florian
  2283. * System.TypeInfo fixed
  2284. + System.Finalize implemented
  2285. + some new keywords for interface support added
  2286. Revision 1.16 2000/11/06 20:30:55 peter
  2287. * more fixes to get make cycle working
  2288. Revision 1.15 2000/11/04 14:25:20 florian
  2289. + merged Attila's changes for interfaces, not tested yet
  2290. Revision 1.14 2000/10/31 22:02:49 peter
  2291. * symtable splitted, no real code changes
  2292. Revision 1.13 2000/10/26 23:40:54 peter
  2293. * fixed crash with call from type decl which is not allowed (merged)
  2294. Revision 1.12 2000/10/21 18:16:12 florian
  2295. * a lot of changes:
  2296. - basic dyn. array support
  2297. - basic C++ support
  2298. - some work for interfaces done
  2299. ....
  2300. Revision 1.11 2000/10/14 10:14:51 peter
  2301. * moehrendorf oct 2000 rewrite
  2302. Revision 1.10 2000/10/01 19:48:25 peter
  2303. * lot of compile updates for cg11
  2304. Revision 1.9 2000/09/24 21:19:50 peter
  2305. * delphi compile fixes
  2306. Revision 1.8 2000/09/24 15:06:22 peter
  2307. * use defines.inc
  2308. Revision 1.7 2000/08/27 16:11:51 peter
  2309. * moved some util functions from globals,cobjects to cutils
  2310. * splitted files into finput,fmodule
  2311. Revision 1.6 2000/08/20 15:12:49 peter
  2312. * auto derefence mode for array pointer (merged)
  2313. Revision 1.5 2000/08/16 18:33:53 peter
  2314. * splitted namedobjectitem.next into indexnext and listnext so it
  2315. can be used in both lists
  2316. * don't allow "word = word" type definitions (merged)
  2317. Revision 1.4 2000/08/16 13:06:06 florian
  2318. + support of 64 bit integer constants
  2319. Revision 1.3 2000/08/04 22:00:52 peter
  2320. * merges from fixes
  2321. Revision 1.2 2000/07/13 11:32:44 michael
  2322. + removed logs
  2323. }