2
0

pdecsub.pas 104 KB

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