pdecsub.pas 106 KB

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