pexpr.pas 143 KB

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