pdecsub.pas 103 KB

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