pdecsub.pas 101 KB

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