pexpr.pas 121 KB

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