pexpr.pas 120 KB

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