pexpr.pas 121 KB

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