pdecsub.pas 149 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl, Daniel Mantione
  3. Does the parsing of the procedures/functions
  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 pdecsub;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cclasses,
  23. { scanner }
  24. tokens,
  25. { symtable }
  26. symconst,symtype,symdef,symsym;
  27. type
  28. tpdflag=(
  29. pd_body, { directive needs a body }
  30. pd_implemen, { directive can be used in implementation section }
  31. pd_interface, { directive can be used in interface section }
  32. pd_object, { directive can be used with object declaration }
  33. pd_record, { directive can be used with record declaration }
  34. pd_procvar, { directive can be used with procvar declaration }
  35. pd_notobject, { directive can not be used with object declaration }
  36. pd_notrecord, { directive can not be used with record declaration }
  37. pd_notobjintf, { directive can not be used with interface declaration }
  38. pd_notprocvar, { directive can not be used with procvar declaration }
  39. pd_dispinterface,{ directive can be used with dispinterface methods }
  40. pd_cppobject, { directive can be used with cppclass }
  41. pd_objcclass, { directive can be used with objcclass }
  42. pd_objcprot, { directive can be used with objcprotocol }
  43. pd_nothelper, { directive can not be used with record/class helper declaration }
  44. pd_javaclass, { directive can be used with Java class }
  45. pd_intfjava { directive can be used with Java interface }
  46. );
  47. tpdflags=set of tpdflag;
  48. // flags of handle_calling_convention routine
  49. thccflag=(
  50. hcc_check, // perform checks and outup errors if found
  51. hcc_insert_hidden_paras // insert hidden parameters
  52. );
  53. thccflags=set of thccflag;
  54. const
  55. hcc_all=[hcc_check,hcc_insert_hidden_paras];
  56. function check_proc_directive(isprocvar:boolean):boolean;
  57. function proc_add_definition(var currpd:tprocdef):boolean;
  58. function proc_get_importname(pd:tprocdef):string;
  59. procedure proc_set_mangledname(pd:tprocdef);
  60. procedure handle_calling_convention(pd:tabstractprocdef;flags:thccflags=hcc_all);
  61. procedure parse_parameter_dec(pd:tabstractprocdef);
  62. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  63. procedure parse_var_proc_directives(sym:tsym);
  64. procedure parse_object_proc_directives(pd:tabstractprocdef);
  65. procedure parse_record_proc_directives(pd:tabstractprocdef);
  66. function parse_proc_head(astruct:tabstractrecorddef;potype:tproctypeoption;isgeneric:boolean;genericdef:tdef;generictypelist:tfphashobjectlist;out pd:tprocdef):boolean;
  67. function parse_proc_dec(isclassmethod:boolean;astruct:tabstractrecorddef;isgeneric:boolean):tprocdef;
  68. procedure parse_proc_dec_finish(pd:tprocdef;isclassmethod:boolean;astruct:tabstractrecorddef);
  69. { parse a record method declaration (not a (class) constructor/destructor) }
  70. function parse_record_method_dec(astruct: tabstractrecorddef; is_classdef: boolean;hadgeneric:boolean): tprocdef;
  71. procedure insert_record_hidden_paras(astruct: trecorddef);
  72. { helper functions - they insert nested objects hierarchy to the symtablestack
  73. with object hierarchy
  74. }
  75. function push_child_hierarchy(obj:tabstractrecorddef):integer;
  76. function pop_child_hierarchy(obj:tabstractrecorddef):integer;
  77. function push_nested_hierarchy(obj:tabstractrecorddef):integer;
  78. function pop_nested_hierarchy(obj:tabstractrecorddef):integer;
  79. implementation
  80. uses
  81. SysUtils,
  82. { common }
  83. cutils,
  84. { global }
  85. globtype,globals,verbose,constexp,
  86. systems,
  87. cpuinfo,
  88. { assembler }
  89. aasmbase,
  90. { symtable }
  91. symbase,symcpu,symtable,defutil,defcmp,
  92. { parameter handling }
  93. paramgr,cpupara,
  94. { pass 1 }
  95. fmodule,node,htypechk,ncon,nld,
  96. objcutil,
  97. { parser }
  98. scanner,
  99. syscinfo,
  100. pbase,pexpr,ptype,pdecl,pparautl,pgenutil
  101. {$ifdef jvm}
  102. ,pjvm
  103. {$endif}
  104. ;
  105. const
  106. { Please leave this here, this module should NOT use
  107. these variables.
  108. Declaring it as string here results in an error when compiling (PFV) }
  109. current_procinfo = 'error';
  110. function push_child_hierarchy(obj:tabstractrecorddef):integer;
  111. var
  112. _class,hp : tobjectdef;
  113. begin
  114. if obj.typ=recorddef then
  115. begin
  116. symtablestack.push(obj.symtable);
  117. result:=1;
  118. exit;
  119. end;
  120. result:=0;
  121. { insert class hierarchy in the reverse order }
  122. hp:=nil;
  123. repeat
  124. _class:=tobjectdef(obj);
  125. while _class.childof<>hp do
  126. _class:=_class.childof;
  127. hp:=_class;
  128. symtablestack.push(_class.symtable);
  129. inc(result);
  130. until hp=obj;
  131. end;
  132. function push_nested_hierarchy(obj:tabstractrecorddef):integer;
  133. begin
  134. result:=0;
  135. if obj.owner.symtabletype in [ObjectSymtable,recordsymtable] then
  136. inc(result,push_nested_hierarchy(tabstractrecorddef(obj.owner.defowner)));
  137. inc(result,push_child_hierarchy(obj));
  138. end;
  139. function pop_child_hierarchy(obj:tabstractrecorddef):integer;
  140. var
  141. _class : tobjectdef;
  142. begin
  143. if obj.typ=recorddef then
  144. begin
  145. symtablestack.pop(obj.symtable);
  146. result:=1;
  147. exit;
  148. end;
  149. result:=0;
  150. _class:=tobjectdef(obj);
  151. while assigned(_class) do
  152. begin
  153. symtablestack.pop(_class.symtable);
  154. _class:=_class.childof;
  155. inc(result);
  156. end;
  157. end;
  158. function pop_nested_hierarchy(obj:tabstractrecorddef):integer;
  159. begin
  160. result:=pop_child_hierarchy(obj);
  161. if obj.owner.symtabletype in [ObjectSymtable,recordsymtable] then
  162. inc(result,pop_nested_hierarchy(tabstractrecorddef(obj.owner.defowner)));
  163. end;
  164. procedure check_msg_para(p:TObject;arg:pointer);
  165. begin
  166. if (tsym(p).typ<>paravarsym) then
  167. exit;
  168. with tparavarsym(p) do
  169. begin
  170. { Count parameters }
  171. if (paranr>=10) then
  172. inc(plongint(arg)^);
  173. { First parameter must be var }
  174. if (paranr=10) and
  175. (varspez<>vs_var) then
  176. MessagePos(fileinfo,parser_e_ill_msg_param);
  177. end;
  178. end;
  179. procedure set_addr_param_regable(p:TObject;arg:pointer);
  180. begin
  181. if (tsym(p).typ<>paravarsym) then
  182. exit;
  183. with tparavarsym(p) do
  184. begin
  185. if (not needs_finalization) and
  186. paramanager.push_addr_param(varspez,vardef,tprocdef(arg).proccalloption) then
  187. varregable:=vr_addr;
  188. end;
  189. end;
  190. procedure parse_parameter_dec(pd:tabstractprocdef);
  191. {
  192. handle_procvar needs the same changes
  193. }
  194. type
  195. tppv = (pv_none,pv_proc,pv_func);
  196. var
  197. sc : TFPObjectList;
  198. hdef : tdef;
  199. arrayelementdef : tdef;
  200. vs : tparavarsym;
  201. i : longint;
  202. srsym : tsym;
  203. pv : tprocvardef;
  204. varspez : Tvarspez;
  205. defaultvalue : tconstsym;
  206. defaultrequired : boolean;
  207. old_block_type : tblock_type;
  208. currparast : tparasymtable;
  209. parseprocvar : tppv;
  210. locationstr : string;
  211. paranr : integer;
  212. dummytype : ttypesym;
  213. explicit_paraloc,
  214. need_array,
  215. is_univ: boolean;
  216. stoptions : TSingleTypeOptions;
  217. procedure handle_default_para_value;
  218. var
  219. convpd : tprocdef;
  220. doconv : tconverttype;
  221. nodetype : tnodetype;
  222. bt : tblock_type;
  223. begin
  224. { only allowed for types that can be represented by a
  225. constant expression }
  226. if try_to_consume(_EQ) then
  227. begin
  228. if (hdef.typ in [recorddef,variantdef,filedef,formaldef]) or
  229. is_object(hdef) or
  230. ((hdef.typ=arraydef) and
  231. not is_dynamic_array(hdef)) then
  232. Message1(type_e_invalid_default_value,FullTypeName(hdef,nil));
  233. vs:=tparavarsym(sc[0]);
  234. if sc.count>1 then
  235. Message(parser_e_default_value_only_one_para);
  236. if not(vs.varspez in [vs_value,vs_const,vs_constref]) then
  237. Message(parser_e_default_value_val_const);
  238. bt:=block_type;
  239. block_type:=bt_const;
  240. { prefix 'def' to the parameter name }
  241. defaultvalue:=ReadConstant('$def'+vs.name,vs.fileinfo,nodetype);
  242. block_type:=bt;
  243. if assigned(defaultvalue) then
  244. begin
  245. include(defaultvalue.symoptions,sp_internal);
  246. pd.parast.insert(defaultvalue);
  247. { check whether the default value is of the correct
  248. type }
  249. if compare_defs_ext(defaultvalue.constdef,hdef,nodetype,doconv,convpd,[])<=te_convert_operator then
  250. MessagePos2(defaultvalue.fileinfo,type_e_incompatible_types,FullTypeName(defaultvalue.constdef,hdef),FullTypeName(hdef,defaultvalue.constdef));
  251. end;
  252. defaultrequired:=true;
  253. end
  254. else
  255. begin
  256. if defaultrequired then
  257. Message1(parser_e_default_value_expected_for_para,vs.name);
  258. end;
  259. end;
  260. begin
  261. old_block_type:=block_type;
  262. explicit_paraloc:=false;
  263. consume(_LKLAMMER);
  264. { Delphi/Kylix supports nonsense like }
  265. { procedure p(); }
  266. if try_to_consume(_RKLAMMER) and
  267. not(m_tp7 in current_settings.modeswitches) then
  268. exit;
  269. { parsing a proc or procvar ? }
  270. currparast:=tparasymtable(pd.parast);
  271. { reset }
  272. sc:=TFPObjectList.create(false);
  273. defaultrequired:=false;
  274. paranr:=0;
  275. block_type:=bt_var;
  276. is_univ:=false;
  277. repeat
  278. parseprocvar:=pv_none;
  279. if try_to_consume(_VAR) then
  280. varspez:=vs_var
  281. else
  282. if try_to_consume(_CONST) then
  283. varspez:=vs_const
  284. else
  285. if (m_out in current_settings.modeswitches) and
  286. try_to_consume(_OUT) then
  287. varspez:=vs_out
  288. else
  289. if try_to_consume(_CONSTREF) then
  290. varspez:=vs_constref
  291. else
  292. if (m_mac in current_settings.modeswitches) and
  293. try_to_consume(_POINTPOINTPOINT) then
  294. begin
  295. include(pd.procoptions,po_varargs);
  296. break;
  297. end
  298. else
  299. if (m_nested_procvars in current_settings.modeswitches) and
  300. try_to_consume(_PROCEDURE) then
  301. begin
  302. parseprocvar:=pv_proc;
  303. varspez:=vs_const;
  304. end
  305. else
  306. if (m_nested_procvars in current_settings.modeswitches) and
  307. try_to_consume(_FUNCTION) then
  308. begin
  309. parseprocvar:=pv_func;
  310. varspez:=vs_const;
  311. end
  312. else
  313. varspez:=vs_value;
  314. defaultvalue:=nil;
  315. hdef:=nil;
  316. { read identifiers and insert with error type }
  317. sc.clear;
  318. repeat
  319. inc(paranr);
  320. vs:=cparavarsym.create(orgpattern,paranr*10,varspez,generrordef,[]);
  321. currparast.insert(vs);
  322. if assigned(vs.owner) then
  323. sc.add(vs)
  324. else
  325. vs.free;
  326. consume(_ID);
  327. until not try_to_consume(_COMMA);
  328. locationstr:='';
  329. { macpas anonymous procvar }
  330. if parseprocvar<>pv_none then
  331. begin
  332. { inline procvar definitions are always nested procvars }
  333. pv:=cprocvardef.create(normal_function_level+1);
  334. if token=_LKLAMMER then
  335. parse_parameter_dec(pv);
  336. if parseprocvar=pv_func then
  337. begin
  338. block_type:=bt_var_type;
  339. consume(_COLON);
  340. single_type(pv.returndef,[]);
  341. block_type:=bt_var;
  342. end;
  343. hdef:=pv;
  344. { possible proc directives }
  345. if check_proc_directive(true) then
  346. begin
  347. dummytype:=ctypesym.create('unnamed',hdef,true);
  348. parse_var_proc_directives(tsym(dummytype));
  349. dummytype.typedef:=nil;
  350. hdef.typesym:=nil;
  351. dummytype.free;
  352. end;
  353. { Add implicit hidden parameters and function result }
  354. handle_calling_convention(pv);
  355. {$ifdef jvm}
  356. { anonymous -> no name }
  357. jvm_create_procvar_class('',pv);
  358. {$endif}
  359. end
  360. else
  361. { read type declaration, force reading for value paras }
  362. if (token=_COLON) or (varspez=vs_value) then
  363. begin
  364. consume(_COLON);
  365. { check for an open array }
  366. need_array:=false;
  367. { bitpacked open array are not yet supported }
  368. if (token=_PACKED) and
  369. not(cs_bitpacking in current_settings.localswitches) then
  370. begin
  371. consume(_PACKED);
  372. need_array:=true;
  373. end;
  374. if (token=_ARRAY) or
  375. need_array then
  376. begin
  377. consume(_ARRAY);
  378. consume(_OF);
  379. { define range and type of range }
  380. hdef:=carraydef.create_openarray;
  381. { array of const ? }
  382. if (token=_CONST) and (m_objpas in current_settings.modeswitches) then
  383. begin
  384. consume(_CONST);
  385. srsym:=search_system_type('TVARREC');
  386. tarraydef(hdef).elementdef:=ttypesym(srsym).typedef;
  387. include(tarraydef(hdef).arrayoptions,ado_IsArrayOfConst);
  388. end
  389. else
  390. begin
  391. { define field type }
  392. if m_delphi in current_settings.modeswitches then
  393. stoptions:=[stoAllowSpecialization]
  394. else
  395. stoptions:=[];
  396. single_type(arrayelementdef,stoptions);
  397. tarraydef(hdef).elementdef:=arrayelementdef;
  398. end;
  399. end
  400. else
  401. begin
  402. if (m_mac in current_settings.modeswitches) then
  403. is_univ:=try_to_consume(_UNIV);
  404. if try_to_consume(_TYPE) then
  405. hdef:=ctypedformaltype
  406. else
  407. begin
  408. block_type:=bt_var_type;
  409. single_type(hdef,[stoAllowSpecialization]);
  410. block_type:=bt_var;
  411. end;
  412. { open string ? }
  413. if is_shortstring(hdef) then
  414. begin
  415. case varspez of
  416. vs_var,vs_out:
  417. begin
  418. { not 100% Delphi-compatible: type xstr=string[255] cannot
  419. become an openstring there, while here it can }
  420. if (cs_openstring in current_settings.moduleswitches) and
  421. (tstringdef(hdef).len=255) then
  422. hdef:=openshortstringtype
  423. end;
  424. vs_value:
  425. begin
  426. { value "openstring" parameters don't make sense (the
  427. original string can never be modified, so there's no
  428. use in passing its original length), so change these
  429. into regular shortstring parameters (seems to be what
  430. Delphi also does) }
  431. if is_open_string(hdef) then
  432. hdef:=cshortstringtype;
  433. end;
  434. end;
  435. end;
  436. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) then
  437. begin
  438. if (idtoken=_LOCATION) then
  439. begin
  440. consume(_LOCATION);
  441. locationstr:=cstringpattern;
  442. consume(_CSTRING);
  443. end
  444. else
  445. begin
  446. if explicit_paraloc then
  447. Message(parser_e_paraloc_all_paras);
  448. locationstr:='';
  449. end;
  450. end
  451. else
  452. locationstr:='';
  453. { default parameter }
  454. if (m_default_para in current_settings.modeswitches) then
  455. handle_default_para_value;
  456. end;
  457. end
  458. else
  459. hdef:=cformaltype;
  460. { File types are only allowed for var and out parameters }
  461. if (hdef.typ=filedef) and
  462. not(varspez in [vs_out,vs_var]) then
  463. CGMessage(cg_e_file_must_call_by_reference);
  464. { Dispinterfaces are restricted to using only automatable types }
  465. if (pd.typ=procdef) and is_dispinterface(tprocdef(pd).struct) and
  466. not is_automatable(hdef) then
  467. Message1(type_e_not_automatable,hdef.typename);
  468. { univ cannot be used with types whose size is not known at compile
  469. time }
  470. if is_univ and
  471. not is_valid_univ_para_type(hdef) then
  472. Message1(parser_e_invalid_univ_para,hdef.typename);
  473. for i:=0 to sc.count-1 do
  474. begin
  475. vs:=tparavarsym(sc[i]);
  476. vs.univpara:=is_univ;
  477. { update varsym }
  478. vs.vardef:=hdef;
  479. vs.defaultconstsym:=defaultvalue;
  480. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) then
  481. begin
  482. if locationstr<>'' then
  483. begin
  484. if sc.count>1 then
  485. Message(parser_e_paraloc_only_one_para);
  486. if (paranr>1) and not(explicit_paraloc) then
  487. Message(parser_e_paraloc_all_paras);
  488. explicit_paraloc:=true;
  489. include(vs.varoptions,vo_has_explicit_paraloc);
  490. if not(paramanager.parseparaloc(vs,locationstr)) then
  491. message(parser_e_illegal_explicit_paraloc);
  492. end
  493. else
  494. if explicit_paraloc then
  495. Message(parser_e_paraloc_all_paras);
  496. end;
  497. end;
  498. until not try_to_consume(_SEMICOLON);
  499. if explicit_paraloc then
  500. begin
  501. pd.has_paraloc_info:=callerside;
  502. include(pd.procoptions,po_explicitparaloc);
  503. end;
  504. { remove parasymtable from stack }
  505. sc.free;
  506. { reset object options }
  507. block_type:=old_block_type;
  508. consume(_RKLAMMER);
  509. end;
  510. function parse_proc_head(astruct:tabstractrecorddef;potype:tproctypeoption;isgeneric:boolean;genericdef:tdef;generictypelist:tfphashobjectlist;out pd:tprocdef):boolean;
  511. var
  512. hs : string;
  513. orgsp,sp,orgspnongen,spnongen : TIDString;
  514. dummysym,srsym : tsym;
  515. checkstack : psymtablestackitem;
  516. oldfilepos,
  517. classstartfilepos,
  518. procstartfilepos : tfileposinfo;
  519. i,
  520. index : longint;
  521. hadspecialize,
  522. firstpart,
  523. freegenericparams,
  524. found,
  525. searchagain : boolean;
  526. st,
  527. genericst: TSymtable;
  528. aprocsym : tprocsym;
  529. popclass : integer;
  530. ImplIntf : TImplementedInterface;
  531. old_parse_generic : boolean;
  532. old_current_structdef: tabstractrecorddef;
  533. old_current_genericdef,
  534. old_current_specializedef: tstoreddef;
  535. lasttoken,lastidtoken: ttoken;
  536. genericparams : tfphashobjectlist;
  537. procedure parse_operator_name;
  538. begin
  539. if (lasttoken in [first_overloaded..last_overloaded]) then
  540. begin
  541. optoken:=token;
  542. end
  543. else
  544. begin
  545. case lasttoken of
  546. _CARET:
  547. Message1(parser_e_overload_operator_failed,'**');
  548. _ID:
  549. case lastidtoken of
  550. _ENUMERATOR:optoken:=_OP_ENUMERATOR;
  551. _EXPLICIT:optoken:=_OP_EXPLICIT;
  552. _INC:optoken:=_OP_INC;
  553. _DEC:optoken:=_OP_DEC;
  554. _INITIALIZE:optoken:=_OP_INITIALIZE;
  555. _FINALIZE:optoken:=_OP_FINALIZE;
  556. _ADDREF:optoken:=_OP_ADDREF;
  557. _COPY:optoken:=_OP_COPY;
  558. else
  559. if (m_delphi in current_settings.modeswitches) then
  560. case lastidtoken of
  561. _IMPLICIT:optoken:=_ASSIGNMENT;
  562. _NEGATIVE:optoken:=_MINUS;
  563. _POSITIVE:optoken:=_PLUS;
  564. _LOGICALNOT:optoken:=_OP_NOT;
  565. _IN:optoken:=_OP_IN;
  566. _EQUAL:optoken:=_EQ;
  567. _NOTEQUAL:optoken:=_NE;
  568. _GREATERTHAN:optoken:=_GT;
  569. _GREATERTHANOREQUAL:optoken:=_GTE;
  570. _LESSTHAN:optoken:=_LT;
  571. _LESSTHANOREQUAL:optoken:=_LTE;
  572. _ADD:optoken:=_PLUS;
  573. _SUBTRACT:optoken:=_MINUS;
  574. _MULTIPLY:optoken:=_STAR;
  575. _DIVIDE:optoken:=_SLASH;
  576. _INTDIVIDE:optoken:=_OP_DIV;
  577. _MODULUS:optoken:=_OP_MOD;
  578. _LEFTSHIFT:optoken:=_OP_SHL;
  579. _RIGHTSHIFT:optoken:=_OP_SHR;
  580. _LOGICALAND:optoken:=_OP_AND;
  581. _LOGICALOR:optoken:=_OP_OR;
  582. _LOGICALXOR:optoken:=_OP_XOR;
  583. _BITWISEAND:optoken:=_OP_AND;
  584. _BITWISEOR:optoken:=_OP_OR;
  585. _BITWISEXOR:optoken:=_OP_XOR;
  586. else
  587. Message1(parser_e_overload_operator_failed,'');
  588. end
  589. else
  590. Message1(parser_e_overload_operator_failed,'');
  591. end
  592. else
  593. Message1(parser_e_overload_operator_failed,'');
  594. end;
  595. end;
  596. sp:=overloaded_names[optoken];
  597. orgsp:=sp;
  598. spnongen:=sp;
  599. orgspnongen:=orgsp;
  600. end;
  601. procedure consume_proc_name;
  602. var
  603. s : string;
  604. i : longint;
  605. sym : ttypesym;
  606. begin
  607. lasttoken:=token;
  608. lastidtoken:=idtoken;
  609. if assigned(genericparams) and freegenericparams then
  610. for i:=0 to genericparams.count-1 do
  611. begin
  612. sym:=ttypesym(genericparams[i]);
  613. if tstoreddef(sym.typedef).is_registered then
  614. begin
  615. sym.typedef.free;
  616. sym.typedef:=nil;
  617. end;
  618. sym.free;
  619. end;
  620. genericparams.free;
  621. genericparams:=nil;
  622. hadspecialize:=false;
  623. if potype=potype_operator then
  624. optoken:=NOTOKEN;
  625. if (potype=potype_operator) and (token<>_ID) then
  626. begin
  627. parse_operator_name;
  628. consume(token);
  629. end
  630. else
  631. begin
  632. sp:=pattern;
  633. orgsp:=orgpattern;
  634. spnongen:=sp;
  635. orgspnongen:=orgsp;
  636. if firstpart and
  637. not (m_delphi in current_settings.modeswitches) and
  638. (idtoken=_SPECIALIZE) then
  639. hadspecialize:=true;
  640. consume(_ID);
  641. if (isgeneric or (m_delphi in current_settings.modeswitches)) and
  642. (token in [_LT,_LSHARPBRACKET]) then
  643. begin
  644. consume(token);
  645. if token in [_GT,_RSHARPBRACKET] then
  646. message(type_e_type_id_expected)
  647. else
  648. begin
  649. genericparams:=parse_generic_parameters(true);
  650. if not assigned(genericparams) then
  651. internalerror(2015061201);
  652. if genericparams.count=0 then
  653. internalerror(2015061202);
  654. s:='';
  655. str(genericparams.count,s);
  656. spnongen:=sp;
  657. orgspnongen:=orgsp;
  658. sp:=sp+'$'+s;
  659. orgsp:=orgsp+'$'+s;
  660. end;
  661. if not try_to_consume(_GT) then
  662. consume(_RSHARPBRACKET);
  663. end;
  664. end;
  665. firstpart:=false;
  666. end;
  667. function search_object_name(sp:TIDString;gen_error:boolean):tsym;
  668. var
  669. storepos:tfileposinfo;
  670. srsymtable:TSymtable;
  671. begin
  672. storepos:=current_tokenpos;
  673. current_tokenpos:=procstartfilepos;
  674. searchsym(sp,result,srsymtable);
  675. if not assigned(result) then
  676. begin
  677. if gen_error then
  678. identifier_not_found(orgsp);
  679. result:=generrorsym;
  680. end;
  681. current_tokenpos:=storepos;
  682. end;
  683. procedure consume_generic_interface;
  684. var
  685. genparalist : tfpobjectlist;
  686. prettyname,
  687. specializename : ansistring;
  688. genname,
  689. ugenname : tidstring;
  690. gencount : string;
  691. begin
  692. consume(_LSHARPBRACKET);
  693. genparalist:=tfpobjectlist.create(false);
  694. if not parse_generic_specialization_types(genparalist,nil,prettyname,specializename) then
  695. srsym:=generrorsym
  696. else
  697. begin
  698. str(genparalist.count,gencount);
  699. genname:=sp+'$'+gencount;
  700. { ToDo: handle nested interfaces }
  701. genname:=generate_generic_name(genname,specializename,'');
  702. ugenname:=upper(genname);
  703. srsym:=search_object_name(ugenname,false);
  704. if not assigned(srsym) then
  705. begin
  706. Message1(type_e_generic_declaration_does_not_match,sp+'<'+prettyname+'>');
  707. srsym:=nil;
  708. exit;
  709. end;
  710. end;
  711. genparalist.free;
  712. consume(_RSHARPBRACKET);
  713. end;
  714. function handle_generic_interface:boolean;
  715. var
  716. i : longint;
  717. sym : ttypesym;
  718. typesrsym : tsym;
  719. typesrsymtable : tsymtable;
  720. specializename,
  721. prettyname: ansistring;
  722. error : boolean;
  723. genname,
  724. ugenname : tidstring;
  725. module : tmodule;
  726. begin
  727. result:=false;
  728. if not assigned(genericparams) then
  729. exit;
  730. specializename:='$';
  731. prettyname:='';
  732. error:=false;
  733. for i:=0 to genericparams.count-1 do
  734. begin
  735. sym:=ttypesym(genericparams[i]);
  736. { ToDo: position }
  737. if not searchsym(upper(sym.RealName),typesrsym,typesrsymtable) then
  738. begin
  739. message1(sym_e_id_not_found,sym.name);
  740. error:=true;
  741. continue;
  742. end;
  743. if typesrsym.typ<>typesym then
  744. begin
  745. message(type_e_type_id_expected);
  746. error:=true;
  747. continue;
  748. end;
  749. module:=find_module_from_symtable(ttypesym(typesrsym).typedef.owner);
  750. if not assigned(module) then
  751. internalerror(2016112803);
  752. specializename:=specializename+'_$'+hexstr(module.moduleid,8)+'$$'+ttypesym(typesrsym).typedef.unique_id_str;
  753. if i>0 then
  754. prettyname:=prettyname+',';
  755. prettyname:=prettyname+ttypesym(typesrsym).prettyname;
  756. end;
  757. result:=true;
  758. if error then
  759. begin
  760. srsym:=generrorsym;
  761. exit;
  762. end;
  763. { ToDo: handle nested interfaces }
  764. genname:=generate_generic_name(sp,specializename,'');
  765. ugenname:=upper(genname);
  766. srsym:=search_object_name(ugenname,false);
  767. if not assigned(srsym) then
  768. begin
  769. Message1(type_e_generic_declaration_does_not_match,sp+'<'+prettyname+'>');
  770. srsym:=generrorsym;
  771. end;
  772. end;
  773. procedure specialize_generic_interface;
  774. var
  775. node : tnode;
  776. begin
  777. node:=factor(false,[ef_type_only,ef_had_specialize]);
  778. if node.nodetype=typen then
  779. begin
  780. sp:=ttypenode(node).typedef.typesym.name;
  781. end
  782. else
  783. sp:='';
  784. end;
  785. begin
  786. sp:='';
  787. orgsp:='';
  788. spnongen:='';
  789. orgspnongen:='';
  790. { Save the position where this procedure really starts }
  791. procstartfilepos:=current_tokenpos;
  792. old_parse_generic:=parse_generic;
  793. firstpart:=true;
  794. result:=false;
  795. pd:=nil;
  796. aprocsym:=nil;
  797. srsym:=nil;
  798. genericparams:=nil;
  799. freegenericparams:=true;
  800. hadspecialize:=false;
  801. if not assigned(genericdef) then
  802. begin
  803. consume_proc_name;
  804. { examine interface map: function/procedure iname.functionname=locfuncname }
  805. if assigned(astruct) and
  806. (astruct.typ=objectdef) and
  807. assigned(tobjectdef(astruct).ImplementedInterfaces) and
  808. (tobjectdef(astruct).ImplementedInterfaces.count>0) and
  809. (
  810. (token=_POINT) or
  811. (
  812. hadspecialize and
  813. (token=_ID)
  814. )
  815. ) then
  816. begin
  817. if hadspecialize and (token=_ID) then
  818. specialize_generic_interface;
  819. consume(_POINT);
  820. if hadspecialize or not handle_generic_interface then
  821. srsym:=search_object_name(sp,true);
  822. { qualifier is interface? }
  823. ImplIntf:=nil;
  824. if assigned(srsym) and
  825. (srsym.typ=typesym) and
  826. (ttypesym(srsym).typedef.typ=objectdef) then
  827. ImplIntf:=find_implemented_interface(tobjectdef(astruct),tobjectdef(ttypesym(srsym).typedef));
  828. if ImplIntf=nil then
  829. begin
  830. Message(parser_e_interface_id_expected);
  831. { error recovery }
  832. consume(_ID);
  833. if try_to_consume(_EQ) then
  834. consume(_ID);
  835. exit;
  836. end
  837. else
  838. { in case of a generic or specialized interface we need to use the
  839. name of the def instead of the symbol, so that always the correct
  840. name is used }
  841. if [df_generic,df_specialization]*ttypesym(srsym).typedef.defoptions<>[] then
  842. sp:=tobjectdef(ttypesym(srsym).typedef).objname^;
  843. { must be a directly implemented interface }
  844. if Assigned(ImplIntf.ImplementsGetter) then
  845. Message2(parser_e_implements_no_mapping,ImplIntf.IntfDef.typename,astruct.objrealname^);
  846. consume(_ID);
  847. { Create unique name <interface>.<method> }
  848. hs:=sp+'.'+pattern;
  849. consume(_EQ);
  850. if assigned(ImplIntf) and
  851. (token=_ID) then
  852. ImplIntf.AddMapping(hs,pattern);
  853. consume(_ID);
  854. result:=true;
  855. exit;
  856. end;
  857. if assigned(genericparams) and assigned(current_genericdef) then
  858. Message(parser_f_no_generic_inside_generic);
  859. { method ? }
  860. srsym:=nil;
  861. if not assigned(astruct) and
  862. (symtablestack.top.symtablelevel=main_program_level) and
  863. try_to_consume(_POINT) then
  864. begin
  865. repeat
  866. classstartfilepos:=procstartfilepos;
  867. searchagain:=false;
  868. { throw the error at the right location }
  869. oldfilepos:=current_filepos;
  870. current_filepos:=procstartfilepos;
  871. if not assigned(astruct) and not assigned(srsym) then
  872. srsym:=search_object_name(sp,true);
  873. current_filepos:=oldfilepos;
  874. { consume proc name }
  875. procstartfilepos:=current_tokenpos;
  876. consume_proc_name;
  877. { qualifier is class name ? }
  878. if (srsym.typ=typesym) and
  879. (ttypesym(srsym).typedef.typ in [objectdef,recorddef]) then
  880. begin
  881. astruct:=tabstractrecorddef(ttypesym(srsym).typedef);
  882. if (token<>_POINT) then
  883. if (potype in [potype_class_constructor,potype_class_destructor]) then
  884. sp:=lower(sp)
  885. else
  886. if (potype=potype_operator) and (optoken=NOTOKEN) then
  887. parse_operator_name;
  888. srsym:=tsym(astruct.symtable.Find(sp));
  889. if assigned(srsym) then
  890. begin
  891. if srsym.typ=procsym then
  892. aprocsym:=tprocsym(srsym)
  893. else
  894. if (srsym.typ=typesym) and
  895. (ttypesym(srsym).typedef.typ in [objectdef,recorddef]) then
  896. begin
  897. searchagain:=true;
  898. consume(_POINT);
  899. end
  900. else
  901. begin
  902. { we use a different error message for tp7 so it looks more compatible }
  903. if (m_fpc in current_settings.modeswitches) then
  904. Message1(parser_e_overloaded_no_procedure,srsym.realname)
  905. else
  906. Message(parser_e_methode_id_expected);
  907. { rename the name to an unique name to avoid an
  908. error when inserting the symbol in the symtable }
  909. orgsp:=orgsp+'$'+tostr(current_filepos.line);
  910. end;
  911. end
  912. else
  913. begin
  914. MessagePos(procstartfilepos,parser_e_methode_id_expected);
  915. { recover by making it a normal procedure instead of method }
  916. astruct:=nil;
  917. end;
  918. end
  919. else
  920. MessagePos(classstartfilepos,parser_e_class_id_expected);
  921. until not searchagain;
  922. end
  923. else
  924. begin
  925. { check for constructor/destructor/class operators which are not allowed here }
  926. if (not parse_only) and
  927. ((potype in [potype_constructor,potype_destructor,
  928. potype_class_constructor,potype_class_destructor]) or
  929. ((potype=potype_operator) and (m_delphi in current_settings.modeswitches))) then
  930. Message(parser_e_only_methods_allowed);
  931. repeat
  932. { only 1 class constructor and destructor is allowed in the class and
  933. the check was already done with oo_has_class_constructor or
  934. oo_has_class_destructor -> skip searching
  935. (bug #28801) }
  936. if (potype in [potype_class_constructor,potype_class_destructor]) then
  937. break;
  938. searchagain:=false;
  939. current_tokenpos:=procstartfilepos;
  940. if (potype=potype_operator)and(optoken=NOTOKEN) then
  941. parse_operator_name;
  942. srsym:=tsym(symtablestack.top.Find(sp));
  943. { Also look in the globalsymtable if we didn't found
  944. the symbol in the localsymtable }
  945. if not assigned(srsym) and
  946. not(parse_only) and
  947. (symtablestack.top=current_module.localsymtable) and
  948. assigned(current_module.globalsymtable) then
  949. srsym:=tsym(current_module.globalsymtable.Find(sp));
  950. { Check if overloaded is a procsym }
  951. if assigned(srsym) then
  952. begin
  953. if srsym.typ=procsym then
  954. aprocsym:=tprocsym(srsym)
  955. else
  956. begin
  957. { when the other symbol is a unit symbol then hide the unit
  958. symbol, this is not supported in tp7 }
  959. if not(m_tp7 in current_settings.modeswitches) and
  960. (srsym.typ=unitsym) then
  961. begin
  962. HideSym(srsym);
  963. searchagain:=true;
  964. end
  965. else
  966. if (m_delphi in current_settings.modeswitches) and
  967. (srsym.typ=absolutevarsym) and
  968. ([vo_is_funcret,vo_is_result]*tabstractvarsym(srsym).varoptions=[vo_is_funcret]) then
  969. begin
  970. HideSym(srsym);
  971. searchagain:=true;
  972. end
  973. else
  974. begin
  975. { we use a different error message for tp7 so it looks more compatible }
  976. if (m_fpc in current_settings.modeswitches) then
  977. Message1(parser_e_overloaded_no_procedure,srsym.realname)
  978. else
  979. Message1(sym_e_duplicate_id,srsym.realname);
  980. { rename the name to an unique name to avoid an
  981. error when inserting the symbol in the symtable }
  982. orgsp:=orgsp+'$'+tostr(current_filepos.line);
  983. end;
  984. end;
  985. end;
  986. until not searchagain;
  987. end;
  988. { test again if assigned, it can be reset to recover }
  989. if not assigned(aprocsym) then
  990. begin
  991. { create a new procsym and set the real filepos }
  992. current_tokenpos:=procstartfilepos;
  993. { for operator we have only one procsym for each overloaded
  994. operation }
  995. if (potype=potype_operator) then
  996. begin
  997. aprocsym:=Tprocsym(symtablestack.top.Find(sp));
  998. if aprocsym=nil then
  999. aprocsym:=cprocsym.create('$'+sp);
  1000. end
  1001. else
  1002. if (potype in [potype_class_constructor,potype_class_destructor]) then
  1003. aprocsym:=cprocsym.create('$'+lower(sp))
  1004. else
  1005. aprocsym:=cprocsym.create(orgsp);
  1006. symtablestack.top.insert(aprocsym);
  1007. end;
  1008. end;
  1009. { to get the correct symtablelevel we must ignore ObjectSymtables }
  1010. st:=nil;
  1011. checkstack:=symtablestack.stack;
  1012. while assigned(checkstack) do
  1013. begin
  1014. st:=checkstack^.symtable;
  1015. if st.symtabletype in [staticsymtable,globalsymtable,localsymtable] then
  1016. break;
  1017. checkstack:=checkstack^.next;
  1018. end;
  1019. pd:=cprocdef.create(st.symtablelevel+1,not assigned(genericdef));
  1020. pd.struct:=astruct;
  1021. pd.procsym:=aprocsym;
  1022. pd.proctypeoption:=potype;
  1023. if assigned(genericparams) then
  1024. begin
  1025. include(pd.defoptions,df_generic);
  1026. { push the parameter symtable so that constraint definitions are added
  1027. there and not in the owner symtable }
  1028. symtablestack.push(pd.parast);
  1029. { register the parameters }
  1030. for i:=0 to genericparams.count-1 do
  1031. begin
  1032. ttypesym(genericparams[i]).register_sym;
  1033. tstoreddef(ttypesym(genericparams[i]).typedef).register_def;
  1034. end;
  1035. insert_generic_parameter_types(pd,nil,genericparams);
  1036. symtablestack.pop(pd.parast);
  1037. freegenericparams:=false;
  1038. parse_generic:=true;
  1039. { also generate a dummy symbol if none exists already }
  1040. if assigned(astruct) then
  1041. dummysym:=tsym(astruct.symtable.find(spnongen))
  1042. else
  1043. begin
  1044. dummysym:=tsym(symtablestack.top.find(spnongen));
  1045. if not assigned(dummysym) and
  1046. (symtablestack.top=current_module.localsymtable) and
  1047. assigned(current_module.globalsymtable) then
  1048. dummysym:=tsym(current_module.globalsymtable.find(spnongen));
  1049. end;
  1050. if not assigned(dummysym) then
  1051. begin
  1052. dummysym:=ctypesym.create(orgspnongen,cundefineddef.create(true),true);
  1053. if assigned(astruct) then
  1054. astruct.symtable.insert(dummysym)
  1055. else
  1056. symtablestack.top.insert(dummysym);
  1057. end;
  1058. include(dummysym.symoptions,sp_generic_dummy);
  1059. { start token recorder for the declaration }
  1060. pd.init_genericdecl;
  1061. current_scanner.startrecordtokens(pd.genericdecltokenbuf);
  1062. end;
  1063. if assigned(genericdef) and not assigned(genericparams) then
  1064. insert_generic_parameter_types(pd,tstoreddef(genericdef),generictypelist);
  1065. { methods inherit df_generic or df_specialization from the objectdef }
  1066. if assigned(pd.struct) and
  1067. (pd.parast.symtablelevel=normal_function_level) then
  1068. begin
  1069. if (df_generic in pd.struct.defoptions) then
  1070. begin
  1071. include(pd.defoptions,df_generic);
  1072. parse_generic:=true;
  1073. end;
  1074. if (df_specialization in pd.struct.defoptions) then
  1075. begin
  1076. if assigned(current_specializedef) then
  1077. begin
  1078. include(pd.defoptions,df_specialization);
  1079. { Find corresponding genericdef, we need it later to
  1080. replay the tokens to generate the body }
  1081. if not assigned(pd.struct.genericdef) then
  1082. internalerror(200512113);
  1083. genericst:=pd.struct.genericdef.GetSymtable(gs_record);
  1084. if not assigned(genericst) then
  1085. internalerror(200512114);
  1086. { when searching for the correct procdef to use as genericdef we need to ignore
  1087. everything except procdefs so that we can find the correct indices }
  1088. index:=0;
  1089. found:=false;
  1090. for i:=0 to pd.owner.deflist.count-1 do
  1091. begin
  1092. if tdef(pd.owner.deflist[i]).typ<>procdef then
  1093. continue;
  1094. if pd.owner.deflist[i]=pd then
  1095. begin
  1096. found:=true;
  1097. break;
  1098. end;
  1099. inc(index);
  1100. end;
  1101. if not found then
  1102. internalerror(2014052301);
  1103. for i:=0 to genericst.deflist.count-1 do
  1104. begin
  1105. if tdef(genericst.deflist[i]).typ<>procdef then
  1106. continue;
  1107. if index=0 then
  1108. pd.genericdef:=tstoreddef(genericst.deflist[i]);
  1109. dec(index);
  1110. end;
  1111. if not assigned(pd.genericdef) or
  1112. (pd.genericdef.typ<>procdef) then
  1113. internalerror(200512115);
  1114. end
  1115. else
  1116. Message(parser_e_explicit_method_implementation_for_specializations_not_allowed);
  1117. end;
  1118. end;
  1119. { methods need to be exported }
  1120. if assigned(astruct) and
  1121. (
  1122. (symtablestack.top.symtabletype in [ObjectSymtable,recordsymtable]) or
  1123. (symtablestack.top.symtablelevel=main_program_level)
  1124. ) then
  1125. include(pd.procoptions,po_global);
  1126. { symbol options that need to be kept per procdef }
  1127. pd.fileinfo:=procstartfilepos;
  1128. pd.visibility:=symtablestack.top.currentvisibility;
  1129. if symtablestack.top.currentlyoptional then
  1130. include(pd.procoptions,po_optional);
  1131. { parse parameters }
  1132. if token=_LKLAMMER then
  1133. begin
  1134. old_current_structdef:=nil;
  1135. old_current_genericdef:=current_genericdef;
  1136. old_current_specializedef:=nil;
  1137. { Add ObjectSymtable to be able to find nested type definitions }
  1138. popclass:=0;
  1139. if assigned(pd.struct) and
  1140. (pd.parast.symtablelevel>=normal_function_level) and
  1141. not(symtablestack.top.symtabletype in [ObjectSymtable,recordsymtable]) then
  1142. begin
  1143. popclass:=push_nested_hierarchy(pd.struct);
  1144. old_current_structdef:=current_structdef;
  1145. old_current_specializedef:=current_specializedef;
  1146. current_structdef:=pd.struct;
  1147. if assigned(current_structdef) and (df_generic in current_structdef.defoptions) then
  1148. current_genericdef:=current_structdef;
  1149. if assigned(current_structdef) and (df_specialization in current_structdef.defoptions) then
  1150. current_specializedef:=current_structdef;
  1151. end;
  1152. if pd.is_generic then
  1153. current_genericdef:=pd;
  1154. { Add parameter symtable }
  1155. if pd.parast.symtabletype<>staticsymtable then
  1156. symtablestack.push(pd.parast);
  1157. parse_parameter_dec(pd);
  1158. if pd.parast.symtabletype<>staticsymtable then
  1159. symtablestack.pop(pd.parast);
  1160. current_genericdef:=old_current_genericdef;
  1161. if popclass>0 then
  1162. begin
  1163. current_structdef:=old_current_structdef;
  1164. current_specializedef:=old_current_specializedef;
  1165. dec(popclass,pop_nested_hierarchy(pd.struct));
  1166. if popclass<>0 then
  1167. internalerror(201011260); // 11 nov 2010 index 0
  1168. end;
  1169. end;
  1170. parse_generic:=old_parse_generic;
  1171. result:=true;
  1172. end;
  1173. procedure parse_proc_dec_finish(pd:tprocdef;isclassmethod:boolean;astruct:tabstractrecorddef);
  1174. var
  1175. locationstr: string;
  1176. i: integer;
  1177. found: boolean;
  1178. procedure read_returndef(pd: tprocdef);
  1179. var
  1180. popclass: integer;
  1181. old_parse_generic: boolean;
  1182. old_current_structdef: tabstractrecorddef;
  1183. old_current_genericdef,
  1184. old_current_specializedef: tstoreddef;
  1185. begin
  1186. old_parse_generic:=parse_generic;
  1187. { Add ObjectSymtable to be able to find generic type definitions }
  1188. popclass:=0;
  1189. old_current_structdef:=nil;
  1190. old_current_genericdef:=current_genericdef;
  1191. old_current_specializedef:=current_specializedef;
  1192. current_genericdef:=nil;
  1193. current_specializedef:=nil;
  1194. if assigned(pd.struct) and
  1195. (pd.parast.symtablelevel>=normal_function_level) and
  1196. not (symtablestack.top.symtabletype in [ObjectSymtable,recordsymtable]) then
  1197. begin
  1198. popclass:=push_nested_hierarchy(pd.struct);
  1199. old_current_structdef:=current_structdef;
  1200. current_structdef:=pd.struct;
  1201. end;
  1202. if df_generic in pd.defoptions then
  1203. begin
  1204. if pd.is_generic then
  1205. current_genericdef:=pd
  1206. else if assigned(pd.struct) then
  1207. current_genericdef:=pd.struct
  1208. else
  1209. internalerror(2016090202);
  1210. end;
  1211. if df_specialization in pd.defoptions then
  1212. begin
  1213. if pd.is_specialization then
  1214. current_specializedef:=pd
  1215. else if assigned(pd.struct) then
  1216. current_specializedef:=pd.struct
  1217. else
  1218. internalerror(2016090203);
  1219. end;
  1220. parse_generic:=(df_generic in pd.defoptions);
  1221. if pd.is_generic or pd.is_specialization then
  1222. symtablestack.push(pd.parast);
  1223. single_type(pd.returndef,[stoAllowSpecialization]);
  1224. // Issue #24863, enabled only for the main progra commented out for now because it breaks building of RTL and needs extensive
  1225. // testing and/or RTL patching.
  1226. {
  1227. if ((pd.returndef=cvarianttype) or (pd.returndef=colevarianttype)) and
  1228. not(cs_compilesystem in current_settings.moduleswitches) then
  1229. current_module.flags:=current_module.flags or uf_uses_variants;
  1230. }
  1231. if is_dispinterface(pd.struct) and not is_automatable(pd.returndef) then
  1232. Message1(type_e_not_automatable,pd.returndef.typename);
  1233. if pd.is_generic or pd.is_specialization then
  1234. symtablestack.pop(pd.parast);
  1235. if popclass>0 then
  1236. begin
  1237. current_structdef:=old_current_structdef;
  1238. dec(popclass,pop_nested_hierarchy(pd.struct));
  1239. if popclass<>0 then
  1240. internalerror(201012020);
  1241. end;
  1242. current_genericdef:=old_current_genericdef;
  1243. current_specializedef:=old_current_specializedef;
  1244. parse_generic:=old_parse_generic;
  1245. end;
  1246. begin
  1247. locationstr:='';
  1248. case pd.proctypeoption of
  1249. potype_procedure:
  1250. begin
  1251. pd.returndef:=voidtype;
  1252. if isclassmethod then
  1253. include(pd.procoptions,po_classmethod);
  1254. end;
  1255. potype_function:
  1256. begin
  1257. if try_to_consume(_COLON) then
  1258. begin
  1259. read_returndef(pd);
  1260. if (target_info.system in [system_m68k_amiga]) then
  1261. begin
  1262. if (idtoken=_LOCATION) then
  1263. begin
  1264. if po_explicitparaloc in pd.procoptions then
  1265. begin
  1266. consume(_LOCATION);
  1267. locationstr:=cstringpattern;
  1268. consume(_CSTRING);
  1269. end
  1270. else
  1271. { I guess this needs a new message... (KB) }
  1272. Message(parser_e_paraloc_all_paras);
  1273. end
  1274. else
  1275. begin
  1276. if po_explicitparaloc in pd.procoptions then
  1277. { assign default locationstr, if none specified }
  1278. { and we've arguments with explicit paraloc }
  1279. locationstr:='D0';
  1280. end;
  1281. end;
  1282. end
  1283. else
  1284. begin
  1285. if (
  1286. parse_only and
  1287. not(is_interface(pd.struct))
  1288. ) or
  1289. (m_repeat_forward in current_settings.modeswitches) then
  1290. begin
  1291. consume(_COLON);
  1292. consume_all_until(_SEMICOLON);
  1293. end;
  1294. end;
  1295. if isclassmethod then
  1296. include(pd.procoptions,po_classmethod);
  1297. end;
  1298. potype_constructor,
  1299. potype_class_constructor:
  1300. begin
  1301. if not isclassmethod and
  1302. assigned(pd) and
  1303. assigned(pd.struct) then
  1304. begin
  1305. { Set return type, class constructors return the
  1306. created instance, object constructors return boolean }
  1307. if is_class(pd.struct) or
  1308. is_record(pd.struct) or
  1309. is_javaclass(pd.struct) then
  1310. pd.returndef:=pd.struct
  1311. else
  1312. if is_objectpascal_helper(pd.struct) then
  1313. pd.returndef:=tobjectdef(pd.struct).extendeddef
  1314. else
  1315. {$ifdef CPU64bitaddr}
  1316. pd.returndef:=bool64type;
  1317. {$else CPU64bitaddr}
  1318. pd.returndef:=bool32type;
  1319. {$endif CPU64bitaddr}
  1320. end
  1321. else
  1322. pd.returndef:=voidtype;
  1323. end;
  1324. potype_class_destructor,
  1325. potype_destructor:
  1326. begin
  1327. if assigned(pd) then
  1328. pd.returndef:=voidtype;
  1329. end;
  1330. potype_operator:
  1331. begin
  1332. { operators always need to be searched in all units (that
  1333. contain operators) }
  1334. include(pd.procoptions,po_overload);
  1335. pd.procsym.owner.includeoption(sto_has_operator);
  1336. if pd.parast.symtablelevel>normal_function_level then
  1337. Message(parser_e_no_local_operator);
  1338. if isclassmethod then
  1339. begin
  1340. include(pd.procoptions,po_classmethod);
  1341. { any class operator is also static }
  1342. include(pd.procoptions,po_staticmethod);
  1343. end;
  1344. if token<>_ID then
  1345. begin
  1346. if not(m_result in current_settings.modeswitches) then
  1347. consume(_ID);
  1348. end
  1349. else
  1350. begin
  1351. pd.resultname:=stringdup(orgpattern);
  1352. consume(_ID);
  1353. end;
  1354. { operators without result (management operators) }
  1355. if optoken in [_OP_INITIALIZE, _OP_FINALIZE, _OP_ADDREF, _OP_COPY] then
  1356. begin
  1357. { single var parameter to point the record }
  1358. if (optoken in [_OP_INITIALIZE, _OP_FINALIZE, _OP_ADDREF]) and
  1359. (
  1360. (pd.parast.SymList.Count<>1) or
  1361. (tparavarsym(pd.parast.SymList[0]).vardef<>pd.struct) or
  1362. (tparavarsym(pd.parast.SymList[0]).varspez<>vs_var)
  1363. ) then
  1364. Message(parser_e_overload_impossible)
  1365. { constref (source) and var (dest) parameter to point the records }
  1366. else if (optoken=_OP_COPY) and
  1367. (
  1368. (pd.parast.SymList.Count<>2) or
  1369. (tparavarsym(pd.parast.SymList[0]).vardef<>pd.struct) or
  1370. (tparavarsym(pd.parast.SymList[0]).varspez<>vs_constref) or
  1371. (tparavarsym(pd.parast.SymList[1]).vardef<>pd.struct) or
  1372. (tparavarsym(pd.parast.SymList[1]).varspez<>vs_var)
  1373. ) then
  1374. Message(parser_e_overload_impossible);
  1375. trecordsymtable(pd.procsym.Owner).includemanagementoperator(
  1376. token2managementoperator(optoken));
  1377. pd.returndef:=voidtype
  1378. end
  1379. else
  1380. if not try_to_consume(_COLON) then
  1381. begin
  1382. consume(_COLON);
  1383. pd.returndef:=generrordef;
  1384. consume_all_until(_SEMICOLON);
  1385. end
  1386. else
  1387. begin
  1388. read_returndef(pd);
  1389. { check that class operators have either return type of structure or }
  1390. { at least one argument of that type }
  1391. if (po_classmethod in pd.procoptions) and
  1392. (pd.returndef <> pd.struct) then
  1393. begin
  1394. found:=false;
  1395. for i := 0 to pd.parast.SymList.Count - 1 do
  1396. if tparavarsym(pd.parast.SymList[i]).vardef=pd.struct then
  1397. begin
  1398. found:=true;
  1399. break;
  1400. end;
  1401. if not found then
  1402. if assigned(pd.struct) then
  1403. Message1(parser_e_at_least_one_argument_must_be_of_type,pd.struct.RttiName)
  1404. else
  1405. MessagePos(pd.fileinfo,type_e_type_id_expected);
  1406. end;
  1407. if not assigned(pd.struct) or assigned(astruct) then
  1408. begin
  1409. if (optoken in [_ASSIGNMENT,_OP_EXPLICIT]) and
  1410. equal_defs(pd.returndef,tparavarsym(pd.parast.SymList[0]).vardef) and
  1411. (pd.returndef.typ<>undefineddef) and (tparavarsym(pd.parast.SymList[0]).vardef.typ<>undefineddef) then
  1412. message(parser_e_no_such_assignment)
  1413. else if not isoperatoracceptable(pd,optoken) then
  1414. Message(parser_e_overload_impossible);
  1415. end;
  1416. end;
  1417. end;
  1418. else
  1419. internalerror(2015052202);
  1420. end;
  1421. { file types can't be function results }
  1422. if assigned(pd) and
  1423. (pd.returndef.typ=filedef) then
  1424. message(parser_e_illegal_function_result);
  1425. { support procedure proc stdcall export; }
  1426. if not(check_proc_directive(false)) then
  1427. begin
  1428. if (token=_COLON) and not(Assigned(pd) and is_void(pd.returndef)) then
  1429. begin
  1430. message(parser_e_field_not_allowed_here);
  1431. consume_all_until(_SEMICOLON);
  1432. end;
  1433. consume(_SEMICOLON);
  1434. end;
  1435. if locationstr<>'' then
  1436. begin
  1437. if not(paramanager.parsefuncretloc(pd,upper(locationstr))) then
  1438. { I guess this needs a new message... (KB) }
  1439. message(parser_e_illegal_explicit_paraloc);
  1440. end;
  1441. end;
  1442. function parse_proc_dec(isclassmethod:boolean;astruct:tabstractrecorddef;isgeneric:boolean):tprocdef;
  1443. var
  1444. pd : tprocdef;
  1445. old_block_type : tblock_type;
  1446. recover : boolean;
  1447. procedure finish_intf_mapping;
  1448. begin
  1449. if token=_COLON then
  1450. begin
  1451. message(parser_e_field_not_allowed_here);
  1452. consume_all_until(_SEMICOLON);
  1453. end;
  1454. consume(_SEMICOLON);
  1455. end;
  1456. begin
  1457. pd:=nil;
  1458. recover:=false;
  1459. case token of
  1460. _FUNCTION :
  1461. begin
  1462. consume(_FUNCTION);
  1463. if parse_proc_head(astruct,potype_function,isgeneric,nil,nil,pd) then
  1464. begin
  1465. { pd=nil when it is a interface mapping }
  1466. if assigned(pd) then
  1467. parse_proc_dec_finish(pd,isclassmethod,astruct)
  1468. else
  1469. finish_intf_mapping;
  1470. end
  1471. else
  1472. begin
  1473. { recover }
  1474. consume(_COLON);
  1475. consume_all_until(_SEMICOLON);
  1476. recover:=true;
  1477. end;
  1478. end;
  1479. _PROCEDURE :
  1480. begin
  1481. consume(_PROCEDURE);
  1482. if parse_proc_head(astruct,potype_procedure,isgeneric,nil,nil,pd) then
  1483. begin
  1484. { pd=nil when it is an interface mapping }
  1485. if assigned(pd) then
  1486. parse_proc_dec_finish(pd,isclassmethod,astruct)
  1487. else
  1488. finish_intf_mapping;
  1489. end
  1490. else
  1491. recover:=true;
  1492. end;
  1493. _CONSTRUCTOR :
  1494. begin
  1495. consume(_CONSTRUCTOR);
  1496. if isclassmethod then
  1497. recover:=not parse_proc_head(astruct,potype_class_constructor,false,nil,nil,pd)
  1498. else
  1499. recover:=not parse_proc_head(astruct,potype_constructor,false,nil,nil,pd);
  1500. if not recover then
  1501. parse_proc_dec_finish(pd,isclassmethod,astruct);
  1502. end;
  1503. _DESTRUCTOR :
  1504. begin
  1505. consume(_DESTRUCTOR);
  1506. if isclassmethod then
  1507. recover:=not parse_proc_head(astruct,potype_class_destructor,false,nil,nil,pd)
  1508. else
  1509. recover:=not parse_proc_head(astruct,potype_destructor,false,nil,nil,pd);
  1510. if not recover then
  1511. parse_proc_dec_finish(pd,isclassmethod,astruct);
  1512. end;
  1513. else
  1514. if (token=_OPERATOR) or
  1515. (isclassmethod and (idtoken=_OPERATOR)) then
  1516. begin
  1517. { we need to set the block type to bt_body, so that operator names
  1518. like ">", "=>" or "<>" are parsed correctly instead of e.g.
  1519. _LSHARPBRACKET and _RSHARPBRACKET for "<>" }
  1520. old_block_type:=block_type;
  1521. block_type:=bt_body;
  1522. consume(_OPERATOR);
  1523. parse_proc_head(astruct,potype_operator,false,nil,nil,pd);
  1524. block_type:=old_block_type;
  1525. if assigned(pd) then
  1526. parse_proc_dec_finish(pd,isclassmethod,astruct)
  1527. else
  1528. begin
  1529. { recover }
  1530. try_to_consume(_ID);
  1531. consume(_COLON);
  1532. consume_all_until(_SEMICOLON);
  1533. recover:=true;
  1534. end;
  1535. end;
  1536. end;
  1537. if recover and not(check_proc_directive(false)) then
  1538. begin
  1539. if (token=_COLON) and not(Assigned(pd) and is_void(pd.returndef)) then
  1540. begin
  1541. message(parser_e_field_not_allowed_here);
  1542. consume_all_until(_SEMICOLON);
  1543. end;
  1544. consume(_SEMICOLON);
  1545. end;
  1546. { we've parsed the final semicolon, so stop recording tokens }
  1547. if assigned(pd) and
  1548. (df_generic in pd.defoptions) and
  1549. assigned(pd.genericdecltokenbuf) then
  1550. current_scanner.stoprecordtokens;
  1551. result:=pd;
  1552. end;
  1553. function parse_record_method_dec(astruct: tabstractrecorddef; is_classdef: boolean;hadgeneric:boolean): tprocdef;
  1554. var
  1555. oldparse_only: boolean;
  1556. begin
  1557. oldparse_only:=parse_only;
  1558. parse_only:=true;
  1559. result:=parse_proc_dec(is_classdef,astruct,hadgeneric);
  1560. { this is for error recovery as well as forward }
  1561. { interface mappings, i.e. mapping to a method }
  1562. { which isn't declared yet }
  1563. if assigned(result) then
  1564. begin
  1565. parse_record_proc_directives(result);
  1566. { since records have no inheritance, don't allow non-static
  1567. class methods. Delphi does the same. }
  1568. if (result.proctypeoption<>potype_operator) and
  1569. is_classdef and
  1570. not (po_staticmethod in result.procoptions) then
  1571. MessagePos(result.fileinfo, parser_e_class_methods_only_static_in_records);
  1572. // we can't add hidden params here because record is not yet defined
  1573. // and therefore record size which has influence on paramter passing rules may change too
  1574. // look at record_dec to see where calling conventions are applied (issue #0021044)
  1575. handle_calling_convention(result,[hcc_check]);
  1576. { add definition to procsym }
  1577. proc_add_definition(result);
  1578. end;
  1579. maybe_parse_hint_directives(result);
  1580. parse_only:=oldparse_only;
  1581. end;
  1582. procedure insert_record_hidden_paras(astruct: trecorddef);
  1583. var
  1584. pd: tdef;
  1585. i: longint;
  1586. oldpos : tfileposinfo;
  1587. oldparse_only: boolean;
  1588. begin
  1589. // handle calling conventions of record methods
  1590. oldpos:=current_filepos;
  1591. oldparse_only:=parse_only;
  1592. parse_only:=true;
  1593. { don't keep track of procdefs in a separate list, because the
  1594. compiler may add additional procdefs (e.g. property wrappers for
  1595. the jvm backend) }
  1596. for i := 0 to astruct.symtable.deflist.count - 1 do
  1597. begin
  1598. pd:=tdef(astruct.symtable.deflist[i]);
  1599. if pd.typ<>procdef then
  1600. continue;
  1601. current_filepos:=tprocdef(pd).fileinfo;
  1602. handle_calling_convention(tprocdef(pd),[hcc_insert_hidden_paras]);
  1603. end;
  1604. parse_only:=oldparse_only;
  1605. current_filepos:=oldpos;
  1606. end;
  1607. {****************************************************************************
  1608. Procedure directive handlers
  1609. ****************************************************************************}
  1610. procedure pd_compilerproc(pd:tabstractprocdef);
  1611. var
  1612. v : Tconstexprint;
  1613. begin
  1614. { check for optional syssym index }
  1615. if try_to_consume(_COLON) then
  1616. begin
  1617. v:=get_intconst;
  1618. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1619. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(longint)),tostr(high(longint)))
  1620. else if not assigned(tsyssym.find_by_number(longint(v.svalue))) then
  1621. message1(parser_e_invalid_internal_function_index,tostr(v))
  1622. else
  1623. tprocdef(pd).extnumber:=longint(v.svalue);
  1624. end;
  1625. end;
  1626. procedure pd_far(pd:tabstractprocdef);
  1627. begin
  1628. pd.declared_far;
  1629. end;
  1630. procedure pd_near(pd:tabstractprocdef);
  1631. begin
  1632. pd.declared_near;
  1633. end;
  1634. procedure pd_export(pd:tabstractprocdef);
  1635. begin
  1636. if pd.typ<>procdef then
  1637. internalerror(200304264);
  1638. if assigned(tprocdef(pd).struct) then
  1639. Message(parser_e_methods_dont_be_export);
  1640. if pd.parast.symtablelevel>normal_function_level then
  1641. Message(parser_e_dont_nest_export);
  1642. end;
  1643. procedure pd_forward(pd:tabstractprocdef);
  1644. begin
  1645. if pd.typ<>procdef then
  1646. internalerror(200304265);
  1647. tprocdef(pd).forwarddef:=true;
  1648. end;
  1649. procedure pd_alias(pd:tabstractprocdef);
  1650. begin
  1651. if pd.typ<>procdef then
  1652. internalerror(200304266);
  1653. consume(_COLON);
  1654. tprocdef(pd).aliasnames.insert(get_stringconst);
  1655. include(pd.procoptions,po_has_public_name);
  1656. end;
  1657. procedure pd_public(pd:tabstractprocdef);
  1658. begin
  1659. if pd.typ<>procdef then
  1660. internalerror(200304266);
  1661. if try_to_consume(_NAME) then
  1662. begin
  1663. tprocdef(pd).aliasnames.insert(get_stringconst);
  1664. include(pd.procoptions,po_has_public_name);
  1665. end;
  1666. end;
  1667. procedure pd_asmname(pd:tabstractprocdef);
  1668. begin
  1669. if pd.typ<>procdef then
  1670. internalerror(200304267);
  1671. if token=_CCHAR then
  1672. begin
  1673. tprocdef(pd).aliasnames.insert(target_info.Cprefix+pattern);
  1674. consume(_CCHAR)
  1675. end
  1676. else
  1677. begin
  1678. tprocdef(pd).aliasnames.insert(target_info.Cprefix+cstringpattern);
  1679. consume(_CSTRING);
  1680. end;
  1681. { we don't need anything else }
  1682. tprocdef(pd).forwarddef:=false;
  1683. end;
  1684. procedure pd_internconst(pd:tabstractprocdef);
  1685. var v:Tconstexprint;
  1686. begin
  1687. if pd.typ<>procdef then
  1688. internalerror(200304268);
  1689. consume(_COLON);
  1690. v:=get_intconst;
  1691. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1692. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(longint)),tostr(high(longint)))
  1693. else
  1694. Tprocdef(pd).extnumber:=longint(v.svalue);
  1695. end;
  1696. procedure pd_internproc(pd:tabstractprocdef);
  1697. var v:Tconstexprint;
  1698. begin
  1699. if pd.typ<>procdef then
  1700. internalerror(200304268);
  1701. consume(_COLON);
  1702. v:=get_intconst;
  1703. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1704. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(longint)),tostr(high(longint)))
  1705. else
  1706. Tprocdef(pd).extnumber:=longint(v.svalue);
  1707. { the proc is defined }
  1708. tprocdef(pd).forwarddef:=false;
  1709. end;
  1710. procedure pd_interrupt(pd:tabstractprocdef);
  1711. begin
  1712. if pd.parast.symtablelevel>normal_function_level then
  1713. Message(parser_e_dont_nest_interrupt);
  1714. end;
  1715. procedure pd_abstract(pd:tabstractprocdef);
  1716. begin
  1717. if pd.typ<>procdef then
  1718. internalerror(200304269);
  1719. if is_objectpascal_helper(tprocdef(pd).struct) then
  1720. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_ABSTRACT].str);
  1721. if assigned(tprocdef(pd).struct) and
  1722. (oo_is_sealed in tprocdef(pd).struct.objectoptions) then
  1723. Message(parser_e_sealed_class_cannot_have_abstract_methods)
  1724. else if (po_virtualmethod in pd.procoptions) then
  1725. begin
  1726. include(pd.procoptions,po_abstractmethod);
  1727. { one more abstract method }
  1728. inc(tobjectdef(pd.owner.defowner).abstractcnt);
  1729. end
  1730. else
  1731. Message(parser_e_only_virtual_methods_abstract);
  1732. { the method is defined }
  1733. tprocdef(pd).forwarddef:=false;
  1734. end;
  1735. procedure pd_final(pd:tabstractprocdef);
  1736. begin
  1737. if pd.typ<>procdef then
  1738. internalerror(200910170);
  1739. if is_objectpascal_helper(tprocdef(pd).struct) and
  1740. (m_objfpc in current_settings.modeswitches) then
  1741. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_FINAL].str);
  1742. if (po_virtualmethod in pd.procoptions) or
  1743. (is_javaclass(tprocdef(pd).struct) and
  1744. (po_classmethod in pd.procoptions)) then
  1745. include(pd.procoptions,po_finalmethod)
  1746. else
  1747. Message(parser_e_only_virtual_methods_final);
  1748. end;
  1749. procedure pd_enumerator(pd:tabstractprocdef);
  1750. begin
  1751. if pd.typ<>procdef then
  1752. internalerror(200910250);
  1753. if (token = _ID) then
  1754. begin
  1755. if pattern='MOVENEXT' then
  1756. begin
  1757. if oo_has_enumerator_movenext in tprocdef(pd).struct.objectoptions then
  1758. message(parser_e_only_one_enumerator_movenext);
  1759. pd.calcparas;
  1760. if (pd.proctypeoption = potype_function) and is_boolean(pd.returndef) and
  1761. (pd.minparacount = 0) then
  1762. begin
  1763. include(tprocdef(pd).struct.objectoptions, oo_has_enumerator_movenext);
  1764. include(pd.procoptions,po_enumerator_movenext);
  1765. end
  1766. else
  1767. Message(parser_e_enumerator_movenext_is_not_valid)
  1768. end
  1769. else
  1770. Message1(parser_e_invalid_enumerator_identifier, pattern);
  1771. consume(token);
  1772. end
  1773. else
  1774. Message(parser_e_enumerator_identifier_required);
  1775. end;
  1776. procedure pd_virtual(pd:tabstractprocdef);
  1777. {$ifdef WITHDMT}
  1778. var
  1779. pt : tnode;
  1780. {$endif WITHDMT}
  1781. begin
  1782. if (not assigned(pd.owner.defowner) or
  1783. not is_java_class_or_interface(tdef(pd.owner.defowner))) and
  1784. (po_external in pd.procoptions) then
  1785. Message1(parser_e_proc_dir_conflict,'EXTERNAL');
  1786. if pd.typ<>procdef then
  1787. internalerror(2003042610);
  1788. if (pd.proctypeoption=potype_constructor) and
  1789. is_object(tprocdef(pd).struct) then
  1790. Message(parser_e_constructor_cannot_be_not_virtual);
  1791. if pd.is_generic then
  1792. message(parser_e_genfuncs_cannot_be_virtual);
  1793. if is_objectpascal_helper(tprocdef(pd).struct) and
  1794. (m_objfpc in current_settings.modeswitches) then
  1795. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_VIRTUAL].str);
  1796. {$ifdef WITHDMT}
  1797. if is_object(tprocdef(pd).struct) and
  1798. (token<>_SEMICOLON) then
  1799. begin
  1800. { any type of parameter is allowed here! }
  1801. pt:=comp_expr(true);
  1802. if is_constintnode(pt) then
  1803. begin
  1804. include(pd.procoptions,po_msgint);
  1805. pd.messageinf.i:=pt.value;
  1806. end
  1807. else
  1808. Message(parser_e_ill_msg_expr);
  1809. disposetree(pt);
  1810. end;
  1811. {$endif WITHDMT}
  1812. end;
  1813. procedure pd_dispid(pd:tabstractprocdef);
  1814. var pt:Tnode;
  1815. begin
  1816. if pd.typ<>procdef then
  1817. internalerror(200604301);
  1818. pt:=comp_expr([ef_accept_equal]);
  1819. if is_constintnode(pt) then
  1820. if (Tordconstnode(pt).value<int64(low(longint))) or (Tordconstnode(pt).value>int64(high(longint))) then
  1821. message3(type_e_range_check_error_bounds,tostr(Tordconstnode(pt).value),tostr(low(longint)),tostr(high(longint)))
  1822. else
  1823. Tprocdef(pd).dispid:=Tordconstnode(pt).value.svalue
  1824. else
  1825. message(parser_e_dispid_must_be_ord_const);
  1826. pt.free;
  1827. end;
  1828. procedure pd_static(pd:tabstractprocdef);
  1829. begin
  1830. if pd.typ<>procdef then
  1831. internalerror(2013032001);
  1832. if not assigned(tprocdef(pd).struct) then
  1833. internalerror(2013032002);
  1834. include(tprocdef(pd).procsym.symoptions,sp_static);
  1835. { "static" is not allowed for operators or normal methods (except in objects) }
  1836. if (pd.proctypeoption=potype_operator) or
  1837. (
  1838. not (po_classmethod in pd.procoptions) and
  1839. not is_object(tprocdef(pd).struct)
  1840. )
  1841. then
  1842. Message1(parser_e_dir_not_allowed,arraytokeninfo[_STATIC].str);
  1843. include(pd.procoptions,po_staticmethod);
  1844. end;
  1845. procedure pd_override(pd:tabstractprocdef);
  1846. begin
  1847. if pd.typ<>procdef then
  1848. internalerror(2003042611);
  1849. if is_objectpascal_helper(tprocdef(pd).struct) then
  1850. begin
  1851. if m_objfpc in current_settings.modeswitches then
  1852. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_OVERRIDE].str)
  1853. end
  1854. else if not(is_class_or_interface_or_objc_or_java(tprocdef(pd).struct)) then
  1855. Message(parser_e_no_object_override)
  1856. else if is_objccategory(tprocdef(pd).struct) then
  1857. Message(parser_e_no_category_override)
  1858. else if (po_external in pd.procoptions) and
  1859. not is_objc_class_or_protocol(tprocdef(pd).struct) and
  1860. not is_cppclass(tprocdef(pd).struct) and
  1861. not is_java_class_or_interface(tprocdef(pd).struct) then
  1862. Message1(parser_e_proc_dir_conflict,'OVERRIDE');
  1863. end;
  1864. procedure pd_overload(pd:tabstractprocdef);
  1865. begin
  1866. if pd.typ<>procdef then
  1867. internalerror(2003042612);
  1868. include(tprocdef(pd).procsym.symoptions,sp_has_overloaded);
  1869. end;
  1870. procedure pd_message(pd:tabstractprocdef);
  1871. var
  1872. pt : tnode;
  1873. paracnt : longint;
  1874. begin
  1875. if pd.typ<>procdef then
  1876. internalerror(2003042613);
  1877. if is_objectpascal_helper(tprocdef(pd).struct) then
  1878. begin
  1879. if m_objfpc in current_settings.modeswitches then
  1880. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_MESSAGE].str);
  1881. end
  1882. else
  1883. if not is_class(tprocdef(pd).struct) and
  1884. not is_objc_class_or_protocol(tprocdef(pd).struct) then
  1885. Message(parser_e_msg_only_for_classes);
  1886. if ([po_msgstr,po_msgint]*pd.procoptions)<>[] then
  1887. Message(parser_e_multiple_messages);
  1888. { check parameter type }
  1889. if not is_objc_class_or_protocol(tprocdef(pd).struct) then
  1890. begin
  1891. if po_external in pd.procoptions then
  1892. Message1(parser_e_proc_dir_conflict,'MESSAGE');
  1893. paracnt:=0;
  1894. pd.parast.SymList.ForEachCall(@check_msg_para,@paracnt);
  1895. if paracnt<>1 then
  1896. Message(parser_e_ill_msg_param);
  1897. end;
  1898. pt:=comp_expr([ef_accept_equal]);
  1899. { message is 1-character long }
  1900. if is_constcharnode(pt) then
  1901. begin
  1902. include(pd.procoptions,po_msgstr);
  1903. tprocdef(pd).messageinf.str:=stringdup(chr(byte(tordconstnode(pt).value.uvalue and $FF)));
  1904. end
  1905. else if pt.nodetype=stringconstn then
  1906. begin
  1907. include(pd.procoptions,po_msgstr);
  1908. if (tstringconstnode(pt).len>255) then
  1909. Message(parser_e_message_string_too_long);
  1910. tprocdef(pd).messageinf.str:=stringdup(tstringconstnode(pt).value_str);
  1911. end
  1912. else
  1913. if is_constintnode(pt) and
  1914. (is_class(tprocdef(pd).struct) or
  1915. is_objectpascal_helper(tprocdef(pd).struct)) then
  1916. begin
  1917. include(pd.procoptions,po_msgint);
  1918. if (Tordconstnode(pt).value<int64(low(Tprocdef(pd).messageinf.i))) or
  1919. (Tordconstnode(pt).value>int64(high(Tprocdef(pd).messageinf.i))) then
  1920. message3(type_e_range_check_error_bounds,tostr(Tordconstnode(pt).value),tostr(low(Tprocdef(pd).messageinf.i)),tostr(high(Tprocdef(pd).messageinf.i)))
  1921. else
  1922. Tprocdef(pd).messageinf.i:=tordconstnode(pt).value.svalue;
  1923. end
  1924. else
  1925. Message(parser_e_ill_msg_expr);
  1926. { check whether the selector name is valid in case of Objective-C }
  1927. if (po_msgstr in pd.procoptions) and
  1928. is_objc_class_or_protocol(tprocdef(pd).struct) and
  1929. not objcvalidselectorname(@tprocdef(pd).messageinf.str^[1],length(tprocdef(pd).messageinf.str^)) then
  1930. Message1(type_e_invalid_objc_selector_name,tprocdef(pd).messageinf.str^);
  1931. pt.free;
  1932. end;
  1933. procedure pd_reintroduce(pd:tabstractprocdef);
  1934. begin
  1935. if pd.typ<>procdef then
  1936. internalerror(200401211);
  1937. if is_objectpascal_helper(tprocdef(pd).struct) then
  1938. begin
  1939. if m_objfpc in current_settings.modeswitches then
  1940. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_REINTRODUCE].str);
  1941. end
  1942. else
  1943. if not(is_class_or_interface_or_object(tprocdef(pd).struct)) and
  1944. not(is_objccategory(tprocdef(pd).struct)) and
  1945. not(is_javaclass(tprocdef(pd).struct)) then
  1946. Message(parser_e_no_object_reintroduce);
  1947. end;
  1948. procedure pd_syscall(pd:tabstractprocdef);
  1949. procedure include_po_syscall;
  1950. var
  1951. syscall: psyscallinfo;
  1952. begin
  1953. case target_info.system of
  1954. system_arm_palmos,
  1955. system_m68k_palmos,
  1956. system_m68k_atari,
  1957. system_m68k_amiga,
  1958. system_powerpc_amiga:
  1959. include(pd.procoptions,get_default_syscall);
  1960. system_powerpc_morphos,
  1961. system_arm_aros,
  1962. system_i386_aros,
  1963. system_x86_64_aros:
  1964. begin
  1965. syscall:=get_syscall_by_token(idtoken);
  1966. if assigned(syscall) then
  1967. begin
  1968. if target_info.system in syscall^.validon then
  1969. begin
  1970. consume(idtoken);
  1971. include(pd.procoptions,syscall^.procoption);
  1972. end
  1973. end
  1974. else
  1975. include(pd.procoptions,get_default_syscall);
  1976. end;
  1977. end;
  1978. end;
  1979. function po_syscall_to_varoptions: tvaroptions;
  1980. begin
  1981. result:=[vo_is_syscall_lib,vo_is_hidden_para];
  1982. if ([po_syscall_legacy,po_syscall_basereg,po_syscall_basenone] * tprocdef(pd).procoptions) <> [] then
  1983. include(result,vo_has_explicit_paraloc);
  1984. end;
  1985. function po_syscall_to_regname: string;
  1986. begin
  1987. if po_syscall_legacy in tprocdef(pd).procoptions then
  1988. result:='a6'
  1989. { let nobase on MorphOS store the libbase in r12 as well, because
  1990. we will need the libbase anyway during the call generation }
  1991. else if (po_syscall_basenone in tprocdef(pd).procoptions) and
  1992. (target_info.system = system_powerpc_morphos) then
  1993. result:='r12'
  1994. else if po_syscall_basereg in tprocdef(pd).procoptions then
  1995. begin
  1996. case target_info.system of
  1997. system_i386_aros:
  1998. result:='eax';
  1999. system_x86_64_aros:
  2000. result:='rax';
  2001. system_powerpc_morphos:
  2002. result:='r12';
  2003. else
  2004. internalerror(2016090201);
  2005. end;
  2006. end
  2007. else
  2008. internalerror(2016090101);
  2009. end;
  2010. {$if defined(powerpc) or defined(m68k) or defined(i386) or defined(x86_64) or defined(arm)}
  2011. const
  2012. syscall_paranr: array[boolean] of aint =
  2013. ( paranr_syscall_lib_last, paranr_syscall_lib_first );
  2014. var
  2015. vs : tparavarsym;
  2016. sym : tsym;
  2017. symtable : TSymtable;
  2018. v: Tconstexprint;
  2019. vo: tvaroptions;
  2020. paranr: aint;
  2021. {$endif defined(powerpc) or defined(m68k) or defined(i386) or defined(x86_64) or defined(arm)}
  2022. begin
  2023. if (pd.typ<>procdef) and (target_info.system <> system_powerpc_amiga) then
  2024. internalerror(2003042614);
  2025. tprocdef(pd).forwarddef:=false;
  2026. {$if defined(powerpc) or defined(m68k) or defined(i386) or defined(x86_64) or defined(arm)}
  2027. include_po_syscall;
  2028. if target_info.system in [system_arm_palmos, system_m68k_palmos] then
  2029. begin
  2030. v:=get_intconst;
  2031. tprocdef(pd).extnumber:=longint(v.svalue);
  2032. if ((v<0) or (v>high(word))) then
  2033. message(parser_e_range_check_error);
  2034. if try_to_consume(_COMMA) then
  2035. begin
  2036. v:=get_intconst;
  2037. if ((v<0) or (v>high(word))) then
  2038. message(parser_e_range_check_error);
  2039. tprocdef(pd).import_nr:=longint(v.svalue);
  2040. include(pd.procoptions,po_syscall_has_importnr);
  2041. end;
  2042. exit;
  2043. end;
  2044. if target_info.system = system_m68k_atari then
  2045. begin
  2046. v:=get_intconst;
  2047. if ((v<0) or (v>15)) then
  2048. message(parser_e_range_check_error)
  2049. else
  2050. tprocdef(pd).extnumber:=longint(v.svalue);
  2051. v:=get_intconst;
  2052. if ((v<0) or (v>high(smallint))) then
  2053. message(parser_e_range_check_error)
  2054. else
  2055. tprocdef(pd).import_nr:=longint(v.svalue);
  2056. exit;
  2057. end;
  2058. if consume_sym(sym,symtable) then
  2059. if (sym.typ=staticvarsym) and
  2060. ((tabstractvarsym(sym).vardef.typ=pointerdef) or
  2061. is_32bitint(tabstractvarsym(sym).vardef)) then
  2062. begin
  2063. include(pd.procoptions,po_syscall_has_libsym);
  2064. tcpuprocdef(pd).libsym:=sym;
  2065. vo:=po_syscall_to_varoptions;
  2066. paranr:=syscall_paranr[po_syscall_basefirst in tprocdef(pd).procoptions];
  2067. vs:=cparavarsym.create('$syscalllib',paranr,vs_value,tabstractvarsym(sym).vardef,vo);
  2068. if vo_has_explicit_paraloc in vo then
  2069. if not paramanager.parseparaloc(vs,po_syscall_to_regname) then
  2070. internalerror(2016120301);
  2071. pd.parast.insert(vs);
  2072. end
  2073. else
  2074. Message(parser_e_32bitint_or_pointer_variable_expected);
  2075. paramanager.create_funcretloc_info(pd,calleeside);
  2076. paramanager.create_funcretloc_info(pd,callerside);
  2077. v:=get_intconst;
  2078. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  2079. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(Tprocdef(pd).extnumber)),tostr(high(Tprocdef(pd).extnumber)))
  2080. else
  2081. if target_info.system in [system_arm_aros,system_i386_aros,system_x86_64_aros] then
  2082. Tprocdef(pd).extnumber:=v.uvalue * sizeof(pint)
  2083. else
  2084. Tprocdef(pd).extnumber:=v.uvalue;
  2085. {$endif defined(powerpc) or defined(m68k) or defined(i386) or defined(x86_64) or defined(arm)}
  2086. end;
  2087. procedure pd_external(pd:tabstractprocdef);
  2088. {
  2089. If import_dll=nil the procedure is assumed to be in another
  2090. object file. In that object file it should have the name to
  2091. which import_name is pointing to. Otherwise, the procedure is
  2092. assumed to be in the DLL to which import_dll is pointing to. In
  2093. that case either import_nr<>0 or import_name<>nil is true, so
  2094. the procedure is either imported by number or by name. (DM)
  2095. }
  2096. var
  2097. hs : string;
  2098. v:Tconstexprint;
  2099. is_java_external: boolean;
  2100. begin
  2101. if pd.typ<>procdef then
  2102. internalerror(2003042615);
  2103. { Allow specifying a separate external name for methods in external Java
  2104. because its identifier naming constraints are laxer than FPC's
  2105. (e.g., case sensitive).
  2106. Limitation: only allows specifying the symbol name and not the package name,
  2107. and only for external classes/interfaces }
  2108. is_java_external:=
  2109. (pd.typ=procdef) and
  2110. is_java_class_or_interface(tdef(pd.owner.defowner)) and
  2111. (oo_is_external in tobjectdef(pd.owner.defowner).objectoptions);
  2112. with tprocdef(pd) do
  2113. begin
  2114. forwarddef:=false;
  2115. { forbid local external procedures }
  2116. if parast.symtablelevel>normal_function_level then
  2117. Message(parser_e_no_local_proc_external);
  2118. { If the procedure should be imported from a DLL, a constant string follows.
  2119. This isn't really correct, an contant string expression follows
  2120. so we check if an semicolon follows, else a string constant have to
  2121. follow (FK) }
  2122. if not is_java_external and
  2123. not(token=_SEMICOLON) and not(idtoken=_NAME) then
  2124. begin
  2125. { Always add library prefix and suffix to create an uniform name }
  2126. hs:=get_stringconst;
  2127. if ExtractFileExt(hs)='' then
  2128. hs:=ChangeFileExt(hs,target_info.sharedlibext);
  2129. if Copy(hs,1,length(target_info.sharedlibprefix))<>target_info.sharedlibprefix then
  2130. hs:=target_info.sharedlibprefix+hs;
  2131. { the JVM expects java/lang/Object rather than java.lang.Object }
  2132. if target_info.system in systems_jvm then
  2133. Replace(hs,'.','/');
  2134. import_dll:=stringdup(hs);
  2135. include(procoptions,po_has_importdll);
  2136. if (idtoken=_NAME) then
  2137. begin
  2138. consume(_NAME);
  2139. import_name:=stringdup(get_stringconst);
  2140. include(procoptions,po_has_importname);
  2141. if import_name^='' then
  2142. message(parser_e_empty_import_name);
  2143. end;
  2144. if (idtoken=_INDEX) then
  2145. begin
  2146. {After the word index follows the index number in the DLL.}
  2147. consume(_INDEX);
  2148. v:=get_intconst;
  2149. if (v<int64(low(import_nr))) or (v>int64(high(import_nr))) then
  2150. message(parser_e_range_check_error)
  2151. else
  2152. import_nr:=longint(v.svalue);
  2153. end;
  2154. { default is to used the realname of the procedure }
  2155. if (import_nr=0) and not assigned(import_name) then
  2156. begin
  2157. import_name:=stringdup(procsym.realname);
  2158. include(procoptions,po_has_importname);
  2159. end;
  2160. end
  2161. else
  2162. begin
  2163. if (idtoken=_NAME) or
  2164. is_java_external then
  2165. begin
  2166. consume(_NAME);
  2167. import_name:=stringdup(get_stringconst);
  2168. include(procoptions,po_has_importname);
  2169. if import_name^='' then
  2170. message(parser_e_empty_import_name);
  2171. end;
  2172. end;
  2173. end;
  2174. end;
  2175. procedure pd_weakexternal(pd:tabstractprocdef);
  2176. begin
  2177. if not(target_info.system in systems_weak_linking) then
  2178. message(parser_e_weak_external_not_supported)
  2179. else
  2180. pd_external(pd);
  2181. end;
  2182. procedure pd_winapi(pd:tabstractprocdef);
  2183. begin
  2184. if not(target_info.system in systems_wince) then
  2185. pd.proccalloption:=pocall_cdecl
  2186. else
  2187. pd.proccalloption:=pocall_stdcall;
  2188. end;
  2189. procedure pd_hardfloat(pd:tabstractprocdef);
  2190. begin
  2191. if
  2192. {$if defined(arm)}
  2193. (current_settings.fputype=fpu_soft) or
  2194. {$endif defined(arm)}
  2195. (cs_fp_emulation in current_settings.moduleswitches) then
  2196. message(parser_e_cannot_use_hardfloat_in_a_softfloat_environment);
  2197. end;
  2198. type
  2199. pd_handler=procedure(pd:tabstractprocdef);
  2200. proc_dir_rec=record
  2201. idtok : ttoken;
  2202. pd_flags : tpdflags;
  2203. handler : pd_handler;
  2204. pocall : tproccalloption;
  2205. pooption : tprocoptions;
  2206. mutexclpocall : tproccalloptions;
  2207. mutexclpotype : tproctypeoptions;
  2208. mutexclpo : tprocoptions;
  2209. end;
  2210. const
  2211. {Should contain the number of procedure directives we support.}
  2212. num_proc_directives=51;
  2213. proc_direcdata:array[1..num_proc_directives] of proc_dir_rec=
  2214. (
  2215. (
  2216. idtok:_ABSTRACT;
  2217. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord,pd_javaclass];
  2218. handler : @pd_abstract;
  2219. pocall : pocall_none;
  2220. pooption : [po_abstractmethod];
  2221. mutexclpocall : [pocall_internproc];
  2222. mutexclpotype : [];
  2223. mutexclpo : [po_exports,po_interrupt,po_inline]
  2224. ),(
  2225. idtok:_ALIAS;
  2226. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  2227. handler : @pd_alias;
  2228. pocall : pocall_none;
  2229. pooption : [];
  2230. mutexclpocall : [];
  2231. mutexclpotype : [];
  2232. mutexclpo : [po_external,po_inline]
  2233. ),(
  2234. idtok:_ASMNAME;
  2235. pd_flags : [pd_interface,pd_implemen,pd_notobjintf];
  2236. handler : @pd_asmname;
  2237. pocall : pocall_cdecl;
  2238. pooption : [po_external];
  2239. mutexclpocall : [pocall_internproc];
  2240. mutexclpotype : [];
  2241. mutexclpo : [po_external,po_inline]
  2242. ),(
  2243. idtok:_ASSEMBLER;
  2244. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2245. handler : nil;
  2246. pocall : pocall_none;
  2247. pooption : [po_assembler];
  2248. mutexclpocall : [];
  2249. mutexclpotype : [];
  2250. mutexclpo : [po_external]
  2251. ),(
  2252. idtok:_C; {same as cdecl for mode mac}
  2253. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2254. handler : nil;
  2255. pocall : pocall_cdecl;
  2256. pooption : [];
  2257. mutexclpocall : [];
  2258. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2259. mutexclpo : [po_assembler,po_external]
  2260. ),(
  2261. idtok:_CDECL;
  2262. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2263. handler : nil;
  2264. pocall : pocall_cdecl;
  2265. pooption : [];
  2266. mutexclpocall : [];
  2267. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2268. mutexclpo : [po_assembler,po_external]
  2269. ),(
  2270. idtok:_DISPID;
  2271. pd_flags : [pd_dispinterface];
  2272. handler : @pd_dispid;
  2273. pocall : pocall_none;
  2274. pooption : [po_dispid];
  2275. mutexclpocall : [pocall_internproc];
  2276. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2277. mutexclpo : [po_interrupt,po_external,po_inline]
  2278. ),(
  2279. idtok:_DYNAMIC;
  2280. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord];
  2281. handler : @pd_virtual;
  2282. pocall : pocall_none;
  2283. pooption : [po_virtualmethod];
  2284. mutexclpocall : [pocall_internproc];
  2285. mutexclpotype : [potype_class_constructor,potype_class_destructor];
  2286. mutexclpo : [po_exports,po_interrupt,po_external,po_overridingmethod,po_inline]
  2287. ),(
  2288. idtok:_EXPORT;
  2289. pd_flags : [pd_body,pd_interface,pd_implemen,pd_notobjintf,pd_notrecord,pd_nothelper];
  2290. handler : @pd_export;
  2291. pocall : pocall_none;
  2292. pooption : [po_exports,po_global];
  2293. mutexclpocall : [pocall_internproc];
  2294. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2295. mutexclpo : [po_external,po_interrupt,po_inline]
  2296. ),(
  2297. idtok:_EXTERNAL;
  2298. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject,pd_notrecord,pd_nothelper,pd_javaclass,pd_intfjava];
  2299. handler : @pd_external;
  2300. pocall : pocall_none;
  2301. pooption : [po_external];
  2302. mutexclpocall : [pocall_syscall];
  2303. { allowed for external cpp classes }
  2304. mutexclpotype : [{potype_constructor,potype_destructor}potype_class_constructor,potype_class_destructor];
  2305. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  2306. ),(
  2307. idtok:_FAR;
  2308. pd_flags : [pd_implemen,pd_body,pd_interface,pd_procvar,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2309. handler : @pd_far;
  2310. pocall : pocall_none;
  2311. pooption : [];
  2312. mutexclpocall : [pocall_internproc];
  2313. mutexclpotype : [];
  2314. mutexclpo : [po_inline]
  2315. ),(
  2316. idtok:_FAR16;
  2317. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar,pd_notobject,pd_notrecord,pd_nothelper];
  2318. handler : nil;
  2319. pocall : pocall_far16;
  2320. pooption : [];
  2321. mutexclpocall : [];
  2322. mutexclpotype : [];
  2323. mutexclpo : [po_external]
  2324. ),(
  2325. idtok:_FINAL;
  2326. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord,pd_javaclass];
  2327. handler : @pd_final;
  2328. pocall : pocall_none;
  2329. pooption : [po_finalmethod];
  2330. mutexclpocall : [pocall_internproc];
  2331. mutexclpotype : [];
  2332. mutexclpo : [po_exports,po_interrupt,po_inline]
  2333. ),(
  2334. idtok:_FORWARD;
  2335. pd_flags : [pd_implemen,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2336. handler : @pd_forward;
  2337. pocall : pocall_none;
  2338. pooption : [];
  2339. mutexclpocall : [pocall_internproc];
  2340. mutexclpotype : [];
  2341. mutexclpo : [po_external,po_inline]
  2342. ),(
  2343. idtok:_OLDFPCCALL;
  2344. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2345. handler : nil;
  2346. pocall : pocall_oldfpccall;
  2347. pooption : [];
  2348. mutexclpocall : [];
  2349. mutexclpotype : [];
  2350. mutexclpo : []
  2351. ),(
  2352. idtok:_INLINE;
  2353. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2354. handler : nil;
  2355. pocall : pocall_none;
  2356. pooption : [po_inline];
  2357. mutexclpocall : [pocall_safecall];
  2358. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2359. mutexclpo : [po_exports,po_external,po_interrupt,po_virtualmethod,po_iocheck]
  2360. ),(
  2361. idtok:_INTERNCONST;
  2362. pd_flags : [pd_interface,pd_body,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2363. handler : @pd_internconst;
  2364. pocall : pocall_none;
  2365. pooption : [po_internconst];
  2366. mutexclpocall : [];
  2367. mutexclpotype : [potype_operator];
  2368. mutexclpo : []
  2369. ),(
  2370. idtok:_INTERNPROC;
  2371. pd_flags : [pd_interface,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2372. handler : @pd_internproc;
  2373. pocall : pocall_internproc;
  2374. pooption : [];
  2375. mutexclpocall : [];
  2376. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2377. mutexclpo : [po_exports,po_external,po_interrupt,po_assembler,po_iocheck,po_virtualmethod]
  2378. ),(
  2379. idtok:_INTERRUPT;
  2380. pd_flags : [pd_implemen,pd_body,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2381. handler : @pd_interrupt;
  2382. pocall : pocall_oldfpccall;
  2383. pooption : [po_interrupt];
  2384. mutexclpocall : [pocall_internproc,pocall_cdecl,pocall_cppdecl,pocall_stdcall,pocall_mwpascal,
  2385. pocall_pascal,pocall_far16,pocall_oldfpccall,pocall_sysv_abi_cdecl,pocall_ms_abi_cdecl];
  2386. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2387. mutexclpo : [po_external,po_inline,po_exports]
  2388. ),(
  2389. idtok:_IOCHECK;
  2390. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  2391. handler : nil;
  2392. pocall : pocall_none;
  2393. pooption : [po_iocheck];
  2394. mutexclpocall : [pocall_internproc];
  2395. mutexclpotype : [];
  2396. mutexclpo : [po_external]
  2397. ),(
  2398. idtok:_LOCAL;
  2399. pd_flags : [pd_implemen,pd_body];
  2400. handler : nil;
  2401. pocall : pocall_none;
  2402. pooption : [po_kylixlocal];
  2403. mutexclpocall : [pocall_internproc,pocall_far16];
  2404. mutexclpotype : [];
  2405. mutexclpo : [po_external,po_exports]
  2406. ),(
  2407. idtok:_MESSAGE;
  2408. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_objcprot,pd_notrecord];
  2409. handler : @pd_message;
  2410. pocall : pocall_none;
  2411. pooption : []; { can be po_msgstr or po_msgint }
  2412. mutexclpocall : [pocall_internproc];
  2413. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2414. mutexclpo : [po_interrupt,po_inline]
  2415. ),(
  2416. idtok:_MWPASCAL;
  2417. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2418. handler : nil;
  2419. pocall : pocall_mwpascal;
  2420. pooption : [];
  2421. mutexclpocall : [];
  2422. mutexclpotype : [];
  2423. mutexclpo : []
  2424. ),(
  2425. idtok:_NEAR;
  2426. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf,pd_notrecord,pd_nothelper];
  2427. handler : @pd_near;
  2428. pocall : pocall_none;
  2429. pooption : [];
  2430. mutexclpocall : [pocall_internproc];
  2431. mutexclpotype : [];
  2432. mutexclpo : []
  2433. ),(
  2434. idtok:_NORETURN;
  2435. pd_flags : [pd_implemen,pd_interface,pd_body,pd_notobjintf];
  2436. handler : nil;
  2437. pocall : pocall_none;
  2438. pooption : [po_noreturn];
  2439. mutexclpocall : [];
  2440. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2441. mutexclpo : [po_interrupt,po_virtualmethod,po_iocheck]
  2442. ),(
  2443. idtok:_NOSTACKFRAME;
  2444. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  2445. handler : nil;
  2446. pocall : pocall_none;
  2447. pooption : [po_nostackframe];
  2448. mutexclpocall : [pocall_internproc];
  2449. mutexclpotype : [];
  2450. mutexclpo : []
  2451. ),(
  2452. idtok:_OVERLOAD;
  2453. pd_flags : [pd_implemen,pd_interface,pd_body,pd_javaclass,pd_intfjava,pd_objcclass,pd_objcprot];
  2454. handler : @pd_overload;
  2455. pocall : pocall_none;
  2456. pooption : [po_overload];
  2457. mutexclpocall : [pocall_internproc];
  2458. mutexclpotype : [];
  2459. mutexclpo : []
  2460. ),(
  2461. idtok:_OVERRIDE;
  2462. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_javaclass,pd_intfjava,pd_notrecord];
  2463. handler : @pd_override;
  2464. pocall : pocall_none;
  2465. pooption : [po_overridingmethod,po_virtualmethod];
  2466. mutexclpocall : [pocall_internproc];
  2467. mutexclpotype : [];
  2468. mutexclpo : [po_exports,po_interrupt,po_virtualmethod,po_inline]
  2469. ),(
  2470. idtok:_PASCAL;
  2471. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2472. handler : nil;
  2473. pocall : pocall_pascal;
  2474. pooption : [];
  2475. mutexclpocall : [];
  2476. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2477. mutexclpo : [po_external]
  2478. ),(
  2479. idtok:_PUBLIC;
  2480. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2481. handler : @pd_public;
  2482. pocall : pocall_none;
  2483. pooption : [po_public,po_global];
  2484. mutexclpocall : [pocall_internproc];
  2485. mutexclpotype : [];
  2486. mutexclpo : [po_external,po_inline]
  2487. ),(
  2488. idtok:_REGISTER;
  2489. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2490. handler : nil;
  2491. pocall : pocall_register;
  2492. pooption : [];
  2493. mutexclpocall : [];
  2494. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2495. mutexclpo : [po_external]
  2496. ),(
  2497. idtok:_REINTRODUCE;
  2498. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_notrecord,pd_javaclass];
  2499. handler : @pd_reintroduce;
  2500. pocall : pocall_none;
  2501. pooption : [po_reintroduce];
  2502. mutexclpocall : [pocall_internproc];
  2503. mutexclpotype : [];
  2504. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod,po_inline]
  2505. ),(
  2506. idtok:_SAFECALL;
  2507. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2508. handler : nil;
  2509. pocall : pocall_safecall;
  2510. pooption : [];
  2511. mutexclpocall : [];
  2512. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2513. mutexclpo : [po_external]
  2514. ),(
  2515. idtok:_SOFTFLOAT;
  2516. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2517. handler : nil;
  2518. pocall : pocall_softfloat;
  2519. pooption : [];
  2520. mutexclpocall : [];
  2521. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2522. { it's available with po_external because the libgcc floating point routines on the arm
  2523. uses this calling convention }
  2524. mutexclpo : []
  2525. ),(
  2526. idtok:_STATIC;
  2527. pd_flags : [pd_interface,pd_implemen,pd_body,pd_object,pd_record,pd_javaclass,pd_notobjintf];
  2528. handler : @pd_static;
  2529. pocall : pocall_none;
  2530. pooption : [po_staticmethod];
  2531. mutexclpocall : [pocall_internproc];
  2532. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2533. mutexclpo : [po_interrupt,po_exports,po_virtualmethod]
  2534. ),(
  2535. idtok:_STDCALL;
  2536. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2537. handler : nil;
  2538. pocall : pocall_stdcall;
  2539. pooption : [];
  2540. mutexclpocall : [];
  2541. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2542. mutexclpo : [po_external]
  2543. ),(
  2544. idtok:_SYSCALL;
  2545. { Different kind of syscalls are valid for AOS68k, AOSPPC and MOS. }
  2546. { FIX ME!!! MorphOS/AOS68k pd_flags should be:
  2547. pd_interface, pd_implemen, pd_notobject, pd_notobjintf (KB) }
  2548. pd_flags : [pd_interface,pd_implemen,pd_procvar];
  2549. handler : @pd_syscall;
  2550. pocall : pocall_syscall;
  2551. pooption : [];
  2552. mutexclpocall : [];
  2553. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2554. mutexclpo : [po_external,po_assembler,po_interrupt,po_exports]
  2555. ),(
  2556. idtok:_VIRTUAL;
  2557. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord,pd_javaclass];
  2558. handler : @pd_virtual;
  2559. pocall : pocall_none;
  2560. pooption : [po_virtualmethod];
  2561. mutexclpocall : [pocall_internproc];
  2562. mutexclpotype : [potype_class_constructor,potype_class_destructor];
  2563. mutexclpo : [po_interrupt,po_exports,po_overridingmethod,po_inline,po_staticmethod]
  2564. ),(
  2565. idtok:_CPPDECL;
  2566. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2567. handler : nil;
  2568. pocall : pocall_cppdecl;
  2569. pooption : [];
  2570. mutexclpocall : [];
  2571. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2572. mutexclpo : [po_assembler,po_external,po_virtualmethod]
  2573. ),(
  2574. idtok:_VARARGS;
  2575. pd_flags : [pd_interface,pd_implemen,pd_procvar,pd_objcclass,pd_objcprot];
  2576. handler : nil;
  2577. pocall : pocall_none;
  2578. pooption : [po_varargs];
  2579. mutexclpocall : [pocall_internproc,pocall_register,
  2580. pocall_far16,pocall_oldfpccall,pocall_mwpascal];
  2581. mutexclpotype : [];
  2582. mutexclpo : [po_assembler,po_interrupt,po_inline]
  2583. ),(
  2584. idtok:_COMPILERPROC;
  2585. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2586. handler : @pd_compilerproc;
  2587. pocall : pocall_none;
  2588. pooption : [po_compilerproc];
  2589. mutexclpocall : [];
  2590. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2591. mutexclpo : [po_interrupt]
  2592. ),(
  2593. idtok:_WEAKEXTERNAL;
  2594. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject,pd_notrecord,pd_nothelper];
  2595. handler : @pd_weakexternal;
  2596. pocall : pocall_none;
  2597. { mark it both external and weak external, so we don't have to
  2598. adapt all code for external symbols to also check for weak external
  2599. }
  2600. pooption : [po_external,po_weakexternal];
  2601. mutexclpocall : [pocall_internproc,pocall_syscall];
  2602. { allowed for external cpp classes }
  2603. mutexclpotype : [{potype_constructor,potype_destructor}potype_class_constructor,potype_class_destructor];
  2604. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  2605. ),(
  2606. idtok:_WINAPI;
  2607. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2608. handler : @pd_winapi;
  2609. pocall : pocall_none;
  2610. pooption : [];
  2611. mutexclpocall : [pocall_stdcall,pocall_cdecl,pocall_mwpascal,pocall_sysv_abi_cdecl,pocall_ms_abi_cdecl];
  2612. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2613. mutexclpo : [po_external]
  2614. ),(
  2615. idtok:_ENUMERATOR;
  2616. pd_flags : [pd_interface,pd_object,pd_record];
  2617. handler : @pd_enumerator;
  2618. pocall : pocall_none;
  2619. pooption : [];
  2620. mutexclpocall : [pocall_internproc];
  2621. mutexclpotype : [];
  2622. mutexclpo : [po_exports,po_interrupt,po_external,po_inline]
  2623. ),(
  2624. idtok:_RTLPROC;
  2625. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2626. handler : nil;
  2627. pocall : pocall_none;
  2628. pooption : [po_rtlproc];
  2629. mutexclpocall : [];
  2630. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2631. mutexclpo : [po_interrupt]
  2632. ),(
  2633. idtok:_HARDFLOAT;
  2634. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2635. handler : @pd_hardfloat;
  2636. pocall : pocall_hardfloat;
  2637. pooption : [];
  2638. mutexclpocall : [];
  2639. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2640. { it's available with po_external because the libgcc floating point routines on the arm
  2641. uses this calling convention }
  2642. mutexclpo : []
  2643. ),(
  2644. idtok:_SYSV_ABI_DEFAULT;
  2645. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2646. handler : nil;
  2647. pocall : pocall_sysv_abi_default;
  2648. pooption : [];
  2649. mutexclpocall : [];
  2650. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2651. mutexclpo : [po_interrupt]
  2652. ),(
  2653. idtok:_SYSV_ABI_CDECL;
  2654. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2655. handler : nil;
  2656. pocall : pocall_sysv_abi_cdecl;
  2657. pooption : [];
  2658. mutexclpocall : [];
  2659. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2660. mutexclpo : [po_interrupt]
  2661. ),(
  2662. idtok:_MS_ABI_DEFAULT;
  2663. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2664. handler : nil;
  2665. pocall : pocall_ms_abi_default;
  2666. pooption : [];
  2667. mutexclpocall : [];
  2668. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2669. mutexclpo : [po_interrupt]
  2670. ),(
  2671. idtok:_MS_ABI_CDECL;
  2672. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2673. handler : nil;
  2674. pocall : pocall_ms_abi_cdecl;
  2675. pooption : [];
  2676. mutexclpocall : [];
  2677. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2678. mutexclpo : [po_interrupt]
  2679. ),(
  2680. idtok:_VECTORCALL;
  2681. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2682. handler : nil;
  2683. pocall : pocall_vectorcall;
  2684. pooption : [];
  2685. mutexclpocall : [];
  2686. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2687. mutexclpo : [po_interrupt]
  2688. )
  2689. );
  2690. function check_proc_directive(isprocvar:boolean):boolean;
  2691. var
  2692. i : longint;
  2693. begin
  2694. result:=false;
  2695. for i:=1 to num_proc_directives do
  2696. if proc_direcdata[i].idtok=idtoken then
  2697. begin
  2698. if ((not isprocvar) or
  2699. (pd_procvar in proc_direcdata[i].pd_flags)) and
  2700. { don't eat a public directive in classes }
  2701. not((idtoken=_PUBLIC) and (symtablestack.top.symtabletype=ObjectSymtable)) then
  2702. result:=true;
  2703. exit;
  2704. end;
  2705. end;
  2706. function find_proc_directive_index(tok: ttoken): longint; inline;
  2707. begin
  2708. result:=-1;
  2709. for result:=1 to num_proc_directives do
  2710. if proc_direcdata[result].idtok=tok then
  2711. exit;
  2712. result:=-1;
  2713. end;
  2714. function parse_proc_direc(pd:tabstractprocdef;var pdflags:tpdflags):boolean;
  2715. {
  2716. Parse the procedure directive, returns true if a correct directive is found
  2717. }
  2718. var
  2719. p : longint;
  2720. name : TIDString;
  2721. begin
  2722. parse_proc_direc:=false;
  2723. name:=tokeninfo^[idtoken].str;
  2724. { Hint directive? Then exit immediatly }
  2725. if (m_hintdirective in current_settings.modeswitches) then
  2726. begin
  2727. case idtoken of
  2728. _LIBRARY,
  2729. _PLATFORM,
  2730. _UNIMPLEMENTED,
  2731. _EXPERIMENTAL,
  2732. _DEPRECATED :
  2733. if (m_delphi in current_settings.modeswitches) and (pd.typ=procdef) then
  2734. begin
  2735. maybe_parse_hint_directives(tprocdef(pd));
  2736. { could the new token still be a directive? }
  2737. if token<>_ID then
  2738. exit;
  2739. name:=tokeninfo^[idtoken].str;
  2740. end
  2741. else
  2742. exit;
  2743. end;
  2744. end;
  2745. { C directive is MacPas only, because it breaks too much existing code
  2746. on other platforms (PFV) }
  2747. if (idtoken=_C) and
  2748. not(m_mac in current_settings.modeswitches) then
  2749. exit;
  2750. { retrieve data for directive if found }
  2751. p:=find_proc_directive_index(idtoken);
  2752. { Check if the procedure directive is known }
  2753. if p=-1 then
  2754. begin
  2755. { parsing a procvar type the name can be any
  2756. next variable !! }
  2757. if ((pdflags * [pd_procvar,pd_object,pd_record,pd_objcclass,pd_objcprot])=[]) and
  2758. not(idtoken in [_PROPERTY,_GENERIC]) then
  2759. Message1(parser_w_unknown_proc_directive_ignored,pattern);
  2760. exit;
  2761. end;
  2762. { check if method and directive not for object, like public.
  2763. This needs to be checked also for procvars }
  2764. if (pd_notobject in proc_direcdata[p].pd_flags) and
  2765. (symtablestack.top.symtabletype=ObjectSymtable) and
  2766. { directive allowed for cpp classes? }
  2767. not((pd_cppobject in proc_direcdata[p].pd_flags) and is_cppclass(tdef(symtablestack.top.defowner))) and
  2768. not((pd_javaclass in proc_direcdata[p].pd_flags) and is_javaclass(tdef(symtablestack.top.defowner))) and
  2769. not((pd_intfjava in proc_direcdata[p].pd_flags) and is_javainterface(tdef(symtablestack.top.defowner))) then
  2770. exit;
  2771. if (pd_notrecord in proc_direcdata[p].pd_flags) and
  2772. (symtablestack.top.symtabletype=recordsymtable) then
  2773. exit;
  2774. { check if method and directive not for java class }
  2775. if not(pd_javaclass in proc_direcdata[p].pd_flags) and
  2776. is_javaclass(tdef(symtablestack.top.defowner)) then
  2777. exit;
  2778. { check if method and directive not for java interface }
  2779. if not(pd_intfjava in proc_direcdata[p].pd_flags) and
  2780. is_javainterface(tdef(symtablestack.top.defowner)) then
  2781. exit;
  2782. { Conflicts between directives ? }
  2783. if (pd.proctypeoption in proc_direcdata[p].mutexclpotype) or
  2784. (pd.proccalloption in proc_direcdata[p].mutexclpocall) or
  2785. ((pd.procoptions*proc_direcdata[p].mutexclpo)<>[]) then
  2786. begin
  2787. Message1(parser_e_proc_dir_conflict,name);
  2788. exit;
  2789. end;
  2790. { set calling convention }
  2791. if proc_direcdata[p].pocall<>pocall_none then
  2792. begin
  2793. if (po_hascallingconvention in pd.procoptions) then
  2794. begin
  2795. Message2(parser_w_proc_overriding_calling,
  2796. proccalloptionStr[pd.proccalloption],
  2797. proccalloptionStr[proc_direcdata[p].pocall]);
  2798. end;
  2799. { check if the target processor supports this calling convention }
  2800. if not(proc_direcdata[p].pocall in supported_calling_conventions) then
  2801. begin
  2802. Message1(parser_e_illegal_calling_convention,proccalloptionStr[proc_direcdata[p].pocall]);
  2803. { recover }
  2804. proc_direcdata[p].pocall:=pocall_stdcall;
  2805. end;
  2806. pd.proccalloption:=proc_direcdata[p].pocall;
  2807. include(pd.procoptions,po_hascallingconvention);
  2808. end;
  2809. if pd.typ=procdef then
  2810. begin
  2811. { Check if the directive is only for objects }
  2812. if (pd_object in proc_direcdata[p].pd_flags) and
  2813. not assigned(tprocdef(pd).struct) then
  2814. exit;
  2815. { Check if the directive is only for records }
  2816. if (pd_record in proc_direcdata[p].pd_flags) and
  2817. not assigned(tprocdef(pd).struct) then
  2818. exit;
  2819. { check if method and directive not for interface }
  2820. if (pd_notobjintf in proc_direcdata[p].pd_flags) and
  2821. is_interface(tprocdef(pd).struct) then
  2822. exit;
  2823. { check if method and directive not for interface }
  2824. if is_dispinterface(tprocdef(pd).struct) and
  2825. not(pd_dispinterface in proc_direcdata[p].pd_flags) then
  2826. exit;
  2827. { check if method and directive not for objcclass }
  2828. if is_objcclass(tprocdef(pd).struct) and
  2829. not(pd_objcclass in proc_direcdata[p].pd_flags) then
  2830. exit;
  2831. { check if method and directive not for objcprotocol }
  2832. if is_objcprotocol(tprocdef(pd).struct) and
  2833. not(pd_objcprot in proc_direcdata[p].pd_flags) then
  2834. exit;
  2835. { check if method and directive not for record/class helper }
  2836. if is_objectpascal_helper(tprocdef(pd).struct) and
  2837. (pd_nothelper in proc_direcdata[p].pd_flags) then
  2838. end;
  2839. { consume directive, and turn flag on }
  2840. consume(token);
  2841. parse_proc_direc:=true;
  2842. { Check the pd_flags if the directive should be allowed }
  2843. if (pd_interface in pdflags) and
  2844. not(pd_interface in proc_direcdata[p].pd_flags) then
  2845. begin
  2846. Message1(parser_e_proc_dir_not_allowed_in_interface,name);
  2847. exit;
  2848. end;
  2849. if (pd_implemen in pdflags) and
  2850. not(pd_implemen in proc_direcdata[p].pd_flags) then
  2851. begin
  2852. Message1(parser_e_proc_dir_not_allowed_in_implementation,name);
  2853. exit;
  2854. end;
  2855. if (pd_procvar in pdflags) and
  2856. not(pd_procvar in proc_direcdata[p].pd_flags) then
  2857. begin
  2858. Message1(parser_e_proc_dir_not_allowed_in_procvar,name);
  2859. exit;
  2860. end;
  2861. { Return the new pd_flags }
  2862. if not(pd_body in proc_direcdata[p].pd_flags) then
  2863. exclude(pdflags,pd_body);
  2864. { Add the correct flag }
  2865. pd.procoptions:=pd.procoptions+proc_direcdata[p].pooption;
  2866. { Call the handler }
  2867. if pointer(proc_direcdata[p].handler)<>nil then
  2868. proc_direcdata[p].handler(pd);
  2869. end;
  2870. function proc_get_importname(pd:tprocdef):string;
  2871. var
  2872. dllname, importname : string;
  2873. begin
  2874. result:='';
  2875. if not(po_external in pd.procoptions) then
  2876. internalerror(200412151);
  2877. { external name or number is specified }
  2878. if assigned(pd.import_name) or (pd.import_nr<>0) then
  2879. begin
  2880. if assigned(pd.import_dll) then
  2881. dllname:=pd.import_dll^
  2882. else
  2883. dllname:='';
  2884. if assigned(pd.import_name) then
  2885. importname:=pd.import_name^
  2886. else
  2887. importname:='';
  2888. proc_get_importname:=make_dllmangledname(dllname,
  2889. importname,pd.import_nr,pd.proccalloption);
  2890. end
  2891. else
  2892. begin
  2893. { Default names when importing variables }
  2894. case pd.proccalloption of
  2895. pocall_cdecl,
  2896. pocall_sysv_abi_cdecl,
  2897. pocall_ms_abi_cdecl:
  2898. begin
  2899. if assigned(pd.struct) then
  2900. result:=target_info.Cprefix+pd.struct.objrealname^+'_'+pd.procsym.realname
  2901. else
  2902. result:=target_info.Cprefix+pd.procsym.realname;
  2903. end;
  2904. pocall_cppdecl :
  2905. begin
  2906. result:=target_info.Cprefix+pd.cplusplusmangledname;
  2907. end;
  2908. else
  2909. begin
  2910. {In MacPas a single "external" has the same effect as "external name 'xxx'" }
  2911. { but according to MacPas mode description
  2912. Cprefix should still be used PM }
  2913. if (m_mac in current_settings.modeswitches) then
  2914. result:=target_info.Cprefix+tprocdef(pd).procsym.realname
  2915. else
  2916. result:=pd.procsym.realname;
  2917. end;
  2918. end;
  2919. end;
  2920. end;
  2921. procedure compilerproc_set_symbol_name(pd: tprocdef);
  2922. begin
  2923. pd.procsym.realname:='$'+lower(pd.procsym.name);
  2924. end;
  2925. procedure proc_set_mangledname(pd:tprocdef);
  2926. var
  2927. s : string;
  2928. begin
  2929. { When the mangledname is already set we aren't allowed to change
  2930. it because it can already be used somewhere (PFV) }
  2931. if not(po_has_mangledname in pd.procoptions) then
  2932. begin
  2933. if (po_external in pd.procoptions) then
  2934. begin
  2935. { External Procedures are only allowed to change the mangledname
  2936. in their first declaration }
  2937. if (pd.forwarddef or (not pd.hasforward)) then
  2938. begin
  2939. s:=proc_get_importname(pd);
  2940. if s<>'' then
  2941. begin
  2942. pd.setmangledname(s);
  2943. end;
  2944. { since this is an external declaration, there won't be an
  2945. implementation that needs to match the original symbol
  2946. again -> immediately convert here }
  2947. if po_compilerproc in pd.procoptions then
  2948. compilerproc_set_symbol_name(pd);
  2949. end
  2950. end
  2951. else
  2952. { Normal procedures }
  2953. begin
  2954. if (po_compilerproc in pd.procoptions) then
  2955. begin
  2956. pd.setmangledname(lower(pd.procsym.name));
  2957. end;
  2958. end;
  2959. end;
  2960. { Public/exported alias names }
  2961. if (([po_public,po_exports]*pd.procoptions)<>[]) and
  2962. not(po_has_public_name in pd.procoptions) then
  2963. begin
  2964. case pd.proccalloption of
  2965. pocall_cdecl,
  2966. pocall_sysv_abi_cdecl,
  2967. pocall_ms_abi_cdecl:
  2968. begin
  2969. if assigned(pd.struct) then
  2970. pd.aliasnames.insert(target_info.Cprefix+pd.struct.objrealname^+'_'+pd.procsym.realname)
  2971. else
  2972. begin
  2973. { Export names are not mangled on Windows and OS/2, see also pexports.pas }
  2974. if (target_info.system in (systems_all_windows+[system_i386_emx, system_i386_os2])) and
  2975. (po_exports in pd.procoptions) then
  2976. pd.aliasnames.insert(pd.procsym.realname)
  2977. else
  2978. pd.aliasnames.insert(target_info.Cprefix+pd.procsym.realname);
  2979. end;
  2980. end;
  2981. pocall_cppdecl :
  2982. begin
  2983. pd.aliasnames.insert(target_info.Cprefix+pd.cplusplusmangledname);
  2984. end;
  2985. end;
  2986. { prevent adding the alias a second time }
  2987. include(pd.procoptions,po_has_public_name);
  2988. end;
  2989. end;
  2990. procedure handle_calling_convention(pd:tabstractprocdef;flags:thccflags=hcc_all);
  2991. begin
  2992. if hcc_check in flags then
  2993. begin
  2994. { set the default calling convention if none provided }
  2995. if (pd.typ=procdef) and
  2996. (is_objc_class_or_protocol(tprocdef(pd).struct) or
  2997. is_cppclass(tprocdef(pd).struct)) then
  2998. begin
  2999. { none of the explicit calling conventions should be allowed }
  3000. if (po_hascallingconvention in pd.procoptions) then
  3001. internalerror(2009032501);
  3002. if is_cppclass(tprocdef(pd).struct) then
  3003. pd.proccalloption:=pocall_cppdecl
  3004. else
  3005. pd.proccalloption:=pocall_cdecl;
  3006. end
  3007. else if not(po_hascallingconvention in pd.procoptions) then
  3008. pd.proccalloption:=current_settings.defproccall
  3009. else
  3010. begin
  3011. if pd.proccalloption=pocall_none then
  3012. internalerror(200309081);
  3013. end;
  3014. { handle proccall specific settings }
  3015. case pd.proccalloption of
  3016. pocall_cdecl,
  3017. pocall_cppdecl,
  3018. pocall_sysv_abi_cdecl,
  3019. pocall_ms_abi_cdecl:
  3020. begin
  3021. { check C cdecl para types }
  3022. check_c_para(pd);
  3023. end;
  3024. pocall_far16 :
  3025. begin
  3026. { Temporary stub, must be rewritten to support OS/2 far16 }
  3027. Message1(parser_w_proc_directive_ignored,'FAR16');
  3028. end;
  3029. end;
  3030. { Inlining is enabled and supported? }
  3031. if (po_inline in pd.procoptions) and
  3032. not(cs_do_inline in current_settings.localswitches) then
  3033. begin
  3034. { Give an error if inline is not supported by the compiler mode,
  3035. otherwise only give a hint that this procedure will not be inlined }
  3036. if not(m_default_inline in current_settings.modeswitches) then
  3037. Message(parser_e_proc_inline_not_supported)
  3038. else
  3039. Message(parser_h_inlining_disabled);
  3040. exclude(pd.procoptions,po_inline);
  3041. end;
  3042. { For varargs directive also cdecl and external must be defined }
  3043. if (po_varargs in pd.procoptions) then
  3044. begin
  3045. { check first for external in the interface, if available there
  3046. then the cdecl must also be there since there is no implementation
  3047. available to contain it }
  3048. if parse_only then
  3049. begin
  3050. { if external is available, then cdecl must also be available,
  3051. procvars don't need external }
  3052. if not((po_external in pd.procoptions) or
  3053. (pd.typ=procvardef) or
  3054. { for objcclasses this is checked later, because the entire
  3055. class may be external. }
  3056. is_objc_class_or_protocol(tprocdef(pd).struct)) and
  3057. not(pd.proccalloption in (cdecl_pocalls + [pocall_stdcall])) then
  3058. Message(parser_e_varargs_need_cdecl_and_external);
  3059. end
  3060. else
  3061. begin
  3062. { both must be defined now }
  3063. if not((po_external in pd.procoptions) or
  3064. (pd.typ=procvardef)) or
  3065. not(pd.proccalloption in (cdecl_pocalls + [pocall_stdcall])) then
  3066. Message(parser_e_varargs_need_cdecl_and_external);
  3067. end;
  3068. end;
  3069. end;
  3070. if hcc_insert_hidden_paras in flags then
  3071. begin
  3072. { insert hidden high parameters }
  3073. pd.parast.SymList.ForEachCall(@insert_hidden_para,pd);
  3074. { insert hidden self parameter }
  3075. insert_self_and_vmt_para(pd);
  3076. { insert funcret parameter if required }
  3077. insert_funcret_para(pd);
  3078. { Make var parameters regable, this must be done after the calling
  3079. convention is set. }
  3080. { this must be done before parentfp is insert, because getting all cases
  3081. where parentfp must be in a memory location isn't catched properly so
  3082. we put parentfp never in a register }
  3083. pd.parast.SymList.ForEachCall(@set_addr_param_regable,pd);
  3084. { insert parentfp parameter if required }
  3085. insert_parentfp_para(pd);
  3086. end;
  3087. { Calculate parameter tlist }
  3088. pd.calcparas;
  3089. end;
  3090. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  3091. {
  3092. Parse the procedure directives. It does not matter if procedure directives
  3093. are written using ;procdir; or ['procdir'] syntax.
  3094. }
  3095. var
  3096. stoprecording,
  3097. res : boolean;
  3098. begin
  3099. if (m_mac in current_settings.modeswitches) and (cs_externally_visible in current_settings.localswitches) then
  3100. begin
  3101. tprocdef(pd).aliasnames.insert(target_info.Cprefix+tprocdef(pd).procsym.realname);
  3102. include(pd.procoptions,po_public);
  3103. include(pd.procoptions,po_has_public_name);
  3104. include(pd.procoptions,po_global);
  3105. end;
  3106. { methods from external class definitions are all external themselves }
  3107. if (pd.typ=procdef) and
  3108. assigned(tprocdef(pd).struct) and
  3109. (tprocdef(pd).struct.typ=objectdef) and
  3110. (oo_is_external in tobjectdef(tprocdef(pd).struct).objectoptions) then
  3111. tprocdef(pd).make_external;
  3112. { Class constructors and destructor are static class methods in real. }
  3113. { There are many places in the compiler where either class or static }
  3114. { method flag changes the behavior. It is simplier to add them to }
  3115. { the class constructors/destructors options than to fix all the }
  3116. { occurencies. (Paul) }
  3117. if pd.proctypeoption in [potype_class_constructor,potype_class_destructor] then
  3118. begin
  3119. include(pd.procoptions,po_classmethod);
  3120. include(pd.procoptions,po_staticmethod);
  3121. end;
  3122. { for a generic routine we also need to record the procedure }
  3123. { directives, but only if we aren't already recording for a }
  3124. { surrounding generic }
  3125. if pd.is_generic and (pd.typ=procdef) and not current_scanner.is_recording_tokens then
  3126. begin
  3127. current_scanner.startrecordtokens(tprocdef(pd).genericdecltokenbuf);
  3128. stoprecording:=true;
  3129. end
  3130. else
  3131. stoprecording:=false;
  3132. while token in [_ID,_LECKKLAMMER] do
  3133. begin
  3134. if try_to_consume(_LECKKLAMMER) then
  3135. begin
  3136. repeat
  3137. parse_proc_direc(pd,pdflags);
  3138. until not try_to_consume(_COMMA);
  3139. consume(_RECKKLAMMER);
  3140. { we always expect at least '[];' }
  3141. res:=true;
  3142. end
  3143. else
  3144. begin
  3145. res:=parse_proc_direc(pd,pdflags);
  3146. end;
  3147. { A procedure directive normally followed by a semicolon, but in
  3148. a const section or reading a type we should stop when _EQ is found,
  3149. because a constant/default value follows }
  3150. if res then
  3151. begin
  3152. if (block_type=bt_const_type) and
  3153. (token=_EQ) then
  3154. break;
  3155. { support procedure proc;stdcall export; }
  3156. if not(check_proc_directive((pd.typ=procvardef))) then
  3157. begin
  3158. { support "record p : procedure stdcall end;" and
  3159. "var p : procedure stdcall = nil;" }
  3160. if (pd_procvar in pdflags) and
  3161. (token in [_END,_RKLAMMER,_EQ]) then
  3162. break
  3163. else
  3164. begin
  3165. if (token=_COLON) then
  3166. begin
  3167. Message(parser_e_field_not_allowed_here);
  3168. consume_all_until(_SEMICOLON);
  3169. end;
  3170. consume(_SEMICOLON)
  3171. end;
  3172. end;
  3173. end
  3174. else
  3175. break;
  3176. end;
  3177. if stoprecording then
  3178. current_scanner.stoprecordtokens;
  3179. { nostackframe requires assembler, but assembler
  3180. may be specified in the implementation part only,
  3181. and in not required if the function is first forward declared
  3182. if it is a procdef that has forwardef set to true
  3183. we postpone the possible error message to the real implementation
  3184. parse_only does not need to be considered as po_nostackframe
  3185. is an implementation only directive }
  3186. if (po_nostackframe in pd.procoptions) and
  3187. not (po_assembler in pd.procoptions) and
  3188. ((pd.typ<>procdef) or not tprocdef(pd).forwarddef) then
  3189. message(parser_e_nostackframe_without_assembler);
  3190. end;
  3191. procedure parse_var_proc_directives(sym:tsym);
  3192. var
  3193. pdflags : tpdflags;
  3194. pd : tabstractprocdef;
  3195. begin
  3196. pdflags:=[pd_procvar];
  3197. pd:=nil;
  3198. case sym.typ of
  3199. fieldvarsym,
  3200. staticvarsym,
  3201. localvarsym,
  3202. paravarsym :
  3203. pd:=tabstractprocdef(tabstractvarsym(sym).vardef);
  3204. typesym :
  3205. pd:=tabstractprocdef(ttypesym(sym).typedef);
  3206. else
  3207. internalerror(2003042617);
  3208. end;
  3209. if pd.typ<>procvardef then
  3210. internalerror(2003042618);
  3211. { names should never be used anyway }
  3212. parse_proc_directives(pd,pdflags);
  3213. end;
  3214. procedure parse_object_proc_directives(pd:tabstractprocdef);
  3215. var
  3216. pdflags : tpdflags;
  3217. begin
  3218. pdflags:=[pd_object];
  3219. parse_proc_directives(pd,pdflags);
  3220. end;
  3221. procedure parse_record_proc_directives(pd:tabstractprocdef);
  3222. var
  3223. pdflags : tpdflags;
  3224. begin
  3225. pdflags:=[pd_record];
  3226. parse_proc_directives(pd,pdflags);
  3227. end;
  3228. function proc_add_definition(var currpd:tprocdef):boolean;
  3229. function equal_generic_procdefs(fwpd,currpd:tprocdef):boolean;
  3230. var
  3231. i : longint;
  3232. fwtype,
  3233. currtype : ttypesym;
  3234. foundretdef : boolean;
  3235. begin
  3236. result:=false;
  3237. if fwpd.genericparas.count<>currpd.genericparas.count then
  3238. exit;
  3239. { comparing generic declarations is a bit more cumbersome as the
  3240. defs of the generic parameter types are not equal, especially if the
  3241. declaration contains constraints; essentially we have two cases:
  3242. - proc declared in interface of unit (or in class/record/object)
  3243. and defined in implementation; here the fwpd might contain
  3244. constraints while currpd must only contain undefineddefs
  3245. - forward declaration in implementation }
  3246. foundretdef:=false;
  3247. for i:=0 to fwpd.genericparas.count-1 do
  3248. begin
  3249. fwtype:=ttypesym(fwpd.genericparas[i]);
  3250. currtype:=ttypesym(currpd.genericparas[i]);
  3251. { if the type in the currpd isn't a pure undefineddef, then we can
  3252. stop right there }
  3253. if (currtype.typedef.typ<>undefineddef) or (df_genconstraint in currtype.typedef.defoptions) then
  3254. exit;
  3255. if not foundretdef then
  3256. begin
  3257. { if the returndef is the same as this parameter's def then this
  3258. needs to be the case for both procdefs }
  3259. foundretdef:=fwpd.returndef=fwtype.typedef;
  3260. if foundretdef xor (currpd.returndef=currtype.typedef) then
  3261. exit;
  3262. end;
  3263. end;
  3264. if compare_paras(fwpd.paras,currpd.paras,cp_none,[cpo_ignorehidden,cpo_openequalisexact,cpo_ignoreuniv,cpo_generic])<>te_exact then
  3265. exit;
  3266. if not foundretdef then
  3267. begin
  3268. if (df_specialization in tstoreddef(fwpd.returndef).defoptions) and (df_specialization in tstoreddef(currpd.returndef).defoptions) then
  3269. { for specializations we're happy with equal defs instead of exactly the same defs }
  3270. result:=equal_defs(fwpd.returndef,currpd.returndef)
  3271. else
  3272. { the returndef isn't a type parameter, so compare as usual }
  3273. result:=compare_defs(fwpd.returndef,currpd.returndef,nothingn)=te_exact;
  3274. end
  3275. else
  3276. result:=true;
  3277. end;
  3278. {
  3279. Add definition aprocdef to the overloaded definitions of aprocsym. If a
  3280. forwarddef is found and reused it returns true
  3281. }
  3282. var
  3283. fwpd : tprocdef;
  3284. currparasym,
  3285. fwparasym : tsym;
  3286. currparacnt,
  3287. fwparacnt,
  3288. curridx,
  3289. fwidx,
  3290. virtualdirinfo,
  3291. i : longint;
  3292. po_comp : tprocoptions;
  3293. paracompopt: tcompare_paras_options;
  3294. forwardfound : boolean;
  3295. symentry: TSymEntry;
  3296. item : tlinkedlistitem;
  3297. begin
  3298. virtualdirinfo:=-1;
  3299. forwardfound:=false;
  3300. { check overloaded functions if the same function already exists }
  3301. for i:=0 to tprocsym(currpd.procsym).ProcdefList.Count-1 do
  3302. begin
  3303. fwpd:=tprocdef(tprocsym(currpd.procsym).ProcdefList[i]);
  3304. { can happen for internally generated routines }
  3305. if (fwpd=currpd) then
  3306. begin
  3307. result:=true;
  3308. exit;
  3309. end;
  3310. { Skip overloaded definitions that are declared in other units }
  3311. if fwpd.procsym<>currpd.procsym then
  3312. continue;
  3313. { check the parameters, for delphi/tp it is possible to
  3314. leave the parameters away in the implementation (forwarddef=false).
  3315. But for an overload declared function this is not allowed }
  3316. if { check if empty implementation arguments match is allowed }
  3317. (
  3318. not(m_repeat_forward in current_settings.modeswitches) and
  3319. not(currpd.forwarddef) and
  3320. is_bareprocdef(currpd) and
  3321. not(po_overload in fwpd.procoptions)
  3322. ) or
  3323. (
  3324. fwpd.is_generic and
  3325. currpd.is_generic and
  3326. equal_generic_procdefs(fwpd,currpd)
  3327. ) or
  3328. { check arguments, we need to check only the user visible parameters. The hidden parameters
  3329. can be in a different location because of the calling convention, eg. L-R vs. R-L order (PFV)
  3330. don't check default values here, because routines that are the same except for their default
  3331. values should be reported as mismatches (since you can't overload based on different default
  3332. parameter values) }
  3333. (
  3334. (compare_paras(fwpd.paras,currpd.paras,cp_none,[cpo_ignorehidden,cpo_openequalisexact,cpo_ignoreuniv])=te_exact) and
  3335. (compare_defs(fwpd.returndef,currpd.returndef,nothingn)=te_exact)
  3336. ) then
  3337. begin
  3338. { Check if we've found the forwarddef, if found then
  3339. we need to update the forward def with the current
  3340. implementation settings }
  3341. if fwpd.forwarddef then
  3342. begin
  3343. forwardfound:=true;
  3344. if not(m_repeat_forward in current_settings.modeswitches) and
  3345. (fwpd.proccalloption<>currpd.proccalloption) then
  3346. paracompopt:=[cpo_ignorehidden,cpo_comparedefaultvalue,cpo_openequalisexact,cpo_ignoreuniv]
  3347. else
  3348. paracompopt:=[cpo_comparedefaultvalue,cpo_openequalisexact,cpo_ignoreuniv];
  3349. { Check calling convention }
  3350. if (fwpd.proccalloption<>currpd.proccalloption) then
  3351. begin
  3352. { In delphi it is possible to specify the calling
  3353. convention in the interface or implementation if
  3354. there was no convention specified in the other
  3355. part }
  3356. if (m_delphi in current_settings.modeswitches) then
  3357. begin
  3358. if not(po_hascallingconvention in currpd.procoptions) then
  3359. currpd.proccalloption:=fwpd.proccalloption
  3360. else
  3361. if not(po_hascallingconvention in fwpd.procoptions) then
  3362. fwpd.proccalloption:=currpd.proccalloption
  3363. else
  3364. begin
  3365. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  3366. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  3367. { restore interface settings }
  3368. currpd.proccalloption:=fwpd.proccalloption;
  3369. end;
  3370. end
  3371. else
  3372. begin
  3373. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  3374. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  3375. { restore interface settings }
  3376. currpd.proccalloption:=fwpd.proccalloption;
  3377. end;
  3378. end;
  3379. { Check static }
  3380. if (po_staticmethod in fwpd.procoptions) then
  3381. begin
  3382. if not (po_staticmethod in currpd.procoptions) then
  3383. begin
  3384. include(currpd.procoptions, po_staticmethod);
  3385. if (po_classmethod in currpd.procoptions) then
  3386. begin
  3387. { remove self from the hidden paras }
  3388. symentry:=currpd.parast.Find('self');
  3389. if symentry<>nil then
  3390. begin
  3391. currpd.parast.Delete(symentry);
  3392. currpd.calcparas;
  3393. end;
  3394. end;
  3395. end;
  3396. end;
  3397. { Check if the procedure type and return type are correct,
  3398. also the parameters must match also with the type and that
  3399. if the implementation has default parameters, the interface
  3400. also has them and that if they both have them, that they
  3401. have the same value }
  3402. if ((m_repeat_forward in current_settings.modeswitches) or
  3403. not is_bareprocdef(currpd)) and
  3404. (
  3405. (
  3406. fwpd.is_generic and
  3407. currpd.is_generic and
  3408. not equal_generic_procdefs(fwpd,currpd)
  3409. ) or
  3410. (
  3411. (
  3412. not fwpd.is_generic or
  3413. not currpd.is_generic
  3414. ) and
  3415. (
  3416. (compare_paras(fwpd.paras,currpd.paras,cp_all,paracompopt)<>te_exact) or
  3417. (compare_defs(fwpd.returndef,currpd.returndef,nothingn)<>te_exact)
  3418. )
  3419. )
  3420. ) then
  3421. begin
  3422. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  3423. fwpd.fullprocname(false));
  3424. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  3425. break;
  3426. end;
  3427. { Check if both are declared forward }
  3428. if fwpd.forwarddef and currpd.forwarddef then
  3429. begin
  3430. MessagePos1(currpd.fileinfo,parser_e_function_already_declared_public_forward,
  3431. currpd.fullprocname(false));
  3432. end;
  3433. { internconst or internproc only need to be defined once }
  3434. if (fwpd.proccalloption=pocall_internproc) then
  3435. currpd.proccalloption:=fwpd.proccalloption
  3436. else
  3437. if (currpd.proccalloption=pocall_internproc) then
  3438. fwpd.proccalloption:=currpd.proccalloption;
  3439. { Check procedure options, Delphi requires that class is
  3440. repeated in the implementation for class methods }
  3441. if (m_fpc in current_settings.modeswitches) then
  3442. po_comp:=[po_classmethod,po_varargs,po_methodpointer,po_interrupt]
  3443. else
  3444. po_comp:=[po_classmethod,po_methodpointer];
  3445. if ((po_comp * fwpd.procoptions)<>(po_comp * currpd.procoptions)) or
  3446. (fwpd.proctypeoption <> currpd.proctypeoption) or
  3447. { if the implementation version has an "overload" modifier,
  3448. the interface version must also have it (otherwise we can
  3449. get annoying crashes due to interface crc changes) }
  3450. (not(po_overload in fwpd.procoptions) and
  3451. (po_overload in currpd.procoptions)) then
  3452. begin
  3453. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  3454. fwpd.fullprocname(false));
  3455. tprocsym(fwpd.procsym).write_parameter_lists(fwpd);
  3456. { This error is non-fatal, we can recover }
  3457. end;
  3458. { Forward declaration is external? }
  3459. if (po_external in fwpd.procoptions) then
  3460. MessagePos(currpd.fileinfo,parser_e_proc_already_external);
  3461. { check for conflicts with "virtual" if this is a virtual
  3462. method, as "virtual" cannot be repeated in the
  3463. implementation and hence does not get checked against }
  3464. if (po_virtualmethod in fwpd.procoptions) then
  3465. begin
  3466. if virtualdirinfo=-1 then
  3467. begin
  3468. virtualdirinfo:=find_proc_directive_index(_VIRTUAL);
  3469. if virtualdirinfo=-1 then
  3470. internalerror(2018010101);
  3471. end;
  3472. if (proc_direcdata[virtualdirinfo].mutexclpo * currpd.procoptions)<>[] then
  3473. MessagePos1(currpd.fileinfo,parser_e_proc_dir_conflict,tokeninfo^[_VIRTUAL].str);
  3474. end;
  3475. { Check parameters }
  3476. if (m_repeat_forward in current_settings.modeswitches) or
  3477. (currpd.minparacount>0) then
  3478. begin
  3479. { If mangled names are equal then they have the same amount of arguments }
  3480. { We can check the names of the arguments }
  3481. { both symtables are in the same order from left to right }
  3482. curridx:=0;
  3483. fwidx:=0;
  3484. currparacnt:=currpd.parast.SymList.Count;
  3485. fwparacnt:=fwpd.parast.SymList.Count;
  3486. repeat
  3487. { skip default parameter constsyms }
  3488. while (curridx<currparacnt) and
  3489. (tsym(currpd.parast.SymList[curridx]).typ<>paravarsym) do
  3490. inc(curridx);
  3491. while (fwidx<fwparacnt) and
  3492. (tsym(fwpd.parast.SymList[fwidx]).typ<>paravarsym) do
  3493. inc(fwidx);
  3494. { stop when one of the two lists is at the end }
  3495. if (fwidx>=fwparacnt) or (curridx>=currparacnt) then
  3496. break;
  3497. { compare names of parameters, ignore implictly
  3498. renamed parameters }
  3499. currparasym:=tsym(currpd.parast.SymList[curridx]);
  3500. fwparasym:=tsym(fwpd.parast.SymList[fwidx]);
  3501. if not(sp_implicitrename in currparasym.symoptions) and
  3502. not(sp_implicitrename in fwparasym.symoptions) then
  3503. begin
  3504. if (currparasym.name<>fwparasym.name) then
  3505. begin
  3506. MessagePos3(currpd.fileinfo,parser_e_header_different_var_names,
  3507. tprocsym(currpd.procsym).realname,fwparasym.realname,currparasym.realname);
  3508. break;
  3509. end;
  3510. end;
  3511. { next parameter }
  3512. inc(curridx);
  3513. inc(fwidx);
  3514. until false;
  3515. end;
  3516. { Everything is checked, now we can update the forward declaration
  3517. with the new data from the implementation }
  3518. fwpd.forwarddef:=currpd.forwarddef;
  3519. fwpd.hasforward:=true;
  3520. fwpd.procoptions:=fwpd.procoptions+currpd.procoptions;
  3521. { marked as local but exported from unit? }
  3522. if (po_kylixlocal in fwpd.procoptions) and (fwpd.owner.symtabletype=globalsymtable) then
  3523. MessagePos(fwpd.fileinfo,type_e_cant_export_local);
  3524. if fwpd.extnumber=$ffff then
  3525. fwpd.extnumber:=currpd.extnumber;
  3526. while not currpd.aliasnames.empty do
  3527. fwpd.aliasnames.insert(currpd.aliasnames.getfirst);
  3528. { update fileinfo so position references the implementation,
  3529. also update funcretsym if it is already generated }
  3530. fwpd.fileinfo:=currpd.fileinfo;
  3531. if assigned(fwpd.funcretsym) then
  3532. fwpd.funcretsym.fileinfo:=currpd.fileinfo;
  3533. if assigned(currpd.deprecatedmsg) then
  3534. begin
  3535. stringdispose(fwpd.deprecatedmsg);
  3536. fwpd.deprecatedmsg:=stringdup(currpd.deprecatedmsg^);
  3537. end;
  3538. { import names }
  3539. if assigned(currpd.import_dll) then
  3540. begin
  3541. stringdispose(fwpd.import_dll);
  3542. fwpd.import_dll:=stringdup(currpd.import_dll^);
  3543. end;
  3544. if assigned(currpd.import_name) then
  3545. begin
  3546. stringdispose(fwpd.import_name);
  3547. fwpd.import_name:=stringdup(currpd.import_name^);
  3548. end;
  3549. fwpd.import_nr:=currpd.import_nr;
  3550. { for compilerproc defines we need to rename and update the
  3551. symbolname to lowercase so users can' access it (can't do
  3552. it immediately, because then the implementation symbol
  3553. won't be matched) }
  3554. if po_compilerproc in fwpd.procoptions then
  3555. begin
  3556. compilerproc_set_symbol_name(fwpd);
  3557. current_module.add_public_asmsym(fwpd.procsym.realname,AB_GLOBAL,AT_FUNCTION);
  3558. end;
  3559. if po_public in fwpd.procoptions then
  3560. begin
  3561. item:=fwpd.aliasnames.first;
  3562. while assigned(item) do
  3563. begin
  3564. current_module.add_public_asmsym(TCmdStrListItem(item).str,AB_GLOBAL,AT_FUNCTION);
  3565. item:=item.next;
  3566. end;
  3567. end;
  3568. { Release current procdef }
  3569. currpd.owner.deletedef(currpd);
  3570. currpd:=fwpd;
  3571. end
  3572. else
  3573. begin
  3574. { abstract methods aren't forward defined, but this }
  3575. { needs another error message }
  3576. if (po_abstractmethod in fwpd.procoptions) then
  3577. MessagePos(currpd.fileinfo,parser_e_abstract_no_definition)
  3578. else
  3579. begin
  3580. MessagePos(currpd.fileinfo,parser_e_overloaded_have_same_parameters);
  3581. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  3582. end;
  3583. end;
  3584. { we found one proc with the same arguments, there are no others
  3585. so we can stop }
  3586. break;
  3587. end;
  3588. { check for allowing overload directive }
  3589. if not(m_fpc in current_settings.modeswitches) then
  3590. begin
  3591. { overload directive turns on overloading }
  3592. if ((po_overload in currpd.procoptions) or
  3593. (po_overload in fwpd.procoptions)) then
  3594. begin
  3595. { check if all procs have overloading, but not if the proc is a method or
  3596. already declared forward, then the check is already done }
  3597. if not(fwpd.hasforward or
  3598. assigned(currpd.struct) or
  3599. (currpd.forwarddef<>fwpd.forwarddef) or
  3600. ((po_overload in currpd.procoptions) and
  3601. (po_overload in fwpd.procoptions))) then
  3602. begin
  3603. MessagePos1(currpd.fileinfo,parser_e_no_overload_for_all_procs,currpd.procsym.realname);
  3604. break;
  3605. end
  3606. end
  3607. else
  3608. begin
  3609. if not(fwpd.forwarddef) then
  3610. begin
  3611. if (m_tp7 in current_settings.modeswitches) then
  3612. MessagePos(currpd.fileinfo,parser_e_procedure_overloading_is_off)
  3613. else
  3614. MessagePos1(currpd.fileinfo,parser_e_no_overload_for_all_procs,currpd.procsym.realname);
  3615. break;
  3616. end;
  3617. end;
  3618. end; { equal arguments }
  3619. end;
  3620. { if we didn't reuse a forwarddef then we add the procdef to the overloaded
  3621. list }
  3622. if not forwardfound then
  3623. begin
  3624. { can happen in Delphi mode }
  3625. if (currpd.proctypeoption = potype_function) and
  3626. is_void(currpd.returndef) then
  3627. MessagePos1(currpd.fileinfo,parser_e_no_funcret_specified,currpd.procsym.realname);
  3628. tprocsym(currpd.procsym).ProcdefList.Add(currpd);
  3629. if not currpd.forwarddef and (po_public in currpd.procoptions) then
  3630. begin
  3631. item:=currpd.aliasnames.first;
  3632. while assigned(item) do
  3633. begin
  3634. current_module.add_public_asmsym(TCmdStrListItem(item).str,AB_GLOBAL,AT_FUNCTION);
  3635. item:=item.next;
  3636. end;
  3637. end;
  3638. end;
  3639. proc_add_definition:=forwardfound;
  3640. end;
  3641. end.