pdecsub.pas 142 KB

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