pdecsub.pas 148 KB

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