pdecsub.pas 115 KB

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