pdecsub.pas 115 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083
  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. tokens,symconst,symtype,symdef,symsym;
  22. type
  23. tpdflag=(
  24. pd_body, { directive needs a body }
  25. pd_implemen, { directive can be used implementation section }
  26. pd_interface, { directive can be used interface section }
  27. pd_object, { directive can be used object declaration }
  28. pd_procvar, { directive can be used procvar declaration }
  29. pd_notobject, { directive can not be used object declaration }
  30. pd_notobjintf, { directive can not be used interface declaration }
  31. pd_notprocvar, { directive can not be used procvar declaration }
  32. pd_dispinterface,{ directive can be used with dispinterface methods }
  33. pd_cppobject, { directive can be used with cppclass }
  34. pd_objcclass, { directive can be used with objcclass }
  35. pd_objcprot { directive can be used with objcprotocol }
  36. );
  37. tpdflags=set of tpdflag;
  38. function check_proc_directive(isprocvar:boolean):boolean;
  39. procedure insert_funcret_local(pd:tprocdef);
  40. function proc_add_definition(var currpd:tprocdef):boolean;
  41. function proc_get_importname(pd:tprocdef):string;
  42. procedure proc_set_mangledname(pd:tprocdef);
  43. procedure handle_calling_convention(pd:tabstractprocdef);
  44. procedure parse_parameter_dec(pd:tabstractprocdef);
  45. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  46. procedure parse_var_proc_directives(sym:tsym);
  47. procedure parse_object_proc_directives(pd:tabstractprocdef);
  48. function parse_proc_head(aclass:tobjectdef;potype:tproctypeoption;var pd:tprocdef):boolean;
  49. function parse_proc_dec(isclassmethod:boolean; aclass:tobjectdef):tprocdef;
  50. implementation
  51. uses
  52. SysUtils,
  53. { common }
  54. cutils,cclasses,
  55. { global }
  56. globtype,globals,verbose,constexp,
  57. systems,
  58. cpuinfo,
  59. { symtable }
  60. symbase,symtable,defutil,defcmp,paramgr,cpupara,
  61. { pass 1 }
  62. fmodule,node,htypechk,
  63. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,
  64. objcutil,
  65. { parser }
  66. scanner,
  67. pbase,pexpr,ptype,pdecl
  68. ;
  69. const
  70. { Please leave this here, this module should NOT use
  71. these variables.
  72. Declaring it as string here results in an error when compiling (PFV) }
  73. current_procinfo = 'error';
  74. procedure insert_funcret_para(pd:tabstractprocdef);
  75. var
  76. storepos : tfileposinfo;
  77. vs : tparavarsym;
  78. paranr : word;
  79. begin
  80. if not(pd.proctypeoption in [potype_constructor,potype_destructor]) and
  81. not is_void(pd.returndef) and
  82. paramanager.ret_in_param(pd.returndef,pd.proccalloption) then
  83. begin
  84. storepos:=current_tokenpos;
  85. if pd.typ=procdef then
  86. current_tokenpos:=tprocdef(pd).fileinfo;
  87. {$if defined(i386)}
  88. { For left to right add it at the end to be delphi compatible.
  89. In the case of safecalls with safecal-exceptions support the
  90. funcret-para is (from the 'c'-point of view) a normal parameter
  91. which has to be added to the end of the parameter-list }
  92. if (pd.proccalloption in (pushleftright_pocalls)) or
  93. ((tf_safecall_exceptions in target_info.flags) and
  94. (pd.proccalloption=pocall_safecall)) then
  95. paranr:=paranr_result_leftright
  96. else
  97. {$elseif defined(x86) or defined(arm)}
  98. if (target_info.system in systems_all_windows) and
  99. (pd.proccalloption = pocall_safecall) then
  100. paranr:=paranr_result_leftright
  101. else
  102. {$endif}
  103. paranr:=paranr_result;
  104. { Generate result variable accessing function result }
  105. vs:=tparavarsym.create('$result',paranr,vs_var,pd.returndef,[vo_is_funcret,vo_is_hidden_para]);
  106. pd.parast.insert(vs);
  107. { Store the this symbol as funcretsym for procedures }
  108. if pd.typ=procdef then
  109. tprocdef(pd).funcretsym:=vs;
  110. current_tokenpos:=storepos;
  111. end;
  112. end;
  113. procedure insert_parentfp_para(pd:tabstractprocdef);
  114. var
  115. storepos : tfileposinfo;
  116. vs : tparavarsym;
  117. paranr : longint;
  118. begin
  119. if pd.parast.symtablelevel>normal_function_level then
  120. begin
  121. storepos:=current_tokenpos;
  122. if pd.typ=procdef then
  123. current_tokenpos:=tprocdef(pd).fileinfo;
  124. { if no support for nested procvars is activated, use the old
  125. calling convention to pass the parent frame pointer for backwards
  126. compatibility }
  127. if not(m_nested_procvars in current_settings.modeswitches) then
  128. paranr:=paranr_parentfp
  129. { nested procvars require Delphi-style parentfp passing, see
  130. po_delphi_nested_cc declaration for more info }
  131. {$ifdef i386}
  132. else if (pd.proccalloption in pushleftright_pocalls) then
  133. paranr:=paranr_parentfp_delphi_cc_leftright
  134. {$endif i386}
  135. else
  136. paranr:=paranr_parentfp_delphi_cc;
  137. { Generate result variable accessing function result, it
  138. can't be put in a register since it must be accessable
  139. from the framepointer }
  140. vs:=tparavarsym.create('$parentfp',paranr,vs_value
  141. ,voidpointertype,[vo_is_parentfp,vo_is_hidden_para]);
  142. vs.varregable:=vr_none;
  143. pd.parast.insert(vs);
  144. current_tokenpos:=storepos;
  145. end;
  146. end;
  147. procedure insert_self_and_vmt_para(pd:tabstractprocdef);
  148. var
  149. storepos : tfileposinfo;
  150. vs : tparavarsym;
  151. hdef : tdef;
  152. vsp : tvarspez;
  153. aliasvs : tabsolutevarsym;
  154. sl : tpropaccesslist;
  155. begin
  156. if (pd.typ=procdef) and
  157. is_objc_class_or_protocol(tprocdef(pd)._class) and
  158. (pd.parast.symtablelevel=normal_function_level) then
  159. begin
  160. { insert Objective-C self and selector parameters }
  161. vs:=tparavarsym.create('$_cmd',paranr_objc_cmd,vs_value,objc_seltype,[vo_is_msgsel,vo_is_hidden_para]);
  162. pd.parast.insert(vs);
  163. { make accessible to code }
  164. sl:=tpropaccesslist.create;
  165. sl.addsym(sl_load,vs);
  166. aliasvs:=tabsolutevarsym.create_ref('_CMD',objc_seltype,sl);
  167. include(aliasvs.varoptions,vo_is_msgsel);
  168. tlocalsymtable(tprocdef(pd).localst).insert(aliasvs);
  169. if (po_classmethod in pd.procoptions) then
  170. { compatible with what gcc does }
  171. hdef:=objc_idtype
  172. else
  173. hdef:=tprocdef(pd)._class;
  174. vs:=tparavarsym.create('$self',paranr_objc_self,vs_value,hdef,[vo_is_self,vo_is_hidden_para]);
  175. pd.parast.insert(vs);
  176. end
  177. else if (pd.typ=procvardef) and
  178. pd.is_methodpointer then
  179. begin
  180. { Generate self variable }
  181. vs:=tparavarsym.create('$self',paranr_self,vs_value,voidpointertype,[vo_is_self,vo_is_hidden_para]);
  182. pd.parast.insert(vs);
  183. end
  184. else
  185. begin
  186. if (pd.typ=procdef) and
  187. assigned(tprocdef(pd)._class) and
  188. (pd.parast.symtablelevel=normal_function_level) then
  189. begin
  190. { static class methods have no hidden self/vmt pointer }
  191. if pd.no_self_node then
  192. exit;
  193. storepos:=current_tokenpos;
  194. current_tokenpos:=tprocdef(pd).fileinfo;
  195. { Generate VMT variable for constructor/destructor }
  196. if (pd.proctypeoption in [potype_constructor,potype_destructor]) and not(is_cppclass(tprocdef(pd)._class)) then
  197. begin
  198. { can't use classrefdef as type because inheriting
  199. will then always file because of a type mismatch }
  200. vs:=tparavarsym.create('$vmt',paranr_vmt,vs_value,voidpointertype,[vo_is_vmt,vo_is_hidden_para]);
  201. pd.parast.insert(vs);
  202. end;
  203. { Generate self variable, for classes we need
  204. to use the generic voidpointer to be compatible with
  205. methodpointers }
  206. vsp:=vs_value;
  207. if (po_staticmethod in pd.procoptions) or
  208. (po_classmethod in pd.procoptions) then
  209. hdef:=tclassrefdef.create(tprocdef(pd)._class)
  210. else
  211. begin
  212. if is_object(tprocdef(pd)._class) then
  213. vsp:=vs_var;
  214. hdef:=tprocdef(pd)._class;
  215. end;
  216. vs:=tparavarsym.create('$self',paranr_self,vsp,hdef,[vo_is_self,vo_is_hidden_para]);
  217. pd.parast.insert(vs);
  218. current_tokenpos:=storepos;
  219. end;
  220. end;
  221. end;
  222. procedure insert_funcret_local(pd:tprocdef);
  223. var
  224. storepos : tfileposinfo;
  225. vs : tlocalvarsym;
  226. aliasvs : tabsolutevarsym;
  227. sl : tpropaccesslist;
  228. hs : string;
  229. begin
  230. { The result from constructors and destructors can't be accessed directly }
  231. if not(pd.proctypeoption in [potype_constructor,potype_destructor]) and
  232. not is_void(pd.returndef) then
  233. begin
  234. storepos:=current_tokenpos;
  235. current_tokenpos:=pd.fileinfo;
  236. { We need to insert a varsym for the result in the localst
  237. when it is returning in a register }
  238. if not paramanager.ret_in_param(pd.returndef,pd.proccalloption) then
  239. begin
  240. vs:=tlocalvarsym.create('$result',vs_value,pd.returndef,[vo_is_funcret]);
  241. pd.localst.insert(vs);
  242. pd.funcretsym:=vs;
  243. end;
  244. { insert the name of the procedure as alias for the function result,
  245. we can't use realname because that will not work for compilerprocs
  246. as the name is lowercase and unreachable from the code }
  247. if assigned(pd.resultname) then
  248. hs:=pd.resultname^
  249. else
  250. hs:=pd.procsym.name;
  251. sl:=tpropaccesslist.create;
  252. sl.addsym(sl_load,pd.funcretsym);
  253. aliasvs:=tabsolutevarsym.create_ref(hs,pd.returndef,sl);
  254. include(aliasvs.varoptions,vo_is_funcret);
  255. tlocalsymtable(pd.localst).insert(aliasvs);
  256. { insert result also if support is on }
  257. if (m_result in current_settings.modeswitches) then
  258. begin
  259. sl:=tpropaccesslist.create;
  260. sl.addsym(sl_load,pd.funcretsym);
  261. aliasvs:=tabsolutevarsym.create_ref('RESULT',pd.returndef,sl);
  262. include(aliasvs.varoptions,vo_is_funcret);
  263. include(aliasvs.varoptions,vo_is_result);
  264. tlocalsymtable(pd.localst).insert(aliasvs);
  265. end;
  266. current_tokenpos:=storepos;
  267. end;
  268. end;
  269. procedure insert_hidden_para(p:TObject;arg:pointer);
  270. var
  271. hvs : tparavarsym;
  272. pd : tabstractprocdef absolute arg;
  273. begin
  274. if (tsym(p).typ<>paravarsym) then
  275. exit;
  276. with tparavarsym(p) do
  277. begin
  278. { We need a local copy for a value parameter when only the
  279. address is pushed. Open arrays and Array of Const are
  280. an exception because they are allocated at runtime and the
  281. address that is pushed is patched }
  282. if (varspez=vs_value) and
  283. paramanager.push_addr_param(varspez,vardef,pd.proccalloption) and
  284. not(is_open_array(vardef) or
  285. is_array_of_const(vardef)) then
  286. include(varoptions,vo_has_local_copy);
  287. { needs high parameter ? }
  288. if paramanager.push_high_param(varspez,vardef,pd.proccalloption) then
  289. begin
  290. hvs:=tparavarsym.create('$high'+name,paranr+1,vs_const,sinttype,[vo_is_high_para,vo_is_hidden_para]);
  291. hvs.symoptions:=[];
  292. owner.insert(hvs);
  293. end
  294. else
  295. begin
  296. { Give a warning that cdecl routines does not include high()
  297. support }
  298. if (pd.proccalloption in cdecl_pocalls) and
  299. paramanager.push_high_param(varspez,vardef,pocall_default) then
  300. begin
  301. if is_open_string(vardef) then
  302. MessagePos(fileinfo,parser_w_cdecl_no_openstring);
  303. if not(po_external in pd.procoptions) and
  304. (pd.typ<>procvardef) and
  305. not is_objc_class_or_protocol(tprocdef(pd)._class) then
  306. if is_array_of_const(vardef) then
  307. MessagePos(fileinfo,parser_e_varargs_need_cdecl_and_external)
  308. else
  309. MessagePos(fileinfo,parser_w_cdecl_has_no_high);
  310. end;
  311. if (vardef.typ=formaldef) and (Tformaldef(vardef).typed) then
  312. begin
  313. hvs:=tparavarsym.create('$typinfo'+name,paranr+1,vs_const,voidpointertype,
  314. [vo_is_typinfo_para,vo_is_hidden_para]);
  315. owner.insert(hvs);
  316. end;
  317. end;
  318. end;
  319. end;
  320. procedure check_c_para(pd:Tabstractprocdef);
  321. var
  322. i,
  323. lastparaidx : longint;
  324. sym : TSym;
  325. begin
  326. lastparaidx:=pd.parast.SymList.Count-1;
  327. for i:=0 to pd.parast.SymList.Count-1 do
  328. begin
  329. sym:=tsym(pd.parast.SymList[i]);
  330. if (sym.typ=paravarsym) and
  331. (tparavarsym(sym).vardef.typ=arraydef) then
  332. begin
  333. if not is_variant_array(tparavarsym(sym).vardef) and
  334. not is_array_of_const(tparavarsym(sym).vardef) and
  335. (tparavarsym(sym).varspez<>vs_var) then
  336. MessagePos(tparavarsym(sym).fileinfo,parser_h_c_arrays_are_references);
  337. if is_array_of_const(tparavarsym(sym).vardef) and
  338. (i<lastparaidx) and
  339. (tsym(pd.parast.SymList[i+1]).typ=paravarsym) and
  340. not(vo_is_high_para in tparavarsym(pd.parast.SymList[i+1]).varoptions) then
  341. MessagePos(tparavarsym(sym).fileinfo,parser_e_C_array_of_const_must_be_last);
  342. end;
  343. end;
  344. end;
  345. procedure check_msg_para(p:TObject;arg:pointer);
  346. begin
  347. if (tsym(p).typ<>paravarsym) then
  348. exit;
  349. with tparavarsym(p) do
  350. begin
  351. { Count parameters }
  352. if (paranr>=10) then
  353. inc(plongint(arg)^);
  354. { First parameter must be var }
  355. if (paranr=10) and
  356. (varspez<>vs_var) then
  357. MessagePos(fileinfo,parser_e_ill_msg_param);
  358. end;
  359. end;
  360. procedure set_addr_param_regable(p:TObject;arg:pointer);
  361. begin
  362. if (tsym(p).typ<>paravarsym) then
  363. exit;
  364. with tparavarsym(p) do
  365. begin
  366. if not is_managed_type(vardef) and
  367. paramanager.push_addr_param(varspez,vardef,tprocdef(arg).proccalloption) then
  368. varregable:=vr_intreg;
  369. end;
  370. end;
  371. procedure parse_parameter_dec(pd:tabstractprocdef);
  372. {
  373. handle_procvar needs the same changes
  374. }
  375. type
  376. tppv = (pv_none,pv_proc,pv_func);
  377. var
  378. sc : TFPObjectList;
  379. hdef : tdef;
  380. arrayelementdef : tdef;
  381. vs : tparavarsym;
  382. i : longint;
  383. srsym : tsym;
  384. pv : tprocvardef;
  385. varspez : Tvarspez;
  386. defaultvalue : tconstsym;
  387. defaultrequired : boolean;
  388. old_block_type : tblock_type;
  389. currparast : tparasymtable;
  390. parseprocvar : tppv;
  391. locationstr : string;
  392. paranr : integer;
  393. dummytype : ttypesym;
  394. explicit_paraloc,
  395. need_array,
  396. is_univ: boolean;
  397. begin
  398. old_block_type:=block_type;
  399. explicit_paraloc:=false;
  400. consume(_LKLAMMER);
  401. { Delphi/Kylix supports nonsense like }
  402. { procedure p(); }
  403. if try_to_consume(_RKLAMMER) and
  404. not(m_tp7 in current_settings.modeswitches) then
  405. exit;
  406. { parsing a proc or procvar ? }
  407. currparast:=tparasymtable(pd.parast);
  408. { reset }
  409. sc:=TFPObjectList.create(false);
  410. defaultrequired:=false;
  411. paranr:=0;
  412. inc(testcurobject);
  413. block_type:=bt_var;
  414. is_univ:=false;
  415. repeat
  416. parseprocvar:=pv_none;
  417. if try_to_consume(_VAR) then
  418. varspez:=vs_var
  419. else
  420. if try_to_consume(_CONST) then
  421. varspez:=vs_const
  422. else
  423. if (m_out in current_settings.modeswitches) and
  424. try_to_consume(_OUT) then
  425. varspez:=vs_out
  426. else
  427. if try_to_consume(_CONSTREF) then
  428. varspez:=vs_constref
  429. else
  430. if (m_mac in current_settings.modeswitches) and
  431. try_to_consume(_POINTPOINTPOINT) then
  432. begin
  433. include(pd.procoptions,po_varargs);
  434. break;
  435. end
  436. else
  437. if (m_nested_procvars in current_settings.modeswitches) and
  438. try_to_consume(_PROCEDURE) then
  439. begin
  440. parseprocvar:=pv_proc;
  441. varspez:=vs_const;
  442. end
  443. else
  444. if (m_nested_procvars in current_settings.modeswitches) and
  445. try_to_consume(_FUNCTION) then
  446. begin
  447. parseprocvar:=pv_func;
  448. varspez:=vs_const;
  449. end
  450. else
  451. varspez:=vs_value;
  452. defaultvalue:=nil;
  453. hdef:=nil;
  454. { read identifiers and insert with error type }
  455. sc.clear;
  456. repeat
  457. inc(paranr);
  458. vs:=tparavarsym.create(orgpattern,paranr*10,varspez,generrordef,[]);
  459. currparast.insert(vs);
  460. if assigned(vs.owner) then
  461. sc.add(vs)
  462. else
  463. vs.free;
  464. consume(_ID);
  465. until not try_to_consume(_COMMA);
  466. locationstr:='';
  467. { macpas anonymous procvar }
  468. if parseprocvar<>pv_none then
  469. begin
  470. { inline procvar definitions are always nested procvars }
  471. pv:=tprocvardef.create(normal_function_level+1);
  472. if token=_LKLAMMER then
  473. parse_parameter_dec(pv);
  474. if parseprocvar=pv_func then
  475. begin
  476. block_type:=bt_var_type;
  477. consume(_COLON);
  478. single_type(pv.returndef,false,false);
  479. block_type:=bt_var;
  480. end;
  481. hdef:=pv;
  482. { possible proc directives }
  483. if check_proc_directive(true) then
  484. begin
  485. dummytype:=ttypesym.create('unnamed',hdef);
  486. parse_var_proc_directives(tsym(dummytype));
  487. dummytype.typedef:=nil;
  488. hdef.typesym:=nil;
  489. dummytype.free;
  490. end;
  491. { Add implicit hidden parameters and function result }
  492. handle_calling_convention(pv);
  493. end
  494. else
  495. { read type declaration, force reading for value paras }
  496. if (token=_COLON) or (varspez=vs_value) then
  497. begin
  498. consume(_COLON);
  499. { check for an open array }
  500. need_array:=false;
  501. { bitpacked open array are not yet supported }
  502. if (token=_PACKED) and
  503. not(cs_bitpacking in current_settings.localswitches) then
  504. begin
  505. consume(_PACKED);
  506. need_array:=true;
  507. end;
  508. if (token=_ARRAY) or
  509. need_array then
  510. begin
  511. consume(_ARRAY);
  512. consume(_OF);
  513. { define range and type of range }
  514. hdef:=tarraydef.create(0,-1,s32inttype);
  515. { array of const ? }
  516. if (token=_CONST) and (m_objpas in current_settings.modeswitches) then
  517. begin
  518. consume(_CONST);
  519. srsym:=search_system_type('TVARREC');
  520. tarraydef(hdef).elementdef:=ttypesym(srsym).typedef;
  521. include(tarraydef(hdef).arrayoptions,ado_IsArrayOfConst);
  522. end
  523. else
  524. begin
  525. { define field type }
  526. single_type(arrayelementdef,false,false);
  527. tarraydef(hdef).elementdef:=arrayelementdef;
  528. end;
  529. end
  530. else
  531. begin
  532. if (m_mac in current_settings.modeswitches) then
  533. is_univ:=try_to_consume(_UNIV);
  534. if try_to_consume(_TYPE) then
  535. hdef:=ctypedformaltype
  536. else
  537. begin
  538. block_type:=bt_var_type;
  539. single_type(hdef,false,false);
  540. block_type:=bt_var;
  541. end;
  542. { open string ? }
  543. if is_shortstring(hdef) then
  544. begin
  545. case varspez of
  546. vs_var,vs_out,vs_constref:
  547. begin
  548. { not 100% Delphi-compatible: type xstr=string[255] cannot
  549. become an openstring there, while here it can }
  550. if (cs_openstring in current_settings.moduleswitches) and
  551. (tstringdef(hdef).len=255) then
  552. hdef:=openshortstringtype
  553. end;
  554. vs_value:
  555. begin
  556. { value "openstring" parameters don't make sense (the
  557. original string can never be modified, so there's no
  558. use in passing its original length), so change these
  559. into regular shortstring parameters (seems to be what
  560. Delphi also does) }
  561. if is_open_string(hdef) then
  562. hdef:=cshortstringtype;
  563. end;
  564. end;
  565. end;
  566. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) then
  567. begin
  568. if (idtoken=_LOCATION) then
  569. begin
  570. consume(_LOCATION);
  571. locationstr:=cstringpattern;
  572. consume(_CSTRING);
  573. end
  574. else
  575. begin
  576. if explicit_paraloc then
  577. Message(parser_e_paraloc_all_paras);
  578. locationstr:='';
  579. end;
  580. end
  581. else
  582. locationstr:='';
  583. { default parameter }
  584. if (m_default_para in current_settings.modeswitches) then
  585. begin
  586. if try_to_consume(_EQUAL) then
  587. begin
  588. vs:=tparavarsym(sc[0]);
  589. if sc.count>1 then
  590. Message(parser_e_default_value_only_one_para);
  591. { prefix 'def' to the parameter name }
  592. defaultvalue:=ReadConstant('$def'+vs.name,vs.fileinfo);
  593. if assigned(defaultvalue) then
  594. begin
  595. include(defaultvalue.symoptions,sp_internal);
  596. pd.parast.insert(defaultvalue);
  597. end;
  598. defaultrequired:=true;
  599. end
  600. else
  601. begin
  602. if defaultrequired then
  603. Message1(parser_e_default_value_expected_for_para,vs.name);
  604. end;
  605. end;
  606. end;
  607. end
  608. else
  609. hdef:=cformaltype;
  610. { File types are only allowed for var and out parameters }
  611. if (hdef.typ=filedef) and
  612. not(varspez in [vs_out,vs_var]) then
  613. CGMessage(cg_e_file_must_call_by_reference);
  614. { univ cannot be used with types whose size is not known at compile
  615. time }
  616. if is_univ and
  617. not is_valid_univ_para_type(hdef) then
  618. Message1(parser_e_invalid_univ_para,hdef.typename);
  619. for i:=0 to sc.count-1 do
  620. begin
  621. vs:=tparavarsym(sc[i]);
  622. vs.univpara:=is_univ;
  623. { update varsym }
  624. vs.vardef:=hdef;
  625. vs.defaultconstsym:=defaultvalue;
  626. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) then
  627. begin
  628. if locationstr<>'' then
  629. begin
  630. if sc.count>1 then
  631. Message(parser_e_paraloc_only_one_para);
  632. if (paranr>1) and not(explicit_paraloc) then
  633. Message(parser_e_paraloc_all_paras);
  634. explicit_paraloc:=true;
  635. include(vs.varoptions,vo_has_explicit_paraloc);
  636. if not(paramanager.parseparaloc(vs,upper(locationstr))) then
  637. message(parser_e_illegal_explicit_paraloc);
  638. end
  639. else
  640. if explicit_paraloc then
  641. Message(parser_e_paraloc_all_paras);
  642. end;
  643. end;
  644. until not try_to_consume(_SEMICOLON);
  645. if explicit_paraloc then
  646. begin
  647. pd.has_paraloc_info:=callerside;
  648. include(pd.procoptions,po_explicitparaloc);
  649. end;
  650. { remove parasymtable from stack }
  651. sc.free;
  652. { reset object options }
  653. dec(testcurobject);
  654. block_type:=old_block_type;
  655. consume(_RKLAMMER);
  656. end;
  657. function parse_proc_head(aclass:tobjectdef;potype:tproctypeoption;var pd:tprocdef):boolean;
  658. var
  659. hs : string;
  660. orgsp,sp : TIDString;
  661. srsym : tsym;
  662. srsymtable : TSymtable;
  663. checkstack : psymtablestackitem;
  664. storepos,
  665. procstartfilepos : tfileposinfo;
  666. searchagain : boolean;
  667. st,
  668. genericst : TSymtable;
  669. aprocsym : tprocsym;
  670. popclass : boolean;
  671. ImplIntf : TImplementedInterface;
  672. old_parse_generic : boolean;
  673. old_current_objectdef: tobjectdef;
  674. begin
  675. { Save the position where this procedure really starts }
  676. procstartfilepos:=current_tokenpos;
  677. old_parse_generic:=parse_generic;
  678. result:=false;
  679. pd:=nil;
  680. aprocsym:=nil;
  681. if potype=potype_operator then
  682. begin
  683. sp:=overloaded_names[optoken];
  684. orgsp:=sp;
  685. end
  686. else
  687. begin
  688. sp:=pattern;
  689. orgsp:=orgpattern;
  690. consume(_ID);
  691. end;
  692. { examine interface map: function/procedure iname.functionname=locfuncname }
  693. if assigned(aclass) and
  694. assigned(aclass.ImplementedInterfaces) and
  695. (aclass.ImplementedInterfaces.count>0) and
  696. try_to_consume(_POINT) then
  697. begin
  698. storepos:=current_tokenpos;
  699. current_tokenpos:=procstartfilepos;
  700. { get interface syms}
  701. searchsym(sp,srsym,srsymtable);
  702. if not assigned(srsym) then
  703. begin
  704. identifier_not_found(orgsp);
  705. srsym:=generrorsym;
  706. end;
  707. current_tokenpos:=storepos;
  708. { qualifier is interface? }
  709. ImplIntf:=nil;
  710. if (srsym.typ=typesym) and
  711. (ttypesym(srsym).typedef.typ=objectdef) then
  712. ImplIntf:=aclass.find_implemented_interface(tobjectdef(ttypesym(srsym).typedef));
  713. if ImplIntf=nil then
  714. Message(parser_e_interface_id_expected);
  715. consume(_ID);
  716. { Create unique name <interface>.<method> }
  717. hs:=sp+'.'+pattern;
  718. consume(_EQUAL);
  719. if assigned(ImplIntf) and
  720. (token=_ID) then
  721. ImplIntf.AddMapping(hs,pattern);
  722. consume(_ID);
  723. result:=true;
  724. exit;
  725. end;
  726. { method ? }
  727. if not assigned(aclass) and
  728. (potype<>potype_operator) and
  729. (symtablestack.top.symtablelevel=main_program_level) and
  730. try_to_consume(_POINT) then
  731. begin
  732. repeat
  733. searchagain:=false;
  734. if not assigned(aclass) then
  735. begin
  736. { search for object name }
  737. storepos:=current_tokenpos;
  738. current_tokenpos:=procstartfilepos;
  739. searchsym(sp,srsym,srsymtable);
  740. if not assigned(srsym) then
  741. begin
  742. identifier_not_found(orgsp);
  743. srsym:=generrorsym;
  744. end;
  745. current_tokenpos:=storepos;
  746. end;
  747. { consume proc name }
  748. sp:=pattern;
  749. orgsp:=orgpattern;
  750. procstartfilepos:=current_tokenpos;
  751. consume(_ID);
  752. { qualifier is class name ? }
  753. if (srsym.typ=typesym) and
  754. (ttypesym(srsym).typedef.typ=objectdef) then
  755. begin
  756. aclass:=tobjectdef(ttypesym(srsym).typedef);
  757. if (token<>_POINT) and (potype in [potype_class_constructor,potype_class_destructor]) then
  758. sp := lower(sp);
  759. srsym:=tsym(aclass.symtable.Find(sp));
  760. if assigned(srsym) then
  761. begin
  762. if srsym.typ=procsym then
  763. aprocsym:=tprocsym(srsym)
  764. else
  765. if (srsym.typ=typesym) and
  766. (ttypesym(srsym).typedef.typ=objectdef) then
  767. begin
  768. searchagain:=true;
  769. consume(_POINT);
  770. end
  771. else
  772. begin
  773. { we use a different error message for tp7 so it looks more compatible }
  774. if (m_fpc in current_settings.modeswitches) then
  775. Message1(parser_e_overloaded_no_procedure,srsym.realname)
  776. else
  777. Message(parser_e_methode_id_expected);
  778. { rename the name to an unique name to avoid an
  779. error when inserting the symbol in the symtable }
  780. orgsp:=orgsp+'$'+tostr(current_filepos.line);
  781. end;
  782. end
  783. else
  784. begin
  785. Message(parser_e_methode_id_expected);
  786. { recover by making it a normal procedure instead of method }
  787. aclass:=nil;
  788. end;
  789. end
  790. else
  791. Message(parser_e_class_id_expected);
  792. until not searchagain;
  793. end
  794. else
  795. begin
  796. { check for constructor/destructor which is not allowed here }
  797. if (not parse_only) and
  798. (potype in [potype_constructor,potype_destructor,
  799. potype_class_constructor,potype_class_destructor]) then
  800. Message(parser_e_constructors_always_objects);
  801. repeat
  802. searchagain:=false;
  803. current_tokenpos:=procstartfilepos;
  804. srsymtable:=symtablestack.top;
  805. srsym:=tsym(srsymtable.Find(sp));
  806. { Also look in the globalsymtable if we didn't found
  807. the symbol in the localsymtable }
  808. if not assigned(srsym) and
  809. not(parse_only) and
  810. (srsymtable=current_module.localsymtable) and
  811. assigned(current_module.globalsymtable) then
  812. srsym:=tsym(current_module.globalsymtable.Find(sp));
  813. { Check if overloaded is a procsym }
  814. if assigned(srsym) then
  815. begin
  816. if srsym.typ=procsym then
  817. aprocsym:=tprocsym(srsym)
  818. else
  819. begin
  820. { when the other symbol is a unit symbol then hide the unit
  821. symbol, this is not supported in tp7 }
  822. if not(m_tp7 in current_settings.modeswitches) and
  823. (srsym.typ=unitsym) then
  824. begin
  825. HideSym(srsym);
  826. searchagain:=true;
  827. end
  828. else
  829. begin
  830. { we use a different error message for tp7 so it looks more compatible }
  831. if (m_fpc in current_settings.modeswitches) then
  832. Message1(parser_e_overloaded_no_procedure,srsym.realname)
  833. else
  834. Message1(sym_e_duplicate_id,srsym.realname);
  835. { rename the name to an unique name to avoid an
  836. error when inserting the symbol in the symtable }
  837. orgsp:=orgsp+'$'+tostr(current_filepos.line);
  838. end;
  839. end;
  840. end;
  841. until not searchagain;
  842. end;
  843. { test again if assigned, it can be reset to recover }
  844. if not assigned(aprocsym) then
  845. begin
  846. { create a new procsym and set the real filepos }
  847. current_tokenpos:=procstartfilepos;
  848. { for operator we have only one procsym for each overloaded
  849. operation }
  850. if (potype=potype_operator) then
  851. begin
  852. aprocsym:=Tprocsym(symtablestack.top.Find(sp));
  853. if aprocsym=nil then
  854. aprocsym:=tprocsym.create('$'+sp);
  855. end
  856. else
  857. if (potype in [potype_class_constructor,potype_class_destructor]) then
  858. aprocsym:=tprocsym.create('$'+lower(sp))
  859. else
  860. aprocsym:=tprocsym.create(orgsp);
  861. symtablestack.top.insert(aprocsym);
  862. end;
  863. { to get the correct symtablelevel we must ignore ObjectSymtables }
  864. st:=nil;
  865. checkstack:=symtablestack.stack;
  866. while assigned(checkstack) do
  867. begin
  868. st:=checkstack^.symtable;
  869. if st.symtabletype in [staticsymtable,globalsymtable,localsymtable] then
  870. break;
  871. checkstack:=checkstack^.next;
  872. end;
  873. pd:=tprocdef.create(st.symtablelevel+1);
  874. pd._class:=aclass;
  875. pd.procsym:=aprocsym;
  876. pd.proctypeoption:=potype;
  877. { methods inherit df_generic or df_specialization from the objectdef }
  878. if assigned(pd._class) and
  879. (pd.parast.symtablelevel=normal_function_level) then
  880. begin
  881. if (df_generic in pd._class.defoptions) then
  882. begin
  883. include(pd.defoptions,df_generic);
  884. parse_generic:=true;
  885. end;
  886. if (df_specialization in pd._class.defoptions) then
  887. begin
  888. include(pd.defoptions,df_specialization);
  889. { Find corresponding genericdef, we need it later to
  890. replay the tokens to generate the body }
  891. if not assigned(pd._class.genericdef) then
  892. internalerror(200512113);
  893. genericst:=pd._class.genericdef.GetSymtable(gs_record);
  894. if not assigned(genericst) then
  895. internalerror(200512114);
  896. { We are parsing the same objectdef, the def index numbers
  897. are the same }
  898. pd.genericdef:=tstoreddef(genericst.DefList[pd.owner.DefList.IndexOf(pd)]);
  899. if not assigned(pd.genericdef) or
  900. (pd.genericdef.typ<>procdef) then
  901. internalerror(200512115);
  902. end;
  903. end;
  904. { methods need to be exported }
  905. if assigned(aclass) and
  906. (
  907. (symtablestack.top.symtabletype=ObjectSymtable) or
  908. (symtablestack.top.symtablelevel=main_program_level)
  909. ) then
  910. include(pd.procoptions,po_global);
  911. { symbol options that need to be kept per procdef }
  912. pd.fileinfo:=procstartfilepos;
  913. pd.visibility:=symtablestack.top.currentvisibility;
  914. if symtablestack.top.currentlyoptional then
  915. include(pd.procoptions,po_optional);
  916. { parse parameters }
  917. if token=_LKLAMMER then
  918. begin
  919. { Add ObjectSymtable to be able to find generic type definitions }
  920. popclass:=false;
  921. if assigned(pd._class) and
  922. (pd.parast.symtablelevel=normal_function_level) and
  923. (symtablestack.top.symtabletype<>ObjectSymtable) then
  924. begin
  925. symtablestack.push(pd._class.symtable);
  926. old_current_objectdef:=current_objectdef;
  927. current_objectdef:=pd._class;
  928. popclass:=true;
  929. end;
  930. { Add parameter symtable }
  931. if pd.parast.symtabletype<>staticsymtable then
  932. symtablestack.push(pd.parast);
  933. parse_parameter_dec(pd);
  934. if pd.parast.symtabletype<>staticsymtable then
  935. symtablestack.pop(pd.parast);
  936. if popclass then
  937. begin
  938. current_objectdef:=old_current_objectdef;
  939. symtablestack.pop(pd._class.symtable);
  940. end;
  941. end;
  942. parse_generic:=old_parse_generic;
  943. result:=true;
  944. end;
  945. function parse_proc_dec(isclassmethod:boolean; aclass:tobjectdef):tprocdef;
  946. var
  947. pd : tprocdef;
  948. locationstr: string;
  949. old_parse_generic,
  950. popclass: boolean;
  951. old_current_objectdef: tobjectdef;
  952. begin
  953. locationstr:='';
  954. pd:=nil;
  955. case token of
  956. _FUNCTION :
  957. begin
  958. consume(_FUNCTION);
  959. if parse_proc_head(aclass,potype_function,pd) then
  960. begin
  961. { pd=nil when it is a interface mapping }
  962. if assigned(pd) then
  963. begin
  964. if try_to_consume(_COLON) then
  965. begin
  966. old_parse_generic:=parse_generic;
  967. inc(testcurobject);
  968. { Add ObjectSymtable to be able to find generic type definitions }
  969. popclass:=false;
  970. if assigned(pd._class) and
  971. (pd.parast.symtablelevel=normal_function_level) and
  972. (symtablestack.top.symtabletype<>ObjectSymtable) then
  973. begin
  974. symtablestack.push(pd._class.symtable);
  975. popclass:=true;
  976. parse_generic:=(df_generic in pd._class.defoptions);
  977. old_current_objectdef:=current_objectdef;
  978. current_objectdef:=pd._class;
  979. end;
  980. single_type(pd.returndef,false,false);
  981. if popclass then
  982. begin
  983. current_objectdef:=old_current_objectdef;
  984. symtablestack.pop(pd._class.symtable);
  985. end;
  986. dec(testcurobject);
  987. parse_generic:=old_parse_generic;
  988. if (target_info.system in [system_m68k_amiga]) then
  989. begin
  990. if (idtoken=_LOCATION) then
  991. begin
  992. if po_explicitparaloc in pd.procoptions then
  993. begin
  994. consume(_LOCATION);
  995. locationstr:=cstringpattern;
  996. consume(_CSTRING);
  997. end
  998. else
  999. { I guess this needs a new message... (KB) }
  1000. Message(parser_e_paraloc_all_paras);
  1001. end
  1002. else
  1003. begin
  1004. if po_explicitparaloc in pd.procoptions then
  1005. { assign default locationstr, if none specified }
  1006. { and we've arguments with explicit paraloc }
  1007. locationstr:='D0';
  1008. end;
  1009. end;
  1010. end
  1011. else
  1012. begin
  1013. if (
  1014. parse_only and
  1015. not(is_interface(pd._class))
  1016. ) or
  1017. (m_repeat_forward in current_settings.modeswitches) then
  1018. begin
  1019. consume(_COLON);
  1020. consume_all_until(_SEMICOLON);
  1021. end;
  1022. end;
  1023. if isclassmethod then
  1024. include(pd.procoptions,po_classmethod);
  1025. end;
  1026. end
  1027. else
  1028. begin
  1029. { recover }
  1030. consume(_COLON);
  1031. consume_all_until(_SEMICOLON);
  1032. end;
  1033. end;
  1034. _PROCEDURE :
  1035. begin
  1036. consume(_PROCEDURE);
  1037. if parse_proc_head(aclass,potype_procedure,pd) then
  1038. begin
  1039. { pd=nil when it is an interface mapping }
  1040. if assigned(pd) then
  1041. begin
  1042. pd.returndef:=voidtype;
  1043. if isclassmethod then
  1044. include(pd.procoptions,po_classmethod);
  1045. end;
  1046. end;
  1047. end;
  1048. _CONSTRUCTOR :
  1049. begin
  1050. consume(_CONSTRUCTOR);
  1051. if isclassmethod then
  1052. parse_proc_head(aclass,potype_class_constructor,pd)
  1053. else
  1054. parse_proc_head(aclass,potype_constructor,pd);
  1055. if not isclassmethod and
  1056. assigned(pd) and
  1057. assigned(pd._class) then
  1058. begin
  1059. { Set return type, class constructors return the
  1060. created instance, object constructors return boolean }
  1061. if is_class(pd._class) then
  1062. pd.returndef:=pd._class
  1063. else
  1064. {$ifdef CPU64bitaddr}
  1065. pd.returndef:=bool64type;
  1066. {$else CPU64bitaddr}
  1067. pd.returndef:=bool32type;
  1068. {$endif CPU64bitaddr}
  1069. end
  1070. else
  1071. pd.returndef:=voidtype;
  1072. end;
  1073. _DESTRUCTOR :
  1074. begin
  1075. consume(_DESTRUCTOR);
  1076. if isclassmethod then
  1077. parse_proc_head(aclass,potype_class_destructor,pd)
  1078. else
  1079. parse_proc_head(aclass,potype_destructor,pd);
  1080. if assigned(pd) then
  1081. pd.returndef:=voidtype;
  1082. end;
  1083. _OPERATOR :
  1084. begin
  1085. consume(_OPERATOR);
  1086. if (token in [first_overloaded..last_overloaded]) then
  1087. begin
  1088. optoken:=token;
  1089. end
  1090. else
  1091. begin
  1092. { Use the dummy NOTOKEN that is also declared
  1093. for the overloaded_operator[] }
  1094. optoken:=NOTOKEN;
  1095. case token of
  1096. _CARET:
  1097. Message1(parser_e_overload_operator_failed,'**');
  1098. _ID:
  1099. if idtoken = _ENUMERATOR then
  1100. optoken := _OP_ENUMERATOR
  1101. else
  1102. Message1(parser_e_overload_operator_failed,'');
  1103. _UNEQUAL:
  1104. Message1(parser_e_overload_operator_failed,'=');
  1105. else
  1106. Message1(parser_e_overload_operator_failed,'');
  1107. end;
  1108. end;
  1109. consume(token);
  1110. parse_proc_head(aclass,potype_operator,pd);
  1111. if assigned(pd) then
  1112. begin
  1113. { operators always need to be searched in all units }
  1114. include(pd.procoptions,po_overload);
  1115. if pd.parast.symtablelevel>normal_function_level then
  1116. Message(parser_e_no_local_operator);
  1117. if token<>_ID then
  1118. begin
  1119. if not(m_result in current_settings.modeswitches) then
  1120. consume(_ID);
  1121. end
  1122. else
  1123. begin
  1124. pd.resultname:=stringdup(orgpattern);
  1125. consume(_ID);
  1126. end;
  1127. if not try_to_consume(_COLON) then
  1128. begin
  1129. consume(_COLON);
  1130. pd.returndef:=generrordef;
  1131. consume_all_until(_SEMICOLON);
  1132. end
  1133. else
  1134. begin
  1135. single_type(pd.returndef,false,false);
  1136. if (optoken in [_EQUAL,_GT,_LT,_GTE,_LTE]) and
  1137. ((pd.returndef.typ<>orddef) or
  1138. (torddef(pd.returndef).ordtype<>pasbool)) then
  1139. Message(parser_e_comparative_operator_return_boolean);
  1140. if (optoken=_ASSIGNMENT) and
  1141. equal_defs(pd.returndef,tparavarsym(pd.parast.SymList[0]).vardef) then
  1142. message(parser_e_no_such_assignment)
  1143. else if not isoperatoracceptable(pd,optoken) then
  1144. Message(parser_e_overload_impossible);
  1145. end;
  1146. end
  1147. else
  1148. begin
  1149. { recover }
  1150. try_to_consume(_ID);
  1151. consume(_COLON);
  1152. consume_all_until(_SEMICOLON);
  1153. end;
  1154. end;
  1155. end;
  1156. { file types can't be function results }
  1157. if assigned(pd) and
  1158. (pd.returndef.typ=filedef) then
  1159. message(parser_e_illegal_function_result);
  1160. { support procedure proc stdcall export; }
  1161. if not(check_proc_directive(false)) then
  1162. begin
  1163. if (token=_COLON) then
  1164. begin
  1165. message(parser_e_field_not_allowed_here);
  1166. consume_all_until(_SEMICOLON);
  1167. end;
  1168. consume(_SEMICOLON);
  1169. end;
  1170. result:=pd;
  1171. if locationstr<>'' then
  1172. begin
  1173. if not(paramanager.parsefuncretloc(pd,upper(locationstr))) then
  1174. { I guess this needs a new message... (KB) }
  1175. message(parser_e_illegal_explicit_paraloc);
  1176. end;
  1177. end;
  1178. {****************************************************************************
  1179. Procedure directive handlers
  1180. ****************************************************************************}
  1181. procedure pd_far(pd:tabstractprocdef);
  1182. begin
  1183. Message1(parser_w_proc_directive_ignored,'FAR');
  1184. end;
  1185. procedure pd_near(pd:tabstractprocdef);
  1186. begin
  1187. Message1(parser_w_proc_directive_ignored,'NEAR');
  1188. end;
  1189. procedure pd_export(pd:tabstractprocdef);
  1190. begin
  1191. if pd.typ<>procdef then
  1192. internalerror(200304264);
  1193. if assigned(tprocdef(pd)._class) then
  1194. Message(parser_e_methods_dont_be_export);
  1195. if pd.parast.symtablelevel>normal_function_level then
  1196. Message(parser_e_dont_nest_export);
  1197. end;
  1198. procedure pd_forward(pd:tabstractprocdef);
  1199. begin
  1200. if pd.typ<>procdef then
  1201. internalerror(200304265);
  1202. tprocdef(pd).forwarddef:=true;
  1203. end;
  1204. procedure pd_alias(pd:tabstractprocdef);
  1205. begin
  1206. if pd.typ<>procdef then
  1207. internalerror(200304266);
  1208. consume(_COLON);
  1209. tprocdef(pd).aliasnames.insert(get_stringconst);
  1210. include(pd.procoptions,po_has_public_name);
  1211. end;
  1212. procedure pd_public(pd:tabstractprocdef);
  1213. begin
  1214. if pd.typ<>procdef then
  1215. internalerror(200304266);
  1216. if try_to_consume(_NAME) then
  1217. begin
  1218. tprocdef(pd).aliasnames.insert(get_stringconst);
  1219. include(pd.procoptions,po_has_public_name);
  1220. end;
  1221. end;
  1222. procedure pd_asmname(pd:tabstractprocdef);
  1223. begin
  1224. if pd.typ<>procdef then
  1225. internalerror(200304267);
  1226. if token=_CCHAR then
  1227. begin
  1228. tprocdef(pd).aliasnames.insert(target_info.Cprefix+pattern);
  1229. consume(_CCHAR)
  1230. end
  1231. else
  1232. begin
  1233. tprocdef(pd).aliasnames.insert(target_info.Cprefix+cstringpattern);
  1234. consume(_CSTRING);
  1235. end;
  1236. { we don't need anything else }
  1237. tprocdef(pd).forwarddef:=false;
  1238. end;
  1239. procedure pd_internconst(pd:tabstractprocdef);
  1240. var v:Tconstexprint;
  1241. begin
  1242. if pd.typ<>procdef then
  1243. internalerror(200304268);
  1244. consume(_COLON);
  1245. v:=get_intconst;
  1246. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1247. message(parser_e_range_check_error)
  1248. else
  1249. Tprocdef(pd).extnumber:=longint(v.svalue);
  1250. end;
  1251. procedure pd_internproc(pd:tabstractprocdef);
  1252. var v:Tconstexprint;
  1253. begin
  1254. if pd.typ<>procdef then
  1255. internalerror(200304268);
  1256. consume(_COLON);
  1257. v:=get_intconst;
  1258. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1259. message(parser_e_range_check_error)
  1260. else
  1261. Tprocdef(pd).extnumber:=longint(v.svalue);
  1262. { the proc is defined }
  1263. tprocdef(pd).forwarddef:=false;
  1264. end;
  1265. procedure pd_interrupt(pd:tabstractprocdef);
  1266. begin
  1267. if pd.parast.symtablelevel>normal_function_level then
  1268. Message(parser_e_dont_nest_interrupt);
  1269. end;
  1270. procedure pd_abstract(pd:tabstractprocdef);
  1271. begin
  1272. if pd.typ<>procdef then
  1273. internalerror(200304269);
  1274. if oo_is_sealed in tprocdef(pd)._class.objectoptions then
  1275. Message(parser_e_sealed_class_cannot_have_abstract_methods)
  1276. else
  1277. if (po_virtualmethod in pd.procoptions) then
  1278. include(pd.procoptions,po_abstractmethod)
  1279. else
  1280. Message(parser_e_only_virtual_methods_abstract);
  1281. { the method is defined }
  1282. tprocdef(pd).forwarddef:=false;
  1283. end;
  1284. procedure pd_final(pd:tabstractprocdef);
  1285. begin
  1286. if pd.typ<>procdef then
  1287. internalerror(200910170);
  1288. if (po_virtualmethod in pd.procoptions) then
  1289. include(pd.procoptions,po_finalmethod)
  1290. else
  1291. Message(parser_e_only_virtual_methods_final);
  1292. end;
  1293. procedure pd_enumerator(pd:tabstractprocdef);
  1294. begin
  1295. if pd.typ<>procdef then
  1296. internalerror(200910250);
  1297. if (token = _ID) then
  1298. begin
  1299. if pattern='MOVENEXT' then
  1300. begin
  1301. if oo_has_enumerator_movenext in tprocdef(pd)._class.objectoptions then
  1302. message(parser_e_only_one_enumerator_movenext);
  1303. pd.calcparas;
  1304. if (pd.proctypeoption = potype_function) and is_boolean(pd.returndef) and
  1305. (pd.minparacount = 0) then
  1306. begin
  1307. include(tprocdef(pd)._class.objectoptions, oo_has_enumerator_movenext);
  1308. include(pd.procoptions,po_enumerator_movenext);
  1309. end
  1310. else
  1311. Message(parser_e_enumerator_movenext_is_not_valid)
  1312. end
  1313. else
  1314. Message1(parser_e_invalid_enumerator_identifier, pattern);
  1315. consume(token);
  1316. end
  1317. else
  1318. Message(parser_e_enumerator_identifier_required);
  1319. end;
  1320. procedure pd_virtual(pd:tabstractprocdef);
  1321. {$ifdef WITHDMT}
  1322. var
  1323. pt : tnode;
  1324. {$endif WITHDMT}
  1325. begin
  1326. if pd.typ<>procdef then
  1327. internalerror(2003042610);
  1328. if (pd.proctypeoption=potype_constructor) and
  1329. is_object(tprocdef(pd)._class) then
  1330. Message(parser_e_constructor_cannot_be_not_virtual);
  1331. {$ifdef WITHDMT}
  1332. if is_object(tprocdef(pd)._class) and
  1333. (token<>_SEMICOLON) then
  1334. begin
  1335. { any type of parameter is allowed here! }
  1336. pt:=comp_expr(true);
  1337. if is_constintnode(pt) then
  1338. begin
  1339. include(pd.procoptions,po_msgint);
  1340. pd.messageinf.i:=pt.value;
  1341. end
  1342. else
  1343. Message(parser_e_ill_msg_expr);
  1344. disposetree(pt);
  1345. end;
  1346. {$endif WITHDMT}
  1347. end;
  1348. procedure pd_dispid(pd:tabstractprocdef);
  1349. var pt:Tnode;
  1350. begin
  1351. if pd.typ<>procdef then
  1352. internalerror(200604301);
  1353. pt:=comp_expr(true);
  1354. if is_constintnode(pt) then
  1355. if (Tordconstnode(pt).value<int64(low(longint))) or (Tordconstnode(pt).value>int64(high(longint))) then
  1356. message(parser_e_range_check_error)
  1357. else
  1358. Tprocdef(pd).dispid:=Tordconstnode(pt).value.svalue
  1359. else
  1360. message(parser_e_dispid_must_be_ord_const);
  1361. pt.free;
  1362. end;
  1363. procedure pd_static(pd:tabstractprocdef);
  1364. begin
  1365. if pd.typ=procdef then
  1366. include(tprocdef(pd).procsym.symoptions,sp_static);
  1367. include(pd.procoptions,po_staticmethod);
  1368. end;
  1369. procedure pd_override(pd:tabstractprocdef);
  1370. begin
  1371. if pd.typ<>procdef then
  1372. internalerror(2003042611);
  1373. if not(is_class_or_interface_or_objc(tprocdef(pd)._class)) then
  1374. Message(parser_e_no_object_override)
  1375. else if is_objccategory(tprocdef(pd)._class) then
  1376. Message(parser_e_no_category_override);
  1377. end;
  1378. procedure pd_overload(pd:tabstractprocdef);
  1379. begin
  1380. if pd.typ<>procdef then
  1381. internalerror(2003042612);
  1382. include(tprocdef(pd).procsym.symoptions,sp_has_overloaded);
  1383. end;
  1384. procedure pd_message(pd:tabstractprocdef);
  1385. var
  1386. pt : tnode;
  1387. paracnt : longint;
  1388. begin
  1389. if pd.typ<>procdef then
  1390. internalerror(2003042613);
  1391. if not is_class(tprocdef(pd)._class) and
  1392. not is_objc_class_or_protocol(tprocdef(pd)._class) then
  1393. Message(parser_e_msg_only_for_classes);
  1394. if ([po_msgstr,po_msgint]*pd.procoptions)<>[] then
  1395. Message(parser_e_multiple_messages);
  1396. { check parameter type }
  1397. if not is_objc_class_or_protocol(tprocdef(pd)._class) then
  1398. begin
  1399. paracnt:=0;
  1400. pd.parast.SymList.ForEachCall(@check_msg_para,@paracnt);
  1401. if paracnt<>1 then
  1402. Message(parser_e_ill_msg_param);
  1403. end;
  1404. pt:=comp_expr(true);
  1405. { message is 1-character long }
  1406. if is_constcharnode(pt) then
  1407. begin
  1408. include(pd.procoptions,po_msgstr);
  1409. tprocdef(pd).messageinf.str:=stringdup(chr(byte(tordconstnode(pt).value.uvalue and $FF)));
  1410. end
  1411. else if pt.nodetype=stringconstn then
  1412. begin
  1413. include(pd.procoptions,po_msgstr);
  1414. if (tstringconstnode(pt).len>255) then
  1415. Message(parser_e_message_string_too_long);
  1416. tprocdef(pd).messageinf.str:=stringdup(tstringconstnode(pt).value_str);
  1417. end
  1418. else
  1419. if is_constintnode(pt) and
  1420. is_class(tprocdef(pd)._class) then
  1421. begin
  1422. include(pd.procoptions,po_msgint);
  1423. if (Tordconstnode(pt).value<int64(low(Tprocdef(pd).messageinf.i))) or
  1424. (Tordconstnode(pt).value>int64(high(Tprocdef(pd).messageinf.i))) then
  1425. message(parser_e_range_check_error)
  1426. else
  1427. Tprocdef(pd).messageinf.i:=tordconstnode(pt).value.svalue;
  1428. end
  1429. else
  1430. Message(parser_e_ill_msg_expr);
  1431. { check whether the selector name is valid in case of Objective-C }
  1432. if (po_msgstr in pd.procoptions) and
  1433. is_objc_class_or_protocol(tprocdef(pd)._class) and
  1434. not objcvalidselectorname(@tprocdef(pd).messageinf.str^[1],length(tprocdef(pd).messageinf.str^)) then
  1435. Message1(type_e_invalid_objc_selector_name,tprocdef(pd).messageinf.str^);
  1436. pt.free;
  1437. end;
  1438. procedure pd_reintroduce(pd:tabstractprocdef);
  1439. begin
  1440. if pd.typ<>procdef then
  1441. internalerror(200401211);
  1442. if not(is_class_or_interface_or_object(tprocdef(pd)._class)) and
  1443. not(is_objccategory(tprocdef(pd)._class)) then
  1444. Message(parser_e_no_object_reintroduce);
  1445. end;
  1446. procedure pd_syscall(pd:tabstractprocdef);
  1447. {$if defined(powerpc) or defined(m68k)}
  1448. var
  1449. vs : tparavarsym;
  1450. sym : tsym;
  1451. symtable : TSymtable;
  1452. v: Tconstexprint;
  1453. {$endif defined(powerpc) or defined(m68k)}
  1454. begin
  1455. if (pd.typ<>procdef) and (target_info.system <> system_powerpc_amiga) then
  1456. internalerror(2003042614);
  1457. tprocdef(pd).forwarddef:=false;
  1458. {$ifdef m68k}
  1459. if target_info.system in [system_m68k_amiga] then
  1460. begin
  1461. include(pd.procoptions,po_syscall_legacy);
  1462. if consume_sym(sym,symtable) then
  1463. begin
  1464. if (sym.typ=staticvarsym) and
  1465. (
  1466. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1467. is_32bitint(tabstractvarsym(sym).vardef)
  1468. ) then
  1469. begin
  1470. tprocdef(pd).libsym:=sym;
  1471. if po_syscall_legacy in tprocdef(pd).procoptions then
  1472. begin
  1473. vs:=tparavarsym.create('$syscalllib',paranr_syscall_legacy,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1474. paramanager.parseparaloc(vs,'A6');
  1475. pd.parast.insert(vs);
  1476. end
  1477. end
  1478. else
  1479. Message(parser_e_32bitint_or_pointer_variable_expected);
  1480. end;
  1481. (paramanager as tm68kparamanager).create_funcretloc_info(pd,calleeside);
  1482. (paramanager as tm68kparamanager).create_funcretloc_info(pd,callerside);
  1483. v:=get_intconst;
  1484. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1485. message(parser_e_range_check_error)
  1486. else
  1487. Tprocdef(pd).extnumber:=v.uvalue;
  1488. end;
  1489. {$endif m68k}
  1490. {$ifdef powerpc}
  1491. if target_info.system = system_powerpc_amiga then
  1492. begin
  1493. include(pd.procoptions,po_syscall_basesysv);
  1494. if consume_sym(sym,symtable) then
  1495. begin
  1496. if (sym.typ=staticvarsym) and
  1497. (
  1498. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1499. is_32bitint(tabstractvarsym(sym).vardef)
  1500. ) then
  1501. begin
  1502. tprocdef(pd).libsym:=sym;
  1503. vs:=tparavarsym.create('$syscalllib',paranr_syscall_basesysv,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1504. pd.parast.insert(vs);
  1505. end
  1506. else
  1507. Message(parser_e_32bitint_or_pointer_variable_expected);
  1508. end;
  1509. (paramanager as tppcparamanager).create_funcretloc_info(pd,calleeside);
  1510. (paramanager as tppcparamanager).create_funcretloc_info(pd,callerside);
  1511. v:=get_intconst;
  1512. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1513. message(parser_e_range_check_error)
  1514. else
  1515. Tprocdef(pd).extnumber:=v.uvalue;
  1516. end else
  1517. if target_info.system = system_powerpc_morphos then
  1518. begin
  1519. if idtoken=_LEGACY then
  1520. begin
  1521. consume(_LEGACY);
  1522. include(pd.procoptions,po_syscall_legacy);
  1523. end
  1524. else if idtoken=_SYSV then
  1525. begin
  1526. consume(_SYSV);
  1527. include(pd.procoptions,po_syscall_sysv);
  1528. end
  1529. else if idtoken=_BASESYSV then
  1530. begin
  1531. consume(_BASESYSV);
  1532. include(pd.procoptions,po_syscall_basesysv);
  1533. end
  1534. else if idtoken=_SYSVBASE then
  1535. begin
  1536. consume(_SYSVBASE);
  1537. include(pd.procoptions,po_syscall_sysvbase);
  1538. end
  1539. else if idtoken=_R12BASE then
  1540. begin
  1541. consume(_R12BASE);
  1542. include(pd.procoptions,po_syscall_r12base);
  1543. end
  1544. else
  1545. if syscall_convention='LEGACY' then
  1546. include(pd.procoptions,po_syscall_legacy)
  1547. else if syscall_convention='SYSV' then
  1548. include(pd.procoptions,po_syscall_sysv)
  1549. else if syscall_convention='BASESYSV' then
  1550. include(pd.procoptions,po_syscall_basesysv)
  1551. else if syscall_convention='SYSVBASE' then
  1552. include(pd.procoptions,po_syscall_sysvbase)
  1553. else if syscall_convention='R12BASE' then
  1554. include(pd.procoptions,po_syscall_r12base)
  1555. else
  1556. internalerror(2005010404);
  1557. if consume_sym(sym,symtable) then
  1558. begin
  1559. if (sym.typ=staticvarsym) and
  1560. (
  1561. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1562. is_32bitint(tabstractvarsym(sym).vardef)
  1563. ) then
  1564. begin
  1565. tprocdef(pd).libsym:=sym;
  1566. if po_syscall_legacy in tprocdef(pd).procoptions then
  1567. begin
  1568. vs:=tparavarsym.create('$syscalllib',paranr_syscall_legacy,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1569. paramanager.parseparaloc(vs,'A6');
  1570. pd.parast.insert(vs);
  1571. end
  1572. else if po_syscall_sysv in tprocdef(pd).procoptions then
  1573. begin
  1574. { Nothing to be done for sysv here for now, but this might change }
  1575. end
  1576. else if po_syscall_basesysv in tprocdef(pd).procoptions then
  1577. begin
  1578. vs:=tparavarsym.create('$syscalllib',paranr_syscall_basesysv,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1579. pd.parast.insert(vs);
  1580. end
  1581. else if po_syscall_sysvbase in tprocdef(pd).procoptions then
  1582. begin
  1583. vs:=tparavarsym.create('$syscalllib',paranr_syscall_sysvbase,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1584. pd.parast.insert(vs);
  1585. end
  1586. else if po_syscall_r12base in tprocdef(pd).procoptions then
  1587. begin
  1588. vs:=tparavarsym.create('$syscalllib',paranr_syscall_r12base,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1589. paramanager.parseparaloc(vs,'R12');
  1590. pd.parast.insert(vs);
  1591. end
  1592. else
  1593. internalerror(2005010501);
  1594. end
  1595. else
  1596. Message(parser_e_32bitint_or_pointer_variable_expected);
  1597. end;
  1598. (paramanager as tppcparamanager).create_funcretloc_info(pd,calleeside);
  1599. (paramanager as tppcparamanager).create_funcretloc_info(pd,callerside);
  1600. v:=get_intconst;
  1601. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1602. message(parser_e_range_check_error)
  1603. else
  1604. Tprocdef(pd).extnumber:=v.uvalue;
  1605. end;
  1606. {$endif powerpc}
  1607. end;
  1608. procedure pd_external(pd:tabstractprocdef);
  1609. {
  1610. If import_dll=nil the procedure is assumed to be in another
  1611. object file. In that object file it should have the name to
  1612. which import_name is pointing to. Otherwise, the procedure is
  1613. assumed to be in the DLL to which import_dll is pointing to. In
  1614. that case either import_nr<>0 or import_name<>nil is true, so
  1615. the procedure is either imported by number or by name. (DM)
  1616. }
  1617. var
  1618. hs : string;
  1619. v:Tconstexprint;
  1620. begin
  1621. if pd.typ<>procdef then
  1622. internalerror(2003042615);
  1623. with tprocdef(pd) do
  1624. begin
  1625. forwarddef:=false;
  1626. { forbid local external procedures }
  1627. if parast.symtablelevel>normal_function_level then
  1628. Message(parser_e_no_local_proc_external);
  1629. { If the procedure should be imported from a DLL, a constant string follows.
  1630. This isn't really correct, an contant string expression follows
  1631. so we check if an semicolon follows, else a string constant have to
  1632. follow (FK) }
  1633. if not(token=_SEMICOLON) and not(idtoken=_NAME) then
  1634. begin
  1635. { Always add library prefix and suffix to create an uniform name }
  1636. hs:=get_stringconst;
  1637. if ExtractFileExt(hs)='' then
  1638. hs:=ChangeFileExt(hs,target_info.sharedlibext);
  1639. if Copy(hs,1,length(target_info.sharedlibprefix))<>target_info.sharedlibprefix then
  1640. hs:=target_info.sharedlibprefix+hs;
  1641. import_dll:=stringdup(hs);
  1642. include(procoptions,po_has_importdll);
  1643. if (idtoken=_NAME) then
  1644. begin
  1645. consume(_NAME);
  1646. import_name:=stringdup(get_stringconst);
  1647. include(procoptions,po_has_importname);
  1648. if import_name^='' then
  1649. message(parser_e_empty_import_name);
  1650. end;
  1651. if (idtoken=_INDEX) then
  1652. begin
  1653. {After the word index follows the index number in the DLL.}
  1654. consume(_INDEX);
  1655. v:=get_intconst;
  1656. if (v<int64(low(import_nr))) or (v>int64(high(import_nr))) then
  1657. message(parser_e_range_check_error)
  1658. else
  1659. import_nr:=longint(v.svalue);
  1660. end;
  1661. { default is to used the realname of the procedure }
  1662. if (import_nr=0) and not assigned(import_name) then
  1663. begin
  1664. import_name:=stringdup(procsym.realname);
  1665. include(procoptions,po_has_importname);
  1666. end;
  1667. end
  1668. else
  1669. begin
  1670. if (idtoken=_NAME) then
  1671. begin
  1672. consume(_NAME);
  1673. import_name:=stringdup(get_stringconst);
  1674. include(procoptions,po_has_importname);
  1675. if import_name^='' then
  1676. message(parser_e_empty_import_name);
  1677. end;
  1678. end;
  1679. end;
  1680. end;
  1681. procedure pd_weakexternal(pd:tabstractprocdef);
  1682. begin
  1683. if not(target_info.system in systems_weak_linking) then
  1684. message(parser_e_weak_external_not_supported)
  1685. else
  1686. pd_external(pd);
  1687. end;
  1688. type
  1689. pd_handler=procedure(pd:tabstractprocdef);
  1690. proc_dir_rec=record
  1691. idtok : ttoken;
  1692. pd_flags : tpdflags;
  1693. handler : pd_handler;
  1694. pocall : tproccalloption;
  1695. pooption : tprocoptions;
  1696. mutexclpocall : tproccalloptions;
  1697. mutexclpotype : tproctypeoptions;
  1698. mutexclpo : tprocoptions;
  1699. end;
  1700. const
  1701. {Should contain the number of procedure directives we support.}
  1702. num_proc_directives=42;
  1703. proc_direcdata:array[1..num_proc_directives] of proc_dir_rec=
  1704. (
  1705. (
  1706. idtok:_ABSTRACT;
  1707. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1708. handler : @pd_abstract;
  1709. pocall : pocall_none;
  1710. pooption : [po_abstractmethod];
  1711. mutexclpocall : [pocall_internproc];
  1712. mutexclpotype : [];
  1713. mutexclpo : [po_exports,po_interrupt,po_external,po_inline]
  1714. ),(
  1715. idtok:_ALIAS;
  1716. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  1717. handler : @pd_alias;
  1718. pocall : pocall_none;
  1719. pooption : [];
  1720. mutexclpocall : [];
  1721. mutexclpotype : [];
  1722. mutexclpo : [po_external,po_inline]
  1723. ),(
  1724. idtok:_ASMNAME;
  1725. pd_flags : [pd_interface,pd_implemen,pd_notobjintf];
  1726. handler : @pd_asmname;
  1727. pocall : pocall_cdecl;
  1728. pooption : [po_external];
  1729. mutexclpocall : [pocall_internproc];
  1730. mutexclpotype : [];
  1731. mutexclpo : [po_external,po_inline]
  1732. ),(
  1733. idtok:_ASSEMBLER;
  1734. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1735. handler : nil;
  1736. pocall : pocall_none;
  1737. pooption : [po_assembler];
  1738. mutexclpocall : [];
  1739. mutexclpotype : [];
  1740. mutexclpo : [po_external]
  1741. ),(
  1742. idtok:_C; {same as cdecl for mode mac}
  1743. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1744. handler : nil;
  1745. pocall : pocall_cdecl;
  1746. pooption : [];
  1747. mutexclpocall : [];
  1748. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  1749. mutexclpo : [po_assembler,po_external]
  1750. ),(
  1751. idtok:_CDECL;
  1752. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1753. handler : nil;
  1754. pocall : pocall_cdecl;
  1755. pooption : [];
  1756. mutexclpocall : [];
  1757. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  1758. mutexclpo : [po_assembler,po_external]
  1759. ),(
  1760. idtok:_DISPID;
  1761. pd_flags : [pd_dispinterface];
  1762. handler : @pd_dispid;
  1763. pocall : pocall_none;
  1764. pooption : [po_dispid];
  1765. mutexclpocall : [pocall_internproc];
  1766. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  1767. mutexclpo : [po_interrupt,po_external,po_inline]
  1768. ),(
  1769. idtok:_DYNAMIC;
  1770. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1771. handler : @pd_virtual;
  1772. pocall : pocall_none;
  1773. pooption : [po_virtualmethod];
  1774. mutexclpocall : [pocall_internproc];
  1775. mutexclpotype : [potype_class_constructor,potype_class_destructor];
  1776. mutexclpo : [po_exports,po_interrupt,po_external,po_overridingmethod,po_inline]
  1777. ),(
  1778. idtok:_EXPORT;
  1779. pd_flags : [pd_body,pd_interface,pd_implemen,pd_notobjintf];
  1780. handler : @pd_export;
  1781. pocall : pocall_none;
  1782. pooption : [po_exports,po_global];
  1783. mutexclpocall : [pocall_internproc];
  1784. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  1785. mutexclpo : [po_external,po_interrupt,po_inline]
  1786. ),(
  1787. idtok:_EXTERNAL;
  1788. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject];
  1789. handler : @pd_external;
  1790. pocall : pocall_none;
  1791. pooption : [po_external];
  1792. mutexclpocall : [pocall_internproc,pocall_syscall];
  1793. { allowed for external cpp classes }
  1794. mutexclpotype : [{potype_constructor,potype_destructor}potype_class_constructor,potype_class_destructor];
  1795. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  1796. ),(
  1797. idtok:_FAR;
  1798. pd_flags : [pd_implemen,pd_body,pd_interface,pd_procvar,pd_notobject,pd_notobjintf];
  1799. handler : @pd_far;
  1800. pocall : pocall_none;
  1801. pooption : [];
  1802. mutexclpocall : [pocall_internproc];
  1803. mutexclpotype : [];
  1804. mutexclpo : [po_inline]
  1805. ),(
  1806. idtok:_FAR16;
  1807. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar,pd_notobject];
  1808. handler : nil;
  1809. pocall : pocall_far16;
  1810. pooption : [];
  1811. mutexclpocall : [];
  1812. mutexclpotype : [];
  1813. mutexclpo : [po_external]
  1814. ),(
  1815. idtok:_FINAL;
  1816. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1817. handler : @pd_final;
  1818. pocall : pocall_none;
  1819. pooption : [po_finalmethod];
  1820. mutexclpocall : [pocall_internproc];
  1821. mutexclpotype : [];
  1822. mutexclpo : [po_exports,po_interrupt,po_external,po_inline]
  1823. ),(
  1824. idtok:_FORWARD;
  1825. pd_flags : [pd_implemen,pd_notobject,pd_notobjintf];
  1826. handler : @pd_forward;
  1827. pocall : pocall_none;
  1828. pooption : [];
  1829. mutexclpocall : [pocall_internproc];
  1830. mutexclpotype : [];
  1831. mutexclpo : [po_external,po_inline]
  1832. ),(
  1833. idtok:_OLDFPCCALL;
  1834. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1835. handler : nil;
  1836. pocall : pocall_oldfpccall;
  1837. pooption : [];
  1838. mutexclpocall : [];
  1839. mutexclpotype : [];
  1840. mutexclpo : []
  1841. ),(
  1842. idtok:_INLINE;
  1843. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1844. handler : nil;
  1845. pocall : pocall_none;
  1846. pooption : [po_inline];
  1847. mutexclpocall : [];
  1848. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  1849. mutexclpo : [po_exports,po_external,po_interrupt,po_virtualmethod]
  1850. ),(
  1851. idtok:_INTERNCONST;
  1852. pd_flags : [pd_interface,pd_body,pd_notobject,pd_notobjintf];
  1853. handler : @pd_internconst;
  1854. pocall : pocall_none;
  1855. pooption : [po_internconst];
  1856. mutexclpocall : [];
  1857. mutexclpotype : [potype_operator];
  1858. mutexclpo : []
  1859. ),(
  1860. idtok:_INTERNPROC;
  1861. pd_flags : [pd_interface,pd_notobject,pd_notobjintf];
  1862. handler : @pd_internproc;
  1863. pocall : pocall_internproc;
  1864. pooption : [];
  1865. mutexclpocall : [];
  1866. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  1867. mutexclpo : [po_exports,po_external,po_interrupt,po_assembler,po_iocheck,po_virtualmethod]
  1868. ),(
  1869. idtok:_INTERRUPT;
  1870. pd_flags : [pd_implemen,pd_body,pd_notobject,pd_notobjintf];
  1871. handler : @pd_interrupt;
  1872. pocall : pocall_oldfpccall;
  1873. pooption : [po_interrupt];
  1874. mutexclpocall : [pocall_internproc,pocall_cdecl,pocall_cppdecl,pocall_stdcall,
  1875. pocall_pascal,pocall_far16,pocall_oldfpccall];
  1876. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  1877. mutexclpo : [po_external,po_inline]
  1878. ),(
  1879. idtok:_IOCHECK;
  1880. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  1881. handler : nil;
  1882. pocall : pocall_none;
  1883. pooption : [po_iocheck];
  1884. mutexclpocall : [pocall_internproc];
  1885. mutexclpotype : [];
  1886. mutexclpo : [po_external]
  1887. ),(
  1888. idtok:_LOCAL;
  1889. pd_flags : [pd_implemen,pd_body];
  1890. handler : nil;
  1891. pocall : pocall_none;
  1892. pooption : [po_kylixlocal];
  1893. mutexclpocall : [pocall_internproc,pocall_far16];
  1894. mutexclpotype : [];
  1895. mutexclpo : [po_external,po_exports]
  1896. ),(
  1897. idtok:_MESSAGE;
  1898. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass, pd_objcprot];
  1899. handler : @pd_message;
  1900. pocall : pocall_none;
  1901. pooption : []; { can be po_msgstr or po_msgint }
  1902. mutexclpocall : [pocall_internproc];
  1903. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  1904. mutexclpo : [po_interrupt,po_external,po_inline]
  1905. ),(
  1906. idtok:_MWPASCAL;
  1907. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1908. handler : nil;
  1909. pocall : pocall_mwpascal;
  1910. pooption : [];
  1911. mutexclpocall : [];
  1912. mutexclpotype : [];
  1913. mutexclpo : []
  1914. ),(
  1915. idtok:_NEAR;
  1916. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  1917. handler : @pd_near;
  1918. pocall : pocall_none;
  1919. pooption : [];
  1920. mutexclpocall : [pocall_internproc];
  1921. mutexclpotype : [];
  1922. mutexclpo : []
  1923. ),(
  1924. idtok:_NOSTACKFRAME;
  1925. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  1926. handler : nil;
  1927. pocall : pocall_none;
  1928. pooption : [po_nostackframe];
  1929. mutexclpocall : [pocall_internproc];
  1930. mutexclpotype : [];
  1931. mutexclpo : []
  1932. ),(
  1933. idtok:_OVERLOAD;
  1934. pd_flags : [pd_implemen,pd_interface,pd_body];
  1935. handler : @pd_overload;
  1936. pocall : pocall_none;
  1937. pooption : [po_overload];
  1938. mutexclpocall : [pocall_internproc];
  1939. mutexclpotype : [];
  1940. mutexclpo : []
  1941. ),(
  1942. idtok:_OVERRIDE;
  1943. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass];
  1944. handler : @pd_override;
  1945. pocall : pocall_none;
  1946. pooption : [po_overridingmethod,po_virtualmethod];
  1947. mutexclpocall : [pocall_internproc];
  1948. mutexclpotype : [];
  1949. mutexclpo : [po_exports,po_external,po_interrupt,po_virtualmethod,po_inline]
  1950. ),(
  1951. idtok:_PASCAL;
  1952. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1953. handler : nil;
  1954. pocall : pocall_pascal;
  1955. pooption : [];
  1956. mutexclpocall : [];
  1957. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  1958. mutexclpo : [po_external]
  1959. ),(
  1960. idtok:_PUBLIC;
  1961. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobject,pd_notobjintf];
  1962. handler : @pd_public;
  1963. pocall : pocall_none;
  1964. pooption : [po_public,po_global];
  1965. mutexclpocall : [pocall_internproc];
  1966. mutexclpotype : [];
  1967. mutexclpo : [po_external,po_inline]
  1968. ),(
  1969. idtok:_REGISTER;
  1970. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1971. handler : nil;
  1972. pocall : pocall_register;
  1973. pooption : [];
  1974. mutexclpocall : [];
  1975. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  1976. mutexclpo : [po_external]
  1977. ),(
  1978. idtok:_REINTRODUCE;
  1979. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass];
  1980. handler : @pd_reintroduce;
  1981. pocall : pocall_none;
  1982. pooption : [po_reintroduce];
  1983. mutexclpocall : [pocall_internproc];
  1984. mutexclpotype : [];
  1985. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod,po_inline]
  1986. ),(
  1987. idtok:_SAFECALL;
  1988. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1989. handler : nil;
  1990. pocall : pocall_safecall;
  1991. pooption : [];
  1992. mutexclpocall : [];
  1993. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  1994. mutexclpo : [po_external]
  1995. ),(
  1996. idtok:_SOFTFLOAT;
  1997. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1998. handler : nil;
  1999. pocall : pocall_softfloat;
  2000. pooption : [];
  2001. mutexclpocall : [];
  2002. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2003. { it's available with po_external because the libgcc floating point routines on the arm
  2004. uses this calling convention }
  2005. mutexclpo : []
  2006. ),(
  2007. idtok:_STATIC;
  2008. pd_flags : [pd_interface,pd_implemen,pd_body,pd_object,pd_notobjintf];
  2009. handler : @pd_static;
  2010. pocall : pocall_none;
  2011. pooption : [po_staticmethod];
  2012. mutexclpocall : [pocall_internproc];
  2013. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2014. mutexclpo : [po_external,po_interrupt,po_exports]
  2015. ),(
  2016. idtok:_STDCALL;
  2017. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2018. handler : nil;
  2019. pocall : pocall_stdcall;
  2020. pooption : [];
  2021. mutexclpocall : [];
  2022. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2023. mutexclpo : [po_external]
  2024. ),(
  2025. idtok:_SYSCALL;
  2026. { Different kind of syscalls are valid for AOS68k, AOSPPC and MOS. }
  2027. { FIX ME!!! MorphOS/AOS68k pd_flags should be:
  2028. pd_interface, pd_implemen, pd_notobject, pd_notobjintf (KB) }
  2029. pd_flags : [pd_interface,pd_implemen,pd_procvar];
  2030. handler : @pd_syscall;
  2031. pocall : pocall_syscall;
  2032. pooption : [];
  2033. mutexclpocall : [];
  2034. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2035. mutexclpo : [po_external,po_assembler,po_interrupt,po_exports]
  2036. ),(
  2037. idtok:_VIRTUAL;
  2038. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  2039. handler : @pd_virtual;
  2040. pocall : pocall_none;
  2041. pooption : [po_virtualmethod];
  2042. mutexclpocall : [pocall_internproc];
  2043. mutexclpotype : [potype_class_constructor,potype_class_destructor];
  2044. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod,po_inline]
  2045. ),(
  2046. idtok:_CPPDECL;
  2047. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2048. handler : nil;
  2049. pocall : pocall_cppdecl;
  2050. pooption : [];
  2051. mutexclpocall : [];
  2052. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2053. mutexclpo : [po_assembler,po_external,po_virtualmethod]
  2054. ),(
  2055. idtok:_VARARGS;
  2056. pd_flags : [pd_interface,pd_implemen,pd_procvar,pd_objcclass, pd_objcprot];
  2057. handler : nil;
  2058. pocall : pocall_none;
  2059. pooption : [po_varargs];
  2060. mutexclpocall : [pocall_internproc,pocall_stdcall,pocall_register,
  2061. pocall_far16,pocall_oldfpccall,pocall_mwpascal];
  2062. mutexclpotype : [];
  2063. mutexclpo : [po_assembler,po_interrupt,po_inline]
  2064. ),(
  2065. idtok:_COMPILERPROC;
  2066. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2067. handler : nil;
  2068. pocall : pocall_none;
  2069. pooption : [po_compilerproc];
  2070. mutexclpocall : [];
  2071. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2072. mutexclpo : [po_interrupt]
  2073. ),(
  2074. idtok:_WEAKEXTERNAL;
  2075. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject];
  2076. handler : @pd_weakexternal;
  2077. pocall : pocall_none;
  2078. { mark it both external and weak external, so we don't have to
  2079. adapt all code for external symbols to also check for weak external
  2080. }
  2081. pooption : [po_external,po_weakexternal];
  2082. mutexclpocall : [pocall_internproc,pocall_syscall];
  2083. { allowed for external cpp classes }
  2084. mutexclpotype : [{potype_constructor,potype_destructor}potype_class_constructor,potype_class_destructor];
  2085. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  2086. ),(
  2087. idtok:_ENUMERATOR;
  2088. pd_flags : [pd_interface,pd_object];
  2089. handler : @pd_enumerator;
  2090. pocall : pocall_none;
  2091. pooption : [];
  2092. mutexclpocall : [pocall_internproc];
  2093. mutexclpotype : [];
  2094. mutexclpo : [po_exports,po_interrupt,po_external,po_inline]
  2095. )
  2096. );
  2097. function check_proc_directive(isprocvar:boolean):boolean;
  2098. var
  2099. i : longint;
  2100. begin
  2101. result:=false;
  2102. for i:=1 to num_proc_directives do
  2103. if proc_direcdata[i].idtok=idtoken then
  2104. begin
  2105. if ((not isprocvar) or
  2106. (pd_procvar in proc_direcdata[i].pd_flags)) and
  2107. { don't eat a public directive in classes }
  2108. not((idtoken=_PUBLIC) and (symtablestack.top.symtabletype=ObjectSymtable)) then
  2109. result:=true;
  2110. exit;
  2111. end;
  2112. end;
  2113. function parse_proc_direc(pd:tabstractprocdef;var pdflags:tpdflags):boolean;
  2114. {
  2115. Parse the procedure directive, returns true if a correct directive is found
  2116. }
  2117. var
  2118. p : longint;
  2119. found : boolean;
  2120. name : TIDString;
  2121. begin
  2122. parse_proc_direc:=false;
  2123. name:=tokeninfo^[idtoken].str;
  2124. found:=false;
  2125. { Hint directive? Then exit immediatly }
  2126. if (m_hintdirective in current_settings.modeswitches) then
  2127. begin
  2128. case idtoken of
  2129. _LIBRARY,
  2130. _PLATFORM,
  2131. _UNIMPLEMENTED,
  2132. _EXPERIMENTAL,
  2133. _DEPRECATED :
  2134. exit;
  2135. end;
  2136. end;
  2137. { C directive is MacPas only, because it breaks too much existing code
  2138. on other platforms (PFV) }
  2139. if (idtoken=_C) and
  2140. not(m_mac in current_settings.modeswitches) then
  2141. exit;
  2142. { retrieve data for directive if found }
  2143. for p:=1 to num_proc_directives do
  2144. if proc_direcdata[p].idtok=idtoken then
  2145. begin
  2146. found:=true;
  2147. break;
  2148. end;
  2149. { Check if the procedure directive is known }
  2150. if not found then
  2151. begin
  2152. { parsing a procvar type the name can be any
  2153. next variable !! }
  2154. if ((pdflags * [pd_procvar,pd_object,pd_objcclass,pd_objcprot])=[]) and
  2155. not(idtoken=_PROPERTY) then
  2156. Message1(parser_w_unknown_proc_directive_ignored,name);
  2157. exit;
  2158. end;
  2159. { check if method and directive not for object, like public.
  2160. This needs to be checked also for procvars }
  2161. if (pd_notobject in proc_direcdata[p].pd_flags) and
  2162. (symtablestack.top.symtabletype=ObjectSymtable) and
  2163. { directive allowed for cpp classes? }
  2164. not(is_cppclass(tdef(symtablestack.top.defowner)) and (pd_cppobject in proc_direcdata[p].pd_flags)) then
  2165. exit;
  2166. { Conflicts between directives ? }
  2167. if (pd.proctypeoption in proc_direcdata[p].mutexclpotype) or
  2168. (pd.proccalloption in proc_direcdata[p].mutexclpocall) or
  2169. ((pd.procoptions*proc_direcdata[p].mutexclpo)<>[]) then
  2170. begin
  2171. Message1(parser_e_proc_dir_conflict,name);
  2172. exit;
  2173. end;
  2174. { set calling convention }
  2175. if proc_direcdata[p].pocall<>pocall_none then
  2176. begin
  2177. if (po_hascallingconvention in pd.procoptions) then
  2178. begin
  2179. Message2(parser_w_proc_overriding_calling,
  2180. proccalloptionStr[pd.proccalloption],
  2181. proccalloptionStr[proc_direcdata[p].pocall]);
  2182. end;
  2183. { check if the target processor supports this calling convention }
  2184. if not(proc_direcdata[p].pocall in supported_calling_conventions) then
  2185. begin
  2186. Message1(parser_e_illegal_calling_convention,proccalloptionStr[proc_direcdata[p].pocall]);
  2187. { recover }
  2188. proc_direcdata[p].pocall:=pocall_stdcall;
  2189. end;
  2190. pd.proccalloption:=proc_direcdata[p].pocall;
  2191. include(pd.procoptions,po_hascallingconvention);
  2192. end;
  2193. if pd.typ=procdef then
  2194. begin
  2195. { Check if the directive is only for objects }
  2196. if (pd_object in proc_direcdata[p].pd_flags) and
  2197. not assigned(tprocdef(pd)._class) then
  2198. exit;
  2199. { check if method and directive not for interface }
  2200. if (pd_notobjintf in proc_direcdata[p].pd_flags) and
  2201. is_interface(tprocdef(pd)._class) then
  2202. exit;
  2203. { check if method and directive not for interface }
  2204. if is_dispinterface(tprocdef(pd)._class) and
  2205. not(pd_dispinterface in proc_direcdata[p].pd_flags) then
  2206. exit;
  2207. { check if method and directive not for objcclass }
  2208. if is_objcclass(tprocdef(pd)._class) and
  2209. not(pd_objcclass in proc_direcdata[p].pd_flags) then
  2210. exit;
  2211. { check if method and directive not for objcprotocol }
  2212. if is_objcprotocol(tprocdef(pd)._class) and
  2213. not(pd_objcprot in proc_direcdata[p].pd_flags) then
  2214. exit;
  2215. end;
  2216. { consume directive, and turn flag on }
  2217. consume(token);
  2218. parse_proc_direc:=true;
  2219. { Check the pd_flags if the directive should be allowed }
  2220. if (pd_interface in pdflags) and
  2221. not(pd_interface in proc_direcdata[p].pd_flags) then
  2222. begin
  2223. Message1(parser_e_proc_dir_not_allowed_in_interface,name);
  2224. exit;
  2225. end;
  2226. if (pd_implemen in pdflags) and
  2227. not(pd_implemen in proc_direcdata[p].pd_flags) then
  2228. begin
  2229. Message1(parser_e_proc_dir_not_allowed_in_implementation,name);
  2230. exit;
  2231. end;
  2232. if (pd_procvar in pdflags) and
  2233. not(pd_procvar in proc_direcdata[p].pd_flags) then
  2234. begin
  2235. Message1(parser_e_proc_dir_not_allowed_in_procvar,name);
  2236. exit;
  2237. end;
  2238. { Return the new pd_flags }
  2239. if not(pd_body in proc_direcdata[p].pd_flags) then
  2240. exclude(pdflags,pd_body);
  2241. { Add the correct flag }
  2242. pd.procoptions:=pd.procoptions+proc_direcdata[p].pooption;
  2243. { Call the handler }
  2244. if pointer(proc_direcdata[p].handler)<>nil then
  2245. proc_direcdata[p].handler(pd);
  2246. end;
  2247. function proc_get_importname(pd:tprocdef):string;
  2248. function maybe_cprefix(const s:string):string;
  2249. begin
  2250. if not(pd.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  2251. result:=s
  2252. else
  2253. result:=target_info.Cprefix+s;
  2254. end;
  2255. begin
  2256. result:='';
  2257. if not(po_external in pd.procoptions) then
  2258. internalerror(200412151);
  2259. { external name or number is specified }
  2260. if assigned(pd.import_name) or (pd.import_nr<>0) then
  2261. begin
  2262. if assigned(pd.import_dll) then
  2263. begin
  2264. { If we are not using direct dll linking under win32 then imports
  2265. need to use the normal name since two functions can refer to the
  2266. same DLL function. This is also needed for compatability
  2267. with Delphi and TP7 }
  2268. (*
  2269. case target_info.system of
  2270. system_i386_emx,
  2271. system_i386_os2 :
  2272. begin
  2273. { keep normal mangledname }
  2274. if not (Assigned (PD.Import_Name)) then
  2275. Result := PD.MangledName;
  2276. end;
  2277. else
  2278. *)
  2279. if assigned(pd.import_name) then
  2280. begin
  2281. if target_info.system in (systems_all_windows + systems_nativent +
  2282. [system_i386_emx, system_i386_os2]) then
  2283. { cprefix is not used in DLL imports under Windows or OS/2 }
  2284. result:=pd.import_name^
  2285. else
  2286. result:=maybe_cprefix(pd.import_name^);
  2287. end
  2288. else
  2289. result:=ExtractFileName(pd.import_dll^)+'_index_'+tostr(pd.import_nr);
  2290. end
  2291. else
  2292. result:=maybe_cprefix(pd.import_name^);
  2293. end
  2294. else
  2295. begin
  2296. { Default names when importing variables }
  2297. case pd.proccalloption of
  2298. pocall_cdecl :
  2299. begin
  2300. if assigned(pd._class) then
  2301. result:=target_info.Cprefix+pd._class.objrealname^+'_'+pd.procsym.realname
  2302. else
  2303. result:=target_info.Cprefix+pd.procsym.realname;
  2304. end;
  2305. pocall_cppdecl :
  2306. begin
  2307. result:=target_info.Cprefix+pd.cplusplusmangledname;
  2308. end;
  2309. else
  2310. begin
  2311. {In MacPas a single "external" has the same effect as "external name 'xxx'" }
  2312. if (m_mac in current_settings.modeswitches) then
  2313. result:=tprocdef(pd).procsym.realname;
  2314. end;
  2315. end;
  2316. end;
  2317. end;
  2318. procedure proc_set_mangledname(pd:tprocdef);
  2319. var
  2320. s : string;
  2321. begin
  2322. { When the mangledname is already set we aren't allowed to change
  2323. it because it can already be used somewhere (PFV) }
  2324. if not(po_has_mangledname in pd.procoptions) then
  2325. begin
  2326. if (po_external in pd.procoptions) then
  2327. begin
  2328. { External Procedures are only allowed to change the mangledname
  2329. in their first declaration }
  2330. if (pd.forwarddef or (not pd.hasforward)) then
  2331. begin
  2332. s:=proc_get_importname(pd);
  2333. if s<>'' then
  2334. begin
  2335. { Replace ? and @ in import name, since GNU AS does not allow these characters in symbol names. }
  2336. { This allows to import VC++ mangled names from DLLs. }
  2337. { Do not perform replacement, if external symbol is not imported from DLL. }
  2338. if (target_info.system in systems_all_windows) and (pd.import_dll<>nil) then
  2339. begin
  2340. Replace(s,'?','__q$$');
  2341. {$ifdef arm}
  2342. { @ symbol is not allowed in ARM assembler only }
  2343. Replace(s,'@','__a$$');
  2344. {$endif arm}
  2345. end;
  2346. pd.setmangledname(s);
  2347. end;
  2348. end;
  2349. end
  2350. else
  2351. { Normal procedures }
  2352. begin
  2353. if (po_compilerproc in pd.procoptions) then
  2354. begin
  2355. pd.setmangledname(lower(pd.procsym.name));
  2356. end;
  2357. end;
  2358. end;
  2359. { Public/exported alias names }
  2360. if (([po_public,po_exports]*pd.procoptions)<>[]) and
  2361. not(po_has_public_name in pd.procoptions) then
  2362. begin
  2363. case pd.proccalloption of
  2364. pocall_cdecl :
  2365. begin
  2366. if assigned(pd._class) then
  2367. pd.aliasnames.insert(target_info.Cprefix+pd._class.objrealname^+'_'+pd.procsym.realname)
  2368. else
  2369. begin
  2370. { Export names are not mangled on Windows and OS/2, see also pexports.pas }
  2371. if (target_info.system in (systems_all_windows+[system_i386_emx, system_i386_os2])) and
  2372. (po_exports in pd.procoptions) then
  2373. pd.aliasnames.insert(pd.procsym.realname)
  2374. else
  2375. pd.aliasnames.insert(target_info.Cprefix+pd.procsym.realname);
  2376. end;
  2377. end;
  2378. pocall_cppdecl :
  2379. begin
  2380. pd.aliasnames.insert(target_info.Cprefix+pd.cplusplusmangledname);
  2381. end;
  2382. end;
  2383. { prevent adding the alias a second time }
  2384. include(pd.procoptions,po_has_public_name);
  2385. end;
  2386. end;
  2387. procedure handle_calling_convention(pd:tabstractprocdef);
  2388. begin
  2389. { set the default calling convention if none provided }
  2390. if (pd.typ=procdef) and
  2391. (is_objc_class_or_protocol(tprocdef(pd)._class) or
  2392. is_cppclass(tprocdef(pd)._class)) then
  2393. begin
  2394. { none of the explicit calling conventions should be allowed }
  2395. if (po_hascallingconvention in pd.procoptions) then
  2396. internalerror(2009032501);
  2397. if is_cppclass(tprocdef(pd)._class) then
  2398. pd.proccalloption:=pocall_cppdecl
  2399. else
  2400. pd.proccalloption:=pocall_cdecl;
  2401. end
  2402. else if not(po_hascallingconvention in pd.procoptions) then
  2403. pd.proccalloption:=current_settings.defproccall
  2404. else
  2405. begin
  2406. if pd.proccalloption=pocall_none then
  2407. internalerror(200309081);
  2408. end;
  2409. { handle proccall specific settings }
  2410. case pd.proccalloption of
  2411. pocall_cdecl,
  2412. pocall_cppdecl :
  2413. begin
  2414. { check C cdecl para types }
  2415. check_c_para(pd);
  2416. end;
  2417. pocall_far16 :
  2418. begin
  2419. { Temporary stub, must be rewritten to support OS/2 far16 }
  2420. Message1(parser_w_proc_directive_ignored,'FAR16');
  2421. end;
  2422. end;
  2423. { Inlining is enabled and supported? }
  2424. if (po_inline in pd.procoptions) and
  2425. not(cs_do_inline in current_settings.localswitches) then
  2426. begin
  2427. { Give an error if inline is not supported by the compiler mode,
  2428. otherwise only give a warning that this procedure will not be inlined }
  2429. if not(m_default_inline in current_settings.modeswitches) then
  2430. Message(parser_e_proc_inline_not_supported)
  2431. else
  2432. Message(parser_w_inlining_disabled);
  2433. exclude(pd.procoptions,po_inline);
  2434. end;
  2435. { For varargs directive also cdecl and external must be defined }
  2436. if (po_varargs in pd.procoptions) then
  2437. begin
  2438. { check first for external in the interface, if available there
  2439. then the cdecl must also be there since there is no implementation
  2440. available to contain it }
  2441. if parse_only then
  2442. begin
  2443. { if external is available, then cdecl must also be available,
  2444. procvars don't need external }
  2445. if not((po_external in pd.procoptions) or
  2446. (pd.typ=procvardef) or
  2447. { for objcclasses this is checked later, because the entire
  2448. class may be external. }
  2449. is_objc_class_or_protocol(tprocdef(pd)._class)) and
  2450. not(pd.proccalloption in (cdecl_pocalls + [pocall_mwpascal])) then
  2451. Message(parser_e_varargs_need_cdecl_and_external);
  2452. end
  2453. else
  2454. begin
  2455. { both must be defined now }
  2456. if not((po_external in pd.procoptions) or
  2457. (pd.typ=procvardef)) or
  2458. not(pd.proccalloption in (cdecl_pocalls + [pocall_mwpascal])) then
  2459. Message(parser_e_varargs_need_cdecl_and_external);
  2460. end;
  2461. end;
  2462. { insert hidden high parameters }
  2463. pd.parast.SymList.ForEachCall(@insert_hidden_para,pd);
  2464. { insert hidden self parameter }
  2465. insert_self_and_vmt_para(pd);
  2466. { insert funcret parameter if required }
  2467. insert_funcret_para(pd);
  2468. { Make var parameters regable, this must be done after the calling
  2469. convention is set. }
  2470. { this must be done before parentfp is insert, because getting all cases
  2471. where parentfp must be in a memory location isn't catched properly so
  2472. we put parentfp never in a register }
  2473. pd.parast.SymList.ForEachCall(@set_addr_param_regable,pd);
  2474. { insert parentfp parameter if required }
  2475. insert_parentfp_para(pd);
  2476. { Calculate parameter tlist }
  2477. pd.calcparas;
  2478. end;
  2479. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  2480. {
  2481. Parse the procedure directives. It does not matter if procedure directives
  2482. are written using ;procdir; or ['procdir'] syntax.
  2483. }
  2484. var
  2485. res : boolean;
  2486. begin
  2487. if (m_mac in current_settings.modeswitches) and (cs_externally_visible in current_settings.localswitches) then
  2488. begin
  2489. tprocdef(pd).aliasnames.insert(tprocdef(pd).procsym.realname);
  2490. include(pd.procoptions,po_public);
  2491. include(pd.procoptions,po_has_public_name);
  2492. include(pd.procoptions,po_global);
  2493. end;
  2494. { Class constructors and destructor are static class methods in real. }
  2495. { There are many places in the compiler where either class or static }
  2496. { method flag changes the behavior. It is simplier to add them to }
  2497. { the class constructors/destructors options than to fix all the }
  2498. { occurencies. (Paul) }
  2499. if pd.proctypeoption in [potype_class_constructor,potype_class_destructor] then
  2500. begin
  2501. include(pd.procoptions,po_classmethod);
  2502. include(pd.procoptions,po_staticmethod);
  2503. end;
  2504. while token in [_ID,_LECKKLAMMER] do
  2505. begin
  2506. if try_to_consume(_LECKKLAMMER) then
  2507. begin
  2508. repeat
  2509. parse_proc_direc(pd,pdflags);
  2510. until not try_to_consume(_COMMA);
  2511. consume(_RECKKLAMMER);
  2512. { we always expect at least '[];' }
  2513. res:=true;
  2514. end
  2515. else
  2516. begin
  2517. res:=parse_proc_direc(pd,pdflags);
  2518. end;
  2519. { A procedure directive normally followed by a semicolon, but in
  2520. a const section or reading a type we should stop when _EQUAL is found,
  2521. because a constant/default value follows }
  2522. if res then
  2523. begin
  2524. if (block_type=bt_const_type) and
  2525. (token=_EQUAL) then
  2526. break;
  2527. { support procedure proc;stdcall export; }
  2528. if not(check_proc_directive((pd.typ=procvardef))) then
  2529. begin
  2530. { support "record p : procedure stdcall end;" and
  2531. "var p : procedure stdcall = nil;" }
  2532. if (pd_procvar in pdflags) and
  2533. (token in [_END,_RKLAMMER,_EQUAL]) then
  2534. break
  2535. else
  2536. begin
  2537. if (token=_COLON) then
  2538. begin
  2539. Message(parser_e_field_not_allowed_here);
  2540. consume_all_until(_SEMICOLON);
  2541. end;
  2542. consume(_SEMICOLON)
  2543. end;
  2544. end;
  2545. end
  2546. else
  2547. break;
  2548. end;
  2549. end;
  2550. procedure parse_var_proc_directives(sym:tsym);
  2551. var
  2552. pdflags : tpdflags;
  2553. pd : tabstractprocdef;
  2554. begin
  2555. pdflags:=[pd_procvar];
  2556. pd:=nil;
  2557. case sym.typ of
  2558. fieldvarsym,
  2559. staticvarsym,
  2560. localvarsym,
  2561. paravarsym :
  2562. pd:=tabstractprocdef(tabstractvarsym(sym).vardef);
  2563. typesym :
  2564. pd:=tabstractprocdef(ttypesym(sym).typedef);
  2565. else
  2566. internalerror(2003042617);
  2567. end;
  2568. if pd.typ<>procvardef then
  2569. internalerror(2003042618);
  2570. { names should never be used anyway }
  2571. parse_proc_directives(pd,pdflags);
  2572. end;
  2573. procedure parse_object_proc_directives(pd:tabstractprocdef);
  2574. var
  2575. pdflags : tpdflags;
  2576. begin
  2577. pdflags:=[pd_object];
  2578. parse_proc_directives(pd,pdflags);
  2579. end;
  2580. function proc_add_definition(var currpd:tprocdef):boolean;
  2581. {
  2582. Add definition aprocdef to the overloaded definitions of aprocsym. If a
  2583. forwarddef is found and reused it returns true
  2584. }
  2585. var
  2586. fwpd : tprocdef;
  2587. currparasym,
  2588. fwparasym : tsym;
  2589. currparacnt,
  2590. fwparacnt,
  2591. curridx,
  2592. fwidx,
  2593. i : longint;
  2594. po_comp : tprocoptions;
  2595. paracompopt: tcompare_paras_options;
  2596. forwardfound : boolean;
  2597. symentry: TSymEntry;
  2598. begin
  2599. forwardfound:=false;
  2600. { check overloaded functions if the same function already exists }
  2601. for i:=0 to tprocsym(currpd.procsym).ProcdefList.Count-1 do
  2602. begin
  2603. fwpd:=tprocdef(tprocsym(currpd.procsym).ProcdefList[i]);
  2604. { Skip overloaded definitions that are declared in other units }
  2605. if fwpd.procsym<>currpd.procsym then
  2606. continue;
  2607. { check the parameters, for delphi/tp it is possible to
  2608. leave the parameters away in the implementation (forwarddef=false).
  2609. But for an overload declared function this is not allowed }
  2610. if { check if empty implementation arguments match is allowed }
  2611. (
  2612. not(m_repeat_forward in current_settings.modeswitches) and
  2613. not(currpd.forwarddef) and
  2614. is_bareprocdef(currpd) and
  2615. not(po_overload in fwpd.procoptions)
  2616. ) or
  2617. { check arguments, we need to check only the user visible parameters. The hidden parameters
  2618. can be in a different location because of the calling convention, eg. L-R vs. R-L order (PFV) }
  2619. (
  2620. (compare_paras(currpd.paras,fwpd.paras,cp_none,[cpo_comparedefaultvalue,cpo_ignorehidden,cpo_openequalisexact,cpo_ignoreuniv])=te_exact) and
  2621. (fwpd.returndef=currpd.returndef)
  2622. ) then
  2623. begin
  2624. { Check if we've found the forwarddef, if found then
  2625. we need to update the forward def with the current
  2626. implementation settings }
  2627. if fwpd.forwarddef then
  2628. begin
  2629. forwardfound:=true;
  2630. if not(m_repeat_forward in current_settings.modeswitches) and
  2631. (fwpd.proccalloption<>currpd.proccalloption) then
  2632. paracompopt:=[cpo_ignorehidden,cpo_comparedefaultvalue,cpo_openequalisexact,cpo_ignoreuniv]
  2633. else
  2634. paracompopt:=[cpo_comparedefaultvalue,cpo_openequalisexact,cpo_ignoreuniv];
  2635. { Check calling convention }
  2636. if (fwpd.proccalloption<>currpd.proccalloption) then
  2637. begin
  2638. { In delphi it is possible to specify the calling
  2639. convention in the interface or implementation if
  2640. there was no convention specified in the other
  2641. part }
  2642. if (m_delphi in current_settings.modeswitches) then
  2643. begin
  2644. if not(po_hascallingconvention in currpd.procoptions) then
  2645. currpd.proccalloption:=fwpd.proccalloption
  2646. else
  2647. if not(po_hascallingconvention in fwpd.procoptions) then
  2648. fwpd.proccalloption:=currpd.proccalloption
  2649. else
  2650. begin
  2651. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  2652. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2653. { restore interface settings }
  2654. currpd.proccalloption:=fwpd.proccalloption;
  2655. end;
  2656. end
  2657. else
  2658. begin
  2659. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  2660. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2661. { restore interface settings }
  2662. currpd.proccalloption:=fwpd.proccalloption;
  2663. end;
  2664. end;
  2665. { Check static }
  2666. if (po_staticmethod in fwpd.procoptions) then
  2667. begin
  2668. if not (po_staticmethod in currpd.procoptions) then
  2669. begin
  2670. include(currpd.procoptions, po_staticmethod);
  2671. if (po_classmethod in currpd.procoptions) then
  2672. begin
  2673. { remove self from the hidden paras }
  2674. symentry:=currpd.parast.Find('self');
  2675. if symentry<>nil then
  2676. begin
  2677. currpd.parast.Delete(symentry);
  2678. currpd.calcparas;
  2679. end;
  2680. end;
  2681. end;
  2682. end;
  2683. { Check if the procedure type and return type are correct,
  2684. also the parameters must match also with the type }
  2685. if ((m_repeat_forward in current_settings.modeswitches) or
  2686. not is_bareprocdef(currpd)) and
  2687. ((compare_paras(currpd.paras,fwpd.paras,cp_all,paracompopt)<>te_exact) or
  2688. (fwpd.returndef<>currpd.returndef)) then
  2689. begin
  2690. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  2691. fwpd.fullprocname(false));
  2692. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2693. break;
  2694. end;
  2695. { Check if both are declared forward }
  2696. if fwpd.forwarddef and currpd.forwarddef then
  2697. begin
  2698. MessagePos1(currpd.fileinfo,parser_e_function_already_declared_public_forward,
  2699. currpd.fullprocname(false));
  2700. end;
  2701. { internconst or internproc only need to be defined once }
  2702. if (fwpd.proccalloption=pocall_internproc) then
  2703. currpd.proccalloption:=fwpd.proccalloption
  2704. else
  2705. if (currpd.proccalloption=pocall_internproc) then
  2706. fwpd.proccalloption:=currpd.proccalloption;
  2707. { Check procedure options, Delphi requires that class is
  2708. repeated in the implementation for class methods }
  2709. if (m_fpc in current_settings.modeswitches) then
  2710. po_comp:=[po_classmethod,po_varargs,po_methodpointer,po_interrupt]
  2711. else
  2712. po_comp:=[po_classmethod,po_methodpointer];
  2713. if ((po_comp * fwpd.procoptions)<>(po_comp * currpd.procoptions)) or
  2714. (fwpd.proctypeoption <> currpd.proctypeoption) then
  2715. begin
  2716. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  2717. fwpd.fullprocname(false));
  2718. tprocsym(fwpd.procsym).write_parameter_lists(fwpd);
  2719. { This error is non-fatal, we can recover }
  2720. end;
  2721. { Forward declaration is external? }
  2722. if (po_external in fwpd.procoptions) then
  2723. MessagePos(currpd.fileinfo,parser_e_proc_already_external);
  2724. { Check parameters }
  2725. if (m_repeat_forward in current_settings.modeswitches) or
  2726. (currpd.minparacount>0) then
  2727. begin
  2728. { If mangled names are equal then they have the same amount of arguments }
  2729. { We can check the names of the arguments }
  2730. { both symtables are in the same order from left to right }
  2731. curridx:=0;
  2732. fwidx:=0;
  2733. currparacnt:=currpd.parast.SymList.Count;
  2734. fwparacnt:=fwpd.parast.SymList.Count;
  2735. repeat
  2736. { skip default parameter constsyms }
  2737. while (curridx<currparacnt) and
  2738. (tsym(currpd.parast.SymList[curridx]).typ<>paravarsym) do
  2739. inc(curridx);
  2740. while (fwidx<fwparacnt) and
  2741. (tsym(fwpd.parast.SymList[fwidx]).typ<>paravarsym) do
  2742. inc(fwidx);
  2743. { stop when one of the two lists is at the end }
  2744. if (fwidx>=fwparacnt) or (curridx>=currparacnt) then
  2745. break;
  2746. { compare names of parameters, ignore implictly
  2747. renamed parameters }
  2748. currparasym:=tsym(currpd.parast.SymList[curridx]);
  2749. fwparasym:=tsym(fwpd.parast.SymList[fwidx]);
  2750. if not(sp_implicitrename in currparasym.symoptions) and
  2751. not(sp_implicitrename in fwparasym.symoptions) then
  2752. begin
  2753. if (currparasym.name<>fwparasym.name) then
  2754. begin
  2755. MessagePos3(currpd.fileinfo,parser_e_header_different_var_names,
  2756. tprocsym(currpd.procsym).realname,fwparasym.realname,currparasym.realname);
  2757. break;
  2758. end;
  2759. end;
  2760. { next parameter }
  2761. inc(curridx);
  2762. inc(fwidx);
  2763. until false;
  2764. end;
  2765. { Everything is checked, now we can update the forward declaration
  2766. with the new data from the implementation }
  2767. fwpd.forwarddef:=currpd.forwarddef;
  2768. fwpd.hasforward:=true;
  2769. fwpd.procoptions:=fwpd.procoptions+currpd.procoptions;
  2770. { marked as local but exported from unit? }
  2771. if (po_kylixlocal in fwpd.procoptions) and (fwpd.owner.symtabletype=globalsymtable) then
  2772. MessagePos(fwpd.fileinfo,type_e_cant_export_local);
  2773. if fwpd.extnumber=$ffff then
  2774. fwpd.extnumber:=currpd.extnumber;
  2775. while not currpd.aliasnames.empty do
  2776. fwpd.aliasnames.insert(currpd.aliasnames.getfirst);
  2777. { update fileinfo so position references the implementation,
  2778. also update funcretsym if it is already generated }
  2779. fwpd.fileinfo:=currpd.fileinfo;
  2780. if assigned(fwpd.funcretsym) then
  2781. fwpd.funcretsym.fileinfo:=currpd.fileinfo;
  2782. if assigned(currpd.deprecatedmsg) then
  2783. begin
  2784. stringdispose(fwpd.deprecatedmsg);
  2785. fwpd.deprecatedmsg:=stringdup(currpd.deprecatedmsg^);
  2786. end;
  2787. { import names }
  2788. if assigned(currpd.import_dll) then
  2789. begin
  2790. stringdispose(fwpd.import_dll);
  2791. fwpd.import_dll:=stringdup(currpd.import_dll^);
  2792. end;
  2793. if assigned(currpd.import_name) then
  2794. begin
  2795. stringdispose(fwpd.import_name);
  2796. fwpd.import_name:=stringdup(currpd.import_name^);
  2797. end;
  2798. fwpd.import_nr:=currpd.import_nr;
  2799. { for compilerproc defines we need to rename and update the
  2800. symbolname to lowercase }
  2801. if (po_compilerproc in fwpd.procoptions) then
  2802. begin
  2803. { rename to lowercase so users can't access it }
  2804. fwpd.procsym.realname:='$'+lower(fwpd.procsym.name);
  2805. { the mangeled name is already changed by the pd_compilerproc }
  2806. { handler. It must be done immediately because if we have a }
  2807. { call to a compilerproc before it's implementation is }
  2808. { encountered, it must already use the new mangled name (JM) }
  2809. end;
  2810. { Release current procdef }
  2811. currpd.owner.deletedef(currpd);
  2812. currpd:=fwpd;
  2813. end
  2814. else
  2815. begin
  2816. { abstract methods aren't forward defined, but this }
  2817. { needs another error message }
  2818. if (po_abstractmethod in fwpd.procoptions) then
  2819. MessagePos(currpd.fileinfo,parser_e_abstract_no_definition)
  2820. else
  2821. begin
  2822. MessagePos(currpd.fileinfo,parser_e_overloaded_have_same_parameters);
  2823. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2824. end;
  2825. end;
  2826. { we found one proc with the same arguments, there are no others
  2827. so we can stop }
  2828. break;
  2829. end;
  2830. { check for allowing overload directive }
  2831. if not(m_fpc in current_settings.modeswitches) then
  2832. begin
  2833. { overload directive turns on overloading }
  2834. if ((po_overload in currpd.procoptions) or
  2835. (po_overload in fwpd.procoptions)) then
  2836. begin
  2837. { check if all procs have overloading, but not if the proc is a method or
  2838. already declared forward, then the check is already done }
  2839. if not(fwpd.hasforward or
  2840. assigned(currpd._class) or
  2841. (currpd.forwarddef<>fwpd.forwarddef) or
  2842. ((po_overload in currpd.procoptions) and
  2843. (po_overload in fwpd.procoptions))) then
  2844. begin
  2845. MessagePos1(currpd.fileinfo,parser_e_no_overload_for_all_procs,currpd.procsym.realname);
  2846. break;
  2847. end;
  2848. end
  2849. else
  2850. begin
  2851. if not(fwpd.forwarddef) then
  2852. begin
  2853. if (m_tp7 in current_settings.modeswitches) then
  2854. MessagePos(currpd.fileinfo,parser_e_procedure_overloading_is_off)
  2855. else
  2856. MessagePos1(currpd.fileinfo,parser_e_no_overload_for_all_procs,currpd.procsym.realname);
  2857. break;
  2858. end;
  2859. end;
  2860. end; { equal arguments }
  2861. end;
  2862. { if we didn't reuse a forwarddef then we add the procdef to the overloaded
  2863. list }
  2864. if not forwardfound then
  2865. begin
  2866. { can happen in Delphi mode }
  2867. if (currpd.proctypeoption = potype_function) and
  2868. is_void(currpd.returndef) then
  2869. MessagePos1(currpd.fileinfo,parser_e_no_funcret_specified,currpd.procsym.realname);
  2870. tprocsym(currpd.procsym).ProcdefList.Add(currpd);
  2871. end;
  2872. proc_add_definition:=forwardfound;
  2873. end;
  2874. end.