pdecsub.pas 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl, Daniel Mantione
  4. Does the parsing of the procedures/functions
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit pdecsub;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. tokens,symconst,symtype,symdef,symsym;
  23. type
  24. tpdflag=(
  25. pd_body, { directive needs a body }
  26. pd_implemen, { directive can be used implementation section }
  27. pd_interface, { directive can be used interface section }
  28. pd_object, { directive can be used object declaration }
  29. pd_procvar, { directive can be used procvar declaration }
  30. pd_notobject, { directive can not be used object declaration }
  31. pd_notobjintf, { directive can not be used interface declaration }
  32. pd_notprocvar { directive can not be used procvar declaration }
  33. );
  34. tpdflags=set of tpdflag;
  35. function is_proc_directive(tok:ttoken;isprocvar:boolean):boolean;
  36. procedure calc_parast(pd:tabstractprocdef);
  37. procedure insert_funcret_local(pd:tprocdef);
  38. function proc_add_definition(var pd:tprocdef):boolean;
  39. procedure handle_calling_convention(pd:tabstractprocdef);
  40. procedure parse_parameter_dec(pd:tabstractprocdef);
  41. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  42. procedure parse_var_proc_directives(sym:tsym);
  43. procedure parse_object_proc_directives(pd:tabstractprocdef);
  44. function parse_proc_head(aclass:tobjectdef;potype:tproctypeoption;var pd:tprocdef):boolean;
  45. function parse_proc_dec(aclass:tobjectdef):tprocdef;
  46. implementation
  47. uses
  48. {$ifdef delphi}
  49. sysutils,
  50. {$else delphi}
  51. strings,
  52. {$endif delphi}
  53. { common }
  54. cutils,cclasses,
  55. { global }
  56. globtype,globals,verbose,
  57. systems,
  58. cpuinfo,
  59. { aasm }
  60. aasmbase,
  61. { symtable }
  62. symbase,symtable,defutil,defcmp,paramgr,cpupara,
  63. { pass 1 }
  64. node,htypechk,
  65. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,
  66. { parser }
  67. fmodule,scanner,
  68. pbase,pexpr,ptype,pdecl,
  69. { linking }
  70. import,gendef
  71. ;
  72. const
  73. { Please leave this here, this module should NOT use
  74. these variables.
  75. Declaring it as string here results in an error when compiling (PFV) }
  76. current_procinfo = 'error';
  77. procedure insert_funcret_para(pd:tabstractprocdef);
  78. var
  79. storepos : tfileposinfo;
  80. vs : tvarsym;
  81. begin
  82. if not(pd.proctypeoption in [potype_constructor,potype_destructor]) and
  83. not is_void(pd.rettype.def) and
  84. paramanager.ret_in_param(pd.rettype.def,pd.proccalloption) then
  85. begin
  86. storepos:=akttokenpos;
  87. if pd.deftype=procdef then
  88. akttokenpos:=tprocdef(pd).fileinfo;
  89. { Generate result variable accessing function result }
  90. vs:=tvarsym.create('$result',vs_var,pd.rettype);
  91. include(vs.varoptions,vo_is_funcret);
  92. include(vs.varoptions,vo_regable);
  93. pd.parast.insert(vs);
  94. { For left to right add it at the end to be delphi compatible }
  95. if pd.proccalloption in pushleftright_pocalls then
  96. pd.concatpara(nil,vs.vartype,vs,nil,true)
  97. else
  98. pd.insertpara(vs.vartype,vs,nil,true);
  99. { Store the this symbol as funcretsym for procedures }
  100. if pd.deftype=procdef then
  101. tprocdef(pd).funcretsym:=vs;
  102. akttokenpos:=storepos;
  103. end;
  104. end;
  105. procedure insert_parentfp_para(pd:tabstractprocdef);
  106. var
  107. storepos : tfileposinfo;
  108. vs : tvarsym;
  109. begin
  110. if pd.parast.symtablelevel>normal_function_level then
  111. begin
  112. storepos:=akttokenpos;
  113. if pd.deftype=procdef then
  114. akttokenpos:=tprocdef(pd).fileinfo;
  115. { Generate result variable accessing function result }
  116. vs:=tvarsym.create('$parentfp',vs_var,voidpointertype);
  117. include(vs.varoptions,vo_is_parentfp);
  118. pd.parast.insert(vs);
  119. pd.insertpara(vs.vartype,vs,nil,true);
  120. akttokenpos:=storepos;
  121. end;
  122. end;
  123. procedure insert_self_and_vmt_para(pd:tabstractprocdef);
  124. var
  125. storepos : tfileposinfo;
  126. vs : tvarsym;
  127. tt : ttype;
  128. vsp : tvarspez;
  129. begin
  130. if (pd.deftype=procvardef) and
  131. pd.is_methodpointer then
  132. begin
  133. { Generate self variable }
  134. tt:=voidpointertype;
  135. vs:=tvarsym.create('$self',vs_value,tt);
  136. include(vs.varoptions,vo_is_self);
  137. { Insert as hidden parameter }
  138. pd.parast.insert(vs);
  139. pd.insertpara(vs.vartype,vs,nil,true);
  140. end
  141. else
  142. begin
  143. if (pd.deftype=procdef) and
  144. assigned(tprocdef(pd)._class) and
  145. (pd.parast.symtablelevel=normal_function_level) then
  146. begin
  147. storepos:=akttokenpos;
  148. akttokenpos:=tprocdef(pd).fileinfo;
  149. { Generate VMT variable for constructor/destructor }
  150. if pd.proctypeoption in [potype_constructor,potype_destructor] then
  151. begin
  152. { can't use classrefdef as type because inheriting
  153. will then always file because of a type mismatch }
  154. tt:=voidpointertype;
  155. vs:=tvarsym.create('$vmt',vs_value,tt);
  156. include(vs.varoptions,vo_is_vmt);
  157. { Insert as hidden parameter }
  158. pd.parast.insert(vs);
  159. pd.insertpara(vs.vartype,vs,nil,true);
  160. end;
  161. { Generate self variable, for classes we need
  162. to use the generic voidpointer to be compatible with
  163. methodpointers }
  164. vsp:=vs_value;
  165. if (po_staticmethod in pd.procoptions) or
  166. (po_classmethod in pd.procoptions) then
  167. begin
  168. tt.setdef(tprocdef(pd)._class);
  169. tt.setdef(tclassrefdef.create(tt));
  170. end
  171. else
  172. begin
  173. if is_object(tprocdef(pd)._class) then
  174. vsp:=vs_var;
  175. tt.setdef(tprocdef(pd)._class);
  176. end;
  177. vs:=tvarsym.create('$self',vsp,tt);
  178. include(vs.varoptions,vo_is_self);
  179. include(vs.varoptions,vo_regable);
  180. { Insert as hidden parameter }
  181. pd.parast.insert(vs);
  182. pd.insertpara(vs.vartype,vs,nil,true);
  183. akttokenpos:=storepos;
  184. end;
  185. end;
  186. end;
  187. procedure insert_funcret_local(pd:tprocdef);
  188. var
  189. storepos : tfileposinfo;
  190. vs : tvarsym;
  191. sl : tsymlist;
  192. begin
  193. { The result from constructors and destructors can't be accessed directly }
  194. if not(pd.proctypeoption in [potype_constructor,potype_destructor]) and
  195. not is_void(pd.rettype.def) then
  196. begin
  197. storepos:=akttokenpos;
  198. akttokenpos:=pd.fileinfo;
  199. { We always need a localsymtable }
  200. if not assigned(pd.localst) then
  201. pd.insert_localst;
  202. { We need to insert a varsym for the result in the localst
  203. when it is returning in a register }
  204. if not paramanager.ret_in_param(pd.rettype.def,pd.proccalloption) then
  205. begin
  206. vs:=tvarsym.create('$result',vs_value,pd.rettype);
  207. include(vs.varoptions,vo_is_funcret);
  208. if tstoreddef(pd.rettype.def).is_intregable then
  209. include(vs.varoptions,vo_regable);
  210. if tstoreddef(pd.rettype.def).is_fpuregable then
  211. include(vs.varoptions,vo_fpuregable);
  212. pd.localst.insert(vs);
  213. pd.funcretsym:=vs;
  214. end;
  215. { insert the name of the procedure as alias for the function result,
  216. we can't use realname because that will not work for compilerprocs
  217. as the name is lowercase and unreachable from the code }
  218. if pd.resultname='' then
  219. pd.resultname:=pd.procsym.name;
  220. sl:=tsymlist.create;
  221. sl.addsym(sl_load,pd.funcretsym);
  222. vs:=tabsolutesym.create_ref(pd.resultname,pd.rettype,sl);
  223. include(vs.varoptions,vo_is_funcret);
  224. pd.localst.insert(vs);
  225. { insert result also if support is on }
  226. if (m_result in aktmodeswitches) then
  227. begin
  228. sl:=tsymlist.create;
  229. sl.addsym(sl_load,pd.funcretsym);
  230. vs:=tabsolutesym.create_ref('RESULT',pd.rettype,sl);
  231. include(vs.varoptions,vo_is_funcret);
  232. include(vs.varoptions,vo_is_result);
  233. pd.localst.insert(vs);
  234. end;
  235. akttokenpos:=storepos;
  236. end;
  237. end;
  238. procedure insert_hidden_para(pd:tabstractprocdef);
  239. var
  240. currpara : tparaitem;
  241. hvs : tvarsym;
  242. begin
  243. { walk from right to left, so we can insert the
  244. high parameters after the current parameter }
  245. currpara:=tparaitem(pd.para.last);
  246. while assigned(currpara) do
  247. begin
  248. { needs high parameter ? }
  249. if paramanager.push_high_param(currpara.paratyp,currpara.paratype.def,pd.proccalloption) then
  250. begin
  251. if assigned(currpara.parasym) then
  252. begin
  253. hvs:=tvarsym.create('$high'+tvarsym(currpara.parasym).name,vs_const,sinttype);
  254. include(hvs.varoptions,vo_is_high_value);
  255. tvarsym(currpara.parasym).owner.insert(hvs);
  256. end
  257. else
  258. hvs:=nil;
  259. pd.concatpara(currpara,sinttype,hvs,nil,true);
  260. end
  261. else
  262. begin
  263. { Give a warning that cdecl routines does not include high()
  264. support }
  265. if (pd.proccalloption in [pocall_cdecl,pocall_cppdecl]) and
  266. paramanager.push_high_param(currpara.paratyp,currpara.paratype.def,pocall_default) then
  267. begin
  268. if is_open_string(currpara.paratype.def) then
  269. Message(parser_w_cdecl_no_openstring);
  270. if not (po_external in pd.procoptions) then
  271. Message(parser_w_cdecl_has_no_high);
  272. end;
  273. end;
  274. currpara:=tparaitem(currpara.previous);
  275. end;
  276. end;
  277. procedure check_c_para(p:tnamedindexitem;arg:pointer);
  278. begin
  279. if (tsym(p).typ<>varsym) then
  280. exit;
  281. with tvarsym(p) do
  282. begin
  283. case vartype.def.deftype of
  284. arraydef :
  285. begin
  286. if not is_variant_array(vartype.def) and
  287. not is_array_of_const(vartype.def) then
  288. begin
  289. if (varspez<>vs_var) then
  290. Message(parser_h_c_arrays_are_references);
  291. end;
  292. if is_array_of_const(vartype.def) and
  293. assigned(indexnext) and
  294. (tsym(indexnext).typ=varsym) and
  295. not(vo_is_high_value in tvarsym(indexnext).varoptions) then
  296. Message(parser_e_C_array_of_const_must_be_last);
  297. end;
  298. end;
  299. end;
  300. end;
  301. procedure parse_parameter_dec(pd:tabstractprocdef);
  302. {
  303. handle_procvar needs the same changes
  304. }
  305. var
  306. is_procvar : boolean;
  307. sc : tsinglelist;
  308. tt : ttype;
  309. arrayelementtype : ttype;
  310. vs : tvarsym;
  311. srsym : tsym;
  312. hs1 : string;
  313. varspez : Tvarspez;
  314. defaultvalue : tconstsym;
  315. defaultrequired : boolean;
  316. old_object_option : tsymoptions;
  317. currparast : tparasymtable;
  318. explicit_paraloc : boolean;
  319. locationstr : string;
  320. begin
  321. explicit_paraloc:=false;
  322. consume(_LKLAMMER);
  323. { Delphi/Kylix supports nonsense like }
  324. { procedure p(); }
  325. if try_to_consume(_RKLAMMER) and
  326. not(m_tp7 in aktmodeswitches) then
  327. exit;
  328. { parsing a proc or procvar ? }
  329. is_procvar:=(pd.deftype=procvardef);
  330. currparast:=tparasymtable(pd.parast);
  331. { reset }
  332. sc:=tsinglelist.create;
  333. defaultrequired:=false;
  334. { the variables are always public }
  335. old_object_option:=current_object_option;
  336. current_object_option:=[sp_public];
  337. inc(testcurobject);
  338. repeat
  339. if try_to_consume(_VAR) then
  340. varspez:=vs_var
  341. else
  342. if try_to_consume(_CONST) then
  343. varspez:=vs_const
  344. else
  345. if (idtoken=_OUT) and (m_out in aktmodeswitches) then
  346. begin
  347. consume(_OUT);
  348. varspez:=vs_out
  349. end
  350. else
  351. if (token=_POINTPOINTPOINT) and (m_mac in aktmodeswitches) then
  352. begin
  353. consume(_POINTPOINTPOINT);
  354. include(pd.procoptions,po_varargs);
  355. break;
  356. end
  357. else
  358. varspez:=vs_value;
  359. defaultvalue:=nil;
  360. tt.reset;
  361. { read identifiers and insert with error type }
  362. sc.reset;
  363. repeat
  364. vs:=tvarsym.create(orgpattern,varspez,generrortype);
  365. currparast.insert(vs);
  366. if assigned(vs.owner) then
  367. sc.insert(vs)
  368. else
  369. vs.free;
  370. consume(_ID);
  371. until not try_to_consume(_COMMA);
  372. locationstr:='';
  373. { read type declaration, force reading for value and const paras }
  374. if (token=_COLON) or (varspez=vs_value) then
  375. begin
  376. consume(_COLON);
  377. { check for an open array }
  378. if token=_ARRAY then
  379. begin
  380. consume(_ARRAY);
  381. consume(_OF);
  382. { define range and type of range }
  383. tt.setdef(tarraydef.create(0,-1,s32inttype));
  384. { array of const ? }
  385. if (token=_CONST) and (m_objpas in aktmodeswitches) then
  386. begin
  387. consume(_CONST);
  388. srsym:=searchsymonlyin(systemunit,'TVARREC');
  389. if not assigned(srsym) then
  390. InternalError(200404181);
  391. tarraydef(tt.def).setelementtype(ttypesym(srsym).restype);
  392. tarraydef(tt.def).IsArrayOfConst:=true;
  393. end
  394. else
  395. begin
  396. { define field type }
  397. single_type(arrayelementtype,hs1,false);
  398. tarraydef(tt.def).setelementtype(arrayelementtype);
  399. end;
  400. end
  401. else
  402. begin
  403. { open string ? }
  404. if (varspez=vs_var) and
  405. (
  406. (
  407. ((token=_STRING) or (idtoken=_SHORTSTRING)) and
  408. (cs_openstring in aktmoduleswitches) and
  409. not(cs_ansistrings in aktlocalswitches)
  410. ) or
  411. (idtoken=_OPENSTRING)) then
  412. begin
  413. consume(token);
  414. tt:=openshortstringtype;
  415. hs1:='openstring';
  416. end
  417. else
  418. begin
  419. { everything else }
  420. if (m_mac in aktmodeswitches) then
  421. try_to_consume(_UNIV); {currently does nothing}
  422. single_type(tt,hs1,false);
  423. end;
  424. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) then
  425. begin
  426. if (idtoken=_LOCATION) then
  427. begin
  428. consume(_LOCATION);
  429. locationstr:=pattern;
  430. consume(_CSTRING);
  431. end
  432. else
  433. begin
  434. if explicit_paraloc then
  435. Message(parser_e_paraloc_all_paras);
  436. locationstr:='';
  437. end;
  438. end
  439. else
  440. locationstr:='';
  441. { default parameter }
  442. if (m_default_para in aktmodeswitches) then
  443. begin
  444. if try_to_consume(_EQUAL) then
  445. begin
  446. vs:=tvarsym(sc.first);
  447. if assigned(vs.listnext) then
  448. Message(parser_e_default_value_only_one_para);
  449. { prefix 'def' to the parameter name }
  450. defaultvalue:=ReadConstant('$def'+vs.name,vs.fileinfo);
  451. include(defaultvalue.symoptions,sp_internal);
  452. if assigned(defaultvalue) then
  453. tprocdef(pd).parast.insert(defaultvalue);
  454. defaultrequired:=true;
  455. end
  456. else
  457. begin
  458. if defaultrequired then
  459. Message1(parser_e_default_value_expected_for_para,vs.name);
  460. end;
  461. end;
  462. end;
  463. end
  464. else
  465. begin
  466. {$ifndef UseNiceNames}
  467. hs1:='$$$';
  468. {$else UseNiceNames}
  469. hs1:='var';
  470. {$endif UseNiceNames}
  471. tt:=cformaltype;
  472. end;
  473. { File types are only allowed for var parameters }
  474. if (tt.def.deftype=filedef) and
  475. (varspez<>vs_var) then
  476. CGMessage(cg_e_file_must_call_by_reference);
  477. vs:=tvarsym(sc.first);
  478. while assigned(vs) do
  479. begin
  480. { update varsym }
  481. vs.vartype:=tt;
  482. { For proc vars we only need the definitions }
  483. if not is_procvar then
  484. begin
  485. if (varspez in [vs_var,vs_const,vs_out]) and
  486. paramanager.push_addr_param(varspez,tt.def,pd.proccalloption) then
  487. include(vs.varoptions,vo_regable);
  488. end;
  489. pd.concatpara(nil,tt,vs,defaultvalue,false);
  490. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) then
  491. begin
  492. if locationstr<>'' then
  493. begin
  494. if assigned(sc.first.listnext) then
  495. Message(parser_e_paraloc_only_one_para);
  496. if (pd.para.first<>pd.para.last) and not(explicit_paraloc) then
  497. Message(parser_e_paraloc_all_paras);
  498. explicit_paraloc:=true;
  499. if not(paramanager.parseparaloc(tparaitem(pd.para.last),upper(locationstr))) then
  500. message(parser_e_illegal_explicit_paraloc);
  501. end
  502. else
  503. if explicit_paraloc then
  504. Message(parser_e_paraloc_all_paras);
  505. end;
  506. vs:=tvarsym(vs.listnext);
  507. end;
  508. until not try_to_consume(_SEMICOLON);
  509. if explicit_paraloc then
  510. begin
  511. pd.has_paraloc_info:=true;
  512. include(pd.procoptions,po_explicitparaloc);
  513. end;
  514. { remove parasymtable from stack }
  515. sc.free;
  516. { reset object options }
  517. dec(testcurobject);
  518. current_object_option:=old_object_option;
  519. consume(_RKLAMMER);
  520. end;
  521. function parse_proc_head(aclass:tobjectdef;potype:tproctypeoption;var pd:tprocdef):boolean;
  522. var
  523. orgsp,sp : stringid;
  524. sym : tsym;
  525. srsymtable : tsymtable;
  526. storepos,
  527. procstartfilepos : tfileposinfo;
  528. searchagain : boolean;
  529. i : longint;
  530. st : tsymtable;
  531. aprocsym : tprocsym;
  532. begin
  533. { Save the position where this procedure really starts }
  534. procstartfilepos:=akttokenpos;
  535. result:=false;
  536. pd:=nil;
  537. aprocsym:=nil;
  538. if (potype=potype_operator) then
  539. begin
  540. sp:=overloaded_names[optoken];
  541. orgsp:=sp;
  542. end
  543. else
  544. begin
  545. sp:=pattern;
  546. orgsp:=orgpattern;
  547. consume(_ID);
  548. end;
  549. { examine interface map: function/procedure iname.functionname=locfuncname }
  550. if assigned(aclass) and
  551. assigned(aclass.implementedinterfaces) and
  552. (aclass.implementedinterfaces.count>0) and
  553. try_to_consume(_POINT) then
  554. begin
  555. storepos:=akttokenpos;
  556. akttokenpos:=procstartfilepos;
  557. { get interface syms}
  558. searchsym(sp,sym,srsymtable);
  559. if not assigned(sym) then
  560. begin
  561. identifier_not_found(orgsp);
  562. sym:=generrorsym;
  563. end;
  564. akttokenpos:=storepos;
  565. { qualifier is interface? }
  566. if (sym.typ=typesym) and
  567. (ttypesym(sym).restype.def.deftype=objectdef) then
  568. i:=aclass.implementedinterfaces.searchintf(ttypesym(sym).restype.def)
  569. else
  570. i:=-1;
  571. if (i=-1) then
  572. Message(parser_e_interface_id_expected);
  573. consume(_ID);
  574. consume(_EQUAL);
  575. if (token=_ID) then
  576. aclass.implementedinterfaces.addmappings(i,sp,pattern);
  577. consume(_ID);
  578. result:=true;
  579. exit;
  580. end;
  581. { method ? }
  582. if not assigned(aclass) and
  583. (potype<>potype_operator) and
  584. (symtablestack.symtablelevel=main_program_level) and
  585. try_to_consume(_POINT) then
  586. begin
  587. { search for object name }
  588. storepos:=akttokenpos;
  589. akttokenpos:=procstartfilepos;
  590. searchsym(sp,sym,srsymtable);
  591. if not assigned(sym) then
  592. begin
  593. identifier_not_found(orgsp);
  594. sym:=generrorsym;
  595. end;
  596. akttokenpos:=storepos;
  597. { consume proc name }
  598. sp:=pattern;
  599. orgsp:=orgpattern;
  600. procstartfilepos:=akttokenpos;
  601. consume(_ID);
  602. { qualifier is class name ? }
  603. if (sym.typ=typesym) and
  604. (ttypesym(sym).restype.def.deftype=objectdef) then
  605. begin
  606. aclass:=tobjectdef(ttypesym(sym).restype.def);
  607. aprocsym:=tprocsym(aclass.symtable.search(sp));
  608. { we solve this below }
  609. if assigned(aprocsym) then
  610. begin
  611. if aprocsym.typ<>procsym then
  612. begin
  613. { we use a different error message for tp7 so it looks more compatible }
  614. if (m_fpc in aktmodeswitches) then
  615. Message1(parser_e_overloaded_no_procedure,aprocsym.realname)
  616. else
  617. Message(parser_e_methode_id_expected);
  618. { rename the name to an unique name to avoid an
  619. error when inserting the symbol in the symtable }
  620. orgsp:=orgsp+'$'+tostr(aktfilepos.line);
  621. aprocsym:=nil;
  622. end;
  623. end
  624. else
  625. begin
  626. Message(parser_e_methode_id_expected);
  627. { recover by making it a normal procedure instead of method }
  628. aclass:=nil;
  629. end;
  630. end
  631. else
  632. Message(parser_e_class_id_expected);
  633. end
  634. else
  635. begin
  636. { check for constructor/destructor which is not allowed here }
  637. if (not parse_only) and
  638. (potype in [potype_constructor,potype_destructor]) then
  639. Message(parser_e_constructors_always_objects);
  640. repeat
  641. searchagain:=false;
  642. akttokenpos:=procstartfilepos;
  643. aprocsym:=tprocsym(symtablestack.search(sp));
  644. if not(parse_only) and
  645. not assigned(aprocsym) and
  646. (symtablestack.symtabletype=staticsymtable) and
  647. assigned(symtablestack.next) and
  648. (symtablestack.next.unitid=0) then
  649. begin
  650. { The procedure we prepare for is in the implementation
  651. part of the unit we compile. It is also possible that we
  652. are compiling a program, which is also some kind of
  653. implementaion part.
  654. We need to find out if the procedure is global. If it is
  655. global, it is in the global symtable.}
  656. aprocsym:=tprocsym(symtablestack.next.search(sp));
  657. end;
  658. { Check if overloaded is a procsym }
  659. if assigned(aprocsym) and
  660. (aprocsym.typ<>procsym) then
  661. begin
  662. { when the other symbol is a unit symbol then hide the unit
  663. symbol }
  664. if (aprocsym.typ=unitsym) then
  665. begin
  666. aprocsym.owner.rename(aprocsym.name,'hidden'+aprocsym.name);
  667. searchagain:=true;
  668. end
  669. else
  670. begin
  671. { we use a different error message for tp7 so it looks more compatible }
  672. if (m_fpc in aktmodeswitches) then
  673. Message1(parser_e_overloaded_no_procedure,aprocsym.realname)
  674. else
  675. DuplicateSym(aprocsym);
  676. { rename the name to an unique name to avoid an
  677. error when inserting the symbol in the symtable }
  678. orgsp:=orgsp+'$'+tostr(aktfilepos.line);
  679. { generate a new aktprocsym }
  680. aprocsym:=nil;
  681. end;
  682. end;
  683. until not searchagain;
  684. end;
  685. { test again if assigned, it can be reset to recover }
  686. if not assigned(aprocsym) then
  687. begin
  688. { create a new procsym and set the real filepos }
  689. akttokenpos:=procstartfilepos;
  690. { for operator we have only one procsym for each overloaded
  691. operation }
  692. if (potype=potype_operator) then
  693. begin
  694. Aprocsym:=Tprocsym(symtablestack.search(sp));
  695. if Aprocsym=nil then
  696. Aprocsym:=tprocsym.create('$'+sp);
  697. end
  698. else
  699. aprocsym:=tprocsym.create(orgsp);
  700. symtablestack.insert(aprocsym);
  701. end;
  702. { to get the correct symtablelevel we must ignore objectsymtables }
  703. st:=symtablestack;
  704. while not(st.symtabletype in [staticsymtable,globalsymtable,localsymtable]) do
  705. st:=st.next;
  706. pd:=tprocdef.create(st.symtablelevel+1);
  707. pd._class:=aclass;
  708. pd.procsym:=aprocsym;
  709. pd.proctypeoption:=potype;
  710. { methods need to be exported }
  711. if assigned(aclass) and
  712. (
  713. (symtablestack.symtabletype=objectsymtable) or
  714. (symtablestack.symtablelevel=main_program_level)
  715. ) then
  716. include(pd.procoptions,po_public);
  717. { symbol options that need to be kept per procdef }
  718. pd.fileinfo:=procstartfilepos;
  719. pd.symoptions:=current_object_option;
  720. { parse parameters }
  721. if token=_LKLAMMER then
  722. parse_parameter_dec(pd);
  723. result:=true;
  724. end;
  725. function parse_proc_dec(aclass:tobjectdef):tprocdef;
  726. var
  727. pd : tprocdef;
  728. hs : string;
  729. isclassmethod : boolean;
  730. begin
  731. pd:=nil;
  732. { read class method }
  733. if try_to_consume(_CLASS) then
  734. begin
  735. { class method only allowed for procedures and functions }
  736. if not(token in [_FUNCTION,_PROCEDURE]) then
  737. Message(parser_e_procedure_or_function_expected);
  738. isclassmethod:=true;
  739. end
  740. else
  741. isclassmethod:=false;
  742. case token of
  743. _FUNCTION :
  744. begin
  745. consume(_FUNCTION);
  746. if parse_proc_head(aclass,potype_none,pd) then
  747. begin
  748. { pd=nil when it is a interface mapping }
  749. if assigned(pd) then
  750. begin
  751. if try_to_consume(_COLON) then
  752. begin
  753. inc(testcurobject);
  754. single_type(pd.rettype,hs,false);
  755. pd.test_if_fpu_result;
  756. dec(testcurobject);
  757. end
  758. else
  759. begin
  760. if (
  761. parse_only and
  762. not(is_interface(pd._class))
  763. ) or
  764. (m_repeat_forward in aktmodeswitches) then
  765. begin
  766. consume(_COLON);
  767. consume_all_until(_SEMICOLON);
  768. end;
  769. end;
  770. if isclassmethod then
  771. include(pd.procoptions,po_classmethod);
  772. end;
  773. end
  774. else
  775. begin
  776. { recover }
  777. consume(_COLON);
  778. consume_all_until(_SEMICOLON);
  779. end;
  780. end;
  781. _PROCEDURE :
  782. begin
  783. consume(_PROCEDURE);
  784. if parse_proc_head(aclass,potype_none,pd) then
  785. begin
  786. { pd=nil when it is a interface mapping }
  787. if assigned(pd) then
  788. begin
  789. pd.rettype:=voidtype;
  790. if isclassmethod then
  791. include(pd.procoptions,po_classmethod);
  792. end;
  793. end;
  794. end;
  795. _CONSTRUCTOR :
  796. begin
  797. consume(_CONSTRUCTOR);
  798. parse_proc_head(aclass,potype_constructor,pd);
  799. if assigned(pd) and
  800. assigned(pd._class) then
  801. begin
  802. { Set return type, class constructors return the
  803. created instance, object constructors return boolean }
  804. if is_class(pd._class) then
  805. pd.rettype.setdef(pd._class)
  806. else
  807. pd.rettype:=booltype;
  808. end;
  809. end;
  810. _DESTRUCTOR :
  811. begin
  812. consume(_DESTRUCTOR);
  813. parse_proc_head(aclass,potype_destructor,pd);
  814. if assigned(pd) then
  815. pd.rettype:=voidtype;
  816. end;
  817. _OPERATOR :
  818. begin
  819. consume(_OPERATOR);
  820. if (token in [first_overloaded..last_overloaded]) then
  821. begin
  822. optoken:=token;
  823. end
  824. else
  825. begin
  826. Message(parser_e_overload_operator_failed);
  827. { Use the dummy NOTOKEN that is also declared
  828. for the overloaded_operator[] }
  829. optoken:=NOTOKEN;
  830. end;
  831. consume(token);
  832. parse_proc_head(aclass,potype_operator,pd);
  833. if assigned(pd) then
  834. begin
  835. if pd.parast.symtablelevel>normal_function_level then
  836. Message(parser_e_no_local_operator);
  837. if token<>_ID then
  838. begin
  839. if not(m_result in aktmodeswitches) then
  840. consume(_ID);
  841. end
  842. else
  843. begin
  844. pd.resultname:=orgpattern;
  845. consume(_ID);
  846. end;
  847. if not try_to_consume(_COLON) then
  848. begin
  849. consume(_COLON);
  850. pd.rettype:=generrortype;
  851. consume_all_until(_SEMICOLON);
  852. end
  853. else
  854. begin
  855. single_type(pd.rettype,hs,false);
  856. pd.test_if_fpu_result;
  857. if (optoken in [_EQUAL,_GT,_LT,_GTE,_LTE]) and
  858. ((pd.rettype.def.deftype<>orddef) or
  859. (torddef(pd.rettype.def).typ<>bool8bit)) then
  860. Message(parser_e_comparative_operator_return_boolean);
  861. if (optoken=_ASSIGNMENT) and
  862. equal_defs(pd.rettype.def,
  863. tvarsym(pd.parast.symindex.first).vartype.def) then
  864. message(parser_e_no_such_assignment)
  865. else if not isoperatoracceptable(pd,optoken) then
  866. Message(parser_e_overload_impossible);
  867. end;
  868. end
  869. else
  870. begin
  871. { recover }
  872. try_to_consume(_ID);
  873. consume(_COLON);
  874. consume_all_until(_SEMICOLON);
  875. end;
  876. end;
  877. end;
  878. { support procedure proc stdcall export; }
  879. if not(is_proc_directive(token,false)) then
  880. consume(_SEMICOLON);
  881. result:=pd;
  882. end;
  883. {****************************************************************************
  884. Procedure directive handlers
  885. ****************************************************************************}
  886. procedure pd_far(pd:tabstractprocdef);
  887. begin
  888. Message1(parser_w_proc_directive_ignored,'FAR');
  889. end;
  890. procedure pd_near(pd:tabstractprocdef);
  891. begin
  892. Message1(parser_w_proc_directive_ignored,'NEAR');
  893. end;
  894. procedure pd_export(pd:tabstractprocdef);
  895. begin
  896. if pd.deftype<>procdef then
  897. internalerror(200304264);
  898. if assigned(tprocdef(pd)._class) then
  899. Message(parser_e_methods_dont_be_export);
  900. if pd.parast.symtablelevel>normal_function_level then
  901. Message(parser_e_dont_nest_export);
  902. { only os/2 and emx need this }
  903. if target_info.system in [system_i386_os2,system_i386_emx] then
  904. begin
  905. tprocdef(pd).aliasnames.insert(tprocdef(pd).procsym.realname);
  906. if cs_link_deffile in aktglobalswitches then
  907. deffile.AddExport(tprocdef(pd).mangledname);
  908. end;
  909. end;
  910. procedure pd_forward(pd:tabstractprocdef);
  911. begin
  912. if pd.deftype<>procdef then
  913. internalerror(200304265);
  914. tprocdef(pd).forwarddef:=true;
  915. end;
  916. procedure pd_alias(pd:tabstractprocdef);
  917. begin
  918. if pd.deftype<>procdef then
  919. internalerror(200304266);
  920. consume(_COLON);
  921. tprocdef(pd).aliasnames.insert(get_stringconst);
  922. end;
  923. procedure pd_asmname(pd:tabstractprocdef);
  924. begin
  925. if pd.deftype<>procdef then
  926. internalerror(200304267);
  927. tprocdef(pd).setmangledname(target_info.Cprefix+pattern);
  928. if token=_CCHAR then
  929. consume(_CCHAR)
  930. else
  931. consume(_CSTRING);
  932. { we don't need anything else }
  933. tprocdef(pd).forwarddef:=false;
  934. end;
  935. procedure pd_inline(pd:tabstractprocdef);
  936. var
  937. hp : tparaitem;
  938. begin
  939. { check if there is an array of const }
  940. hp:=tparaitem(pd.para.first);
  941. while assigned(hp) do
  942. begin
  943. if assigned(hp.paratype.def) and
  944. (hp.paratype.def.deftype=arraydef) then
  945. begin
  946. with tarraydef(hp.paratype.def) do
  947. if IsVariant or IsConstructor {or IsArrayOfConst} then
  948. begin
  949. Message1(parser_w_not_supported_for_inline,'array of const');
  950. Message(parser_w_inlining_disabled);
  951. pd.proccalloption:=pocall_default;
  952. end;
  953. end;
  954. hp:=tparaitem(hp.next);
  955. end;
  956. end;
  957. procedure pd_intern(pd:tabstractprocdef);
  958. begin
  959. if pd.deftype<>procdef then
  960. internalerror(200304268);
  961. consume(_COLON);
  962. tprocdef(pd).extnumber:=get_intconst;
  963. end;
  964. procedure pd_interrupt(pd:tabstractprocdef);
  965. begin
  966. if pd.parast.symtablelevel>normal_function_level then
  967. Message(parser_e_dont_nest_interrupt);
  968. end;
  969. procedure pd_abstract(pd:tabstractprocdef);
  970. begin
  971. if pd.deftype<>procdef then
  972. internalerror(200304269);
  973. if (po_virtualmethod in pd.procoptions) then
  974. include(pd.procoptions,po_abstractmethod)
  975. else
  976. Message(parser_e_only_virtual_methods_abstract);
  977. { the method is defined }
  978. tprocdef(pd).forwarddef:=false;
  979. end;
  980. procedure pd_virtual(pd:tabstractprocdef);
  981. {$ifdef WITHDMT}
  982. var
  983. pt : tnode;
  984. {$endif WITHDMT}
  985. begin
  986. if pd.deftype<>procdef then
  987. internalerror(2003042610);
  988. if (pd.proctypeoption=potype_constructor) and
  989. is_object(tprocdef(pd)._class) then
  990. Message(parser_e_constructor_cannot_be_not_virtual);
  991. {$ifdef WITHDMT}
  992. if is_object(tprocdef(pd)._class) and
  993. (token<>_SEMICOLON) then
  994. begin
  995. { any type of parameter is allowed here! }
  996. pt:=comp_expr(true);
  997. if is_constintnode(pt) then
  998. begin
  999. include(pd.procoptions,po_msgint);
  1000. pd.messageinf.i:=pt^.value;
  1001. end
  1002. else
  1003. Message(parser_e_ill_msg_expr);
  1004. disposetree(pt);
  1005. end;
  1006. {$endif WITHDMT}
  1007. end;
  1008. procedure pd_static(pd:tabstractprocdef);
  1009. begin
  1010. if (cs_static_keyword in aktmoduleswitches) then
  1011. begin
  1012. if pd.deftype=procdef then
  1013. include(tprocdef(pd).procsym.symoptions,sp_static);
  1014. include(pd.procoptions,po_staticmethod);
  1015. end;
  1016. end;
  1017. procedure pd_override(pd:tabstractprocdef);
  1018. begin
  1019. if pd.deftype<>procdef then
  1020. internalerror(2003042611);
  1021. if not(is_class_or_interface(tprocdef(pd)._class)) then
  1022. Message(parser_e_no_object_override);
  1023. end;
  1024. procedure pd_overload(pd:tabstractprocdef);
  1025. begin
  1026. if pd.deftype<>procdef then
  1027. internalerror(2003042612);
  1028. include(tprocdef(pd).procsym.symoptions,sp_has_overloaded);
  1029. end;
  1030. procedure pd_message(pd:tabstractprocdef);
  1031. var
  1032. pt : tnode;
  1033. begin
  1034. if pd.deftype<>procdef then
  1035. internalerror(2003042613);
  1036. if not is_class(tprocdef(pd)._class) then
  1037. Message(parser_e_msg_only_for_classes);
  1038. { check parameter type }
  1039. if ((pd.minparacount<>1) or
  1040. (pd.maxparacount<>1) or
  1041. (TParaItem(pd.Para.first).paratyp<>vs_var)) then
  1042. Message(parser_e_ill_msg_param);
  1043. pt:=comp_expr(true);
  1044. if pt.nodetype=stringconstn then
  1045. begin
  1046. include(pd.procoptions,po_msgstr);
  1047. tprocdef(pd).messageinf.str:=strnew(tstringconstnode(pt).value_str);
  1048. end
  1049. else
  1050. if is_constintnode(pt) then
  1051. begin
  1052. include(pd.procoptions,po_msgint);
  1053. tprocdef(pd).messageinf.i:=tordconstnode(pt).value;
  1054. end
  1055. else
  1056. Message(parser_e_ill_msg_expr);
  1057. pt.free;
  1058. end;
  1059. procedure pd_reintroduce(pd:tabstractprocdef);
  1060. begin
  1061. if pd.deftype<>procdef then
  1062. internalerror(200401211);
  1063. if not(is_class_or_interface(tprocdef(pd)._class)) then
  1064. Message(parser_e_no_object_reintroduce);
  1065. end;
  1066. procedure pd_syscall(pd:tabstractprocdef);
  1067. {$ifdef powerpc}
  1068. var
  1069. sym : tsym;
  1070. symtable : tsymtable;
  1071. {$endif powerpc}
  1072. begin
  1073. if pd.deftype<>procdef then
  1074. internalerror(2003042614);
  1075. tprocdef(pd).forwarddef:=false;
  1076. {$ifdef powerpc}
  1077. if target_info.system in [system_powerpc_morphos,system_m68k_amiga] then
  1078. begin
  1079. pd.has_paraloc_info:=true;
  1080. include(pd.procoptions,po_explicitparaloc);
  1081. if consume_sym(sym,symtable) then
  1082. begin
  1083. if (sym.typ=varsym) and
  1084. (is_voidpointer(tvarsym(sym).vartype.def) or
  1085. is_32bitint(tvarsym(sym).vartype.def)) then
  1086. begin
  1087. tprocdef(pd).libsym:=sym;
  1088. pd.concatpara(nil,tvarsym(sym).vartype,tvarsym(sym),nil,true);
  1089. paramanager.parseparaloc(tparaitem(pd.para.last),'A6');
  1090. end
  1091. else
  1092. Message(parser_e_32bitint_or_pointer_variable_expected);
  1093. end;
  1094. (paramanager as tppcparamanager).create_funcret_paraloc_info(pd,calleeside);
  1095. (paramanager as tppcparamanager).create_funcret_paraloc_info(pd,callerside);
  1096. end;
  1097. {$endif powerpc}
  1098. tprocdef(pd).extnumber:=get_intconst;
  1099. end;
  1100. procedure pd_external(pd:tabstractprocdef);
  1101. {
  1102. If import_dll=nil the procedure is assumed to be in another
  1103. object file. In that object file it should have the name to
  1104. which import_name is pointing to. Otherwise, the procedure is
  1105. assumed to be in the DLL to which import_dll is pointing to. In
  1106. that case either import_nr<>0 or import_name<>nil is true, so
  1107. the procedure is either imported by number or by name. (DM)
  1108. }
  1109. var
  1110. import_dll,
  1111. import_name : string;
  1112. import_nr : word;
  1113. hpd : tprocdef;
  1114. begin
  1115. if pd.deftype<>procdef then
  1116. internalerror(2003042615);
  1117. tprocdef(pd).forwarddef:=false;
  1118. { forbid local external procedures }
  1119. if pd.parast.symtablelevel>normal_function_level then
  1120. Message(parser_e_no_local_external);
  1121. { If the procedure should be imported from a DLL, a constant string follows.
  1122. This isn't really correct, an contant string expression follows
  1123. so we check if an semicolon follows, else a string constant have to
  1124. follow (FK) }
  1125. import_nr:=0;
  1126. import_name:='';
  1127. if not(token=_SEMICOLON) and not(idtoken=_NAME) then
  1128. begin
  1129. import_dll:=get_stringconst;
  1130. if (idtoken=_NAME) then
  1131. begin
  1132. consume(_NAME);
  1133. import_name:=get_stringconst;
  1134. end;
  1135. if (idtoken=_INDEX) then
  1136. begin
  1137. {After the word index follows the index number in the DLL.}
  1138. consume(_INDEX);
  1139. import_nr:=get_intconst;
  1140. end;
  1141. { default is to used the realname of the procedure }
  1142. if (import_nr=0) and (import_name='') then
  1143. import_name:=tprocdef(pd).procsym.realname;
  1144. { create importlib if not already done }
  1145. if not(current_module.uses_imports) then
  1146. begin
  1147. current_module.uses_imports:=true;
  1148. importlib.preparelib(current_module.modulename^);
  1149. end;
  1150. if not(m_repeat_forward in aktmodeswitches) then
  1151. begin
  1152. { we can only have one overloaded here ! }
  1153. if tprocsym(tprocdef(pd).procsym).procdef_count>1 then
  1154. hpd:=tprocsym(tprocdef(pd).procsym).procdef[2]
  1155. else
  1156. hpd:=tprocdef(pd);
  1157. end
  1158. else
  1159. hpd:=tprocdef(pd);
  1160. importlib.importprocedure(hpd,import_dll,import_nr,import_name);
  1161. end
  1162. else
  1163. begin
  1164. if (idtoken=_NAME) then
  1165. begin
  1166. consume(_NAME);
  1167. import_name:=get_stringconst;
  1168. tprocdef(pd).setmangledname(import_name);
  1169. end
  1170. else if (m_mac in aktmodeswitches) and (token=_SEMICOLON) then
  1171. begin
  1172. {In MacPas a single "external" has the same effect as "external name 'xxx'"}
  1173. { don't do this if the procedure is also cdecl; in that case the c-prefix }
  1174. { must also be used (handled later) }
  1175. if not(pd.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  1176. tprocdef(pd).setmangledname(tprocdef(pd).procsym.realname);
  1177. end;
  1178. end;
  1179. end;
  1180. type
  1181. pd_handler=procedure(pd:tabstractprocdef);
  1182. proc_dir_rec=record
  1183. idtok : ttoken;
  1184. pd_flags : tpdflags;
  1185. handler : pd_handler;
  1186. pocall : tproccalloption;
  1187. pooption : tprocoptions;
  1188. mutexclpocall : tproccalloptions;
  1189. mutexclpotype : tproctypeoptions;
  1190. mutexclpo : tprocoptions;
  1191. end;
  1192. const
  1193. {Should contain the number of procedure directives we support.}
  1194. num_proc_directives=37;
  1195. proc_direcdata:array[1..num_proc_directives] of proc_dir_rec=
  1196. (
  1197. (
  1198. idtok:_ABSTRACT;
  1199. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1200. handler : {$ifdef FPCPROCVAR}@{$endif}pd_abstract;
  1201. pocall : pocall_none;
  1202. pooption : [po_abstractmethod];
  1203. mutexclpocall : [pocall_internproc,pocall_inline];
  1204. mutexclpotype : [];
  1205. mutexclpo : [po_exports,po_interrupt,po_external]
  1206. ),(
  1207. idtok:_ALIAS;
  1208. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  1209. handler : {$ifdef FPCPROCVAR}@{$endif}pd_alias;
  1210. pocall : pocall_none;
  1211. pooption : [];
  1212. mutexclpocall : [pocall_inline];
  1213. mutexclpotype : [];
  1214. mutexclpo : [po_external]
  1215. ),(
  1216. idtok:_ASMNAME;
  1217. pd_flags : [pd_interface,pd_implemen,pd_notobjintf];
  1218. handler : {$ifdef FPCPROCVAR}@{$endif}pd_asmname;
  1219. pocall : pocall_cdecl;
  1220. pooption : [po_external];
  1221. mutexclpocall : [pocall_internproc,pocall_inline];
  1222. mutexclpotype : [];
  1223. mutexclpo : [po_external]
  1224. ),(
  1225. idtok:_ASSEMBLER;
  1226. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  1227. handler : nil;
  1228. pocall : pocall_none;
  1229. pooption : [po_assembler];
  1230. mutexclpocall : [];
  1231. mutexclpotype : [];
  1232. mutexclpo : [po_external]
  1233. ),(
  1234. idtok:_C; {same as cdecl for mode mac}
  1235. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1236. handler : nil;
  1237. pocall : pocall_cdecl;
  1238. pooption : [];
  1239. mutexclpocall : [];
  1240. mutexclpotype : [potype_constructor,potype_destructor];
  1241. mutexclpo : [po_assembler,po_external]
  1242. ),(
  1243. idtok:_CDECL;
  1244. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1245. handler : nil;
  1246. pocall : pocall_cdecl;
  1247. pooption : [];
  1248. mutexclpocall : [];
  1249. mutexclpotype : [potype_constructor,potype_destructor];
  1250. mutexclpo : [po_assembler,po_external]
  1251. ),(
  1252. idtok:_DYNAMIC;
  1253. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1254. handler : {$ifdef FPCPROCVAR}@{$endif}pd_virtual;
  1255. pocall : pocall_none;
  1256. pooption : [po_virtualmethod];
  1257. mutexclpocall : [pocall_internproc,pocall_inline];
  1258. mutexclpotype : [];
  1259. mutexclpo : [po_exports,po_interrupt,po_external,po_overridingmethod]
  1260. ),(
  1261. idtok:_EXPORT;
  1262. pd_flags : [pd_body,pd_interface,pd_implemen,pd_notobjintf];
  1263. handler : {$ifdef FPCPROCVAR}@{$endif}pd_export;
  1264. pocall : pocall_none;
  1265. pooption : [po_exports,po_public];
  1266. mutexclpocall : [pocall_internproc,pocall_inline];
  1267. mutexclpotype : [potype_constructor,potype_destructor];
  1268. mutexclpo : [po_external,po_interrupt]
  1269. ),(
  1270. idtok:_EXTERNAL;
  1271. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf];
  1272. handler : {$ifdef FPCPROCVAR}@{$endif}pd_external;
  1273. pocall : pocall_none;
  1274. pooption : [po_external];
  1275. mutexclpocall : [pocall_internproc,pocall_inline,pocall_syscall];
  1276. mutexclpotype : [potype_constructor,potype_destructor];
  1277. mutexclpo : [po_exports,po_interrupt,po_assembler]
  1278. ),(
  1279. idtok:_FAR;
  1280. pd_flags : [pd_implemen,pd_body,pd_interface,pd_procvar,pd_notobject,pd_notobjintf];
  1281. handler : {$ifdef FPCPROCVAR}@{$endif}pd_far;
  1282. pocall : pocall_none;
  1283. pooption : [];
  1284. mutexclpocall : [pocall_internproc,pocall_inline];
  1285. mutexclpotype : [];
  1286. mutexclpo : []
  1287. ),(
  1288. idtok:_FAR16;
  1289. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar,pd_notobject];
  1290. handler : nil;
  1291. pocall : pocall_far16;
  1292. pooption : [];
  1293. mutexclpocall : [];
  1294. mutexclpotype : [];
  1295. mutexclpo : [po_external]
  1296. ),(
  1297. idtok:_FORWARD;
  1298. pd_flags : [pd_implemen,pd_notobject,pd_notobjintf];
  1299. handler : {$ifdef FPCPROCVAR}@{$endif}pd_forward;
  1300. pocall : pocall_none;
  1301. pooption : [];
  1302. mutexclpocall : [pocall_internproc,pocall_inline];
  1303. mutexclpotype : [];
  1304. mutexclpo : [po_external]
  1305. ),(
  1306. idtok:_OLDFPCCALL;
  1307. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1308. handler : nil;
  1309. pocall : pocall_oldfpccall;
  1310. pooption : [];
  1311. mutexclpocall : [];
  1312. mutexclpotype : [];
  1313. mutexclpo : []
  1314. ),(
  1315. idtok:_INLINE;
  1316. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1317. handler : {$ifdef FPCPROCVAR}@{$endif}pd_inline;
  1318. pocall : pocall_inline;
  1319. pooption : [];
  1320. mutexclpocall : [];
  1321. mutexclpotype : [potype_constructor,potype_destructor];
  1322. mutexclpo : [po_exports,po_external,po_interrupt,po_virtualmethod]
  1323. ),(
  1324. idtok:_INTERNCONST;
  1325. pd_flags : [pd_implemen,pd_body,pd_notobject,pd_notobjintf];
  1326. handler : {$ifdef FPCPROCVAR}@{$endif}pd_intern;
  1327. pocall : pocall_none;
  1328. pooption : [po_internconst];
  1329. mutexclpocall : [];
  1330. mutexclpotype : [potype_operator];
  1331. mutexclpo : []
  1332. ),(
  1333. idtok:_INTERNPROC;
  1334. pd_flags : [pd_implemen,pd_notobject,pd_notobjintf];
  1335. handler : {$ifdef FPCPROCVAR}@{$endif}pd_intern;
  1336. pocall : pocall_internproc;
  1337. pooption : [];
  1338. mutexclpocall : [];
  1339. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1340. mutexclpo : [po_exports,po_external,po_interrupt,po_assembler,po_iocheck,po_virtualmethod]
  1341. ),(
  1342. idtok:_INTERRUPT;
  1343. pd_flags : [pd_implemen,pd_body,pd_notobject,pd_notobjintf];
  1344. handler : {$ifdef FPCPROCVAR}@{$endif}pd_interrupt;
  1345. pocall : pocall_none;
  1346. pooption : [po_interrupt];
  1347. mutexclpocall : [pocall_internproc,pocall_cdecl,pocall_cppdecl,pocall_stdcall,
  1348. pocall_inline,pocall_pascal,pocall_far16,pocall_oldfpccall];
  1349. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1350. mutexclpo : [po_external]
  1351. ),(
  1352. idtok:_IOCHECK;
  1353. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  1354. handler : nil;
  1355. pocall : pocall_none;
  1356. pooption : [po_iocheck];
  1357. mutexclpocall : [pocall_internproc];
  1358. mutexclpotype : [];
  1359. mutexclpo : [po_external]
  1360. ),(
  1361. idtok:_MESSAGE;
  1362. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1363. handler : {$ifdef FPCPROCVAR}@{$endif}pd_message;
  1364. pocall : pocall_none;
  1365. pooption : []; { can be po_msgstr or po_msgint }
  1366. mutexclpocall : [pocall_inline,pocall_internproc];
  1367. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1368. mutexclpo : [po_interrupt,po_external]
  1369. ),(
  1370. idtok:_NEAR;
  1371. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  1372. handler : {$ifdef FPCPROCVAR}@{$endif}pd_near;
  1373. pocall : pocall_none;
  1374. pooption : [];
  1375. mutexclpocall : [pocall_internproc];
  1376. mutexclpotype : [];
  1377. mutexclpo : []
  1378. ),(
  1379. idtok:_NOSTACKFRAME;
  1380. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  1381. handler : nil;
  1382. pocall : pocall_none;
  1383. pooption : [po_nostackframe];
  1384. mutexclpocall : [pocall_internproc];
  1385. mutexclpotype : [];
  1386. mutexclpo : []
  1387. ),(
  1388. idtok:_OVERLOAD;
  1389. pd_flags : [pd_implemen,pd_interface,pd_body];
  1390. handler : {$ifdef FPCPROCVAR}@{$endif}pd_overload;
  1391. pocall : pocall_none;
  1392. pooption : [po_overload];
  1393. mutexclpocall : [pocall_internproc];
  1394. mutexclpotype : [];
  1395. mutexclpo : []
  1396. ),(
  1397. idtok:_OVERRIDE;
  1398. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1399. handler : {$ifdef FPCPROCVAR}@{$endif}pd_override;
  1400. pocall : pocall_none;
  1401. pooption : [po_overridingmethod,po_virtualmethod];
  1402. mutexclpocall : [pocall_inline,pocall_internproc];
  1403. mutexclpotype : [];
  1404. mutexclpo : [po_exports,po_external,po_interrupt,po_virtualmethod]
  1405. ),(
  1406. idtok:_PASCAL;
  1407. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1408. handler : nil;
  1409. pocall : pocall_pascal;
  1410. pooption : [];
  1411. mutexclpocall : [];
  1412. mutexclpotype : [potype_constructor,potype_destructor];
  1413. mutexclpo : [po_external]
  1414. ),(
  1415. idtok:_PUBLIC;
  1416. pd_flags : [pd_implemen,pd_body,pd_notobject,pd_notobjintf];
  1417. handler : nil;
  1418. pocall : pocall_none;
  1419. pooption : [po_public];
  1420. mutexclpocall : [pocall_internproc,pocall_inline];
  1421. mutexclpotype : [];
  1422. mutexclpo : [po_external]
  1423. ),(
  1424. idtok:_REGISTER;
  1425. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1426. handler : nil;
  1427. pocall : pocall_register;
  1428. pooption : [];
  1429. mutexclpocall : [];
  1430. mutexclpotype : [potype_constructor,potype_destructor];
  1431. mutexclpo : [po_external]
  1432. ),(
  1433. idtok:_REINTRODUCE;
  1434. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1435. handler : {$ifdef FPCPROCVAR}@{$endif}pd_reintroduce;
  1436. pocall : pocall_none;
  1437. pooption : [po_reintroduce];
  1438. mutexclpocall : [pocall_inline,pocall_internproc];
  1439. mutexclpotype : [];
  1440. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod]
  1441. ),(
  1442. idtok:_SAFECALL;
  1443. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1444. handler : nil;
  1445. pocall : pocall_safecall;
  1446. pooption : [];
  1447. mutexclpocall : [];
  1448. mutexclpotype : [potype_constructor,potype_destructor];
  1449. mutexclpo : [po_external]
  1450. ),(
  1451. idtok:_SAVEREGISTERS;
  1452. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  1453. handler : nil;
  1454. pocall : pocall_none;
  1455. pooption : [po_saveregisters];
  1456. mutexclpocall : [pocall_internproc];
  1457. mutexclpotype : [potype_constructor,potype_destructor];
  1458. mutexclpo : [po_external]
  1459. ),(
  1460. idtok:_SOFTFLOAT;
  1461. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1462. handler : nil;
  1463. pocall : pocall_softfloat;
  1464. pooption : [];
  1465. mutexclpocall : [];
  1466. mutexclpotype : [potype_constructor,potype_destructor];
  1467. { it's available with po_external because the libgcc floating point routines on the arm
  1468. uses this calling convention }
  1469. mutexclpo : []
  1470. ),(
  1471. idtok:_STATIC;
  1472. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1473. handler : {$ifdef FPCPROCVAR}@{$endif}pd_static;
  1474. pocall : pocall_none;
  1475. pooption : [po_staticmethod];
  1476. mutexclpocall : [pocall_inline,pocall_internproc];
  1477. mutexclpotype : [potype_constructor,potype_destructor];
  1478. mutexclpo : [po_external,po_interrupt,po_exports]
  1479. ),(
  1480. idtok:_STDCALL;
  1481. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1482. handler : nil;
  1483. pocall : pocall_stdcall;
  1484. pooption : [];
  1485. mutexclpocall : [];
  1486. mutexclpotype : [potype_constructor,potype_destructor];
  1487. mutexclpo : [po_external]
  1488. ),(
  1489. idtok:_SYSCALL;
  1490. pd_flags : [pd_interface,pd_implemen,pd_notobject,pd_notobjintf];
  1491. handler : {$ifdef FPCPROCVAR}@{$endif}pd_syscall;
  1492. pocall : pocall_syscall;
  1493. pooption : [];
  1494. mutexclpocall : [];
  1495. mutexclpotype : [potype_constructor,potype_destructor];
  1496. mutexclpo : [po_external,po_assembler,po_interrupt,po_exports]
  1497. ),(
  1498. idtok:_VIRTUAL;
  1499. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1500. handler : {$ifdef FPCPROCVAR}@{$endif}pd_virtual;
  1501. pocall : pocall_none;
  1502. pooption : [po_virtualmethod];
  1503. mutexclpocall : [pocall_inline,pocall_internproc];
  1504. mutexclpotype : [];
  1505. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod]
  1506. ),(
  1507. idtok:_CPPDECL;
  1508. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1509. handler : nil;
  1510. pocall : pocall_cppdecl;
  1511. pooption : [];
  1512. mutexclpocall : [];
  1513. mutexclpotype : [potype_constructor,potype_destructor];
  1514. mutexclpo : [po_assembler,po_external,po_virtualmethod]
  1515. ),(
  1516. idtok:_VARARGS;
  1517. pd_flags : [pd_interface,pd_implemen,pd_procvar];
  1518. handler : nil;
  1519. pocall : pocall_none;
  1520. pooption : [po_varargs];
  1521. mutexclpocall : [pocall_internproc,pocall_stdcall,pocall_register,
  1522. pocall_inline,pocall_far16,pocall_oldfpccall];
  1523. mutexclpotype : [];
  1524. mutexclpo : [po_assembler,po_interrupt]
  1525. ),(
  1526. idtok:_COMPILERPROC;
  1527. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1528. handler : nil;
  1529. pocall : pocall_compilerproc;
  1530. pooption : [];
  1531. mutexclpocall : [];
  1532. mutexclpotype : [potype_constructor,potype_destructor];
  1533. mutexclpo : [po_interrupt]
  1534. )
  1535. );
  1536. function is_proc_directive(tok:ttoken;isprocvar:boolean):boolean;
  1537. var
  1538. i : longint;
  1539. begin
  1540. is_proc_directive:=false;
  1541. for i:=1 to num_proc_directives do
  1542. if proc_direcdata[i].idtok=idtoken then
  1543. begin
  1544. if ((not isprocvar) or
  1545. (pd_procvar in proc_direcdata[i].pd_flags)) and
  1546. { don't eat a public directive in classes }
  1547. not((proc_direcdata[i].idtok=_PUBLIC) and (symtablestack.symtabletype=objectsymtable)) then
  1548. is_proc_directive:=true;
  1549. exit;
  1550. end;
  1551. end;
  1552. function parse_proc_direc(pd:tabstractprocdef;var pdflags:tpdflags):boolean;
  1553. {
  1554. Parse the procedure directive, returns true if a correct directive is found
  1555. }
  1556. var
  1557. p : longint;
  1558. found : boolean;
  1559. name : stringid;
  1560. begin
  1561. parse_proc_direc:=false;
  1562. name:=tokeninfo^[idtoken].str;
  1563. found:=false;
  1564. { Hint directive? Then exit immediatly }
  1565. if (m_hintdirective in aktmodeswitches) then
  1566. begin
  1567. case idtoken of
  1568. _LIBRARY,
  1569. _PLATFORM,
  1570. _UNIMPLEMENTED,
  1571. _DEPRECATED :
  1572. exit;
  1573. end;
  1574. end;
  1575. { C directive is MAC only, because it breaks too much existing code
  1576. on other platforms (PFV) }
  1577. if (idtoken=_C) and
  1578. not(m_mac in aktmodeswitches) then
  1579. exit;
  1580. { retrieve data for directive if found }
  1581. for p:=1 to num_proc_directives do
  1582. if proc_direcdata[p].idtok=idtoken then
  1583. begin
  1584. found:=true;
  1585. break;
  1586. end;
  1587. { Check if the procedure directive is known }
  1588. if not found then
  1589. begin
  1590. { parsing a procvar type the name can be any
  1591. next variable !! }
  1592. if (pdflags * [pd_procvar,pd_object])=[] then
  1593. Message1(parser_w_unknown_proc_directive_ignored,name);
  1594. exit;
  1595. end;
  1596. { static needs a special treatment }
  1597. if (idtoken=_STATIC) and not (cs_static_keyword in aktmoduleswitches) then
  1598. exit;
  1599. { Conflicts between directives ? }
  1600. if (pd.proctypeoption in proc_direcdata[p].mutexclpotype) or
  1601. (pd.proccalloption in proc_direcdata[p].mutexclpocall) or
  1602. ((pd.procoptions*proc_direcdata[p].mutexclpo)<>[]) then
  1603. begin
  1604. Message1(parser_e_proc_dir_conflict,name);
  1605. exit;
  1606. end;
  1607. { set calling convention }
  1608. if proc_direcdata[p].pocall<>pocall_none then
  1609. begin
  1610. if (po_hascallingconvention in pd.procoptions) then
  1611. begin
  1612. Message2(parser_w_proc_overriding_calling,
  1613. proccalloptionStr[pd.proccalloption],
  1614. proccalloptionStr[proc_direcdata[p].pocall]);
  1615. end;
  1616. { check if the target processor supports this calling convention }
  1617. if not(proc_direcdata[p].pocall in supported_calling_conventions) then
  1618. begin
  1619. Message1(parser_e_illegal_calling_convention,proccalloptionStr[proc_direcdata[p].pocall]);
  1620. { recover }
  1621. proc_direcdata[p].pocall:=pocall_stdcall;
  1622. end;
  1623. pd.proccalloption:=proc_direcdata[p].pocall;
  1624. include(pd.procoptions,po_hascallingconvention);
  1625. end;
  1626. { check if method and directive not for object, like public.
  1627. This needs to be checked also for procvars }
  1628. if (pd_notobject in proc_direcdata[p].pd_flags) and
  1629. (pd.owner.symtabletype=objectsymtable) then
  1630. exit;
  1631. if pd.deftype=procdef then
  1632. begin
  1633. { Check if the directive is only for objects }
  1634. if (pd_object in proc_direcdata[p].pd_flags) and
  1635. not assigned(tprocdef(pd)._class) then
  1636. exit;
  1637. { check if method and directive not for interface }
  1638. if (pd_notobjintf in proc_direcdata[p].pd_flags) and
  1639. is_interface(tprocdef(pd)._class) then
  1640. exit;
  1641. end
  1642. { don't eat public in object/class declarions }
  1643. else if (pd.deftype=procvardef) and (proc_direcdata[p].idtok=_PUBLIC) and
  1644. (symtablestack.symtabletype=objectsymtable) then
  1645. exit;
  1646. { consume directive, and turn flag on }
  1647. consume(token);
  1648. parse_proc_direc:=true;
  1649. { Check the pd_flags if the directive should be allowed }
  1650. if (pd_interface in pdflags) and
  1651. not(pd_interface in proc_direcdata[p].pd_flags) then
  1652. begin
  1653. Message1(parser_e_proc_dir_not_allowed_in_interface,name);
  1654. exit;
  1655. end;
  1656. if (pd_implemen in pdflags) and
  1657. not(pd_implemen in proc_direcdata[p].pd_flags) then
  1658. begin
  1659. Message1(parser_e_proc_dir_not_allowed_in_implementation,name);
  1660. exit;
  1661. end;
  1662. if (pd_procvar in pdflags) and
  1663. not(pd_procvar in proc_direcdata[p].pd_flags) then
  1664. begin
  1665. Message1(parser_e_proc_dir_not_allowed_in_procvar,name);
  1666. exit;
  1667. end;
  1668. { Return the new pd_flags }
  1669. if not(pd_body in proc_direcdata[p].pd_flags) then
  1670. exclude(pdflags,pd_body);
  1671. { Add the correct flag }
  1672. pd.procoptions:=pd.procoptions+proc_direcdata[p].pooption;
  1673. { Call the handler }
  1674. if pointer({$ifndef FPCPROCVAR}@{$endif}proc_direcdata[p].handler)<>nil then
  1675. proc_direcdata[p].handler(pd);
  1676. end;
  1677. procedure handle_calling_convention(pd:tabstractprocdef);
  1678. begin
  1679. { set the default calling convention if none provided }
  1680. if not(po_hascallingconvention in pd.procoptions) then
  1681. pd.proccalloption:=aktdefproccall
  1682. else
  1683. begin
  1684. if pd.proccalloption=pocall_none then
  1685. internalerror(200309081);
  1686. end;
  1687. { handle proccall specific settings }
  1688. case pd.proccalloption of
  1689. pocall_cdecl :
  1690. begin
  1691. { set mangledname }
  1692. if (pd.deftype=procdef) then
  1693. begin
  1694. if not tprocdef(pd).has_mangledname then
  1695. begin
  1696. if assigned(tprocdef(pd)._class) then
  1697. tprocdef(pd).setmangledname(target_info.Cprefix+tprocdef(pd)._class.objrealname^+'_'+tprocdef(pd).procsym.realname)
  1698. else
  1699. tprocdef(pd).setmangledname(target_info.Cprefix+tprocdef(pd).procsym.realname);
  1700. end;
  1701. { check C cdecl para types }
  1702. pd.parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}check_c_para,nil);
  1703. end;
  1704. end;
  1705. pocall_cppdecl :
  1706. begin
  1707. { set mangledname }
  1708. if (pd.deftype=procdef) then
  1709. begin
  1710. if not tprocdef(pd).has_mangledname then
  1711. tprocdef(pd).setmangledname(target_info.Cprefix+tprocdef(pd).cplusplusmangledname);
  1712. { check C cdecl para types }
  1713. pd.parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}check_c_para,nil);
  1714. end;
  1715. end;
  1716. pocall_compilerproc :
  1717. begin
  1718. if (pd.deftype<>procdef) then
  1719. internalerror(200110232);
  1720. if (target_info.system<>system_i386_watcom) then
  1721. tprocdef(pd).setmangledname(lower(tprocdef(pd).procsym.name));
  1722. end;
  1723. pocall_far16 :
  1724. begin
  1725. { Temporary stub, must be rewritten to support OS/2 far16 }
  1726. Message1(parser_w_proc_directive_ignored,'FAR16');
  1727. end;
  1728. pocall_inline :
  1729. begin
  1730. if not(cs_support_inline in aktmoduleswitches) then
  1731. begin
  1732. Message(parser_e_proc_inline_not_supported);
  1733. pd.proccalloption:=pocall_default;
  1734. end;
  1735. end;
  1736. end;
  1737. { For varargs directive also cdecl and external must be defined }
  1738. if (po_varargs in pd.procoptions) then
  1739. begin
  1740. { check first for external in the interface, if available there
  1741. then the cdecl must also be there since there is no implementation
  1742. available to contain it }
  1743. if parse_only then
  1744. begin
  1745. { if external is available, then cdecl must also be available,
  1746. procvars don't need external }
  1747. if not((po_external in pd.procoptions) or
  1748. (pd.deftype=procvardef)) and
  1749. not(pd.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  1750. Message(parser_e_varargs_need_cdecl_and_external);
  1751. end
  1752. else
  1753. begin
  1754. { both must be defined now }
  1755. if not((po_external in pd.procoptions) or
  1756. (pd.deftype=procvardef)) or
  1757. not(pd.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  1758. Message(parser_e_varargs_need_cdecl_and_external);
  1759. end;
  1760. end;
  1761. { add mangledname to external list }
  1762. if (pd.deftype=procdef) and
  1763. (po_external in pd.procoptions) and
  1764. target_info.DllScanSupported then
  1765. current_module.externals.insert(tExternalsItem.create(tprocdef(pd).mangledname));
  1766. end;
  1767. procedure calc_parast(pd:tabstractprocdef);
  1768. var
  1769. currpara : tparaitem;
  1770. begin
  1771. { insert hidden high parameters }
  1772. insert_hidden_para(pd);
  1773. { insert hidden self parameter }
  1774. insert_self_and_vmt_para(pd);
  1775. { insert funcret parameter if required }
  1776. insert_funcret_para(pd);
  1777. { insert parentfp parameter if required }
  1778. insert_parentfp_para(pd);
  1779. currpara:=tparaitem(pd.para.first);
  1780. while assigned(currpara) do
  1781. begin
  1782. if not(assigned(currpara.parasym) and (currpara.parasym.typ=varsym)) then
  1783. internalerror(200304232);
  1784. { connect parasym to paraitem }
  1785. tvarsym(currpara.parasym).paraitem:=currpara;
  1786. { We need a local copy for a value parameter when only the
  1787. address is pushed. Open arrays and Array of Const are
  1788. an exception because they are allocated at runtime and the
  1789. address that is pushed is patched }
  1790. if (currpara.paratyp=vs_value) and
  1791. paramanager.push_addr_param(currpara.paratyp,currpara.paratype.def,pd.proccalloption) and
  1792. not(is_open_array(currpara.paratype.def) or
  1793. is_array_of_const(currpara.paratype.def)) then
  1794. include(tvarsym(currpara.parasym).varoptions,vo_has_local_copy);
  1795. currpara:=tparaitem(currpara.next);
  1796. end;
  1797. end;
  1798. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  1799. {
  1800. Parse the procedure directives. It does not matter if procedure directives
  1801. are written using ;procdir; or ['procdir'] syntax.
  1802. }
  1803. var
  1804. res : boolean;
  1805. begin
  1806. if (m_mac in aktmodeswitches) and (cs_externally_visible in aktlocalswitches) then
  1807. begin
  1808. tprocdef(pd).aliasnames.insert(tprocdef(pd).procsym.realname);
  1809. include(pd.procoptions,po_public);
  1810. end;
  1811. while token in [_ID,_LECKKLAMMER] do
  1812. begin
  1813. if try_to_consume(_LECKKLAMMER) then
  1814. begin
  1815. repeat
  1816. parse_proc_direc(pd,pdflags);
  1817. until not try_to_consume(_COMMA);
  1818. consume(_RECKKLAMMER);
  1819. { we always expect at least '[];' }
  1820. res:=true;
  1821. end
  1822. else
  1823. begin
  1824. res:=parse_proc_direc(pd,pdflags);
  1825. end;
  1826. { A procedure directive normally followed by a semicolon, but in
  1827. a const section or reading a type we should stop when _EQUAL is found,
  1828. because a constant/default value follows }
  1829. if res then
  1830. begin
  1831. if (block_type in [bt_const,bt_type]) and
  1832. (token=_EQUAL) then
  1833. break;
  1834. { support procedure proc;stdcall export; }
  1835. if not(is_proc_directive(token,(pd.deftype=procvardef))) then
  1836. consume(_SEMICOLON);
  1837. end
  1838. else
  1839. break;
  1840. end;
  1841. end;
  1842. procedure parse_var_proc_directives(sym:tsym);
  1843. var
  1844. pdflags : tpdflags;
  1845. pd : tabstractprocdef;
  1846. begin
  1847. pdflags:=[pd_procvar];
  1848. pd:=nil;
  1849. case sym.typ of
  1850. varsym :
  1851. pd:=tabstractprocdef(tvarsym(sym).vartype.def);
  1852. typedconstsym :
  1853. pd:=tabstractprocdef(ttypedconstsym(sym).typedconsttype.def);
  1854. typesym :
  1855. pd:=tabstractprocdef(ttypesym(sym).restype.def);
  1856. else
  1857. internalerror(2003042617);
  1858. end;
  1859. if pd.deftype<>procvardef then
  1860. internalerror(2003042618);
  1861. { names should never be used anyway }
  1862. parse_proc_directives(pd,pdflags);
  1863. end;
  1864. procedure parse_object_proc_directives(pd:tabstractprocdef);
  1865. var
  1866. pdflags : tpdflags;
  1867. begin
  1868. pdflags:=[pd_object];
  1869. parse_proc_directives(pd,pdflags);
  1870. end;
  1871. function proc_add_definition(var pd:tprocdef):boolean;
  1872. {
  1873. Add definition aprocdef to the overloaded definitions of aprocsym. If a
  1874. forwarddef is found and reused it returns true
  1875. }
  1876. var
  1877. hd : tprocdef;
  1878. ad,fd : tsym;
  1879. s1,s2 : stringid;
  1880. i : cardinal;
  1881. forwardfound : boolean;
  1882. po_comp : tprocoptions;
  1883. aprocsym : tprocsym;
  1884. begin
  1885. forwardfound:=false;
  1886. aprocsym:=tprocsym(pd.procsym);
  1887. { check overloaded functions if the same function already exists }
  1888. for i:=1 to aprocsym.procdef_count do
  1889. begin
  1890. hd:=aprocsym.procdef[i];
  1891. { Skip overloaded definitions that are declared in other
  1892. units }
  1893. if hd.procsym<>aprocsym then
  1894. continue;
  1895. { check the parameters, for delphi/tp it is possible to
  1896. leave the parameters away in the implementation (forwarddef=false).
  1897. But for an overload declared function this is not allowed }
  1898. if { check if empty implementation arguments match is allowed }
  1899. (
  1900. not(m_repeat_forward in aktmodeswitches) and
  1901. not(pd.forwarddef) and
  1902. (pd.maxparacount=0) and
  1903. not(po_overload in hd.procoptions)
  1904. ) or
  1905. { check arguments }
  1906. (
  1907. (compare_paras(pd.para,hd.para,cp_none,[cpo_comparedefaultvalue])>=te_equal) and
  1908. { for operators equal_paras is not enough !! }
  1909. ((pd.proctypeoption<>potype_operator) or (optoken<>_ASSIGNMENT) or
  1910. equal_defs(hd.rettype.def,pd.rettype.def))
  1911. ) then
  1912. begin
  1913. { Check if we've found the forwarddef, if found then
  1914. we need to update the forward def with the current
  1915. implementation settings }
  1916. if hd.forwarddef then
  1917. begin
  1918. forwardfound:=true;
  1919. { Check if the procedure type and return type are correct,
  1920. also the parameters must match also with the type }
  1921. if (hd.proctypeoption<>pd.proctypeoption) or
  1922. (
  1923. (m_repeat_forward in aktmodeswitches) and
  1924. (not((pd.maxparacount=0) or
  1925. (compare_paras(pd.para,hd.para,cp_all,[cpo_comparedefaultvalue])>=te_equal)))
  1926. ) or
  1927. (
  1928. ((m_repeat_forward in aktmodeswitches) or
  1929. not(is_void(pd.rettype.def))) and
  1930. (not equal_defs(hd.rettype.def,pd.rettype.def))) then
  1931. begin
  1932. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,
  1933. pd.fullprocname(false));
  1934. aprocsym.write_parameter_lists(pd);
  1935. break;
  1936. end;
  1937. { Check if both are declared forward }
  1938. if hd.forwarddef and pd.forwarddef then
  1939. begin
  1940. MessagePos1(pd.fileinfo,parser_e_function_already_declared_public_forward,
  1941. pd.fullprocname(false));
  1942. end;
  1943. { internconst or internproc only need to be defined once }
  1944. if (hd.proccalloption=pocall_internproc) then
  1945. pd.proccalloption:=hd.proccalloption
  1946. else
  1947. if (pd.proccalloption=pocall_internproc) then
  1948. hd.proccalloption:=pd.proccalloption;
  1949. if (po_internconst in hd.procoptions) then
  1950. include(pd.procoptions,po_internconst)
  1951. else if (po_internconst in pd.procoptions) then
  1952. include(hd.procoptions,po_internconst);
  1953. { Check calling convention }
  1954. if (hd.proccalloption<>pd.proccalloption) then
  1955. begin
  1956. { In delphi it is possible to specify the calling
  1957. convention in the interface or implementation if
  1958. there was no convention specified in the other
  1959. part }
  1960. if (m_delphi in aktmodeswitches) then
  1961. begin
  1962. if not(po_hascallingconvention in pd.procoptions) then
  1963. pd.proccalloption:=hd.proccalloption
  1964. else
  1965. if not(po_hascallingconvention in hd.procoptions) then
  1966. hd.proccalloption:=pd.proccalloption
  1967. else
  1968. begin
  1969. MessagePos(pd.fileinfo,parser_e_call_convention_dont_match_forward);
  1970. aprocsym.write_parameter_lists(pd);
  1971. { restore interface settings }
  1972. pd.proccalloption:=hd.proccalloption;
  1973. end;
  1974. end
  1975. else
  1976. begin
  1977. MessagePos(pd.fileinfo,parser_e_call_convention_dont_match_forward);
  1978. aprocsym.write_parameter_lists(pd);
  1979. { restore interface settings }
  1980. pd.proccalloption:=hd.proccalloption;
  1981. end;
  1982. end;
  1983. { Check procedure options, Delphi requires that class is
  1984. repeated in the implementation for class methods }
  1985. if (m_fpc in aktmodeswitches) then
  1986. po_comp:=[po_varargs,po_methodpointer,po_interrupt]
  1987. else
  1988. po_comp:=[po_classmethod,po_methodpointer];
  1989. if ((po_comp * hd.procoptions)<>(po_comp * pd.procoptions)) then
  1990. begin
  1991. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,
  1992. pd.fullprocname(false));
  1993. aprocsym.write_parameter_lists(pd);
  1994. { This error is non-fatal, we can recover }
  1995. end;
  1996. { Check manglednames }
  1997. if (m_repeat_forward in aktmodeswitches) or
  1998. (pd.minparacount>0) then
  1999. begin
  2000. { If mangled names are equal then they have the same amount of arguments }
  2001. { We can check the names of the arguments }
  2002. { both symtables are in the same order from left to right }
  2003. ad:=tsym(hd.parast.symindex.first);
  2004. fd:=tsym(pd.parast.symindex.first);
  2005. repeat
  2006. { skip default parameter constsyms }
  2007. while assigned(ad) and (ad.typ<>varsym) do
  2008. ad:=tsym(ad.indexnext);
  2009. while assigned(fd) and (fd.typ<>varsym) do
  2010. fd:=tsym(fd.indexnext);
  2011. { stop when one of the two lists is at the end }
  2012. if not assigned(ad) or not assigned(fd) then
  2013. break;
  2014. { retrieve names, remove reg for register parameters }
  2015. s1:=ad.name;
  2016. s2:=fd.name;
  2017. { compare names }
  2018. if (s1<>s2) then
  2019. begin
  2020. MessagePos3(pd.fileinfo,parser_e_header_different_var_names,
  2021. aprocsym.name,s1,s2);
  2022. break;
  2023. end;
  2024. ad:=tsym(ad.indexnext);
  2025. fd:=tsym(fd.indexnext);
  2026. until false;
  2027. if assigned(ad) xor assigned(fd) then
  2028. internalerror(200204178);
  2029. end;
  2030. { Everything is checked, now we can update the forward declaration
  2031. with the new data from the implementation }
  2032. hd.forwarddef:=pd.forwarddef;
  2033. hd.hasforward:=true;
  2034. hd.procoptions:=hd.procoptions+pd.procoptions;
  2035. if hd.extnumber=65535 then
  2036. hd.extnumber:=pd.extnumber;
  2037. while not pd.aliasnames.empty do
  2038. hd.aliasnames.insert(pd.aliasnames.getfirst);
  2039. { update fileinfo so position references the implementation,
  2040. also update funcretsym if it is already generated }
  2041. hd.fileinfo:=pd.fileinfo;
  2042. if assigned(hd.funcretsym) then
  2043. hd.funcretsym.fileinfo:=pd.fileinfo;
  2044. { update mangledname if the implementation has a fixed mangledname set }
  2045. if pd.has_mangledname then
  2046. begin
  2047. { rename also asmsymbol first, because the name can already be used }
  2048. objectlibrary.renameasmsymbol(hd.mangledname,pd.mangledname);
  2049. hd.setmangledname(pd.mangledname);
  2050. end;
  2051. { for compilerproc defines we need to rename and update the
  2052. symbolname to lowercase }
  2053. if (pd.proccalloption=pocall_compilerproc) then
  2054. begin
  2055. { rename to lowercase so users can't access it }
  2056. aprocsym.owner.rename(aprocsym.name,lower(aprocsym.name));
  2057. { also update the realname that is stored in the ppu }
  2058. stringdispose(aprocsym._realname);
  2059. aprocsym._realname:=stringdup('$'+aprocsym.name);
  2060. { the mangeled name is already changed by the pd_compilerproc }
  2061. { handler. It must be done immediately because if we have a }
  2062. { call to a compilerproc before it's implementation is }
  2063. { encountered, it must already use the new mangled name (JM) }
  2064. end;
  2065. { the procdef will be released by the symtable, we release
  2066. at least the parast }
  2067. pd.releasemem;
  2068. pd:=hd;
  2069. end
  2070. else
  2071. begin
  2072. { abstract methods aren't forward defined, but this }
  2073. { needs another error message }
  2074. if (po_abstractmethod in hd.procoptions) then
  2075. MessagePos(pd.fileinfo,parser_e_abstract_no_definition)
  2076. else
  2077. MessagePos(pd.fileinfo,parser_e_overloaded_have_same_parameters);
  2078. end;
  2079. { we found one proc with the same arguments, there are no others
  2080. so we can stop }
  2081. break;
  2082. end;
  2083. { check for allowing overload directive }
  2084. if not(m_fpc in aktmodeswitches) then
  2085. begin
  2086. { overload directive turns on overloading }
  2087. if ((po_overload in pd.procoptions) or
  2088. (po_overload in hd.procoptions)) then
  2089. begin
  2090. { check if all procs have overloading, but not if the proc is a method or
  2091. already declared forward, then the check is already done }
  2092. if not(hd.hasforward or
  2093. assigned(pd._class) or
  2094. (pd.forwarddef<>hd.forwarddef) or
  2095. ((po_overload in pd.procoptions) and
  2096. (po_overload in hd.procoptions))) then
  2097. begin
  2098. MessagePos1(pd.fileinfo,parser_e_no_overload_for_all_procs,aprocsym.realname);
  2099. break;
  2100. end;
  2101. end
  2102. else
  2103. begin
  2104. if not(hd.forwarddef) then
  2105. begin
  2106. MessagePos(pd.fileinfo,parser_e_procedure_overloading_is_off);
  2107. break;
  2108. end;
  2109. end;
  2110. end; { equal arguments }
  2111. end;
  2112. { if we didn't reuse a forwarddef then we add the procdef to the overloaded
  2113. list }
  2114. if not forwardfound then
  2115. begin
  2116. aprocsym.addprocdef(pd);
  2117. { add overloadnumber for unique naming, the overloadcount is
  2118. counted per module and 0 for the first procedure }
  2119. pd.overloadnumber:=aprocsym.overloadcount;
  2120. inc(aprocsym.overloadcount);
  2121. end;
  2122. proc_add_definition:=forwardfound;
  2123. end;
  2124. end.
  2125. {
  2126. $Log$
  2127. Revision 1.187 2004-08-22 11:24:27 peter
  2128. * don't insert result variables for constructor/destructors
  2129. Revision 1.186 2004/08/13 17:53:37 jonas
  2130. * only set the mangled name immediately for external procedures in macpas
  2131. mode if the procedure isn't cdecl (so that the c-prefix is taken into
  2132. account, necessary for Mac OS X)
  2133. Revision 1.185 2004/08/08 12:35:09 florian
  2134. * proc. var declarations in a class doesn't eat a public anymore
  2135. Revision 1.184 2004/07/17 13:51:57 florian
  2136. * function result location for syscalls on MOS hopefully correctly set now
  2137. Revision 1.183 2004/07/14 23:19:21 olle
  2138. + added external facilities for macpas
  2139. Revision 1.182 2004/06/20 08:55:30 florian
  2140. * logs truncated
  2141. Revision 1.181 2004/06/16 20:07:09 florian
  2142. * dwarf branch merged
  2143. Revision 1.180 2004/05/23 20:54:39 peter
  2144. * fixed 3114
  2145. Revision 1.179 2004/05/23 19:06:26 peter
  2146. * expect : after function when it is a forwarddef
  2147. Revision 1.178 2004/05/12 13:21:09 karoly
  2148. * few small changes to add syscall support to M68k/Amiga target
  2149. Revision 1.177 2004/05/11 22:52:48 olle
  2150. * Moved import_implicit_external to symsym
  2151. }