pdecsub.pas 103 KB

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