pdecsub.pas 151 KB

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