pexpr.pas 93 KB

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