pexpr.pas 112 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Does parsing of expression for Free Pascal
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit pexpr;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. symtype,symdef,symbase,
  22. node,ncal,
  23. tokens,globtype,globals,constexp;
  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. procedure string_dec(var def: tdef; allowtypedef: boolean);
  31. function parse_paras(__colon,__namedpara : boolean;end_of_paras : ttoken) : tnode;
  32. { the ID token has to be consumed before calling this function }
  33. procedure do_member_read(classh:tobjectdef;getaddr : boolean;sym : tsym;var p1 : tnode;var again : boolean;callflags:tcallnodeflags);
  34. function get_intconst:TConstExprInt;
  35. function get_stringconst:string;
  36. implementation
  37. uses
  38. { common }
  39. cutils,
  40. { global }
  41. verbose,
  42. systems,widestr,
  43. { symtable }
  44. symconst,symtable,symsym,defutil,defcmp,
  45. { module }
  46. fmodule,ppu,
  47. { pass 1 }
  48. pass_1,htypechk,
  49. nmat,nadd,nmem,nset,ncnv,ninl,ncon,nld,nflw,nbas,nutils,
  50. { parser }
  51. scanner,
  52. pbase,pinline,ptype,
  53. { codegen }
  54. cgbase,procinfo,cpuinfo
  55. ;
  56. { sub_expr(opmultiply) is need to get -1 ** 4 to be
  57. read as - (1**4) and not (-1)**4 PM }
  58. type
  59. Toperator_precedence=(opcompare,opaddition,opmultiply,oppower);
  60. const
  61. highest_precedence = oppower;
  62. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):tnode;forward;
  63. const
  64. { true, if the inherited call is anonymous }
  65. anon_inherited : boolean = false;
  66. { last def found, only used by anon. inherited calls to insert proper type casts }
  67. srdef : tdef = nil;
  68. procedure string_dec(var def:tdef; allowtypedef: boolean);
  69. { reads a string type with optional length }
  70. { and returns a pointer to the string }
  71. { definition }
  72. var
  73. p : tnode;
  74. begin
  75. def:=cshortstringtype;
  76. consume(_STRING);
  77. if (token=_LECKKLAMMER) then
  78. begin
  79. if not(allowtypedef) then
  80. Message(parser_e_no_local_para_def);
  81. consume(_LECKKLAMMER);
  82. p:=comp_expr(true);
  83. if not is_constintnode(p) then
  84. begin
  85. Message(parser_e_illegal_expression);
  86. { error recovery }
  87. consume(_RECKKLAMMER);
  88. end
  89. else
  90. begin
  91. if (tordconstnode(p).value<=0) then
  92. begin
  93. Message(parser_e_invalid_string_size);
  94. tordconstnode(p).value:=255;
  95. end;
  96. consume(_RECKKLAMMER);
  97. if tordconstnode(p).value>255 then
  98. begin
  99. { longstring is currently unsupported (CEC)! }
  100. { t:=tstringdef.createlong(tordconstnode(p).value))}
  101. Message(parser_e_invalid_string_size);
  102. tordconstnode(p).value:=255;
  103. def:=tstringdef.createshort(int64(tordconstnode(p).value));
  104. end
  105. else
  106. if tordconstnode(p).value<>255 then
  107. def:=tstringdef.createshort(int64(tordconstnode(p).value));
  108. end;
  109. p.free;
  110. end
  111. else
  112. begin
  113. if cs_ansistrings in current_settings.localswitches then
  114. def:=cansistringtype
  115. else
  116. def:=cshortstringtype;
  117. end;
  118. end;
  119. function parse_paras(__colon,__namedpara : boolean;end_of_paras : ttoken) : tnode;
  120. var
  121. p1,p2,argname : tnode;
  122. prev_in_args,
  123. old_named_args_allowed,
  124. old_allow_array_constructor : boolean;
  125. begin
  126. if token=end_of_paras then
  127. begin
  128. parse_paras:=nil;
  129. exit;
  130. end;
  131. { save old values }
  132. prev_in_args:=in_args;
  133. old_allow_array_constructor:=allow_array_constructor;
  134. old_named_args_allowed:=named_args_allowed;
  135. { set para parsing values }
  136. in_args:=true;
  137. named_args_allowed:=false;
  138. allow_array_constructor:=true;
  139. p2:=nil;
  140. repeat
  141. if __namedpara then
  142. begin
  143. if token=_COMMA then
  144. begin
  145. { empty parameter }
  146. p2:=ccallparanode.create(cnothingnode.create,p2);
  147. end
  148. else
  149. begin
  150. named_args_allowed:=true;
  151. p1:=comp_expr(true);
  152. named_args_allowed:=false;
  153. if found_arg_name then
  154. begin
  155. argname:=p1;
  156. p1:=comp_expr(true);
  157. p2:=ccallparanode.create(p1,p2);
  158. tcallparanode(p2).parametername:=argname;
  159. end
  160. else
  161. p2:=ccallparanode.create(p1,p2);
  162. found_arg_name:=false;
  163. end;
  164. end
  165. else
  166. begin
  167. p1:=comp_expr(true);
  168. p2:=ccallparanode.create(p1,p2);
  169. end;
  170. { it's for the str(l:5,s); }
  171. if __colon and (token=_COLON) then
  172. begin
  173. consume(_COLON);
  174. p1:=comp_expr(true);
  175. p2:=ccallparanode.create(p1,p2);
  176. include(tcallparanode(p2).callparaflags,cpf_is_colon_para);
  177. if try_to_consume(_COLON) then
  178. begin
  179. p1:=comp_expr(true);
  180. p2:=ccallparanode.create(p1,p2);
  181. include(tcallparanode(p2).callparaflags,cpf_is_colon_para);
  182. end
  183. end;
  184. until not try_to_consume(_COMMA);
  185. allow_array_constructor:=old_allow_array_constructor;
  186. in_args:=prev_in_args;
  187. named_args_allowed:=old_named_args_allowed;
  188. parse_paras:=p2;
  189. end;
  190. function gen_c_style_operator(ntyp:tnodetype;p1,p2:tnode) : tnode;
  191. var
  192. hp : tnode;
  193. hdef : tdef;
  194. temp : ttempcreatenode;
  195. newstatement : tstatementnode;
  196. begin
  197. { Properties are not allowed, because the write can
  198. be different from the read }
  199. if (nf_isproperty in p1.flags) then
  200. begin
  201. Message(type_e_variable_id_expected);
  202. { We can continue with the loading,
  203. it'll not create errors. Only the expected
  204. result can be wrong }
  205. end;
  206. hp:=p1;
  207. while assigned(hp) and
  208. (hp.nodetype in [derefn,subscriptn,vecn,typeconvn]) do
  209. hp:=tunarynode(hp).left;
  210. if not assigned(hp) then
  211. internalerror(200410121);
  212. if (hp.nodetype=calln) then
  213. begin
  214. typecheckpass(p1);
  215. result:=internalstatements(newstatement);
  216. hdef:=tpointerdef.create(p1.resultdef);
  217. temp:=ctempcreatenode.create(hdef,sizeof(pint),tt_persistent,false);
  218. addstatement(newstatement,temp);
  219. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(temp),caddrnode.create_internal(p1)));
  220. addstatement(newstatement,cassignmentnode.create(
  221. cderefnode.create(ctemprefnode.create(temp)),
  222. caddnode.create(ntyp,
  223. cderefnode.create(ctemprefnode.create(temp)),
  224. p2)));
  225. addstatement(newstatement,ctempdeletenode.create(temp));
  226. end
  227. else
  228. result:=cassignmentnode.create(p1,caddnode.create(ntyp,p1.getcopy,p2));
  229. end;
  230. function statement_syssym(l : byte) : tnode;
  231. var
  232. p1,p2,paras : tnode;
  233. err,
  234. prev_in_args : boolean;
  235. begin
  236. prev_in_args:=in_args;
  237. case l of
  238. in_new_x :
  239. begin
  240. if afterassignment or in_args then
  241. statement_syssym:=new_function
  242. else
  243. statement_syssym:=new_dispose_statement(true);
  244. end;
  245. in_dispose_x :
  246. begin
  247. statement_syssym:=new_dispose_statement(false);
  248. end;
  249. in_ord_x :
  250. begin
  251. consume(_LKLAMMER);
  252. in_args:=true;
  253. p1:=comp_expr(true);
  254. consume(_RKLAMMER);
  255. p1:=geninlinenode(in_ord_x,false,p1);
  256. statement_syssym := p1;
  257. end;
  258. in_exit :
  259. begin
  260. if try_to_consume(_LKLAMMER) then
  261. begin
  262. if not (m_mac in current_settings.modeswitches) then
  263. begin
  264. if not(try_to_consume(_RKLAMMER)) then
  265. begin
  266. p1:=comp_expr(true);
  267. consume(_RKLAMMER);
  268. if (block_type=bt_except) then
  269. begin
  270. Message(parser_e_exit_with_argument_not__possible);
  271. { recovery }
  272. p1.free;
  273. p1:=nil;
  274. end
  275. else if (not assigned(current_procinfo) or
  276. is_void(current_procinfo.procdef.returndef)) then
  277. begin
  278. Message(parser_e_void_function);
  279. { recovery }
  280. p1.free;
  281. p1:=nil;
  282. end;
  283. end
  284. else
  285. p1:=nil;
  286. end
  287. else
  288. begin
  289. if not (current_procinfo.procdef.procsym.name = pattern) then
  290. Message(parser_e_macpas_exit_wrong_param);
  291. consume(_ID);
  292. consume(_RKLAMMER);
  293. p1:=nil;
  294. end
  295. end
  296. else
  297. p1:=nil;
  298. statement_syssym:=cexitnode.create(p1);
  299. end;
  300. in_break :
  301. begin
  302. statement_syssym:=cbreaknode.create
  303. end;
  304. in_continue :
  305. begin
  306. statement_syssym:=ccontinuenode.create
  307. end;
  308. in_leave :
  309. begin
  310. if m_mac in current_settings.modeswitches then
  311. statement_syssym:=cbreaknode.create
  312. else
  313. begin
  314. Message1(sym_e_id_not_found, orgpattern);
  315. statement_syssym:=cerrornode.create;
  316. end;
  317. end;
  318. in_cycle :
  319. begin
  320. if m_mac in current_settings.modeswitches then
  321. statement_syssym:=ccontinuenode.create
  322. else
  323. begin
  324. Message1(sym_e_id_not_found, orgpattern);
  325. statement_syssym:=cerrornode.create;
  326. end;
  327. end;
  328. in_typeof_x :
  329. begin
  330. consume(_LKLAMMER);
  331. in_args:=true;
  332. p1:=comp_expr(true);
  333. consume(_RKLAMMER);
  334. if p1.nodetype=typen then
  335. ttypenode(p1).allowed:=true;
  336. { Allow classrefdef, which is required for
  337. Typeof(self) in static class methods }
  338. if not(is_objc_class_or_protocol(p1.resultdef)) and
  339. ((p1.resultdef.typ = objectdef) or
  340. (assigned(current_procinfo) and
  341. ((po_classmethod in current_procinfo.procdef.procoptions) or
  342. (po_staticmethod in current_procinfo.procdef.procoptions)) and
  343. (p1.resultdef.typ=classrefdef))) then
  344. statement_syssym:=geninlinenode(in_typeof_x,false,p1)
  345. else
  346. begin
  347. Message(parser_e_class_id_expected);
  348. p1.destroy;
  349. statement_syssym:=cerrornode.create;
  350. end;
  351. end;
  352. in_sizeof_x,
  353. in_bitsizeof_x :
  354. begin
  355. consume(_LKLAMMER);
  356. in_args:=true;
  357. p1:=comp_expr(true);
  358. consume(_RKLAMMER);
  359. if (p1.nodetype<>typen) and
  360. (
  361. (is_object(p1.resultdef) and
  362. (oo_has_constructor in tobjectdef(p1.resultdef).objectoptions)) or
  363. is_open_array(p1.resultdef) or
  364. is_array_of_const(p1.resultdef) or
  365. is_open_string(p1.resultdef)
  366. ) then
  367. begin
  368. statement_syssym:=geninlinenode(in_sizeof_x,false,p1);
  369. { no packed bit support for these things }
  370. if (l = in_bitsizeof_x) then
  371. statement_syssym:=caddnode.create(muln,statement_syssym,cordconstnode.create(8,sinttype,true));
  372. end
  373. else
  374. begin
  375. if (l = in_sizeof_x) or
  376. (not((p1.nodetype = vecn) and
  377. is_packed_array(tvecnode(p1).left.resultdef)) and
  378. not((p1.nodetype = subscriptn) and
  379. is_packed_record_or_object(tsubscriptnode(p1).left.resultdef))) then
  380. begin
  381. statement_syssym:=cordconstnode.create(p1.resultdef.size,sinttype,true);
  382. if (l = in_bitsizeof_x) then
  383. statement_syssym:=caddnode.create(muln,statement_syssym,cordconstnode.create(8,sinttype,true));
  384. end
  385. else
  386. statement_syssym:=cordconstnode.create(p1.resultdef.packedbitsize,sinttype,true);
  387. { p1 not needed !}
  388. p1.destroy;
  389. end;
  390. end;
  391. in_typeinfo_x,
  392. in_objc_encode_x :
  393. begin
  394. if (l=in_typeinfo_x) or
  395. (m_objectivec1 in current_settings.modeswitches) then
  396. begin
  397. consume(_LKLAMMER);
  398. in_args:=true;
  399. p1:=comp_expr(true);
  400. { When reading a class type it is parsed as loadvmtaddrn,
  401. typeinfo only needs the type so we remove the loadvmtaddrn }
  402. if p1.nodetype=loadvmtaddrn then
  403. begin
  404. p2:=tloadvmtaddrnode(p1).left;
  405. tloadvmtaddrnode(p1).left:=nil;
  406. p1.free;
  407. p1:=p2;
  408. end;
  409. if p1.nodetype=typen then
  410. ttypenode(p1).allowed:=true;
  411. { else
  412. begin
  413. p1.destroy;
  414. p1:=cerrornode.create;
  415. Message(parser_e_illegal_parameter_list);
  416. end;}
  417. consume(_RKLAMMER);
  418. p2:=geninlinenode(l,false,p1);
  419. statement_syssym:=p2;
  420. end
  421. else
  422. begin
  423. Message1(sym_e_id_not_found, orgpattern);
  424. statement_syssym:=cerrornode.create;
  425. end;
  426. end;
  427. in_unaligned_x :
  428. begin
  429. err:=false;
  430. consume(_LKLAMMER);
  431. in_args:=true;
  432. p1:=comp_expr(true);
  433. p2:=ccallparanode.create(p1,nil);
  434. p2:=geninlinenode(in_unaligned_x,false,p2);
  435. consume(_RKLAMMER);
  436. statement_syssym:=p2;
  437. end;
  438. in_assigned_x :
  439. begin
  440. err:=false;
  441. consume(_LKLAMMER);
  442. in_args:=true;
  443. p1:=comp_expr(true);
  444. { When reading a class type it is parsed as loadvmtaddrn,
  445. typeinfo only needs the type so we remove the loadvmtaddrn }
  446. if p1.nodetype=loadvmtaddrn then
  447. begin
  448. p2:=tloadvmtaddrnode(p1).left;
  449. tloadvmtaddrnode(p1).left:=nil;
  450. p1.free;
  451. p1:=p2;
  452. end;
  453. if not codegenerror then
  454. begin
  455. case p1.resultdef.typ of
  456. procdef, { procvar }
  457. pointerdef,
  458. procvardef,
  459. classrefdef : ;
  460. objectdef :
  461. if not is_class_or_interface_or_objc(p1.resultdef) then
  462. begin
  463. Message(parser_e_illegal_parameter_list);
  464. err:=true;
  465. end;
  466. arraydef :
  467. if not is_dynamic_array(p1.resultdef) then
  468. begin
  469. Message(parser_e_illegal_parameter_list);
  470. err:=true;
  471. end;
  472. else
  473. begin
  474. Message(parser_e_illegal_parameter_list);
  475. err:=true;
  476. end;
  477. end;
  478. end
  479. else
  480. err:=true;
  481. if not err then
  482. begin
  483. p2:=ccallparanode.create(p1,nil);
  484. p2:=geninlinenode(in_assigned_x,false,p2);
  485. end
  486. else
  487. begin
  488. p1.free;
  489. p2:=cerrornode.create;
  490. end;
  491. consume(_RKLAMMER);
  492. statement_syssym:=p2;
  493. end;
  494. in_addr_x :
  495. begin
  496. consume(_LKLAMMER);
  497. in_args:=true;
  498. p1:=comp_expr(true);
  499. p1:=caddrnode.create(p1);
  500. if cs_typed_addresses in current_settings.localswitches then
  501. include(p1.flags,nf_typedaddr);
  502. consume(_RKLAMMER);
  503. statement_syssym:=p1;
  504. end;
  505. in_ofs_x :
  506. begin
  507. consume(_LKLAMMER);
  508. in_args:=true;
  509. p1:=comp_expr(true);
  510. p1:=caddrnode.create(p1);
  511. do_typecheckpass(p1);
  512. { Ofs() returns a cardinal/qword, not a pointer }
  513. p1.resultdef:=uinttype;
  514. consume(_RKLAMMER);
  515. statement_syssym:=p1;
  516. end;
  517. in_seg_x :
  518. begin
  519. consume(_LKLAMMER);
  520. in_args:=true;
  521. p1:=comp_expr(true);
  522. p1:=geninlinenode(in_seg_x,false,p1);
  523. consume(_RKLAMMER);
  524. statement_syssym:=p1;
  525. end;
  526. in_high_x,
  527. in_low_x :
  528. begin
  529. consume(_LKLAMMER);
  530. in_args:=true;
  531. p1:=comp_expr(true);
  532. p2:=geninlinenode(l,false,p1);
  533. consume(_RKLAMMER);
  534. statement_syssym:=p2;
  535. end;
  536. in_succ_x,
  537. in_pred_x :
  538. begin
  539. consume(_LKLAMMER);
  540. in_args:=true;
  541. p1:=comp_expr(true);
  542. p2:=geninlinenode(l,false,p1);
  543. consume(_RKLAMMER);
  544. statement_syssym:=p2;
  545. end;
  546. in_inc_x,
  547. in_dec_x :
  548. begin
  549. consume(_LKLAMMER);
  550. in_args:=true;
  551. p1:=comp_expr(true);
  552. if try_to_consume(_COMMA) then
  553. p2:=ccallparanode.create(comp_expr(true),nil)
  554. else
  555. p2:=nil;
  556. p2:=ccallparanode.create(p1,p2);
  557. statement_syssym:=geninlinenode(l,false,p2);
  558. consume(_RKLAMMER);
  559. end;
  560. in_slice_x:
  561. begin
  562. if not(in_args) then
  563. begin
  564. message(parser_e_illegal_slice);
  565. consume(_LKLAMMER);
  566. in_args:=true;
  567. comp_expr(true).free;
  568. if try_to_consume(_COMMA) then
  569. comp_expr(true).free;
  570. statement_syssym:=cerrornode.create;
  571. consume(_RKLAMMER);
  572. end
  573. else
  574. begin
  575. consume(_LKLAMMER);
  576. in_args:=true;
  577. p1:=comp_expr(true);
  578. Consume(_COMMA);
  579. if not(codegenerror) then
  580. p2:=ccallparanode.create(comp_expr(true),nil)
  581. else
  582. p2:=cerrornode.create;
  583. p2:=ccallparanode.create(p1,p2);
  584. statement_syssym:=geninlinenode(l,false,p2);
  585. consume(_RKLAMMER);
  586. end;
  587. end;
  588. in_initialize_x:
  589. begin
  590. statement_syssym:=inline_initialize;
  591. end;
  592. in_finalize_x:
  593. begin
  594. statement_syssym:=inline_finalize;
  595. end;
  596. in_copy_x:
  597. begin
  598. statement_syssym:=inline_copy;
  599. end;
  600. in_concat_x :
  601. begin
  602. consume(_LKLAMMER);
  603. in_args:=true;
  604. { Translate to x:=x+y[+z]. The addnode will do the
  605. type checking }
  606. p2:=nil;
  607. repeat
  608. p1:=comp_expr(true);
  609. if p2<>nil then
  610. p2:=caddnode.create(addn,p2,p1)
  611. else
  612. begin
  613. { Force string type if it isn't yet }
  614. if not(
  615. (p1.resultdef.typ=stringdef) or
  616. is_chararray(p1.resultdef) or
  617. is_char(p1.resultdef)
  618. ) then
  619. inserttypeconv(p1,cshortstringtype);
  620. p2:=p1;
  621. end;
  622. until not try_to_consume(_COMMA);
  623. consume(_RKLAMMER);
  624. statement_syssym:=p2;
  625. end;
  626. in_read_x,
  627. in_readln_x,
  628. in_readstr_x:
  629. begin
  630. if try_to_consume(_LKLAMMER) then
  631. begin
  632. paras:=parse_paras(false,false,_RKLAMMER);
  633. consume(_RKLAMMER);
  634. end
  635. else
  636. paras:=nil;
  637. p1:=geninlinenode(l,false,paras);
  638. statement_syssym := p1;
  639. end;
  640. in_setlength_x:
  641. begin
  642. statement_syssym := inline_setlength;
  643. end;
  644. in_objc_selector_x:
  645. begin
  646. if (m_objectivec1 in current_settings.modeswitches) then
  647. begin
  648. consume(_LKLAMMER);
  649. in_args:=true;
  650. { don't turn procsyms into calls (getaddr = true) }
  651. p1:=factor(true);
  652. p2:=geninlinenode(l,false,p1);
  653. consume(_RKLAMMER);
  654. statement_syssym:=p2;
  655. end
  656. else
  657. begin
  658. Message1(sym_e_id_not_found, orgpattern);
  659. statement_syssym:=cerrornode.create;
  660. end;
  661. end;
  662. in_length_x:
  663. begin
  664. consume(_LKLAMMER);
  665. in_args:=true;
  666. p1:=comp_expr(true);
  667. p2:=geninlinenode(l,false,p1);
  668. consume(_RKLAMMER);
  669. statement_syssym:=p2;
  670. end;
  671. in_write_x,
  672. in_writeln_x,
  673. in_writestr_x :
  674. begin
  675. if try_to_consume(_LKLAMMER) then
  676. begin
  677. paras:=parse_paras(true,false,_RKLAMMER);
  678. consume(_RKLAMMER);
  679. end
  680. else
  681. paras:=nil;
  682. p1 := geninlinenode(l,false,paras);
  683. statement_syssym := p1;
  684. end;
  685. in_str_x_string :
  686. begin
  687. consume(_LKLAMMER);
  688. paras:=parse_paras(true,false,_RKLAMMER);
  689. consume(_RKLAMMER);
  690. p1 := geninlinenode(l,false,paras);
  691. statement_syssym := p1;
  692. end;
  693. in_val_x:
  694. Begin
  695. consume(_LKLAMMER);
  696. in_args := true;
  697. p1:= ccallparanode.create(comp_expr(true), nil);
  698. consume(_COMMA);
  699. p2 := ccallparanode.create(comp_expr(true),p1);
  700. if try_to_consume(_COMMA) then
  701. p2 := ccallparanode.create(comp_expr(true),p2);
  702. consume(_RKLAMMER);
  703. p2 := geninlinenode(l,false,p2);
  704. statement_syssym := p2;
  705. End;
  706. in_include_x_y,
  707. in_exclude_x_y :
  708. begin
  709. consume(_LKLAMMER);
  710. in_args:=true;
  711. p1:=comp_expr(true);
  712. consume(_COMMA);
  713. p2:=comp_expr(true);
  714. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,nil)));
  715. consume(_RKLAMMER);
  716. end;
  717. in_pack_x_y_z,
  718. in_unpack_x_y_z :
  719. begin
  720. consume(_LKLAMMER);
  721. in_args:=true;
  722. p1:=comp_expr(true);
  723. consume(_COMMA);
  724. p2:=comp_expr(true);
  725. consume(_COMMA);
  726. paras:=comp_expr(true);
  727. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,ccallparanode.create(paras,nil))));
  728. consume(_RKLAMMER);
  729. end;
  730. in_assert_x_y :
  731. begin
  732. consume(_LKLAMMER);
  733. in_args:=true;
  734. p1:=comp_expr(true);
  735. if try_to_consume(_COMMA) then
  736. p2:=comp_expr(true)
  737. else
  738. begin
  739. { then insert an empty string }
  740. p2:=cstringconstnode.createstr('');
  741. end;
  742. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,nil)));
  743. consume(_RKLAMMER);
  744. end;
  745. in_get_frame:
  746. begin
  747. statement_syssym:=geninlinenode(l,false,nil);
  748. end;
  749. (*
  750. in_get_caller_frame:
  751. begin
  752. if try_to_consume(_LKLAMMER) then
  753. begin
  754. {You used to call get_caller_frame as get_caller_frame(get_frame),
  755. however, as a stack frame may not exist, it does more harm than
  756. good, so ignore it.}
  757. in_args:=true;
  758. p1:=comp_expr(true);
  759. p1.destroy;
  760. consume(_RKLAMMER);
  761. end;
  762. statement_syssym:=geninlinenode(l,false,nil);
  763. end;
  764. *)
  765. else
  766. internalerror(15);
  767. end;
  768. in_args:=prev_in_args;
  769. end;
  770. function maybe_load_methodpointer(st:TSymtable;var p1:tnode):boolean;
  771. begin
  772. maybe_load_methodpointer:=false;
  773. if not assigned(p1) then
  774. begin
  775. case st.symtabletype of
  776. withsymtable :
  777. begin
  778. if (st.defowner.typ=objectdef) then
  779. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  780. end;
  781. ObjectSymtable :
  782. begin
  783. if (assigned(current_procinfo) and ([po_staticmethod,po_classmethod] <= current_procinfo.procdef.procoptions)) then
  784. { We are calling from the static class method which has no self node }
  785. p1:=cloadvmtaddrnode.create(ctypenode.create(current_procinfo.procdef._class))
  786. else
  787. p1:=load_self_node;
  788. { We are calling a member }
  789. maybe_load_methodpointer:=true;
  790. end;
  791. end;
  792. end;
  793. end;
  794. { reads the parameter for a subroutine call }
  795. procedure do_proc_call(sym:tsym;st:TSymtable;obj:tobjectdef;getaddr:boolean;var again : boolean;var p1:tnode;callflags:tcallnodeflags);
  796. var
  797. membercall,
  798. prevafterassn : boolean;
  799. i : integer;
  800. para,p2 : tnode;
  801. currpara : tparavarsym;
  802. aprocdef : tprocdef;
  803. begin
  804. prevafterassn:=afterassignment;
  805. afterassignment:=false;
  806. membercall:=false;
  807. aprocdef:=nil;
  808. { when it is a call to a member we need to load the
  809. methodpointer first
  810. }
  811. membercall:=maybe_load_methodpointer(st,p1);
  812. { When we are expecting a procvar we also need
  813. to get the address in some cases }
  814. if assigned(getprocvardef) then
  815. begin
  816. if (block_type=bt_const) or
  817. getaddr then
  818. begin
  819. aprocdef:=Tprocsym(sym).Find_procdef_byprocvardef(getprocvardef);
  820. getaddr:=true;
  821. end
  822. else
  823. if (m_tp_procvar in current_settings.modeswitches) or
  824. (m_mac_procvar in current_settings.modeswitches) then
  825. begin
  826. aprocdef:=Tprocsym(sym).Find_procdef_byprocvardef(getprocvardef);
  827. if assigned(aprocdef) then
  828. getaddr:=true;
  829. end;
  830. end;
  831. { only need to get the address of the procedure? }
  832. if getaddr then
  833. begin
  834. { Retrieve info which procvar to call. For tp_procvar the
  835. aprocdef is already loaded above so we can reuse it }
  836. if not assigned(aprocdef) and
  837. assigned(getprocvardef) then
  838. aprocdef:=Tprocsym(sym).Find_procdef_byprocvardef(getprocvardef);
  839. { generate a methodcallnode or proccallnode }
  840. { we shouldn't convert things like @tcollection.load }
  841. p2:=cloadnode.create_procvar(sym,aprocdef,st);
  842. if assigned(p1) then
  843. begin
  844. { for loading methodpointer of an inherited function
  845. we use self as instance and load the address of
  846. the function directly and not through the vmt (PFV) }
  847. if (cnf_inherited in callflags) then
  848. begin
  849. include(p2.flags,nf_inherited);
  850. p1.free;
  851. p1:=load_self_node;
  852. end;
  853. if (p1.nodetype<>typen) then
  854. tloadnode(p2).set_mp(p1)
  855. else
  856. p1.free;
  857. end;
  858. p1:=p2;
  859. { no postfix operators }
  860. again:=false;
  861. end
  862. else
  863. begin
  864. para:=nil;
  865. if anon_inherited then
  866. begin
  867. if not assigned(current_procinfo) then
  868. internalerror(200305054);
  869. for i:=0 to current_procinfo.procdef.paras.count-1 do
  870. begin
  871. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  872. if not(vo_is_hidden_para in currpara.varoptions) then
  873. begin
  874. { inheritance by msgint? }
  875. if assigned(srdef) then
  876. { anonymous inherited via msgid calls only require a var parameter for
  877. both methods, so we need some type casting here }
  878. para:=ccallparanode.create(ctypeconvnode.create_internal(ctypeconvnode.create_internal(
  879. cloadnode.create(currpara,currpara.owner),cformaltype),tparavarsym(tprocdef(srdef).paras[i]).vardef),
  880. para)
  881. else
  882. para:=ccallparanode.create(cloadnode.create(currpara,currpara.owner),para);
  883. end;
  884. end;
  885. end
  886. else
  887. begin
  888. if try_to_consume(_LKLAMMER) then
  889. begin
  890. para:=parse_paras(false,false,_RKLAMMER);
  891. consume(_RKLAMMER);
  892. end;
  893. end;
  894. { indicate if this call was generated by a member and
  895. no explicit self is used, this is needed to determine
  896. how to handle a destructor call (PFV) }
  897. if membercall then
  898. include(callflags,cnf_member_call);
  899. if assigned(obj) then
  900. begin
  901. if (st.symtabletype<>ObjectSymtable) then
  902. internalerror(200310031);
  903. p1:=ccallnode.create(para,tprocsym(sym),obj.symtable,p1,callflags);
  904. end
  905. else
  906. p1:=ccallnode.create(para,tprocsym(sym),st,p1,callflags);
  907. end;
  908. afterassignment:=prevafterassn;
  909. end;
  910. procedure handle_procvar(pv : tprocvardef;var p2 : tnode);
  911. var
  912. hp,hp2 : tnode;
  913. hpp : ^tnode;
  914. currprocdef : tprocdef;
  915. begin
  916. if not assigned(pv) then
  917. internalerror(200301121);
  918. if (m_tp_procvar in current_settings.modeswitches) or
  919. (m_mac_procvar in current_settings.modeswitches) then
  920. begin
  921. hp:=p2;
  922. hpp:=@p2;
  923. while assigned(hp) and
  924. (hp.nodetype=typeconvn) do
  925. begin
  926. hp:=ttypeconvnode(hp).left;
  927. { save orignal address of the old tree so we can replace the node }
  928. hpp:=@hp;
  929. end;
  930. if (hp.nodetype=calln) and
  931. { a procvar can't have parameters! }
  932. not assigned(tcallnode(hp).left) then
  933. begin
  934. currprocdef:=tcallnode(hp).symtableprocentry.Find_procdef_byprocvardef(pv);
  935. if assigned(currprocdef) then
  936. begin
  937. hp2:=cloadnode.create_procvar(tprocsym(tcallnode(hp).symtableprocentry),currprocdef,tcallnode(hp).symtableproc);
  938. if (po_methodpointer in pv.procoptions) then
  939. tloadnode(hp2).set_mp(tcallnode(hp).methodpointer.getcopy);
  940. hp.destroy;
  941. { replace the old callnode with the new loadnode }
  942. hpp^:=hp2;
  943. end;
  944. end;
  945. end;
  946. end;
  947. { the following procedure handles the access to a property symbol }
  948. procedure handle_propertysym(propsym : tpropertysym;st : TSymtable;var p1 : tnode);
  949. var
  950. paras : tnode;
  951. p2 : tnode;
  952. membercall : boolean;
  953. callflags : tcallnodeflags;
  954. propaccesslist : tpropaccesslist;
  955. static_name : shortstring;
  956. sym: tsym;
  957. srsymtable : tsymtable;
  958. begin
  959. { property parameters? read them only if the property really }
  960. { has parameters }
  961. paras:=nil;
  962. if (ppo_hasparameters in propsym.propoptions) then
  963. begin
  964. if try_to_consume(_LECKKLAMMER) then
  965. begin
  966. paras:=parse_paras(false,false,_RECKKLAMMER);
  967. consume(_RECKKLAMMER);
  968. end;
  969. end;
  970. { indexed property }
  971. if (ppo_indexed in propsym.propoptions) then
  972. begin
  973. p2:=cordconstnode.create(propsym.index,propsym.indexdef,true);
  974. paras:=ccallparanode.create(p2,paras);
  975. end;
  976. { we need only a write property if a := follows }
  977. { if not(afterassignment) and not(in_args) then }
  978. if token=_ASSIGNMENT then
  979. begin
  980. if getpropaccesslist(propsym,palt_write,propaccesslist) then
  981. begin
  982. sym:=propaccesslist.firstsym^.sym;
  983. case sym.typ of
  984. procsym :
  985. begin
  986. callflags:=[];
  987. { generate the method call }
  988. membercall:=maybe_load_methodpointer(st,p1);
  989. if membercall then
  990. include(callflags,cnf_member_call);
  991. p1:=ccallnode.create(paras,tprocsym(sym),st,p1,callflags);
  992. addsymref(sym);
  993. paras:=nil;
  994. consume(_ASSIGNMENT);
  995. { read the expression }
  996. if propsym.propdef.typ=procvardef then
  997. getprocvardef:=tprocvardef(propsym.propdef);
  998. p2:=comp_expr(true);
  999. if assigned(getprocvardef) then
  1000. handle_procvar(getprocvardef,p2);
  1001. tcallnode(p1).left:=ccallparanode.create(p2,tcallnode(p1).left);
  1002. { mark as property, both the tcallnode and the real call block }
  1003. include(p1.flags,nf_isproperty);
  1004. getprocvardef:=nil;
  1005. end;
  1006. fieldvarsym :
  1007. begin
  1008. { generate access code }
  1009. if (sp_static in sym.symoptions) then
  1010. begin
  1011. static_name:=lower(sym.owner.name^)+'_'+sym.name;
  1012. searchsym_in_class(tobjectdef(sym.owner.defowner),tobjectdef(sym.owner.defowner),static_name,sym,srsymtable);
  1013. if assigned(sym) then
  1014. check_hints(sym,sym.symoptions,sym.deprecatedmsg);
  1015. p1.free;
  1016. p1:=nil;
  1017. { static syms are always stored as absolutevarsym to handle scope and storage properly }
  1018. propaccesslist_to_node(p1,nil,tabsolutevarsym(sym).ref);
  1019. end
  1020. else
  1021. propaccesslist_to_node(p1,st,propaccesslist);
  1022. include(p1.flags,nf_isproperty);
  1023. consume(_ASSIGNMENT);
  1024. { read the expression }
  1025. p2:=comp_expr(true);
  1026. p1:=cassignmentnode.create(p1,p2);
  1027. end
  1028. else
  1029. begin
  1030. p1:=cerrornode.create;
  1031. Message(parser_e_no_procedure_to_access_property);
  1032. end;
  1033. end;
  1034. end
  1035. else
  1036. begin
  1037. p1:=cerrornode.create;
  1038. Message(parser_e_no_procedure_to_access_property);
  1039. end;
  1040. end
  1041. else
  1042. begin
  1043. if getpropaccesslist(propsym,palt_read,propaccesslist) then
  1044. begin
  1045. sym := propaccesslist.firstsym^.sym;
  1046. case sym.typ of
  1047. fieldvarsym :
  1048. begin
  1049. { generate access code }
  1050. if (sp_static in sym.symoptions) then
  1051. begin
  1052. static_name:=lower(sym.owner.name^)+'_'+sym.name;
  1053. searchsym_in_class(tobjectdef(sym.owner.defowner),tobjectdef(sym.owner.defowner),static_name,sym,srsymtable);
  1054. if assigned(sym) then
  1055. check_hints(sym,sym.symoptions,sym.deprecatedmsg);
  1056. p1.free;
  1057. p1:=nil;
  1058. { static syms are always stored as absolutevarsym to handle scope and storage properly }
  1059. propaccesslist_to_node(p1,nil,tabsolutevarsym(sym).ref);
  1060. end
  1061. else
  1062. propaccesslist_to_node(p1,st,propaccesslist);
  1063. include(p1.flags,nf_isproperty);
  1064. end;
  1065. procsym :
  1066. begin
  1067. callflags:=[];
  1068. { generate the method call }
  1069. membercall:=maybe_load_methodpointer(st,p1);
  1070. if membercall then
  1071. include(callflags,cnf_member_call);
  1072. p1:=ccallnode.create(paras,tprocsym(sym),st,p1,callflags);
  1073. paras:=nil;
  1074. include(p1.flags,nf_isproperty);
  1075. end
  1076. else
  1077. begin
  1078. p1:=cerrornode.create;
  1079. Message(type_e_mismatch);
  1080. end;
  1081. end;
  1082. end
  1083. else
  1084. begin
  1085. { error, no function to read property }
  1086. p1:=cerrornode.create;
  1087. Message(parser_e_no_procedure_to_access_property);
  1088. end;
  1089. end;
  1090. { release paras if not used }
  1091. if assigned(paras) then
  1092. paras.free;
  1093. end;
  1094. { the ID token has to be consumed before calling this function }
  1095. procedure do_member_read(classh:tobjectdef;getaddr : boolean;sym : tsym;var p1 : tnode;var again : boolean;callflags:tcallnodeflags);
  1096. var
  1097. static_name : string;
  1098. isclassref : boolean;
  1099. srsymtable : TSymtable;
  1100. begin
  1101. if sym=nil then
  1102. begin
  1103. { pattern is still valid unless
  1104. there is another ID just after the ID of sym }
  1105. Message1(sym_e_id_no_member,orgpattern);
  1106. p1.free;
  1107. p1:=cerrornode.create;
  1108. { try to clean up }
  1109. again:=false;
  1110. end
  1111. else
  1112. begin
  1113. if assigned(p1) then
  1114. begin
  1115. if not assigned(p1.resultdef) then
  1116. do_typecheckpass(p1);
  1117. isclassref:=(p1.resultdef.typ=classrefdef);
  1118. end
  1119. else
  1120. isclassref:=false;
  1121. { we assume, that only procsyms and varsyms are in an object }
  1122. { symbol table, for classes, properties are allowed }
  1123. case sym.typ of
  1124. procsym:
  1125. begin
  1126. do_proc_call(sym,sym.owner,classh,
  1127. (getaddr and not(token in [_CARET,_POINT])),
  1128. again,p1,callflags);
  1129. { we need to know which procedure is called }
  1130. do_typecheckpass(p1);
  1131. { calling using classref? }
  1132. if isclassref and
  1133. (p1.nodetype=calln) and
  1134. assigned(tcallnode(p1).procdefinition) and
  1135. not(po_classmethod in tcallnode(p1).procdefinition.procoptions) and
  1136. not(tcallnode(p1).procdefinition.proctypeoption=potype_constructor) then
  1137. Message(parser_e_only_class_members_via_class_ref);
  1138. end;
  1139. fieldvarsym:
  1140. begin
  1141. if (sp_static in sym.symoptions) then
  1142. begin
  1143. static_name:=lower(sym.owner.name^)+'_'+sym.name;
  1144. searchsym_in_class(tobjectdef(sym.owner.defowner),tobjectdef(sym.owner.defowner),static_name,sym,srsymtable);
  1145. if assigned(sym) then
  1146. check_hints(sym,sym.symoptions,sym.deprecatedmsg);
  1147. p1.free;
  1148. p1:=nil;
  1149. { static syms are always stored as absolutevarsym to handle scope and storage properly }
  1150. propaccesslist_to_node(p1,nil,tabsolutevarsym(sym).ref);
  1151. end
  1152. else
  1153. begin
  1154. if isclassref then
  1155. if assigned(p1) and
  1156. (
  1157. is_self_node(p1) or
  1158. (assigned(current_procinfo) and ([po_staticmethod,po_classmethod] <= current_procinfo.procdef.procoptions) and
  1159. (current_procinfo.procdef._class = classh))) then
  1160. Message(parser_e_only_class_members)
  1161. else
  1162. Message(parser_e_only_class_members_via_class_ref);
  1163. p1:=csubscriptnode.create(sym,p1);
  1164. end;
  1165. end;
  1166. propertysym:
  1167. begin
  1168. if isclassref and not (sp_static in sym.symoptions) then
  1169. Message(parser_e_only_class_members_via_class_ref);
  1170. handle_propertysym(tpropertysym(sym),sym.owner,p1);
  1171. end;
  1172. typesym:
  1173. begin
  1174. p1:=ctypenode.create(ttypesym(sym).typedef);
  1175. end;
  1176. constsym:
  1177. begin
  1178. p1:=genconstsymtree(tconstsym(sym));
  1179. end
  1180. else
  1181. internalerror(16);
  1182. end;
  1183. end;
  1184. end;
  1185. {****************************************************************************
  1186. Factor
  1187. ****************************************************************************}
  1188. function is_member_read(sym: tsym; st: tsymtable; var p1: tnode;
  1189. out memberparentdef: tdef): boolean;
  1190. var
  1191. hdef : tdef;
  1192. begin
  1193. result:=true;
  1194. memberparentdef:=nil;
  1195. case st.symtabletype of
  1196. ObjectSymtable:
  1197. begin
  1198. memberparentdef:=tdef(st.defowner);
  1199. exit;
  1200. end;
  1201. WithSymtable:
  1202. begin
  1203. if assigned(p1) then
  1204. internalerror(2007012002);
  1205. hdef:=tnode(twithsymtable(st).withrefnode).resultdef;
  1206. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  1207. if not(hdef.typ in [objectdef,classrefdef]) then
  1208. exit;
  1209. if (hdef.typ=classrefdef) then
  1210. hdef:=tclassrefdef(hdef).pointeddef;
  1211. memberparentdef:=hdef;
  1212. end;
  1213. else
  1214. result:=false;
  1215. end;
  1216. end;
  1217. {$maxfpuregisters 0}
  1218. function factor(getaddr : boolean) : tnode;
  1219. {---------------------------------------------
  1220. Factor_read_id
  1221. ---------------------------------------------}
  1222. procedure factor_read_id(out p1:tnode;var again:boolean);
  1223. var
  1224. pc : pchar;
  1225. srsym : tsym;
  1226. unit_found : boolean;
  1227. srsymtable : TSymtable;
  1228. hdef : tdef;
  1229. orgstoredpattern,
  1230. storedpattern : string;
  1231. len : longint;
  1232. t : ttoken;
  1233. begin
  1234. { allow post fix operators }
  1235. again:=true;
  1236. { first check for identifier }
  1237. if token<>_ID then
  1238. begin
  1239. srsym:=generrorsym;
  1240. srsymtable:=nil;
  1241. consume(_ID);
  1242. end
  1243. else
  1244. begin
  1245. searchsym(pattern,srsym,srsymtable);
  1246. { handle unit specification like System.Writeln }
  1247. unit_found:=try_consume_unitsym(srsym,srsymtable,t);
  1248. storedpattern:=pattern;
  1249. orgstoredpattern:=orgpattern;
  1250. consume(t);
  1251. { named parameter support }
  1252. found_arg_name:=false;
  1253. if not(unit_found) and
  1254. named_args_allowed and
  1255. (token=_ASSIGNMENT) then
  1256. begin
  1257. found_arg_name:=true;
  1258. p1:=cstringconstnode.createstr(storedpattern);
  1259. consume(_ASSIGNMENT);
  1260. exit;
  1261. end;
  1262. { if nothing found give error and return errorsym }
  1263. if assigned(srsym) then
  1264. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg)
  1265. else
  1266. begin
  1267. identifier_not_found(orgstoredpattern);
  1268. srsym:=generrorsym;
  1269. srsymtable:=nil;
  1270. end;
  1271. end;
  1272. { Access to funcret or need to call the function? }
  1273. if (srsym.typ in [absolutevarsym,localvarsym,paravarsym]) and
  1274. (vo_is_funcret in tabstractvarsym(srsym).varoptions) and
  1275. { result(x) is not allowed }
  1276. not(vo_is_result in tabstractvarsym(srsym).varoptions) and
  1277. (
  1278. (token=_LKLAMMER) or
  1279. (
  1280. (
  1281. (m_tp7 in current_settings.modeswitches) or
  1282. (m_delphi in current_settings.modeswitches)
  1283. ) and
  1284. (afterassignment or in_args)
  1285. )
  1286. ) then
  1287. begin
  1288. hdef:=tdef(srsym.owner.defowner);
  1289. if assigned(hdef) and
  1290. (hdef.typ=procdef) then
  1291. srsym:=tprocdef(hdef).procsym
  1292. else
  1293. begin
  1294. Message(parser_e_illegal_expression);
  1295. srsym:=generrorsym;
  1296. end;
  1297. srsymtable:=srsym.owner;
  1298. end;
  1299. begin
  1300. case srsym.typ of
  1301. absolutevarsym :
  1302. begin
  1303. if (tabsolutevarsym(srsym).abstyp=tovar) then
  1304. begin
  1305. p1:=nil;
  1306. propaccesslist_to_node(p1,nil,tabsolutevarsym(srsym).ref);
  1307. p1:=ctypeconvnode.create(p1,tabsolutevarsym(srsym).vardef);
  1308. include(p1.flags,nf_absolute);
  1309. end
  1310. else
  1311. p1:=cloadnode.create(srsym,srsymtable);
  1312. end;
  1313. staticvarsym,
  1314. localvarsym,
  1315. paravarsym,
  1316. fieldvarsym :
  1317. begin
  1318. { check if we are reading a field of an object/class/ }
  1319. { record. is_member_read() will deal with withsymtables }
  1320. { if needed. }
  1321. p1:=nil;
  1322. if is_member_read(srsym,srsymtable,p1,hdef) then
  1323. begin
  1324. { if the field was originally found in an }
  1325. { objectsymtable, it means it's part of self
  1326. if only method from which it was called is
  1327. not class static }
  1328. if (srsymtable.symtabletype=ObjectSymtable) then
  1329. if (assigned(current_procinfo) and ([po_staticmethod,po_classmethod] <= current_procinfo.procdef.procoptions)) then
  1330. p1:=cloadvmtaddrnode.create(ctypenode.create(current_procinfo.procdef._class))
  1331. else
  1332. p1:=load_self_node;
  1333. { now, if the field itself is part of an objectsymtab }
  1334. { (it can be even if it was found in a withsymtable, }
  1335. { e.g., "with classinstance do field := 5"), then }
  1336. { let do_member_read handle it }
  1337. if (srsym.owner.symtabletype=ObjectSymtable) then
  1338. do_member_read(tobjectdef(hdef),getaddr,srsym,p1,again,[])
  1339. else
  1340. { otherwise it's a regular record subscript }
  1341. p1:=csubscriptnode.create(srsym,p1);
  1342. end
  1343. else
  1344. { regular non-field load }
  1345. p1:=cloadnode.create(srsym,srsymtable);
  1346. end;
  1347. syssym :
  1348. begin
  1349. p1:=statement_syssym(tsyssym(srsym).number);
  1350. end;
  1351. typesym :
  1352. begin
  1353. hdef:=ttypesym(srsym).typedef;
  1354. if not assigned(hdef) then
  1355. begin
  1356. again:=false;
  1357. end
  1358. else
  1359. begin
  1360. { We need to know if this unit uses Variants }
  1361. if (hdef=cvarianttype) and
  1362. not(cs_compilesystem in current_settings.moduleswitches) then
  1363. current_module.flags:=current_module.flags or uf_uses_variants;
  1364. if try_to_consume(_LKLAMMER) then
  1365. begin
  1366. p1:=comp_expr(true);
  1367. consume(_RKLAMMER);
  1368. p1:=ctypeconvnode.create_explicit(p1,hdef);
  1369. end
  1370. else { not LKLAMMER }
  1371. if (token=_POINT) and
  1372. is_object(hdef) then
  1373. begin
  1374. consume(_POINT);
  1375. if assigned(current_objectdef) and
  1376. not(getaddr) then
  1377. begin
  1378. if current_objectdef.is_related(tobjectdef(hdef)) then
  1379. begin
  1380. p1:=ctypenode.create(hdef);
  1381. { search also in inherited methods }
  1382. searchsym_in_class(tobjectdef(hdef),current_objectdef,pattern,srsym,srsymtable);
  1383. if assigned(srsym) then
  1384. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1385. consume(_ID);
  1386. do_member_read(tobjectdef(hdef),false,srsym,p1,again,[]);
  1387. end
  1388. else
  1389. begin
  1390. Message(parser_e_no_super_class);
  1391. again:=false;
  1392. end;
  1393. end
  1394. else
  1395. begin
  1396. { allows @TObject.Load }
  1397. { also allows static methods and variables }
  1398. p1:=ctypenode.create(hdef);
  1399. { TP allows also @TMenu.Load if Load is only }
  1400. { defined in an anchestor class }
  1401. srsym:=search_class_member(tobjectdef(hdef),pattern);
  1402. if assigned(srsym) then
  1403. begin
  1404. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1405. if not(getaddr) and not(sp_static in srsym.symoptions) then
  1406. Message(sym_e_only_static_in_static)
  1407. else
  1408. begin
  1409. consume(_ID);
  1410. do_member_read(tobjectdef(hdef),getaddr,srsym,p1,again,[]);
  1411. end;
  1412. end
  1413. else
  1414. Message1(sym_e_id_no_member,orgpattern);
  1415. end;
  1416. end
  1417. else
  1418. begin
  1419. { class reference ? }
  1420. if is_class(hdef) or
  1421. is_objcclass(hdef) then
  1422. begin
  1423. if getaddr and (token=_POINT) then
  1424. begin
  1425. consume(_POINT);
  1426. { allows @Object.Method }
  1427. { also allows static methods and variables }
  1428. p1:=ctypenode.create(hdef);
  1429. { TP allows also @TMenu.Load if Load is only }
  1430. { defined in an anchestor class }
  1431. srsym:=search_class_member(tobjectdef(hdef),pattern);
  1432. if assigned(srsym) then
  1433. begin
  1434. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1435. consume(_ID);
  1436. do_member_read(tobjectdef(hdef),getaddr,srsym,p1,again,[]);
  1437. end
  1438. else
  1439. begin
  1440. Message1(sym_e_id_no_member,orgpattern);
  1441. consume(_ID);
  1442. end;
  1443. end
  1444. else
  1445. begin
  1446. p1:=ctypenode.create(hdef);
  1447. { For a type block we simply return only
  1448. the type. For all other blocks we return
  1449. a loadvmt node }
  1450. if not(block_type in [bt_type,bt_const_type,bt_var_type]) then
  1451. p1:=cloadvmtaddrnode.create(p1);
  1452. end;
  1453. end
  1454. else
  1455. p1:=ctypenode.create(hdef);
  1456. end;
  1457. end;
  1458. end;
  1459. enumsym :
  1460. begin
  1461. p1:=genenumnode(tenumsym(srsym));
  1462. end;
  1463. constsym :
  1464. begin
  1465. if tconstsym(srsym).consttyp=constresourcestring then
  1466. begin
  1467. p1:=cloadnode.create(srsym,srsymtable);
  1468. do_typecheckpass(p1);
  1469. p1.resultdef:=cansistringtype;
  1470. end
  1471. else
  1472. p1:=genconstsymtree(tconstsym(srsym));
  1473. end;
  1474. procsym :
  1475. begin
  1476. p1:=nil;
  1477. { check if it's a method/class method }
  1478. if is_member_read(srsym,srsymtable,p1,hdef) then
  1479. begin
  1480. { not srsymtable.symtabletype since that can be }
  1481. { withsymtable as well }
  1482. if (srsym.owner.symtabletype=ObjectSymtable) then
  1483. do_member_read(tobjectdef(hdef),getaddr,srsym,p1,again,[])
  1484. else
  1485. { no procsyms in records (yet) }
  1486. internalerror(2007012006);
  1487. end
  1488. else
  1489. { regular procedure/function call }
  1490. do_proc_call(srsym,srsymtable,nil,
  1491. (getaddr and not(token in [_CARET,_POINT])),
  1492. again,p1,[]);
  1493. end;
  1494. propertysym :
  1495. begin
  1496. p1:=nil;
  1497. { property of a class/object? }
  1498. if is_member_read(srsym,srsymtable,p1,hdef) then
  1499. begin
  1500. if (srsymtable.symtabletype=ObjectSymtable) then
  1501. if (assigned(current_procinfo) and ([po_staticmethod,po_classmethod] <= current_procinfo.procdef.procoptions)) then
  1502. { no self node in static class methods }
  1503. p1:=cloadvmtaddrnode.create(ctypenode.create(hdef))
  1504. else
  1505. p1:=load_self_node;
  1506. { not srsymtable.symtabletype since that can be }
  1507. { withsymtable as well }
  1508. if (srsym.owner.symtabletype=ObjectSymtable) then
  1509. do_member_read(tobjectdef(hdef),getaddr,srsym,p1,again,[])
  1510. else
  1511. { no propertysyms in records (yet) }
  1512. internalerror(2009111510);
  1513. end
  1514. else
  1515. { no method pointer }
  1516. begin
  1517. handle_propertysym(tpropertysym(srsym),srsymtable,p1);
  1518. end;
  1519. end;
  1520. labelsym :
  1521. begin
  1522. { Support @label }
  1523. if getaddr then
  1524. begin
  1525. if srsym.owner<>current_procinfo.procdef.localst then
  1526. CGMessage(parser_e_label_outside_proc);
  1527. p1:=cloadnode.create(srsym,srsym.owner)
  1528. end
  1529. else
  1530. begin
  1531. consume(_COLON);
  1532. if tlabelsym(srsym).defined then
  1533. Message(sym_e_label_already_defined);
  1534. if symtablestack.top.symtablelevel<>srsymtable.symtablelevel then
  1535. begin
  1536. tlabelsym(srsym).nonlocal:=true;
  1537. exclude(current_procinfo.procdef.procoptions,po_inline);
  1538. end;
  1539. tlabelsym(srsym).defined:=true;
  1540. p1:=clabelnode.create(nil,tlabelsym(srsym));
  1541. tlabelsym(srsym).code:=p1;
  1542. end;
  1543. end;
  1544. errorsym :
  1545. begin
  1546. p1:=cerrornode.create;
  1547. if try_to_consume(_LKLAMMER) then
  1548. begin
  1549. parse_paras(false,false,_RKLAMMER);
  1550. consume(_RKLAMMER);
  1551. end;
  1552. end;
  1553. else
  1554. begin
  1555. p1:=cerrornode.create;
  1556. Message(parser_e_illegal_expression);
  1557. end;
  1558. end; { end case }
  1559. end;
  1560. end;
  1561. {---------------------------------------------
  1562. Factor_Read_Set
  1563. ---------------------------------------------}
  1564. { Read a set between [] }
  1565. function factor_read_set:tnode;
  1566. var
  1567. p1,p2 : tnode;
  1568. lastp,
  1569. buildp : tarrayconstructornode;
  1570. begin
  1571. buildp:=nil;
  1572. { be sure that a least one arrayconstructn is used, also for an
  1573. empty [] }
  1574. if token=_RECKKLAMMER then
  1575. buildp:=carrayconstructornode.create(nil,buildp)
  1576. else
  1577. repeat
  1578. p1:=comp_expr(true);
  1579. if try_to_consume(_POINTPOINT) then
  1580. begin
  1581. p2:=comp_expr(true);
  1582. p1:=carrayconstructorrangenode.create(p1,p2);
  1583. end;
  1584. { insert at the end of the tree, to get the correct order }
  1585. if not assigned(buildp) then
  1586. begin
  1587. buildp:=carrayconstructornode.create(p1,nil);
  1588. lastp:=buildp;
  1589. end
  1590. else
  1591. begin
  1592. lastp.right:=carrayconstructornode.create(p1,nil);
  1593. lastp:=tarrayconstructornode(lastp.right);
  1594. end;
  1595. { there could be more elements }
  1596. until not try_to_consume(_COMMA);
  1597. factor_read_set:=buildp;
  1598. end;
  1599. {---------------------------------------------
  1600. PostFixOperators
  1601. ---------------------------------------------}
  1602. { returns whether or not p1 has been changed }
  1603. function postfixoperators(var p1:tnode;var again:boolean): boolean;
  1604. { tries to avoid syntax errors after invalid qualifiers }
  1605. procedure recoverconsume_postfixops;
  1606. begin
  1607. repeat
  1608. if not try_to_consume(_CARET) then
  1609. if try_to_consume(_POINT) then
  1610. try_to_consume(_ID)
  1611. else if try_to_consume(_LECKKLAMMER) then
  1612. begin
  1613. repeat
  1614. comp_expr(true);
  1615. until not try_to_consume(_COMMA);
  1616. consume(_RECKKLAMMER);
  1617. end
  1618. else if try_to_consume(_LKLAMMER) then
  1619. begin
  1620. repeat
  1621. comp_expr(true);
  1622. until not try_to_consume(_COMMA);
  1623. consume(_RKLAMMER);
  1624. end
  1625. else
  1626. break;
  1627. until false;
  1628. end;
  1629. procedure handle_variantarray;
  1630. var
  1631. p4 : tnode;
  1632. newstatement : tstatementnode;
  1633. tempresultvariant,
  1634. temp : ttempcreatenode;
  1635. paras : tcallparanode;
  1636. newblock : tnode;
  1637. countindices : aint;
  1638. begin
  1639. { create statements with call initialize the arguments and
  1640. call fpc_dynarr_setlength }
  1641. newblock:=internalstatements(newstatement);
  1642. { get temp for array of indicies,
  1643. we set the real size later }
  1644. temp:=ctempcreatenode.create(s32inttype,4,tt_persistent,false);
  1645. addstatement(newstatement,temp);
  1646. countindices:=0;
  1647. repeat
  1648. p4:=comp_expr(true);
  1649. addstatement(newstatement,cassignmentnode.create(
  1650. ctemprefnode.create_offset(temp,countindices*s32inttype.size),p4));
  1651. inc(countindices);
  1652. until not try_to_consume(_COMMA);
  1653. { set real size }
  1654. temp.size:=countindices*s32inttype.size;
  1655. consume(_RECKKLAMMER);
  1656. { we need only a write access if a := follows }
  1657. if token=_ASSIGNMENT then
  1658. begin
  1659. consume(_ASSIGNMENT);
  1660. p4:=comp_expr(true);
  1661. { create call to fpc_vararray_put }
  1662. paras:=ccallparanode.create(cordconstnode.create
  1663. (countindices,s32inttype,true),
  1664. ccallparanode.create(caddrnode.create_internal
  1665. (ctemprefnode.create(temp)),
  1666. ccallparanode.create(ctypeconvnode.create_internal(p4,cvarianttype),
  1667. ccallparanode.create(ctypeconvnode.create_internal(p1,cvarianttype)
  1668. ,nil))));
  1669. addstatement(newstatement,ccallnode.createintern('fpc_vararray_put',paras));
  1670. addstatement(newstatement,ctempdeletenode.create(temp));
  1671. end
  1672. else
  1673. begin
  1674. { create temp for result }
  1675. tempresultvariant:=ctempcreatenode.create(cvarianttype,cvarianttype.size,tt_persistent,true);
  1676. addstatement(newstatement,tempresultvariant);
  1677. { create call to fpc_vararray_get }
  1678. paras:=ccallparanode.create(cordconstnode.create
  1679. (countindices,s32inttype,true),
  1680. ccallparanode.create(caddrnode.create_internal
  1681. (ctemprefnode.create(temp)),
  1682. ccallparanode.create(p1,
  1683. ccallparanode.create(
  1684. ctemprefnode.create(tempresultvariant)
  1685. ,nil))));
  1686. addstatement(newstatement,ccallnode.createintern('fpc_vararray_get',paras));
  1687. addstatement(newstatement,ctempdeletenode.create(temp));
  1688. { the last statement should return the value as
  1689. location and type, this is done be referencing the
  1690. temp and converting it first from a persistent temp to
  1691. normal temp }
  1692. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempresultvariant));
  1693. addstatement(newstatement,ctemprefnode.create(tempresultvariant));
  1694. end;
  1695. p1:=newblock;
  1696. end;
  1697. var
  1698. protsym : tpropertysym;
  1699. p2,p3 : tnode;
  1700. srsym : tsym;
  1701. srsymtable : TSymtable;
  1702. classh : tobjectdef;
  1703. { shouldn't be used that often, so the extra overhead is ok to save
  1704. stack space }
  1705. dispatchstring : ansistring;
  1706. nodechanged : boolean;
  1707. label
  1708. skipreckklammercheck;
  1709. begin
  1710. result:=false;
  1711. again:=true;
  1712. while again do
  1713. begin
  1714. { we need the resultdef }
  1715. do_typecheckpass_changed(p1,nodechanged);
  1716. result:=result or nodechanged;
  1717. if codegenerror then
  1718. begin
  1719. recoverconsume_postfixops;
  1720. exit;
  1721. end;
  1722. { handle token }
  1723. case token of
  1724. _CARET:
  1725. begin
  1726. consume(_CARET);
  1727. { support tp/mac procvar^ if the procvar returns a
  1728. pointer type }
  1729. if ((m_tp_procvar in current_settings.modeswitches) or
  1730. (m_mac_procvar in current_settings.modeswitches)) and
  1731. (p1.resultdef.typ=procvardef) and
  1732. (tprocvardef(p1.resultdef).returndef.typ=pointerdef) then
  1733. begin
  1734. p1:=ccallnode.create_procvar(nil,p1);
  1735. typecheckpass(p1);
  1736. end;
  1737. if (p1.resultdef.typ<>pointerdef) then
  1738. begin
  1739. { ^ as binary operator is a problem!!!! (FK) }
  1740. again:=false;
  1741. Message(parser_e_invalid_qualifier);
  1742. recoverconsume_postfixops;
  1743. p1.destroy;
  1744. p1:=cerrornode.create;
  1745. end
  1746. else
  1747. p1:=cderefnode.create(p1);
  1748. end;
  1749. _LECKKLAMMER:
  1750. begin
  1751. if is_class_or_interface_or_object(p1.resultdef) then
  1752. begin
  1753. { default property }
  1754. protsym:=search_default_property(tobjectdef(p1.resultdef));
  1755. if not(assigned(protsym)) then
  1756. begin
  1757. p1.destroy;
  1758. p1:=cerrornode.create;
  1759. again:=false;
  1760. message(parser_e_no_default_property_available);
  1761. end
  1762. else
  1763. begin
  1764. { The property symbol is referenced indirect }
  1765. protsym.IncRefCount;
  1766. handle_propertysym(protsym,protsym.owner,p1);
  1767. end;
  1768. end
  1769. else
  1770. begin
  1771. consume(_LECKKLAMMER);
  1772. repeat
  1773. { in all of the cases below, p1 is changed }
  1774. case p1.resultdef.typ of
  1775. pointerdef:
  1776. begin
  1777. { support delphi autoderef }
  1778. if (tpointerdef(p1.resultdef).pointeddef.typ=arraydef) and
  1779. (m_autoderef in current_settings.modeswitches) then
  1780. p1:=cderefnode.create(p1);
  1781. p2:=comp_expr(true);
  1782. { Support Pbytevar[0..9] which returns array [0..9].}
  1783. if try_to_consume(_POINTPOINT) then
  1784. p2:=crangenode.create(p2,comp_expr(true));
  1785. p1:=cvecnode.create(p1,p2);
  1786. end;
  1787. variantdef:
  1788. begin
  1789. handle_variantarray;
  1790. { the RECKKLAMMER is already read }
  1791. goto skipreckklammercheck;
  1792. end;
  1793. stringdef :
  1794. begin
  1795. p2:=comp_expr(true);
  1796. { Support string[0..9] which returns array [0..9] of char.}
  1797. if try_to_consume(_POINTPOINT) then
  1798. p2:=crangenode.create(p2,comp_expr(true));
  1799. p1:=cvecnode.create(p1,p2);
  1800. end;
  1801. arraydef:
  1802. begin
  1803. p2:=comp_expr(true);
  1804. { support SEG:OFS for go32v2 Mem[] }
  1805. if (target_info.system in [system_i386_go32v2,system_i386_watcom]) and
  1806. (p1.nodetype=loadn) and
  1807. assigned(tloadnode(p1).symtableentry) and
  1808. assigned(tloadnode(p1).symtableentry.owner.name) and
  1809. (tloadnode(p1).symtableentry.owner.name^='SYSTEM') and
  1810. ((tloadnode(p1).symtableentry.name='MEM') or
  1811. (tloadnode(p1).symtableentry.name='MEMW') or
  1812. (tloadnode(p1).symtableentry.name='MEML')) then
  1813. begin
  1814. if try_to_consume(_COLON) then
  1815. begin
  1816. p3:=caddnode.create(muln,cordconstnode.create($10,s32inttype,false),p2);
  1817. p2:=comp_expr(true);
  1818. p2:=caddnode.create(addn,p2,p3);
  1819. if try_to_consume(_POINTPOINT) then
  1820. { Support mem[$a000:$0000..$07ff] which returns array [0..$7ff] of memtype.}
  1821. p2:=crangenode.create(p2,caddnode.create(addn,comp_expr(true),p3.getcopy));
  1822. p1:=cvecnode.create(p1,p2);
  1823. include(tvecnode(p1).flags,nf_memseg);
  1824. include(tvecnode(p1).flags,nf_memindex);
  1825. end
  1826. else
  1827. begin
  1828. if try_to_consume(_POINTPOINT) then
  1829. { Support mem[$80000000..$80000002] which returns array [0..2] of memtype.}
  1830. p2:=crangenode.create(p2,comp_expr(true));
  1831. p1:=cvecnode.create(p1,p2);
  1832. include(tvecnode(p1).flags,nf_memindex);
  1833. end;
  1834. end
  1835. else
  1836. begin
  1837. if try_to_consume(_POINTPOINT) then
  1838. { Support arrayvar[0..9] which returns array [0..9] of arraytype.}
  1839. p2:=crangenode.create(p2,comp_expr(true));
  1840. p1:=cvecnode.create(p1,p2);
  1841. end;
  1842. end;
  1843. else
  1844. begin
  1845. if p1.resultdef.typ<>undefineddef then
  1846. Message(parser_e_invalid_qualifier);
  1847. p1.destroy;
  1848. p1:=cerrornode.create;
  1849. comp_expr(true);
  1850. again:=false;
  1851. end;
  1852. end;
  1853. do_typecheckpass(p1);
  1854. until not try_to_consume(_COMMA);
  1855. consume(_RECKKLAMMER);
  1856. { handle_variantarray eats the RECKKLAMMER and jumps here }
  1857. skipreckklammercheck:
  1858. end;
  1859. end;
  1860. _POINT :
  1861. begin
  1862. consume(_POINT);
  1863. if (p1.resultdef.typ=pointerdef) and
  1864. (m_autoderef in current_settings.modeswitches) then
  1865. begin
  1866. p1:=cderefnode.create(p1);
  1867. do_typecheckpass(p1);
  1868. end;
  1869. case p1.resultdef.typ of
  1870. recorddef:
  1871. begin
  1872. if token=_ID then
  1873. begin
  1874. srsym:=tsym(trecorddef(p1.resultdef).symtable.Find(pattern));
  1875. if assigned(srsym) and
  1876. (srsym.typ=fieldvarsym) then
  1877. begin
  1878. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1879. p1:=csubscriptnode.create(srsym,p1)
  1880. end
  1881. else
  1882. begin
  1883. Message1(sym_e_illegal_field,pattern);
  1884. p1.destroy;
  1885. p1:=cerrornode.create;
  1886. end;
  1887. end;
  1888. consume(_ID);
  1889. end;
  1890. variantdef:
  1891. begin
  1892. { dispatch call? }
  1893. if token=_ID then
  1894. begin
  1895. dispatchstring:=orgpattern;
  1896. consume(_ID);
  1897. if try_to_consume(_LKLAMMER) then
  1898. begin
  1899. p2:=parse_paras(false,true,_RKLAMMER);
  1900. consume(_RKLAMMER);
  1901. end
  1902. else
  1903. p2:=nil;
  1904. { property setter? }
  1905. if (token=_ASSIGNMENT) and not(afterassignment) then
  1906. begin
  1907. consume(_ASSIGNMENT);
  1908. { read the expression }
  1909. p3:=comp_expr(true);
  1910. { concat value parameter too }
  1911. p2:=ccallparanode.create(p3,p2);
  1912. { passing p3 here is only for information purposes }
  1913. p1:=translate_disp_call(p1,p2,p3,dispatchstring,0,false);
  1914. end
  1915. else
  1916. begin
  1917. p1:=translate_disp_call(p1,p2,nil,dispatchstring,0,
  1918. { this is only an approximation
  1919. setting useresult if not necessary is only a waste of time, no more, no less (FK) }
  1920. afterassignment or in_args or (token<>_SEMICOLON));
  1921. end;
  1922. end
  1923. else { Error }
  1924. Consume(_ID);
  1925. end;
  1926. classrefdef:
  1927. begin
  1928. if token=_ID then
  1929. begin
  1930. classh:=tobjectdef(tclassrefdef(p1.resultdef).pointeddef);
  1931. searchsym_in_class(classh,classh,pattern,srsym,srsymtable);
  1932. if assigned(srsym) then
  1933. begin
  1934. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1935. consume(_ID);
  1936. do_member_read(classh,getaddr,srsym,p1,again,[]);
  1937. end
  1938. else
  1939. begin
  1940. Message1(sym_e_id_no_member,orgpattern);
  1941. p1.destroy;
  1942. p1:=cerrornode.create;
  1943. { try to clean up }
  1944. consume(_ID);
  1945. end;
  1946. end
  1947. else { Error }
  1948. Consume(_ID);
  1949. end;
  1950. objectdef:
  1951. begin
  1952. if token=_ID then
  1953. begin
  1954. classh:=tobjectdef(p1.resultdef);
  1955. searchsym_in_class(classh,classh,pattern,srsym,srsymtable);
  1956. if assigned(srsym) then
  1957. begin
  1958. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1959. consume(_ID);
  1960. do_member_read(classh,getaddr,srsym,p1,again,[]);
  1961. end
  1962. else
  1963. begin
  1964. Message1(sym_e_id_no_member,orgpattern);
  1965. p1.destroy;
  1966. p1:=cerrornode.create;
  1967. { try to clean up }
  1968. consume(_ID);
  1969. end;
  1970. end
  1971. else { Error }
  1972. Consume(_ID);
  1973. end;
  1974. pointerdef:
  1975. begin
  1976. if (p1.resultdef=objc_idtype) then
  1977. begin
  1978. { objc's id type can be used to call any
  1979. Objective-C method of any Objective-C class
  1980. type that's currently in scope }
  1981. if search_objc_method(pattern,srsym,srsymtable) then
  1982. begin
  1983. consume(_ID);
  1984. do_proc_call(srsym,srsymtable,nil,
  1985. (getaddr and not(token in [_CARET,_POINT])),
  1986. again,p1,[cnf_objc_id_call]);
  1987. { we need to know which procedure is called }
  1988. do_typecheckpass(p1);
  1989. end
  1990. else
  1991. begin
  1992. consume(_ID);
  1993. Message(parser_e_methode_id_expected);
  1994. end;
  1995. end
  1996. else
  1997. begin
  1998. Message(parser_e_invalid_qualifier);
  1999. if tpointerdef(p1.resultdef).pointeddef.typ in [recorddef,objectdef,classrefdef] then
  2000. Message(parser_h_maybe_deref_caret_missing);
  2001. end
  2002. end;
  2003. else
  2004. begin
  2005. if p1.resultdef.typ<>undefineddef then
  2006. Message(parser_e_invalid_qualifier);
  2007. p1.destroy;
  2008. p1:=cerrornode.create;
  2009. { Error }
  2010. consume(_ID);
  2011. end;
  2012. end;
  2013. end;
  2014. else
  2015. begin
  2016. { is this a procedure variable ? }
  2017. if assigned(p1.resultdef) and
  2018. (p1.resultdef.typ=procvardef) then
  2019. begin
  2020. { Typenode for typecasting or expecting a procvar }
  2021. if (p1.nodetype=typen) or
  2022. (
  2023. assigned(getprocvardef) and
  2024. equal_defs(p1.resultdef,getprocvardef)
  2025. ) then
  2026. begin
  2027. if try_to_consume(_LKLAMMER) then
  2028. begin
  2029. p1:=comp_expr(true);
  2030. consume(_RKLAMMER);
  2031. p1:=ctypeconvnode.create_explicit(p1,p1.resultdef);
  2032. end
  2033. else
  2034. again:=false
  2035. end
  2036. else
  2037. begin
  2038. if try_to_consume(_LKLAMMER) then
  2039. begin
  2040. p2:=parse_paras(false,false,_RKLAMMER);
  2041. consume(_RKLAMMER);
  2042. p1:=ccallnode.create_procvar(p2,p1);
  2043. { proc():= is never possible }
  2044. if token=_ASSIGNMENT then
  2045. begin
  2046. Message(parser_e_illegal_expression);
  2047. p1.free;
  2048. p1:=cerrornode.create;
  2049. again:=false;
  2050. end;
  2051. end
  2052. else
  2053. again:=false;
  2054. end;
  2055. end
  2056. else
  2057. again:=false;
  2058. end;
  2059. end;
  2060. { we only try again if p1 was changed }
  2061. if again or
  2062. (p1.nodetype=errorn) then
  2063. result:=true;
  2064. end; { while again }
  2065. end;
  2066. {---------------------------------------------
  2067. Factor (Main)
  2068. ---------------------------------------------}
  2069. var
  2070. l : longint;
  2071. ic : int64;
  2072. qc : qword;
  2073. p1 : tnode;
  2074. code : integer;
  2075. srsym : tsym;
  2076. srsymtable : TSymtable;
  2077. pd : tprocdef;
  2078. hclassdef : tobjectdef;
  2079. d : bestreal;
  2080. cur : currency;
  2081. hs,hsorg : string;
  2082. hdef : tdef;
  2083. filepos : tfileposinfo;
  2084. again,
  2085. updatefpos,
  2086. nodechanged : boolean;
  2087. begin
  2088. { can't keep a copy of p1 and compare pointers afterwards, because
  2089. p1 may be freed and reallocated in the same place! }
  2090. updatefpos:=false;
  2091. p1:=nil;
  2092. filepos:=current_tokenpos;
  2093. again:=false;
  2094. if token=_ID then
  2095. begin
  2096. again:=true;
  2097. { Handle references to self }
  2098. if (idtoken=_SELF) and
  2099. not(block_type in [bt_const,bt_type,bt_const_type,bt_var_type]) and
  2100. assigned(current_objectdef) then
  2101. begin
  2102. p1:=load_self_node;
  2103. consume(_ID);
  2104. again:=true;
  2105. end
  2106. else
  2107. factor_read_id(p1,again);
  2108. if assigned(p1) then
  2109. begin
  2110. { factor_read_id will set the filepos to after the id,
  2111. and in case of _SELF the filepos will already be the
  2112. same as filepos (so setting it again doesn't hurt). }
  2113. p1.fileinfo:=filepos;
  2114. filepos:=current_tokenpos;
  2115. end;
  2116. { handle post fix operators }
  2117. updatefpos:=postfixoperators(p1,again);
  2118. end
  2119. else
  2120. begin
  2121. updatefpos:=true;
  2122. case token of
  2123. _RETURN :
  2124. begin
  2125. consume(_RETURN);
  2126. if not(token in [_SEMICOLON,_ELSE,_END]) then
  2127. p1 := cexitnode.create(comp_expr(true))
  2128. else
  2129. p1 := cexitnode.create(nil);
  2130. end;
  2131. _INHERITED :
  2132. begin
  2133. again:=true;
  2134. consume(_INHERITED);
  2135. if assigned(current_procinfo) and
  2136. assigned(current_objectdef) then
  2137. begin
  2138. hclassdef:=current_objectdef.childof;
  2139. { Objective-C categories *replace* methods in the class
  2140. they extend, or add methods to it. So calling an
  2141. inherited method always calls the method inherited from
  2142. the parent of the extended class }
  2143. if is_objccategory(current_objectdef) then
  2144. hclassdef:=hclassdef.childof;
  2145. { if inherited; only then we need the method with
  2146. the same name }
  2147. if token in endtokens then
  2148. begin
  2149. hs:=current_procinfo.procdef.procsym.name;
  2150. hsorg:=current_procinfo.procdef.procsym.realname;
  2151. anon_inherited:=true;
  2152. { For message methods we need to search using the message
  2153. number or string }
  2154. pd:=tprocdef(tprocsym(current_procinfo.procdef.procsym).ProcdefList[0]);
  2155. srdef:=nil;
  2156. if (po_msgint in pd.procoptions) then
  2157. searchsym_in_class_by_msgint(hclassdef,pd.messageinf.i,srdef,srsym,srsymtable)
  2158. else
  2159. if (po_msgstr in pd.procoptions) then
  2160. searchsym_in_class_by_msgstr(hclassdef,pd.messageinf.str^,srsym,srsymtable)
  2161. else
  2162. searchsym_in_class(hclassdef,current_objectdef,hs,srsym,srsymtable);
  2163. end
  2164. else
  2165. begin
  2166. hs:=pattern;
  2167. hsorg:=orgpattern;
  2168. consume(_ID);
  2169. anon_inherited:=false;
  2170. searchsym_in_class(hclassdef,current_objectdef,hs,srsym,srsymtable);
  2171. end;
  2172. if assigned(srsym) then
  2173. begin
  2174. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  2175. { load the procdef from the inherited class and
  2176. not from self }
  2177. case srsym.typ of
  2178. procsym:
  2179. begin
  2180. hdef:=hclassdef;
  2181. if (po_classmethod in current_procinfo.procdef.procoptions) or
  2182. (po_staticmethod in current_procinfo.procdef.procoptions) then
  2183. hdef:=tclassrefdef.create(hdef);
  2184. p1:=ctypenode.create(hdef);
  2185. end;
  2186. propertysym:
  2187. ;
  2188. else
  2189. begin
  2190. Message(parser_e_methode_id_expected);
  2191. p1:=cerrornode.create;
  2192. end;
  2193. end;
  2194. do_member_read(hclassdef,getaddr,srsym,p1,again,[cnf_inherited,cnf_anon_inherited]);
  2195. end
  2196. else
  2197. begin
  2198. if anon_inherited then
  2199. begin
  2200. { For message methods we need to call DefaultHandler }
  2201. if (po_msgint in pd.procoptions) or
  2202. (po_msgstr in pd.procoptions) then
  2203. begin
  2204. searchsym_in_class(hclassdef,hclassdef,'DEFAULTHANDLER',srsym,srsymtable);
  2205. if not assigned(srsym) or
  2206. (srsym.typ<>procsym) then
  2207. internalerror(200303171);
  2208. p1:=nil;
  2209. do_proc_call(srsym,srsym.owner,hclassdef,false,again,p1,[]);
  2210. end
  2211. else
  2212. begin
  2213. { we need to ignore the inherited; }
  2214. p1:=cnothingnode.create;
  2215. end;
  2216. end
  2217. else
  2218. begin
  2219. Message1(sym_e_id_no_member,hsorg);
  2220. p1:=cerrornode.create;
  2221. end;
  2222. again:=false;
  2223. end;
  2224. { turn auto inheriting off }
  2225. anon_inherited:=false;
  2226. end
  2227. else
  2228. begin
  2229. Message(parser_e_generic_methods_only_in_methods);
  2230. again:=false;
  2231. p1:=cerrornode.create;
  2232. end;
  2233. postfixoperators(p1,again);
  2234. end;
  2235. _INTCONST :
  2236. begin
  2237. {Try first wether the value fits in an int64.}
  2238. val(pattern,ic,code);
  2239. if code=0 then
  2240. begin
  2241. consume(_INTCONST);
  2242. int_to_type(ic,hdef);
  2243. p1:=cordconstnode.create(ic,hdef,true);
  2244. end
  2245. else
  2246. begin
  2247. { try qword next }
  2248. val(pattern,qc,code);
  2249. if code=0 then
  2250. begin
  2251. consume(_INTCONST);
  2252. int_to_type(qc,hdef);
  2253. p1:=cordconstnode.create(qc,hdef,true);
  2254. end;
  2255. end;
  2256. if code<>0 then
  2257. begin
  2258. { finally float }
  2259. val(pattern,d,code);
  2260. if code<>0 then
  2261. begin
  2262. Message(parser_e_invalid_integer);
  2263. consume(_INTCONST);
  2264. l:=1;
  2265. p1:=cordconstnode.create(l,sinttype,true);
  2266. end
  2267. else
  2268. begin
  2269. consume(_INTCONST);
  2270. p1:=crealconstnode.create(d,pbestrealtype^);
  2271. end;
  2272. end
  2273. else
  2274. { the necessary range checking has already been done by val }
  2275. tordconstnode(p1).rangecheck:=false;
  2276. end;
  2277. _REALNUMBER :
  2278. begin
  2279. val(pattern,d,code);
  2280. if code<>0 then
  2281. begin
  2282. Message(parser_e_error_in_real);
  2283. d:=1.0;
  2284. end;
  2285. consume(_REALNUMBER);
  2286. {$ifdef FPC_REAL2REAL_FIXED}
  2287. if current_settings.fputype=fpu_none then
  2288. Message(parser_e_unsupported_real);
  2289. if (current_settings.minfpconstprec=s32real) and
  2290. (d = single(d)) then
  2291. p1:=crealconstnode.create(d,s32floattype)
  2292. else if (current_settings.minfpconstprec=s64real) and
  2293. (d = double(d)) then
  2294. p1:=crealconstnode.create(d,s64floattype)
  2295. else
  2296. {$endif FPC_REAL2REAL_FIXED}
  2297. p1:=crealconstnode.create(d,pbestrealtype^);
  2298. {$ifdef FPC_HAS_STR_CURRENCY}
  2299. val(pattern,cur,code);
  2300. if code=0 then
  2301. trealconstnode(p1).value_currency:=cur;
  2302. {$endif FPC_HAS_STR_CURRENCY}
  2303. end;
  2304. _STRING :
  2305. begin
  2306. string_dec(hdef,true);
  2307. { STRING can be also a type cast }
  2308. if try_to_consume(_LKLAMMER) then
  2309. begin
  2310. p1:=comp_expr(true);
  2311. consume(_RKLAMMER);
  2312. p1:=ctypeconvnode.create_explicit(p1,hdef);
  2313. { handle postfix operators here e.g. string(a)[10] }
  2314. again:=true;
  2315. postfixoperators(p1,again);
  2316. end
  2317. else
  2318. p1:=ctypenode.create(hdef);
  2319. end;
  2320. _FILE :
  2321. begin
  2322. hdef:=cfiletype;
  2323. consume(_FILE);
  2324. { FILE can be also a type cast }
  2325. if try_to_consume(_LKLAMMER) then
  2326. begin
  2327. p1:=comp_expr(true);
  2328. consume(_RKLAMMER);
  2329. p1:=ctypeconvnode.create_explicit(p1,hdef);
  2330. { handle postfix operators here e.g. string(a)[10] }
  2331. again:=true;
  2332. postfixoperators(p1,again);
  2333. end
  2334. else
  2335. begin
  2336. p1:=ctypenode.create(hdef);
  2337. end;
  2338. end;
  2339. _CSTRING :
  2340. begin
  2341. p1:=cstringconstnode.createstr(pattern);
  2342. consume(_CSTRING);
  2343. end;
  2344. _CCHAR :
  2345. begin
  2346. p1:=cordconstnode.create(ord(pattern[1]),cchartype,true);
  2347. consume(_CCHAR);
  2348. end;
  2349. _CWSTRING:
  2350. begin
  2351. p1:=cstringconstnode.createwstr(patternw);
  2352. consume(_CWSTRING);
  2353. end;
  2354. _CWCHAR:
  2355. begin
  2356. p1:=cordconstnode.create(ord(getcharwidestring(patternw,0)),cwidechartype,true);
  2357. consume(_CWCHAR);
  2358. end;
  2359. _KLAMMERAFFE :
  2360. begin
  2361. consume(_KLAMMERAFFE);
  2362. got_addrn:=true;
  2363. { support both @<x> and @(<x>) }
  2364. if try_to_consume(_LKLAMMER) then
  2365. begin
  2366. p1:=factor(true);
  2367. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2368. begin
  2369. again:=true;
  2370. postfixoperators(p1,again);
  2371. end
  2372. else
  2373. consume(_RKLAMMER);
  2374. end
  2375. else
  2376. p1:=factor(true);
  2377. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2378. begin
  2379. again:=true;
  2380. postfixoperators(p1,again);
  2381. end;
  2382. got_addrn:=false;
  2383. p1:=caddrnode.create(p1);
  2384. p1.fileinfo:=filepos;
  2385. if cs_typed_addresses in current_settings.localswitches then
  2386. include(p1.flags,nf_typedaddr);
  2387. { Store the procvar that we are expecting, the
  2388. addrn will use the information to find the correct
  2389. procdef or it will return an error }
  2390. if assigned(getprocvardef) and
  2391. (taddrnode(p1).left.nodetype = loadn) then
  2392. taddrnode(p1).getprocvardef:=getprocvardef;
  2393. end;
  2394. _LKLAMMER :
  2395. begin
  2396. consume(_LKLAMMER);
  2397. p1:=comp_expr(true);
  2398. consume(_RKLAMMER);
  2399. { it's not a good solution }
  2400. { but (a+b)^ makes some problems }
  2401. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2402. begin
  2403. again:=true;
  2404. postfixoperators(p1,again);
  2405. end;
  2406. end;
  2407. _LECKKLAMMER :
  2408. begin
  2409. consume(_LECKKLAMMER);
  2410. p1:=factor_read_set;
  2411. consume(_RECKKLAMMER);
  2412. end;
  2413. _PLUS :
  2414. begin
  2415. consume(_PLUS);
  2416. p1:=factor(false);
  2417. { we must generate a new node to do 0+<p1> otherwise the + will
  2418. not be checked }
  2419. p1:=caddnode.create(addn,genintconstnode(0),p1);
  2420. end;
  2421. _MINUS :
  2422. begin
  2423. consume(_MINUS);
  2424. if (token = _INTCONST) then
  2425. begin
  2426. { ugly hack, but necessary to be able to parse }
  2427. { -9223372036854775808 as int64 (JM) }
  2428. pattern := '-'+pattern;
  2429. p1:=sub_expr(oppower,false);
  2430. { -1 ** 4 should be - (1 ** 4) and not
  2431. (-1) ** 4
  2432. This was the reason of tw0869.pp test failure PM }
  2433. if p1.nodetype=starstarn then
  2434. begin
  2435. if tbinarynode(p1).left.nodetype=ordconstn then
  2436. begin
  2437. tordconstnode(tbinarynode(p1).left).value:=-tordconstnode(tbinarynode(p1).left).value;
  2438. p1:=cunaryminusnode.create(p1);
  2439. end
  2440. else if tbinarynode(p1).left.nodetype=realconstn then
  2441. begin
  2442. trealconstnode(tbinarynode(p1).left).value_real:=-trealconstnode(tbinarynode(p1).left).value_real;
  2443. trealconstnode(tbinarynode(p1).left).value_currency:=-trealconstnode(tbinarynode(p1).left).value_currency;
  2444. p1:=cunaryminusnode.create(p1);
  2445. end
  2446. else
  2447. internalerror(20021029);
  2448. end;
  2449. end
  2450. else
  2451. begin
  2452. p1:=sub_expr(oppower,false);
  2453. p1:=cunaryminusnode.create(p1);
  2454. end;
  2455. end;
  2456. _OP_NOT :
  2457. begin
  2458. consume(_OP_NOT);
  2459. p1:=factor(false);
  2460. p1:=cnotnode.create(p1);
  2461. end;
  2462. _TRUE :
  2463. begin
  2464. consume(_TRUE);
  2465. p1:=cordconstnode.create(1,booltype,false);
  2466. end;
  2467. _FALSE :
  2468. begin
  2469. consume(_FALSE);
  2470. p1:=cordconstnode.create(0,booltype,false);
  2471. end;
  2472. _NIL :
  2473. begin
  2474. consume(_NIL);
  2475. p1:=cnilnode.create;
  2476. { It's really ugly code nil^, but delphi allows it }
  2477. if token in [_CARET] then
  2478. begin
  2479. again:=true;
  2480. postfixoperators(p1,again);
  2481. end;
  2482. end;
  2483. _OBJCPROTOCOL:
  2484. begin
  2485. { The @protocol keyword is used in two ways in Objective-C:
  2486. 1) to declare protocols (~ Object Pascal interfaces)
  2487. 2) to obtain the metaclass (~ Object Pascal) "class of")
  2488. of a declared protocol
  2489. This code is for handling the second case. Because of 1),
  2490. we cannot simply use a system unit symbol.
  2491. }
  2492. consume(_OBJCPROTOCOL);
  2493. consume(_LKLAMMER);
  2494. p1:=factor(false);
  2495. consume(_RKLAMMER);
  2496. p1:=cinlinenode.create(in_objc_protocol_x,false,p1);
  2497. end;
  2498. else
  2499. begin
  2500. Message(parser_e_illegal_expression);
  2501. p1:=cerrornode.create;
  2502. { recover }
  2503. consume(token);
  2504. end;
  2505. end;
  2506. end;
  2507. { generate error node if no node is created }
  2508. if not assigned(p1) then
  2509. begin
  2510. {$ifdef EXTDEBUG}
  2511. Comment(V_Warning,'factor: p1=nil');
  2512. {$endif}
  2513. p1:=cerrornode.create;
  2514. updatefpos:=true;
  2515. end;
  2516. { get the resultdef for the node }
  2517. if (not assigned(p1.resultdef)) then
  2518. begin
  2519. do_typecheckpass_changed(p1,nodechanged);
  2520. updatefpos:=updatefpos or nodechanged;
  2521. end;
  2522. if assigned(p1) and
  2523. updatefpos then
  2524. p1.fileinfo:=filepos;
  2525. factor:=p1;
  2526. end;
  2527. {$maxfpuregisters default}
  2528. {****************************************************************************
  2529. Sub_Expr
  2530. ****************************************************************************}
  2531. const
  2532. { Warning these stay be ordered !! }
  2533. operator_levels:array[Toperator_precedence] of set of Ttoken=
  2534. ([_LT,_LTE,_GT,_GTE,_EQUAL,_UNEQUAL,_OP_IN],
  2535. [_PLUS,_MINUS,_OP_OR,_PIPE,_OP_XOR],
  2536. [_CARET,_SYMDIF,_STARSTAR,_STAR,_SLASH,
  2537. _OP_AS,_OP_IS,_OP_AND,_AMPERSAND,_OP_DIV,_OP_MOD,_OP_SHL,_OP_SHR],
  2538. [_STARSTAR] );
  2539. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):tnode;
  2540. {Reads a subexpression while the operators are of the current precedence
  2541. level, or any higher level. Replaces the old term, simpl_expr and
  2542. simpl2_expr.}
  2543. var
  2544. p1,p2 : tnode;
  2545. oldt : Ttoken;
  2546. filepos : tfileposinfo;
  2547. begin
  2548. if pred_level=highest_precedence then
  2549. p1:=factor(false)
  2550. else
  2551. p1:=sub_expr(succ(pred_level),true);
  2552. repeat
  2553. if (token in operator_levels[pred_level]) and
  2554. ((token<>_EQUAL) or accept_equal) then
  2555. begin
  2556. oldt:=token;
  2557. filepos:=current_tokenpos;
  2558. consume(token);
  2559. if pred_level=highest_precedence then
  2560. p2:=factor(false)
  2561. else
  2562. p2:=sub_expr(succ(pred_level),true);
  2563. case oldt of
  2564. _PLUS :
  2565. p1:=caddnode.create(addn,p1,p2);
  2566. _MINUS :
  2567. p1:=caddnode.create(subn,p1,p2);
  2568. _STAR :
  2569. p1:=caddnode.create(muln,p1,p2);
  2570. _SLASH :
  2571. p1:=caddnode.create(slashn,p1,p2);
  2572. _EQUAL :
  2573. p1:=caddnode.create(equaln,p1,p2);
  2574. _GT :
  2575. p1:=caddnode.create(gtn,p1,p2);
  2576. _LT :
  2577. p1:=caddnode.create(ltn,p1,p2);
  2578. _GTE :
  2579. p1:=caddnode.create(gten,p1,p2);
  2580. _LTE :
  2581. p1:=caddnode.create(lten,p1,p2);
  2582. _SYMDIF :
  2583. p1:=caddnode.create(symdifn,p1,p2);
  2584. _STARSTAR :
  2585. p1:=caddnode.create(starstarn,p1,p2);
  2586. _OP_AS :
  2587. p1:=casnode.create(p1,p2);
  2588. _OP_IN :
  2589. p1:=cinnode.create(p1,p2);
  2590. _OP_IS :
  2591. p1:=cisnode.create(p1,p2);
  2592. _OP_OR,
  2593. _PIPE {macpas only} :
  2594. begin
  2595. p1:=caddnode.create(orn,p1,p2);
  2596. if (oldt = _PIPE) then
  2597. include(p1.flags,nf_short_bool);
  2598. end;
  2599. _OP_AND,
  2600. _AMPERSAND {macpas only} :
  2601. begin
  2602. p1:=caddnode.create(andn,p1,p2);
  2603. if (oldt = _AMPERSAND) then
  2604. include(p1.flags,nf_short_bool);
  2605. end;
  2606. _OP_DIV :
  2607. p1:=cmoddivnode.create(divn,p1,p2);
  2608. _OP_NOT :
  2609. p1:=cnotnode.create(p1);
  2610. _OP_MOD :
  2611. p1:=cmoddivnode.create(modn,p1,p2);
  2612. _OP_SHL :
  2613. p1:=cshlshrnode.create(shln,p1,p2);
  2614. _OP_SHR :
  2615. p1:=cshlshrnode.create(shrn,p1,p2);
  2616. _OP_XOR :
  2617. p1:=caddnode.create(xorn,p1,p2);
  2618. _ASSIGNMENT :
  2619. p1:=cassignmentnode.create(p1,p2);
  2620. _UNEQUAL :
  2621. p1:=caddnode.create(unequaln,p1,p2);
  2622. end;
  2623. p1.fileinfo:=filepos;
  2624. end
  2625. else
  2626. break;
  2627. until false;
  2628. sub_expr:=p1;
  2629. end;
  2630. function comp_expr(accept_equal : boolean):tnode;
  2631. var
  2632. oldafterassignment : boolean;
  2633. p1 : tnode;
  2634. begin
  2635. oldafterassignment:=afterassignment;
  2636. afterassignment:=true;
  2637. p1:=sub_expr(opcompare,accept_equal);
  2638. { get the resultdef for this expression }
  2639. if not assigned(p1.resultdef) then
  2640. do_typecheckpass(p1);
  2641. afterassignment:=oldafterassignment;
  2642. comp_expr:=p1;
  2643. end;
  2644. function expr : tnode;
  2645. var
  2646. p1,p2 : tnode;
  2647. filepos : tfileposinfo;
  2648. oldafterassignment,
  2649. updatefpos : boolean;
  2650. begin
  2651. oldafterassignment:=afterassignment;
  2652. p1:=sub_expr(opcompare,true);
  2653. { get the resultdef for this expression }
  2654. if not assigned(p1.resultdef) then
  2655. do_typecheckpass(p1);
  2656. filepos:=current_tokenpos;
  2657. if token in [_ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  2658. afterassignment:=true;
  2659. updatefpos:=true;
  2660. case token of
  2661. _POINTPOINT :
  2662. begin
  2663. consume(_POINTPOINT);
  2664. p2:=sub_expr(opcompare,true);
  2665. p1:=crangenode.create(p1,p2);
  2666. end;
  2667. _ASSIGNMENT :
  2668. begin
  2669. consume(_ASSIGNMENT);
  2670. if (p1.resultdef.typ=procvardef) then
  2671. getprocvardef:=tprocvardef(p1.resultdef);
  2672. p2:=sub_expr(opcompare,true);
  2673. if assigned(getprocvardef) then
  2674. handle_procvar(getprocvardef,p2);
  2675. getprocvardef:=nil;
  2676. p1:=cassignmentnode.create(p1,p2);
  2677. end;
  2678. _PLUSASN :
  2679. begin
  2680. consume(_PLUSASN);
  2681. p2:=sub_expr(opcompare,true);
  2682. p1:=gen_c_style_operator(addn,p1,p2);
  2683. end;
  2684. _MINUSASN :
  2685. begin
  2686. consume(_MINUSASN);
  2687. p2:=sub_expr(opcompare,true);
  2688. p1:=gen_c_style_operator(subn,p1,p2);
  2689. end;
  2690. _STARASN :
  2691. begin
  2692. consume(_STARASN );
  2693. p2:=sub_expr(opcompare,true);
  2694. p1:=gen_c_style_operator(muln,p1,p2);
  2695. end;
  2696. _SLASHASN :
  2697. begin
  2698. consume(_SLASHASN );
  2699. p2:=sub_expr(opcompare,true);
  2700. p1:=gen_c_style_operator(slashn,p1,p2);
  2701. end;
  2702. else
  2703. updatefpos:=false;
  2704. end;
  2705. { get the resultdef for this expression }
  2706. if not assigned(p1.resultdef) then
  2707. do_typecheckpass(p1);
  2708. afterassignment:=oldafterassignment;
  2709. if updatefpos then
  2710. p1.fileinfo:=filepos;
  2711. expr:=p1;
  2712. end;
  2713. function get_intconst:TConstExprInt;
  2714. {Reads an expression, tries to evalute it and check if it is an integer
  2715. constant. Then the constant is returned.}
  2716. var
  2717. p:tnode;
  2718. begin
  2719. result:=0;
  2720. p:=comp_expr(true);
  2721. if not codegenerror then
  2722. begin
  2723. if (p.nodetype<>ordconstn) or
  2724. not(is_integer(p.resultdef)) then
  2725. Message(parser_e_illegal_expression)
  2726. else
  2727. result:=tordconstnode(p).value;
  2728. end;
  2729. p.free;
  2730. end;
  2731. function get_stringconst:string;
  2732. {Reads an expression, tries to evaluate it and checks if it is a string
  2733. constant. Then the constant is returned.}
  2734. var
  2735. p:tnode;
  2736. begin
  2737. get_stringconst:='';
  2738. p:=comp_expr(true);
  2739. if p.nodetype<>stringconstn then
  2740. begin
  2741. if (p.nodetype=ordconstn) and is_char(p.resultdef) then
  2742. get_stringconst:=char(int64(tordconstnode(p).value))
  2743. else
  2744. Message(parser_e_illegal_expression);
  2745. end
  2746. else
  2747. get_stringconst:=strpas(tstringconstnode(p).value_str);
  2748. p.free;
  2749. end;
  2750. end.