pdecsub.pas 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276
  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. const
  24. pd_global = $1; { directive must be global }
  25. pd_body = $2; { directive needs a body }
  26. pd_implemen = $4; { directive can be used implementation section }
  27. pd_interface = $8; { directive can be used interface section }
  28. pd_object = $10; { directive can be used object declaration }
  29. pd_procvar = $20; { directive can be used procvar declaration }
  30. pd_notobject = $40; { directive can not be used object declaration }
  31. pd_notobjintf= $80; { directive can not be used interface declaration }
  32. function is_proc_directive(tok:ttoken):boolean;
  33. procedure parameter_dec(aktprocdef:tabstractprocdef);
  34. procedure parse_proc_directives(var pdflags:word);
  35. procedure handle_calling_convention(sym:tprocsym;def:tabstractprocdef);
  36. procedure calc_parasymtable_addresses(def:tprocdef);
  37. procedure parse_proc_head(options:tproctypeoption);
  38. procedure parse_proc_dec;
  39. procedure parse_var_proc_directives(var sym : tsym);
  40. procedure parse_object_proc_directives(var sym : tprocsym);
  41. function proc_add_definition(aprocsym:tprocsym;var aprocdef : tprocdef) : boolean;
  42. implementation
  43. uses
  44. {$ifdef delphi}
  45. sysutils,
  46. {$else delphi}
  47. strings,
  48. {$endif delphi}
  49. { common }
  50. cutils,cclasses,
  51. { global }
  52. globtype,globals,verbose,
  53. systems,cpubase,
  54. { aasm }
  55. aasmbase,aasmtai,aasmcpu,
  56. { symtable }
  57. symbase,symtable,defutil,defcmp,paramgr,
  58. { pass 1 }
  59. node,htypechk,
  60. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,
  61. { parser }
  62. fmodule,scanner,
  63. pbase,pexpr,ptype,pdecl,
  64. { linking }
  65. import,gendef,
  66. { codegen }
  67. cpuinfo,cgbase
  68. ;
  69. procedure resetvaluepara(p:tnamedindexitem;arg:pointer);
  70. begin
  71. if tsym(p).typ=varsym then
  72. with tvarsym(p) do
  73. if copy(name,1,3)='val' then
  74. aktprocdef.parast.symsearch.rename(name,copy(name,4,length(name)));
  75. end;
  76. procedure checkparatype(p:tnamedindexitem;arg:pointer);
  77. var
  78. highname : string;
  79. begin
  80. if tsym(p).typ<>varsym then
  81. exit;
  82. with tvarsym(p) do
  83. begin
  84. if assigned(vartype.def) and
  85. (vartype.def.deftype=arraydef) and
  86. {not is_array_constructor(vartype.def) and}
  87. not is_variant_array(vartype.def) and
  88. not is_array_of_const(vartype.def) then
  89. begin
  90. if (varspez<>vs_var) then
  91. Message(parser_h_c_arrays_are_references);
  92. varspez:=vs_var;
  93. end;
  94. if assigned(vartype.def) and
  95. (is_array_of_const(vartype.def) or
  96. is_open_array(vartype.def) or
  97. is_shortstring(vartype.def)) then
  98. begin
  99. if is_open_string(vartype.def) then
  100. begin
  101. { change type to normal short string }
  102. Message(parser_w_cdecl_no_openstring);
  103. vartype:=cshortstringtype;
  104. end;
  105. if assigned(indexnext) and
  106. (tsym(indexnext).typ=varsym) and
  107. (copy(tvarsym(indexnext).name,1,4)='high') then
  108. begin
  109. { removing it is too complicated,
  110. we just hide it PM }
  111. highname:='hidden'+copy(tvarsym(indexnext).name,5,high(name));
  112. Message(parser_w_cdecl_has_no_high);
  113. owner.rename(tvarsym(indexnext).name,highname);
  114. if is_array_of_const(vartype.def) and
  115. assigned(indexnext.indexnext) then
  116. Message(parser_e_C_array_of_const_must_be_last);
  117. end
  118. else
  119. begin
  120. if is_array_of_const(vartype.def) and
  121. assigned(indexnext) then
  122. Message(parser_e_C_array_of_const_must_be_last);
  123. end;
  124. end;
  125. end;
  126. end;
  127. procedure parameter_dec(aktprocdef:tabstractprocdef);
  128. {
  129. handle_procvar needs the same changes
  130. }
  131. var
  132. is_procvar : boolean;
  133. sc : tsinglelist;
  134. htype,
  135. tt : ttype;
  136. arrayelementtype : ttype;
  137. hvs,
  138. vs : tvarsym;
  139. srsym : tsym;
  140. hs1 : string;
  141. varspez : Tvarspez;
  142. inserthigh : boolean;
  143. tdefaultvalue : tconstsym;
  144. defaultrequired : boolean;
  145. old_object_option : tsymoptions;
  146. dummyst : tparasymtable;
  147. currparast : tparasymtable;
  148. begin
  149. consume(_LKLAMMER);
  150. { Delphi/Kylix supports nonsense like }
  151. { procedure p(); }
  152. if try_to_consume(_RKLAMMER) and
  153. not(m_tp7 in aktmodeswitches) then
  154. exit;
  155. { parsing a proc or procvar ? }
  156. is_procvar:=(aktprocdef.deftype=procvardef);
  157. { create dummy symtable for procvars }
  158. if is_procvar then
  159. begin
  160. dummyst:=tparasymtable.create;
  161. currparast:=dummyst;
  162. end
  163. else
  164. begin
  165. currparast:=tparasymtable(tprocdef(aktprocdef).parast);
  166. end;
  167. { reset }
  168. sc:=tsinglelist.create;
  169. defaultrequired:=false;
  170. { the variables are always public }
  171. old_object_option:=current_object_option;
  172. current_object_option:=[sp_public];
  173. inc(testcurobject);
  174. repeat
  175. if try_to_consume(_VAR) then
  176. varspez:=vs_var
  177. else
  178. if try_to_consume(_CONST) then
  179. varspez:=vs_const
  180. else
  181. if (idtoken=_OUT) and (m_out in aktmodeswitches) then
  182. begin
  183. consume(_OUT);
  184. varspez:=vs_out
  185. end
  186. else
  187. varspez:=vs_value;
  188. inserthigh:=false;
  189. tdefaultvalue:=nil;
  190. tt.reset;
  191. { self is only allowed in procvars and class methods }
  192. if (idtoken=_SELF) and
  193. (is_procvar or
  194. (assigned(procinfo._class) and is_class(procinfo._class))) then
  195. begin
  196. if varspez <> vs_value then
  197. CGMessage(parser_e_self_call_by_value);
  198. if not is_procvar then
  199. begin
  200. htype.setdef(procinfo._class);
  201. vs:=tvarsym.create('@',htype);
  202. vs.varspez:=vs_var;
  203. { insert the sym in the parasymtable }
  204. tprocdef(aktprocdef).parast.insert(vs);
  205. inc(procinfo.selfpointer_offset,vs.address);
  206. end
  207. else
  208. vs:=nil;
  209. { must also be included for procvars to allow the proc2procvar }
  210. { type conversions (po_containsself is in po_comp) (JM) }
  211. include(aktprocdef.procoptions,po_containsself);
  212. consume(idtoken);
  213. consume(_COLON);
  214. single_type(tt,hs1,false);
  215. { this must be call-by-value, but we generate already an }
  216. { an error above if that's not the case (JM) }
  217. aktprocdef.concatpara(tt,vs,varspez,nil);
  218. { check the types for procedures only }
  219. if not is_procvar then
  220. begin
  221. if compare_defs(tt.def,procinfo._class,nothingn)=te_incompatible then
  222. CGMessage2(type_e_incompatible_types,tt.def.typename,procinfo._class.typename);
  223. end;
  224. end
  225. else
  226. begin
  227. { read identifiers and insert with error type }
  228. sc.reset;
  229. repeat
  230. vs:=tvarsym.create(orgpattern,generrortype);
  231. currparast.insert(vs);
  232. sc.insert(vs);
  233. consume(_ID);
  234. until not try_to_consume(_COMMA);
  235. { read type declaration, force reading for value and const paras }
  236. if (token=_COLON) or (varspez=vs_value) then
  237. begin
  238. consume(_COLON);
  239. { check for an open array }
  240. if token=_ARRAY then
  241. begin
  242. consume(_ARRAY);
  243. consume(_OF);
  244. { define range and type of range }
  245. tt.setdef(tarraydef.create(0,-1,s32bittype));
  246. { array of const ? }
  247. if (token=_CONST) and (m_objpas in aktmodeswitches) then
  248. begin
  249. consume(_CONST);
  250. srsym:=searchsymonlyin(systemunit,'TVARREC');
  251. if not assigned(srsym) then
  252. InternalError(1234124);
  253. tarraydef(tt.def).setelementtype(ttypesym(srsym).restype);
  254. tarraydef(tt.def).IsArrayOfConst:=true;
  255. end
  256. else
  257. begin
  258. { define field type }
  259. single_type(arrayelementtype,hs1,false);
  260. tarraydef(tt.def).setelementtype(arrayelementtype);
  261. end;
  262. inserthigh:=true;
  263. end
  264. else
  265. begin
  266. { open string ? }
  267. if (varspez=vs_var) and
  268. (
  269. (
  270. ((token=_STRING) or (idtoken=_SHORTSTRING)) and
  271. (cs_openstring in aktmoduleswitches) and
  272. not(cs_ansistrings in aktlocalswitches)
  273. ) or
  274. (idtoken=_OPENSTRING)) then
  275. begin
  276. consume(token);
  277. tt:=openshortstringtype;
  278. hs1:='openstring';
  279. inserthigh:=true;
  280. end
  281. else
  282. begin
  283. { everything else }
  284. single_type(tt,hs1,false);
  285. end;
  286. { default parameter }
  287. if (m_default_para in aktmodeswitches) then
  288. begin
  289. if try_to_consume(_EQUAL) then
  290. begin
  291. vs:=tvarsym(sc.first);
  292. if assigned(vs.listnext) then
  293. Message(parser_e_default_value_only_one_para);
  294. { prefix 'def' to the parameter name }
  295. tdefaultvalue:=ReadConstant('$def'+vs.name,vs.fileinfo);
  296. if assigned(tdefaultvalue) then
  297. tprocdef(aktprocdef).parast.insert(tdefaultvalue);
  298. defaultrequired:=true;
  299. end
  300. else
  301. begin
  302. if defaultrequired then
  303. Message1(parser_e_default_value_expected_for_para,vs.name);
  304. end;
  305. end;
  306. end;
  307. end
  308. else
  309. begin
  310. {$ifndef UseNiceNames}
  311. hs1:='$$$';
  312. {$else UseNiceNames}
  313. hs1:='var';
  314. {$endif UseNiceNames}
  315. tt:=cformaltype;
  316. end;
  317. { For proc vars we only need the definitions }
  318. if not is_procvar then
  319. begin
  320. vs:=tvarsym(sc.first);
  321. while assigned(vs) do
  322. begin
  323. { update varsym }
  324. vs.vartype:=tt;
  325. vs.varspez:=varspez;
  326. if (varspez in [vs_var,vs_const,vs_out]) and
  327. paramanager.push_addr_param(tt.def,aktprocdef.proccalloption) then
  328. include(vs.varoptions,vo_regable);
  329. { do we need a local copy? Then rename the varsym, do this after the
  330. insert so the dup id checking is done correctly }
  331. if (varspez=vs_value) and
  332. paramanager.push_addr_param(tt.def,aktprocdef.proccalloption) and
  333. not(is_open_array(tt.def) or is_array_of_const(tt.def)) then
  334. currparast.rename(vs.name,'val'+vs.name);
  335. { also need to push a high value? }
  336. if inserthigh then
  337. begin
  338. hvs:=tvarsym.create('$high'+vs.name,s32bittype);
  339. hvs.varspez:=vs_const;
  340. {$ifdef vs_hidden}
  341. aktprocdef.concatpara(s32bittype,hvs,vs_hidden,nil);
  342. {$endif vs_hidden}
  343. currparast.insert(hvs);
  344. end;
  345. aktprocdef.concatpara(tt,vs,varspez,tdefaultvalue);
  346. vs:=tvarsym(vs.listnext);
  347. end;
  348. end
  349. else
  350. begin
  351. vs:=tvarsym(sc.first);
  352. while assigned(vs) do
  353. begin
  354. { don't insert a parasym, the varsyms will be
  355. disposed }
  356. aktprocdef.concatpara(tt,nil,varspez,tdefaultvalue);
  357. vs:=tvarsym(vs.listnext);
  358. end;
  359. end;
  360. end;
  361. { set the new mangled name }
  362. until not try_to_consume(_SEMICOLON);
  363. { remove parasymtable from stack }
  364. if is_procvar then
  365. dummyst.free;
  366. sc.free;
  367. { reset object options }
  368. dec(testcurobject);
  369. current_object_option:=old_object_option;
  370. consume(_RKLAMMER);
  371. end;
  372. procedure parse_proc_head(options:tproctypeoption);
  373. var
  374. orgsp,sp:stringid;
  375. paramoffset:longint;
  376. sym:tsym;
  377. st : tsymtable;
  378. srsymtable : tsymtable;
  379. storepos,procstartfilepos : tfileposinfo;
  380. i: longint;
  381. begin
  382. { Save the position where this procedure really starts }
  383. procstartfilepos:=akttokenpos;
  384. aktprocdef:=nil;
  385. if (options=potype_operator) then
  386. begin
  387. sp:=overloaded_names[optoken];
  388. orgsp:=sp;
  389. end
  390. else
  391. begin
  392. sp:=pattern;
  393. orgsp:=orgpattern;
  394. consume(_ID);
  395. end;
  396. { examine interface map: function/procedure iname.functionname=locfuncname }
  397. if parse_only and
  398. assigned(procinfo._class) and
  399. assigned(procinfo._class.implementedinterfaces) and
  400. (procinfo._class.implementedinterfaces.count>0) and
  401. try_to_consume(_POINT) then
  402. begin
  403. storepos:=akttokenpos;
  404. akttokenpos:=procstartfilepos;
  405. { get interface syms}
  406. searchsym(sp,sym,srsymtable);
  407. if not assigned(sym) then
  408. begin
  409. identifier_not_found(orgsp);
  410. sym:=generrorsym;
  411. end;
  412. akttokenpos:=storepos;
  413. { load proc name }
  414. if sym.typ=typesym then
  415. i:=procinfo._class.implementedinterfaces.searchintf(ttypesym(sym).restype.def);
  416. { qualifier is interface name? }
  417. if (sym.typ<>typesym) or (ttypesym(sym).restype.def.deftype<>objectdef) or
  418. (i=-1) then
  419. begin
  420. Message(parser_e_interface_id_expected);
  421. aktprocsym:=nil;
  422. end
  423. else
  424. begin
  425. aktprocsym:=tprocsym(procinfo._class.implementedinterfaces.interfaces(i).symtable.search(sp));
  426. { the method can be declared after the mapping FK
  427. if not(assigned(aktprocsym)) then
  428. Message(parser_e_methode_id_expected);
  429. }
  430. end;
  431. consume(_ID);
  432. consume(_EQUAL);
  433. if (token=_ID) { and assigned(aktprocsym) } then
  434. procinfo._class.implementedinterfaces.addmappings(i,sp,pattern);
  435. consume(_ID);
  436. exit;
  437. end;
  438. { method ? }
  439. if not(parse_only) and
  440. (lexlevel=normal_function_level) and
  441. try_to_consume(_POINT) then
  442. begin
  443. { search for object name }
  444. storepos:=akttokenpos;
  445. akttokenpos:=procstartfilepos;
  446. searchsym(sp,sym,srsymtable);
  447. if not assigned(sym) then
  448. begin
  449. identifier_not_found(orgsp);
  450. sym:=generrorsym;
  451. end;
  452. akttokenpos:=storepos;
  453. { consume proc name }
  454. sp:=pattern;
  455. orgsp:=orgpattern;
  456. procstartfilepos:=akttokenpos;
  457. consume(_ID);
  458. { qualifier is class name ? }
  459. if (sym.typ<>typesym) or
  460. (ttypesym(sym).restype.def.deftype<>objectdef) then
  461. begin
  462. Message(parser_e_class_id_expected);
  463. aktprocsym:=nil;
  464. aktprocdef:=nil;
  465. end
  466. else
  467. begin
  468. { used to allow private syms to be seen }
  469. aktobjectdef:=tobjectdef(ttypesym(sym).restype.def);
  470. procinfo._class:=tobjectdef(ttypesym(sym).restype.def);
  471. aktprocsym:=tprocsym(procinfo._class.symtable.search(sp));
  472. {The procedure has been found. So it is
  473. a global one. Set the flags to mark this.}
  474. procinfo.flags:=procinfo.flags or pi_is_global;
  475. aktobjectdef:=nil;
  476. { we solve this below }
  477. if not(assigned(aktprocsym)) then
  478. Message(parser_e_methode_id_expected);
  479. end;
  480. end
  481. else
  482. begin
  483. { check for constructor/destructor which is not allowed here }
  484. if (not parse_only) and
  485. (options in [potype_constructor,potype_destructor]) then
  486. Message(parser_e_constructors_always_objects);
  487. akttokenpos:=procstartfilepos;
  488. aktprocsym:=tprocsym(symtablestack.search(sp));
  489. if not(parse_only) then
  490. begin
  491. {The procedure we prepare for is in the implementation
  492. part of the unit we compile. It is also possible that we
  493. are compiling a program, which is also some kind of
  494. implementaion part.
  495. We need to find out if the procedure is global. If it is
  496. global, it is in the global symtable.}
  497. if not assigned(aktprocsym) and
  498. (symtablestack.symtabletype=staticsymtable) and
  499. assigned(symtablestack.next) and
  500. (symtablestack.next.unitid=0) then
  501. begin
  502. {Search the procedure in the global symtable.}
  503. aktprocsym:=tprocsym(symtablestack.next.search(sp));
  504. if assigned(aktprocsym) then
  505. begin
  506. {Check if it is a procedure.}
  507. if aktprocsym.typ<>procsym then
  508. DuplicateSym(aktprocsym);
  509. {The procedure has been found. So it is
  510. a global one. Set the flags to mark this.}
  511. procinfo.flags:=procinfo.flags or pi_is_global;
  512. end;
  513. end;
  514. end;
  515. end;
  516. if assigned(aktprocsym) then
  517. begin
  518. { Check if overloaded is a procsym }
  519. if aktprocsym.typ<>procsym then
  520. begin
  521. { when the other symbol is a unit symbol then hide the unit
  522. symbol. Only in tp mode because it's bad programming }
  523. if (m_duplicate_names in aktmodeswitches) and
  524. (aktprocsym.typ=unitsym) then
  525. begin
  526. aktprocsym.owner.rename(aktprocsym.name,'hidden'+aktprocsym.name);
  527. end
  528. else
  529. begin
  530. { we use a different error message for tp7 so it looks more compatible }
  531. if (m_fpc in aktmodeswitches) then
  532. Message1(parser_e_overloaded_no_procedure,aktprocsym.realname)
  533. else
  534. DuplicateSym(aktprocsym);
  535. { rename the name to an unique name to avoid an
  536. error when inserting the symbol in the symtable }
  537. orgsp:=orgsp+'$'+tostr(aktfilepos.line);
  538. end;
  539. { generate a new aktprocsym }
  540. aktprocsym:=nil;
  541. end;
  542. end;
  543. { test again if assigned, it can be reset to recover }
  544. if not assigned(aktprocsym) then
  545. begin
  546. { create a new procsym and set the real filepos }
  547. akttokenpos:=procstartfilepos;
  548. { for operator we have only one procsym for each overloaded
  549. operation }
  550. if (options=potype_operator) then
  551. begin
  552. { is the current overload sym already in the current unit }
  553. if assigned(overloaded_operators[optoken]) and
  554. (overloaded_operators[optoken].owner=symtablestack) then
  555. aktprocsym:=overloaded_operators[optoken]
  556. else
  557. begin
  558. { create the procsym with saving the original case }
  559. aktprocsym:=tprocsym.create('$'+sp);
  560. if assigned(overloaded_operators[optoken]) then
  561. overloaded_operators[optoken].concat_procdefs_to(aktprocsym);
  562. end;
  563. end
  564. else
  565. aktprocsym:=tprocsym.create(orgsp);
  566. symtablestack.insert(aktprocsym);
  567. end;
  568. st:=symtablestack;
  569. aktprocdef:=tprocdef.create;
  570. aktprocdef.symtablelevel:=symtablestack.symtablelevel;
  571. if assigned(procinfo._class) then
  572. aktprocdef._class := procinfo._class;
  573. { set the options from the caller (podestructor or poconstructor) }
  574. aktprocdef.proctypeoption:=options;
  575. { add procsym to the procdef }
  576. aktprocdef.procsym:=aktprocsym;
  577. { save file position }
  578. aktprocdef.fileinfo:=procstartfilepos;
  579. { this must also be inserted in the right symtable !! PM }
  580. { otherwise we get subbtle problems with
  581. definitions of args defs in staticsymtable for
  582. implementation of a global method }
  583. if token=_LKLAMMER then
  584. parameter_dec(aktprocdef);
  585. { calculate the offset of the parameters }
  586. paramoffset:=target_info.first_parm_offset;
  587. { calculate frame pointer offset }
  588. if lexlevel>normal_function_level then
  589. begin
  590. procinfo.framepointer_offset:=paramoffset;
  591. inc(paramoffset,pointer_size);
  592. { this is needed to get correct framepointer push for local
  593. forward functions !! }
  594. aktprocdef.parast.symtablelevel:=lexlevel;
  595. end;
  596. if assigned (procinfo._Class) and
  597. is_object(procinfo._Class) and
  598. (aktprocdef.proctypeoption in [potype_constructor,potype_destructor]) then
  599. inc(paramoffset,pointer_size);
  600. { self pointer offset, must be done after parsing the parameters }
  601. { self isn't pushed in nested procedure of methods }
  602. if assigned(procinfo._class) and (lexlevel=normal_function_level) then
  603. begin
  604. procinfo.selfpointer_offset:=paramoffset;
  605. if assigned(aktprocdef) and
  606. not(po_containsself in aktprocdef.procoptions) then
  607. inc(paramoffset,pointer_size);
  608. end;
  609. { con/-destructor flag ? }
  610. if assigned (procinfo._Class) and
  611. is_class(procinfo._class) and
  612. (aktprocdef.proctypeoption in [potype_destructor,potype_constructor]) then
  613. inc(paramoffset,pointer_size);
  614. procinfo.para_offset:=paramoffset;
  615. { so we only restore the symtable now }
  616. symtablestack:=st;
  617. if (options=potype_operator) then
  618. overloaded_operators[optoken]:=aktprocsym;
  619. end;
  620. procedure parse_proc_dec;
  621. var
  622. hs : string;
  623. isclassmethod : boolean;
  624. begin
  625. inc(lexlevel);
  626. { read class method }
  627. if token=_CLASS then
  628. begin
  629. consume(_CLASS);
  630. isclassmethod:=true;
  631. end
  632. else
  633. isclassmethod:=false;
  634. case token of
  635. _FUNCTION : begin
  636. consume(_FUNCTION);
  637. parse_proc_head(potype_none);
  638. if token<>_COLON then
  639. begin
  640. if assigned(aktprocsym) and
  641. not(is_interface(aktprocdef._class)) and
  642. not(aktprocdef.forwarddef) or
  643. (m_repeat_forward in aktmodeswitches) then
  644. begin
  645. consume(_COLON);
  646. consume_all_until(_SEMICOLON);
  647. end;
  648. end
  649. else
  650. begin
  651. consume(_COLON);
  652. inc(testcurobject);
  653. single_type(aktprocdef.rettype,hs,false);
  654. aktprocdef.test_if_fpu_result;
  655. if (aktprocdef.rettype.def.deftype=stringdef) and
  656. (tstringdef(aktprocdef.rettype.def).string_typ<>st_shortstring) then
  657. procinfo.no_fast_exit:=true;
  658. dec(testcurobject);
  659. end;
  660. end;
  661. _PROCEDURE : begin
  662. consume(_PROCEDURE);
  663. parse_proc_head(potype_none);
  664. if assigned(aktprocsym) then
  665. aktprocdef.rettype:=voidtype;
  666. end;
  667. _CONSTRUCTOR : begin
  668. consume(_CONSTRUCTOR);
  669. parse_proc_head(potype_constructor);
  670. if assigned(procinfo._class) and
  671. is_class(procinfo._class) then
  672. begin
  673. { CLASS constructors return the created instance }
  674. aktprocdef.rettype.setdef(procinfo._class);
  675. end
  676. else
  677. begin
  678. { OBJECT constructors return a boolean }
  679. aktprocdef.rettype:=booltype;
  680. end;
  681. end;
  682. _DESTRUCTOR : begin
  683. consume(_DESTRUCTOR);
  684. parse_proc_head(potype_destructor);
  685. aktprocdef.rettype:=voidtype;
  686. end;
  687. _OPERATOR : begin
  688. if lexlevel>normal_function_level then
  689. Message(parser_e_no_local_operator);
  690. consume(_OPERATOR);
  691. if (token in [first_overloaded..last_overloaded]) then
  692. begin
  693. procinfo.flags:=procinfo.flags or pi_operator;
  694. optoken:=token;
  695. end
  696. else
  697. begin
  698. Message(parser_e_overload_operator_failed);
  699. { Use the dummy NOTOKEN that is also declared
  700. for the overloaded_operator[] }
  701. optoken:=NOTOKEN;
  702. end;
  703. consume(Token);
  704. parse_proc_head(potype_operator);
  705. if token<>_ID then
  706. begin
  707. otsym:=nil;
  708. if not(m_result in aktmodeswitches) then
  709. consume(_ID);
  710. end
  711. else
  712. begin
  713. otsym:=tvarsym.create(pattern,voidtype);
  714. consume(_ID);
  715. end;
  716. if not try_to_consume(_COLON) then
  717. begin
  718. consume(_COLON);
  719. aktprocdef.rettype:=generrortype;
  720. consume_all_until(_SEMICOLON);
  721. end
  722. else
  723. begin
  724. single_type(aktprocdef.rettype,hs,false);
  725. aktprocdef.test_if_fpu_result;
  726. if (optoken in [_EQUAL,_GT,_LT,_GTE,_LTE]) and
  727. ((aktprocdef.rettype.def.deftype<>orddef) or
  728. (torddef(aktprocdef.rettype.def).typ<>bool8bit)) then
  729. Message(parser_e_comparative_operator_return_boolean);
  730. if assigned(otsym) then
  731. otsym.vartype.def:=aktprocdef.rettype.def;
  732. if (optoken=_ASSIGNMENT) and
  733. equal_defs(aktprocdef.rettype.def,
  734. tvarsym(aktprocdef.parast.symindex.first).vartype.def) then
  735. message(parser_e_no_such_assignment)
  736. else if not isoperatoracceptable(aktprocdef,optoken) then
  737. Message(parser_e_overload_impossible);
  738. end;
  739. end;
  740. end;
  741. if isclassmethod and
  742. assigned(aktprocsym) then
  743. include(aktprocdef.procoptions,po_classmethod);
  744. { support procedure proc;stdcall export; in Delphi mode only }
  745. if not((m_delphi in aktmodeswitches) and
  746. is_proc_directive(token)) then
  747. consume(_SEMICOLON);
  748. dec(lexlevel);
  749. end;
  750. {****************************************************************************
  751. Procedure directive handlers
  752. ****************************************************************************}
  753. procedure pd_far;
  754. begin
  755. Message1(parser_w_proc_directive_ignored,'FAR');
  756. end;
  757. procedure pd_near;
  758. begin
  759. Message1(parser_w_proc_directive_ignored,'NEAR');
  760. end;
  761. procedure pd_export;
  762. begin
  763. if assigned(procinfo._class) then
  764. Message(parser_e_methods_dont_be_export);
  765. if lexlevel<>normal_function_level then
  766. Message(parser_e_dont_nest_export);
  767. { only os/2 needs this }
  768. if target_info.system=system_i386_os2 then
  769. begin
  770. aktprocdef.aliasnames.insert(aktprocsym.realname);
  771. procinfo.exported:=true;
  772. if cs_link_deffile in aktglobalswitches then
  773. deffile.AddExport(aktprocdef.mangledname);
  774. end;
  775. end;
  776. procedure pd_forward;
  777. begin
  778. aktprocdef.forwarddef:=true;
  779. end;
  780. procedure pd_alias;
  781. begin
  782. consume(_COLON);
  783. aktprocdef.aliasnames.insert(get_stringconst);
  784. end;
  785. procedure pd_asmname;
  786. begin
  787. aktprocdef.setmangledname(target_info.Cprefix+pattern);
  788. if token=_CCHAR then
  789. consume(_CCHAR)
  790. else
  791. consume(_CSTRING);
  792. { we don't need anything else }
  793. aktprocdef.forwarddef:=false;
  794. end;
  795. procedure pd_inline;
  796. var
  797. hp : tparaitem;
  798. begin
  799. { check if there is an array of const }
  800. hp:=tparaitem(aktprocdef.para.first);
  801. while assigned(hp) do
  802. begin
  803. if assigned(hp.paratype.def) and
  804. (hp.paratype.def.deftype=arraydef) then
  805. begin
  806. with tarraydef(hp.paratype.def) do
  807. if IsVariant or IsConstructor {or IsArrayOfConst} then
  808. begin
  809. Message1(parser_w_not_supported_for_inline,'array of const');
  810. Message(parser_w_inlining_disabled);
  811. aktprocdef.proccalloption:=pocall_fpccall;
  812. end;
  813. end;
  814. hp:=tparaitem(hp.next);
  815. end;
  816. end;
  817. procedure pd_intern;
  818. begin
  819. consume(_COLON);
  820. aktprocdef.extnumber:=get_intconst;
  821. end;
  822. procedure pd_interrupt;
  823. begin
  824. if lexlevel<>normal_function_level then
  825. Message(parser_e_dont_nest_interrupt);
  826. end;
  827. procedure pd_abstract;
  828. begin
  829. if (po_virtualmethod in aktprocdef.procoptions) then
  830. include(aktprocdef.procoptions,po_abstractmethod)
  831. else
  832. Message(parser_e_only_virtual_methods_abstract);
  833. { the method is defined }
  834. aktprocdef.forwarddef:=false;
  835. end;
  836. procedure pd_virtual;
  837. {$ifdef WITHDMT}
  838. var
  839. pt : tnode;
  840. {$endif WITHDMT}
  841. begin
  842. if (aktprocdef.proctypeoption=potype_constructor) and
  843. is_object(aktprocdef._class) then
  844. Message(parser_e_constructor_cannot_be_not_virtual);
  845. {$ifdef WITHDMT}
  846. if is_object(aktprocdef._class) and
  847. (token<>_SEMICOLON) then
  848. begin
  849. { any type of parameter is allowed here! }
  850. pt:=comp_expr(true);
  851. if is_constintnode(pt) then
  852. begin
  853. include(aktprocdef.procoptions,po_msgint);
  854. aktprocdef.messageinf.i:=pt^.value;
  855. end
  856. else
  857. Message(parser_e_ill_msg_expr);
  858. disposetree(pt);
  859. end;
  860. {$endif WITHDMT}
  861. end;
  862. procedure pd_static;
  863. begin
  864. if (cs_static_keyword in aktmoduleswitches) then
  865. begin
  866. include(aktprocsym.symoptions,sp_static);
  867. include(aktprocdef.procoptions,po_staticmethod);
  868. end;
  869. end;
  870. procedure pd_override;
  871. begin
  872. if not(is_class_or_interface(aktprocdef._class)) then
  873. Message(parser_e_no_object_override);
  874. end;
  875. procedure pd_overload;
  876. begin
  877. include(aktprocsym.symoptions,sp_has_overloaded);
  878. end;
  879. procedure pd_message;
  880. var
  881. pt : tnode;
  882. begin
  883. { check parameter type }
  884. if not(po_containsself in aktprocdef.procoptions) and
  885. ((aktprocdef.minparacount<>1) or
  886. (aktprocdef.maxparacount<>1) or
  887. (TParaItem(aktprocdef.Para.first).paratyp<>vs_var)) then
  888. Message(parser_e_ill_msg_param);
  889. pt:=comp_expr(true);
  890. if pt.nodetype=stringconstn then
  891. begin
  892. include(aktprocdef.procoptions,po_msgstr);
  893. aktprocdef.messageinf.str:=strnew(tstringconstnode(pt).value_str);
  894. end
  895. else
  896. if is_constintnode(pt) then
  897. begin
  898. include(aktprocdef.procoptions,po_msgint);
  899. aktprocdef.messageinf.i:=tordconstnode(pt).value;
  900. end
  901. else
  902. Message(parser_e_ill_msg_expr);
  903. pt.free;
  904. end;
  905. procedure pd_reintroduce;
  906. begin
  907. Message1(parser_w_proc_directive_ignored,'REINTRODUCE');
  908. end;
  909. procedure pd_syscall;
  910. begin
  911. aktprocdef.forwarddef:=false;
  912. aktprocdef.extnumber:=get_intconst;
  913. end;
  914. procedure pd_external;
  915. {
  916. If import_dll=nil the procedure is assumed to be in another
  917. object file. In that object file it should have the name to
  918. which import_name is pointing to. Otherwise, the procedure is
  919. assumed to be in the DLL to which import_dll is pointing to. In
  920. that case either import_nr<>0 or import_name<>nil is true, so
  921. the procedure is either imported by number or by name. (DM)
  922. }
  923. var
  924. pd : tprocdef;
  925. import_dll,
  926. import_name : string;
  927. import_nr : word;
  928. begin
  929. aktprocdef.forwarddef:=false;
  930. { forbid local external procedures }
  931. if lexlevel>normal_function_level then
  932. Message(parser_e_no_local_external);
  933. { If the procedure should be imported from a DLL, a constant string follows.
  934. This isn't really correct, an contant string expression follows
  935. so we check if an semicolon follows, else a string constant have to
  936. follow (FK) }
  937. import_nr:=0;
  938. import_name:='';
  939. if not(token=_SEMICOLON) and not(idtoken=_NAME) then
  940. begin
  941. import_dll:=get_stringconst;
  942. if (idtoken=_NAME) then
  943. begin
  944. consume(_NAME);
  945. import_name:=get_stringconst;
  946. end;
  947. if (idtoken=_INDEX) then
  948. begin
  949. {After the word index follows the index number in the DLL.}
  950. consume(_INDEX);
  951. import_nr:=get_intconst;
  952. end;
  953. { default is to used the realname of the procedure }
  954. if (import_nr=0) and (import_name='') then
  955. import_name:=aktprocsym.realname;
  956. { create importlib if not already done }
  957. if not(current_module.uses_imports) then
  958. begin
  959. current_module.uses_imports:=true;
  960. importlib.preparelib(current_module.modulename^);
  961. end;
  962. if not(m_repeat_forward in aktmodeswitches) then
  963. begin
  964. { we can only have one overloaded here ! }
  965. if aktprocsym.procdef_count>1 then
  966. pd:=aktprocsym.procdef[2]
  967. else
  968. pd:=aktprocdef;
  969. end
  970. else
  971. pd:=aktprocdef;
  972. importlib.importproceduredef(pd,import_dll,import_nr,import_name);
  973. end
  974. else
  975. begin
  976. if (idtoken=_NAME) then
  977. begin
  978. consume(_NAME);
  979. import_name:=get_stringconst;
  980. aktprocdef.setmangledname(import_name);
  981. end;
  982. end;
  983. end;
  984. type
  985. pd_handler=procedure;
  986. proc_dir_rec=record
  987. idtok : ttoken;
  988. pd_flags : longint;
  989. handler : pd_handler;
  990. pocall : tproccalloption;
  991. pooption : tprocoptions;
  992. mutexclpocall : tproccalloptions;
  993. mutexclpotype : tproctypeoptions;
  994. mutexclpo : tprocoptions;
  995. end;
  996. const
  997. {Should contain the number of procedure directives we support.}
  998. num_proc_directives=36;
  999. proc_direcdata:array[1..num_proc_directives] of proc_dir_rec=
  1000. (
  1001. (
  1002. idtok:_ABSTRACT;
  1003. pd_flags : pd_interface+pd_object+pd_notobjintf;
  1004. handler : {$ifdef FPCPROCVAR}@{$endif}pd_abstract;
  1005. pocall : pocall_none;
  1006. pooption : [po_abstractmethod];
  1007. mutexclpocall : [pocall_internproc,pocall_inline];
  1008. mutexclpotype : [potype_constructor,potype_destructor];
  1009. mutexclpo : [po_exports,po_interrupt,po_external]
  1010. ),(
  1011. idtok:_ALIAS;
  1012. pd_flags : pd_implemen+pd_body+pd_notobjintf;
  1013. handler : {$ifdef FPCPROCVAR}@{$endif}pd_alias;
  1014. pocall : pocall_none;
  1015. pooption : [];
  1016. mutexclpocall : [pocall_inline];
  1017. mutexclpotype : [];
  1018. mutexclpo : [po_external]
  1019. ),(
  1020. idtok:_ASMNAME;
  1021. pd_flags : pd_interface+pd_implemen+pd_notobjintf;
  1022. handler : {$ifdef FPCPROCVAR}@{$endif}pd_asmname;
  1023. pocall : pocall_cdecl;
  1024. pooption : [po_external];
  1025. mutexclpocall : [pocall_internproc,pocall_inline];
  1026. mutexclpotype : [];
  1027. mutexclpo : [po_external]
  1028. ),(
  1029. idtok:_ASSEMBLER;
  1030. pd_flags : pd_implemen+pd_body+pd_notobjintf;
  1031. handler : nil;
  1032. pocall : pocall_none;
  1033. pooption : [po_assembler];
  1034. mutexclpocall : [];
  1035. mutexclpotype : [];
  1036. mutexclpo : [po_external]
  1037. ),(
  1038. idtok:_CDECL;
  1039. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  1040. handler : nil;
  1041. pocall : pocall_cdecl;
  1042. pooption : [];
  1043. mutexclpocall : [];
  1044. mutexclpotype : [];
  1045. mutexclpo : [po_assembler,po_external,po_virtualmethod]
  1046. ),(
  1047. idtok:_DYNAMIC;
  1048. pd_flags : pd_interface+pd_object+pd_notobjintf;
  1049. handler : {$ifdef FPCPROCVAR}@{$endif}pd_virtual;
  1050. pocall : pocall_none;
  1051. pooption : [po_virtualmethod];
  1052. mutexclpocall : [pocall_internproc,pocall_inline];
  1053. mutexclpotype : [];
  1054. mutexclpo : [po_exports,po_interrupt,po_external]
  1055. ),(
  1056. idtok:_EXPORT;
  1057. pd_flags : pd_body+pd_global+pd_interface+pd_implemen{??}+pd_notobjintf;
  1058. handler : {$ifdef FPCPROCVAR}@{$endif}pd_export;
  1059. pocall : pocall_none;
  1060. pooption : [po_exports];
  1061. mutexclpocall : [pocall_internproc,pocall_inline];
  1062. mutexclpotype : [];
  1063. mutexclpo : [po_external,po_interrupt]
  1064. ),(
  1065. idtok:_EXTERNAL;
  1066. pd_flags : pd_implemen+pd_interface+pd_notobject+pd_notobjintf;
  1067. handler : {$ifdef FPCPROCVAR}@{$endif}pd_external;
  1068. pocall : pocall_none;
  1069. pooption : [po_external];
  1070. mutexclpocall : [pocall_internproc,pocall_inline,pocall_palmossyscall];
  1071. mutexclpotype : [];
  1072. mutexclpo : [po_exports,po_interrupt,po_assembler]
  1073. ),(
  1074. idtok:_FAR;
  1075. pd_flags : pd_implemen+pd_body+pd_interface+pd_procvar+pd_notobject+pd_notobjintf;
  1076. handler : {$ifdef FPCPROCVAR}@{$endif}pd_far;
  1077. pocall : pocall_none;
  1078. pooption : [];
  1079. mutexclpocall : [pocall_internproc,pocall_inline];
  1080. mutexclpotype : [];
  1081. mutexclpo : []
  1082. ),(
  1083. idtok:_FAR16;
  1084. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar+pd_notobject;
  1085. handler : nil;
  1086. pocall : pocall_far16;
  1087. pooption : [];
  1088. mutexclpocall : [];
  1089. mutexclpotype : [];
  1090. mutexclpo : [po_external,po_leftright]
  1091. ),(
  1092. idtok:_FORWARD;
  1093. pd_flags : pd_implemen+pd_notobject+pd_notobjintf;
  1094. handler : {$ifdef FPCPROCVAR}@{$endif}pd_forward;
  1095. pocall : pocall_none;
  1096. pooption : [];
  1097. mutexclpocall : [pocall_internproc,pocall_inline];
  1098. mutexclpotype : [];
  1099. mutexclpo : [po_external]
  1100. ),(
  1101. idtok:_FPCCALL;
  1102. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  1103. handler : nil;
  1104. pocall : pocall_fpccall;
  1105. pooption : [];
  1106. mutexclpocall : [];
  1107. mutexclpotype : [];
  1108. mutexclpo : [po_leftright]
  1109. ),(
  1110. idtok:_INLINE;
  1111. pd_flags : pd_interface+pd_implemen+pd_body+pd_notobjintf;
  1112. handler : {$ifdef FPCPROCVAR}@{$endif}pd_inline;
  1113. pocall : pocall_inline;
  1114. pooption : [];
  1115. mutexclpocall : [];
  1116. mutexclpotype : [potype_constructor,potype_destructor];
  1117. mutexclpo : [po_exports,po_external,po_interrupt]
  1118. ),(
  1119. idtok:_INTERNCONST;
  1120. pd_flags : pd_implemen+pd_body+pd_notobject+pd_notobjintf;
  1121. handler : {$ifdef FPCPROCVAR}@{$endif}pd_intern;
  1122. pocall : pocall_none;
  1123. pooption : [po_internconst];
  1124. mutexclpocall : [];
  1125. mutexclpotype : [potype_operator];
  1126. mutexclpo : []
  1127. ),(
  1128. idtok:_INTERNPROC;
  1129. pd_flags : pd_implemen+pd_notobject+pd_notobjintf;
  1130. handler : {$ifdef FPCPROCVAR}@{$endif}pd_intern;
  1131. pocall : pocall_internproc;
  1132. pooption : [];
  1133. mutexclpocall : [];
  1134. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1135. mutexclpo : [po_exports,po_external,po_interrupt,po_assembler,po_iocheck,po_leftright]
  1136. ),(
  1137. idtok:_INTERRUPT;
  1138. pd_flags : pd_implemen+pd_body+pd_notobject+pd_notobjintf;
  1139. handler : {$ifdef FPCPROCVAR}@{$endif}pd_interrupt;
  1140. pocall : pocall_none;
  1141. pooption : [po_interrupt];
  1142. mutexclpocall : [pocall_internproc,pocall_cdecl,pocall_cppdecl,
  1143. pocall_inline,pocall_pascal,pocall_system,pocall_far16,pocall_fpccall];
  1144. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1145. mutexclpo : [po_external,po_leftright,po_clearstack]
  1146. ),(
  1147. idtok:_IOCHECK;
  1148. pd_flags : pd_implemen+pd_body+pd_notobjintf;
  1149. handler : nil;
  1150. pocall : pocall_none;
  1151. pooption : [po_iocheck];
  1152. mutexclpocall : [pocall_internproc];
  1153. mutexclpotype : [];
  1154. mutexclpo : [po_external]
  1155. ),(
  1156. idtok:_MESSAGE;
  1157. pd_flags : pd_interface+pd_object+pd_notobjintf;
  1158. handler : {$ifdef FPCPROCVAR}@{$endif}pd_message;
  1159. pocall : pocall_none;
  1160. pooption : []; { can be po_msgstr or po_msgint }
  1161. mutexclpocall : [pocall_inline,pocall_internproc];
  1162. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1163. mutexclpo : [po_interrupt,po_external]
  1164. ),(
  1165. idtok:_NEAR;
  1166. pd_flags : pd_implemen+pd_body+pd_procvar+pd_notobjintf;
  1167. handler : {$ifdef FPCPROCVAR}@{$endif}pd_near;
  1168. pocall : pocall_none;
  1169. pooption : [];
  1170. mutexclpocall : [pocall_internproc];
  1171. mutexclpotype : [];
  1172. mutexclpo : []
  1173. ),(
  1174. idtok:_OVERLOAD;
  1175. pd_flags : pd_implemen+pd_interface+pd_body;
  1176. handler : {$ifdef FPCPROCVAR}@{$endif}pd_overload;
  1177. pocall : pocall_none;
  1178. pooption : [po_overload];
  1179. mutexclpocall : [pocall_internproc];
  1180. mutexclpotype : [];
  1181. mutexclpo : []
  1182. ),(
  1183. idtok:_OVERRIDE;
  1184. pd_flags : pd_interface+pd_object+pd_notobjintf;
  1185. handler : {$ifdef FPCPROCVAR}@{$endif}pd_override;
  1186. pocall : pocall_none;
  1187. pooption : [po_overridingmethod,po_virtualmethod];
  1188. mutexclpocall : [pocall_inline,pocall_internproc];
  1189. mutexclpotype : [];
  1190. mutexclpo : [po_exports,po_external,po_interrupt]
  1191. ),(
  1192. idtok:_PASCAL;
  1193. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  1194. handler : nil;
  1195. pocall : pocall_pascal;
  1196. pooption : [];
  1197. mutexclpocall : [];
  1198. mutexclpotype : [];
  1199. mutexclpo : [po_external]
  1200. ),(
  1201. idtok:_POPSTACK;
  1202. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  1203. handler : nil;
  1204. pocall : pocall_none;
  1205. pooption : [po_clearstack];
  1206. mutexclpocall : [pocall_inline,pocall_internproc,pocall_stdcall];
  1207. mutexclpotype : [];
  1208. mutexclpo : [po_assembler,po_external]
  1209. ),(
  1210. idtok:_PUBLIC;
  1211. pd_flags : pd_implemen+pd_body+pd_global+pd_notobject+pd_notobjintf;
  1212. handler : nil;
  1213. pocall : pocall_none;
  1214. pooption : [];
  1215. mutexclpocall : [pocall_internproc,pocall_inline];
  1216. mutexclpotype : [];
  1217. mutexclpo : [po_external]
  1218. ),(
  1219. idtok:_REGISTER;
  1220. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  1221. handler : nil;
  1222. pocall : pocall_register;
  1223. pooption : [];
  1224. mutexclpocall : [];
  1225. mutexclpotype : [];
  1226. mutexclpo : [po_external]
  1227. ),(
  1228. idtok:_REINTRODUCE;
  1229. pd_flags : pd_interface+pd_object;
  1230. handler : {$ifdef FPCPROCVAR}@{$endif}pd_reintroduce;
  1231. pocall : pocall_none;
  1232. pooption : [];
  1233. mutexclpocall : [];
  1234. mutexclpotype : [];
  1235. mutexclpo : []
  1236. ),(
  1237. idtok:_SAFECALL;
  1238. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  1239. handler : nil;
  1240. pocall : pocall_safecall;
  1241. pooption : [];
  1242. mutexclpocall : [];
  1243. mutexclpotype : [];
  1244. mutexclpo : [po_external]
  1245. ),(
  1246. idtok:_SAVEREGISTERS;
  1247. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar+pd_notobjintf;
  1248. handler : nil;
  1249. pocall : pocall_none;
  1250. pooption : [po_saveregisters];
  1251. mutexclpocall : [pocall_internproc];
  1252. mutexclpotype : [];
  1253. mutexclpo : [po_external]
  1254. ),(
  1255. idtok:_STATIC;
  1256. pd_flags : pd_interface+pd_object+pd_notobjintf;
  1257. handler : {$ifdef FPCPROCVAR}@{$endif}pd_static;
  1258. pocall : pocall_none;
  1259. pooption : [po_staticmethod];
  1260. mutexclpocall : [pocall_inline,pocall_internproc];
  1261. mutexclpotype : [potype_constructor,potype_destructor];
  1262. mutexclpo : [po_external,po_interrupt,po_exports]
  1263. ),(
  1264. idtok:_STDCALL;
  1265. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  1266. handler : nil;
  1267. pocall : pocall_stdcall;
  1268. pooption : [];
  1269. mutexclpocall : [];
  1270. mutexclpotype : [];
  1271. mutexclpo : [po_external]
  1272. ),(
  1273. idtok:_SYSCALL;
  1274. pd_flags : pd_interface+pd_implemen+pd_notobjintf;
  1275. handler : {$ifdef FPCPROCVAR}@{$endif}pd_syscall;
  1276. pocall : pocall_palmossyscall;
  1277. pooption : [];
  1278. mutexclpocall : [];
  1279. mutexclpotype : [];
  1280. mutexclpo : [po_external,po_assembler,po_interrupt,po_exports]
  1281. ),(
  1282. idtok:_SYSTEM;
  1283. pd_flags : pd_implemen+pd_notobject+pd_notobjintf;
  1284. handler : nil;
  1285. pocall : pocall_system;
  1286. pooption : [];
  1287. mutexclpocall : [];
  1288. mutexclpotype : [];
  1289. mutexclpo : [po_external,po_assembler,po_interrupt]
  1290. ),(
  1291. idtok:_VIRTUAL;
  1292. pd_flags : pd_interface+pd_object+pd_notobjintf;
  1293. handler : {$ifdef FPCPROCVAR}@{$endif}pd_virtual;
  1294. pocall : pocall_none;
  1295. pooption : [po_virtualmethod];
  1296. mutexclpocall : [pocall_inline,pocall_internproc];
  1297. mutexclpotype : [];
  1298. mutexclpo : [po_external,po_interrupt,po_exports]
  1299. ),(
  1300. idtok:_CPPDECL;
  1301. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  1302. handler : nil;
  1303. pocall : pocall_cppdecl;
  1304. pooption : [po_savestdregs];
  1305. mutexclpocall : [];
  1306. mutexclpotype : [];
  1307. mutexclpo : [po_assembler,po_external,po_virtualmethod]
  1308. ),(
  1309. idtok:_VARARGS;
  1310. pd_flags : pd_interface+pd_implemen+pd_procvar;
  1311. handler : nil;
  1312. pocall : pocall_none;
  1313. pooption : [po_varargs];
  1314. mutexclpocall : [pocall_internproc,pocall_stdcall,pocall_register,
  1315. pocall_inline,pocall_far16,pocall_fpccall];
  1316. mutexclpotype : [];
  1317. mutexclpo : [po_assembler,po_interrupt,po_leftright]
  1318. ),(
  1319. idtok:_COMPILERPROC;
  1320. pd_flags : pd_interface+pd_implemen+pd_body+pd_notobjintf;
  1321. handler : nil;
  1322. pocall : pocall_compilerproc;
  1323. pooption : [];
  1324. mutexclpocall : [];
  1325. mutexclpotype : [];
  1326. mutexclpo : [po_interrupt]
  1327. )
  1328. );
  1329. function is_proc_directive(tok:ttoken):boolean;
  1330. var
  1331. i : longint;
  1332. begin
  1333. is_proc_directive:=false;
  1334. for i:=1 to num_proc_directives do
  1335. if proc_direcdata[i].idtok=idtoken then
  1336. begin
  1337. is_proc_directive:=true;
  1338. exit;
  1339. end;
  1340. end;
  1341. function parse_proc_direc(var pdflags:word):boolean;
  1342. {
  1343. Parse the procedure directive, returns true if a correct directive is found
  1344. }
  1345. var
  1346. p : longint;
  1347. found : boolean;
  1348. name : stringid;
  1349. begin
  1350. parse_proc_direc:=false;
  1351. name:=tokeninfo^[idtoken].str;
  1352. found:=false;
  1353. { Hint directive? Then exit immediatly }
  1354. if (m_hintdirective in aktmodeswitches) then
  1355. begin
  1356. case idtoken of
  1357. _LIBRARY,
  1358. _PLATFORM,
  1359. _UNIMPLEMENTED,
  1360. _DEPRECATED :
  1361. exit;
  1362. end;
  1363. end;
  1364. { retrieve data for directive if found }
  1365. for p:=1 to num_proc_directives do
  1366. if proc_direcdata[p].idtok=idtoken then
  1367. begin
  1368. found:=true;
  1369. break;
  1370. end;
  1371. { Check if the procedure directive is known }
  1372. if not found then
  1373. begin
  1374. { parsing a procvar type the name can be any
  1375. next variable !! }
  1376. if (pdflags and (pd_procvar or pd_object))=0 then
  1377. Message1(parser_w_unknown_proc_directive_ignored,name);
  1378. exit;
  1379. end;
  1380. { static needs a special treatment }
  1381. if (idtoken=_STATIC) and not (cs_static_keyword in aktmoduleswitches) then
  1382. exit;
  1383. { Conflicts between directives ? }
  1384. if (aktprocdef.proctypeoption in proc_direcdata[p].mutexclpotype) or
  1385. (aktprocdef.proccalloption in proc_direcdata[p].mutexclpocall) or
  1386. ((aktprocdef.procoptions*proc_direcdata[p].mutexclpo)<>[]) then
  1387. begin
  1388. Message1(parser_e_proc_dir_conflict,name);
  1389. exit;
  1390. end;
  1391. { set calling convention }
  1392. if proc_direcdata[p].pocall<>pocall_none then
  1393. begin
  1394. if aktprocdef.proccalloption<>pocall_none then
  1395. begin
  1396. Message2(parser_w_proc_overriding_calling,
  1397. proccalloptionStr[aktprocdef.proccalloption],
  1398. proccalloptionStr[proc_direcdata[p].pocall]);
  1399. end;
  1400. aktprocdef.proccalloption:=proc_direcdata[p].pocall;
  1401. end;
  1402. { check if method and directive not for object, like public.
  1403. This needs to be checked also for procvars }
  1404. if ((proc_direcdata[p].pd_flags and pd_notobject)<>0) and
  1405. (aktprocdef.owner.symtabletype=objectsymtable) then
  1406. exit;
  1407. if aktprocdef.deftype=procdef then
  1408. begin
  1409. { Check if the directive is only for objects }
  1410. if ((proc_direcdata[p].pd_flags and pd_object)<>0) and
  1411. not assigned(aktprocdef._class) then
  1412. exit;
  1413. { check if method and directive not for interface }
  1414. if ((proc_direcdata[p].pd_flags and pd_notobjintf)<>0) and
  1415. is_interface(aktprocdef._class) then
  1416. exit;
  1417. end;
  1418. { consume directive, and turn flag on }
  1419. consume(token);
  1420. parse_proc_direc:=true;
  1421. { Check the pd_flags if the directive should be allowed }
  1422. if ((pdflags and pd_interface)<>0) and
  1423. ((proc_direcdata[p].pd_flags and pd_interface)=0) then
  1424. begin
  1425. Message1(parser_e_proc_dir_not_allowed_in_interface,name);
  1426. exit;
  1427. end;
  1428. if ((pdflags and pd_implemen)<>0) and
  1429. ((proc_direcdata[p].pd_flags and pd_implemen)=0) then
  1430. begin
  1431. Message1(parser_e_proc_dir_not_allowed_in_implementation,name);
  1432. exit;
  1433. end;
  1434. if ((pdflags and pd_procvar)<>0) and
  1435. ((proc_direcdata[p].pd_flags and pd_procvar)=0) then
  1436. begin
  1437. Message1(parser_e_proc_dir_not_allowed_in_procvar,name);
  1438. exit;
  1439. end;
  1440. { Return the new pd_flags }
  1441. if (proc_direcdata[p].pd_flags and pd_body)=0 then
  1442. pdflags:=pdflags and (not pd_body);
  1443. if (proc_direcdata[p].pd_flags and pd_global)<>0 then
  1444. pdflags:=pdflags or pd_global;
  1445. { Add the correct flag }
  1446. aktprocdef.procoptions:=aktprocdef.procoptions+proc_direcdata[p].pooption;
  1447. { Call the handler }
  1448. if pointer({$ifndef FPCPROCVAR}@{$endif}proc_direcdata[p].handler)<>nil then
  1449. proc_direcdata[p].handler{$ifdef FPCPROCVAR}(){$endif};
  1450. end;
  1451. procedure handle_calling_convention(sym:tprocsym;def:tabstractprocdef);
  1452. begin
  1453. { set the default calling convention }
  1454. if def.proccalloption=pocall_none then
  1455. def.proccalloption:=aktdefproccall;
  1456. case def.proccalloption of
  1457. pocall_cdecl :
  1458. begin
  1459. { use popstack and save std registers }
  1460. include(def.procoptions,po_clearstack);
  1461. include(def.procoptions,po_savestdregs);
  1462. { set mangledname }
  1463. if (def.deftype=procdef) then
  1464. begin
  1465. if not tprocdef(def).has_mangledname then
  1466. begin
  1467. if assigned(tprocdef(def)._class) then
  1468. tprocdef(def).setmangledname(target_info.Cprefix+tprocdef(def)._class.objrealname^+'_'+sym.realname)
  1469. else
  1470. tprocdef(def).setmangledname(target_info.Cprefix+sym.realname);
  1471. end;
  1472. if not assigned(tprocdef(def).parast) then
  1473. internalerror(200110234);
  1474. { do not copy on local !! }
  1475. tprocdef(def).parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}resetvaluepara,nil);
  1476. { check C cdecl para types }
  1477. tprocdef(def).parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}checkparatype,nil);
  1478. { Adjust alignment to match cdecl or stdcall }
  1479. tprocdef(def).parast.dataalignment:=std_param_align;
  1480. end;
  1481. end;
  1482. pocall_cppdecl :
  1483. begin
  1484. if not assigned(sym) then
  1485. internalerror(200110231);
  1486. { use popstack and save std registers }
  1487. include(def.procoptions,po_clearstack);
  1488. include(def.procoptions,po_savestdregs);
  1489. { set mangledname }
  1490. if (def.deftype=procdef) then
  1491. begin
  1492. if not tprocdef(def).has_mangledname then
  1493. tprocdef(def).setmangledname(target_info.Cprefix+tprocdef(def).cplusplusmangledname);
  1494. if not assigned(tprocdef(def).parast) then
  1495. internalerror(200110235);
  1496. { do not copy on local !! }
  1497. tprocdef(def).parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}resetvaluepara,nil);
  1498. { check C cdecl para types }
  1499. tprocdef(def).parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}checkparatype,nil);
  1500. { Adjust alignment to match cdecl or stdcall }
  1501. tprocdef(def).parast.dataalignment:=std_param_align;
  1502. end;
  1503. end;
  1504. pocall_stdcall :
  1505. begin
  1506. include(def.procoptions,po_savestdregs);
  1507. if (def.deftype=procdef) then
  1508. begin
  1509. if not assigned(tprocdef(def).parast) then
  1510. internalerror(200110236);
  1511. { Adjust alignment to match cdecl or stdcall }
  1512. tprocdef(def).parast.dataalignment:=std_param_align;
  1513. end;
  1514. end;
  1515. pocall_safecall :
  1516. begin
  1517. include(def.procoptions,po_savestdregs);
  1518. end;
  1519. pocall_compilerproc :
  1520. begin
  1521. if (not assigned(sym)) or
  1522. (def.deftype<>procdef) then
  1523. internalerror(200110232);
  1524. tprocdef(def).setmangledname(lower(sym.name));
  1525. end;
  1526. pocall_pascal :
  1527. begin
  1528. include(def.procoptions,po_leftright);
  1529. end;
  1530. pocall_register :
  1531. begin
  1532. Message1(parser_w_proc_directive_ignored,'REGISTER');
  1533. end;
  1534. pocall_far16 :
  1535. begin
  1536. { Temporary stub, must be rewritten to support OS/2 far16 }
  1537. Message1(parser_w_proc_directive_ignored,'FAR16');
  1538. end;
  1539. pocall_system :
  1540. begin
  1541. include(def.procoptions,po_clearstack);
  1542. if (not assigned(sym)) or
  1543. (def.deftype<>procdef) then
  1544. internalerror(200110233);
  1545. if not tprocdef(def).has_mangledname then
  1546. tprocdef(def).setmangledname(sym.realname);
  1547. end;
  1548. pocall_palmossyscall :
  1549. begin
  1550. { use popstack and save std registers }
  1551. include(def.procoptions,po_clearstack);
  1552. include(def.procoptions,po_savestdregs);
  1553. if (def.deftype=procdef) then
  1554. begin
  1555. if not assigned(tprocdef(def).parast) then
  1556. internalerror(200110236);
  1557. { do not copy on local !! }
  1558. tprocdef(def).parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}resetvaluepara,nil);
  1559. { Adjust positions of args for cdecl or stdcall }
  1560. tprocdef(def).parast.dataalignment:=std_param_align;
  1561. end;
  1562. end;
  1563. pocall_inline :
  1564. begin
  1565. if not(cs_support_inline in aktmoduleswitches) then
  1566. begin
  1567. Message(parser_e_proc_inline_not_supported);
  1568. def.proccalloption:=pocall_fpccall;
  1569. end;
  1570. end;
  1571. end;
  1572. { add mangledname to external list }
  1573. if (def.deftype=procdef) and
  1574. (po_external in def.procoptions) and
  1575. target_info.DllScanSupported then
  1576. current_module.externals.insert(tExternalsItem.create(tprocdef(def).mangledname));
  1577. end;
  1578. procedure calc_parasymtable_addresses(def:tprocdef);
  1579. var
  1580. lastps,
  1581. highps,ps : tsym;
  1582. st : tsymtable;
  1583. begin
  1584. st:=def.parast;
  1585. if po_leftright in def.procoptions then
  1586. begin
  1587. { pushed in reversed order, left to right }
  1588. highps:=nil;
  1589. lastps:=nil;
  1590. while assigned(st.symindex.first) and (lastps<>tsym(st.symindex.first)) do
  1591. begin
  1592. ps:=tsym(st.symindex.first);
  1593. while assigned(ps.indexnext) and (tsym(ps.indexnext)<>lastps) do
  1594. ps:=tsym(ps.indexnext);
  1595. if (ps.typ=varsym) and
  1596. (copy(ps.name,1,6)<>'hidden') then
  1597. begin
  1598. { Wait with inserting the high value, it needs to be inserted
  1599. after the corresponding parameter }
  1600. if Copy(ps.name,1,4)='high' then
  1601. highps:=ps
  1602. else
  1603. begin
  1604. st.insertvardata(ps);
  1605. { add also the high tree if it was saved }
  1606. if assigned(highps) then
  1607. begin
  1608. st.insertvardata(highps);
  1609. highps:=nil;
  1610. end;
  1611. end;
  1612. end;
  1613. lastps:=ps;
  1614. end;
  1615. if assigned(highps) then
  1616. internalerror(200208257);
  1617. end
  1618. else
  1619. begin
  1620. { pushed in normal order, right to left }
  1621. ps:=tsym(st.symindex.first);
  1622. while assigned(ps) do
  1623. begin
  1624. if ps.typ=varsym then
  1625. st.insertvardata(ps);
  1626. ps:=tsym(ps.indexnext);
  1627. end;
  1628. end;
  1629. end;
  1630. procedure parse_proc_directives(var pdflags:word);
  1631. {
  1632. Parse the procedure directives. It does not matter if procedure directives
  1633. are written using ;procdir; or ['procdir'] syntax.
  1634. }
  1635. var
  1636. res : boolean;
  1637. begin
  1638. while token in [_ID,_LECKKLAMMER] do
  1639. begin
  1640. if try_to_consume(_LECKKLAMMER) then
  1641. begin
  1642. repeat
  1643. parse_proc_direc(pdflags);
  1644. until not try_to_consume(_COMMA);
  1645. consume(_RECKKLAMMER);
  1646. { we always expect at least '[];' }
  1647. res:=true;
  1648. end
  1649. else
  1650. begin
  1651. res:=parse_proc_direc(pdflags);
  1652. end;
  1653. { A procedure directive normally followed by a semicolon, but in
  1654. a const section we should stop when _EQUAL is found }
  1655. if res then
  1656. begin
  1657. if (block_type=bt_const) and
  1658. (token=_EQUAL) then
  1659. break;
  1660. { support procedure proc;stdcall export; in Delphi mode only }
  1661. if not((m_delphi in aktmodeswitches) and
  1662. is_proc_directive(token)) then
  1663. consume(_SEMICOLON);
  1664. end
  1665. else
  1666. break;
  1667. end;
  1668. handle_calling_convention(aktprocsym,aktprocdef);
  1669. { calculate addresses in parasymtable }
  1670. if aktprocdef.deftype=procdef then
  1671. calc_parasymtable_addresses(aktprocdef);
  1672. end;
  1673. procedure parse_var_proc_directives(var sym : tsym);
  1674. var
  1675. pdflags : word;
  1676. oldsym : tprocsym;
  1677. olddef : tprocdef;
  1678. pd : tabstractprocdef;
  1679. begin
  1680. oldsym:=aktprocsym;
  1681. olddef:=aktprocdef;
  1682. pdflags:=pd_procvar;
  1683. { we create a temporary aktprocsym to read the directives }
  1684. aktprocsym:=tprocsym.create(sym.name);
  1685. case sym.typ of
  1686. varsym :
  1687. pd:=tabstractprocdef(tvarsym(sym).vartype.def);
  1688. typedconstsym :
  1689. pd:=tabstractprocdef(ttypedconstsym(sym).typedconsttype.def);
  1690. typesym :
  1691. pd:=tabstractprocdef(ttypesym(sym).restype.def);
  1692. else
  1693. internalerror(994932432);
  1694. end;
  1695. if pd.deftype<>procvardef then
  1696. internalerror(994932433);
  1697. tabstractprocdef(aktprocdef):=pd;
  1698. { names should never be used anyway }
  1699. inc(lexlevel);
  1700. parse_proc_directives(pdflags);
  1701. dec(lexlevel);
  1702. aktprocsym.free;
  1703. aktprocsym:=oldsym;
  1704. aktprocdef:=olddef;
  1705. end;
  1706. procedure parse_object_proc_directives(var sym : tprocsym);
  1707. var
  1708. pdflags : word;
  1709. begin
  1710. pdflags:=pd_object;
  1711. inc(lexlevel);
  1712. parse_proc_directives(pdflags);
  1713. dec(lexlevel);
  1714. if (po_containsself in aktprocdef.procoptions) and
  1715. (([po_msgstr,po_msgint]*aktprocdef.procoptions)=[]) then
  1716. Message(parser_e_self_in_non_message_handler);
  1717. end;
  1718. function proc_add_definition(aprocsym:tprocsym;var aprocdef : tprocdef) : boolean;
  1719. {
  1720. Add definition aprocdef to the overloaded definitions of aprocsym. If a
  1721. forwarddef is found and reused it returns true
  1722. }
  1723. var
  1724. hd : tprocdef;
  1725. ad,fd : tsym;
  1726. forwardfound : boolean;
  1727. i : cardinal;
  1728. begin
  1729. forwardfound:=false;
  1730. { check overloaded functions if the same function already exists }
  1731. for i:=1 to aprocsym.procdef_count do
  1732. begin
  1733. hd:=aprocsym.procdef[i];
  1734. { Skip overloaded definitions that are declared in other
  1735. units }
  1736. if hd.procsym<>aprocsym then
  1737. continue;
  1738. { check the parameters, for delphi/tp it is possible to
  1739. leave the parameters away in the implementation (forwarddef=false).
  1740. But for an overload declared function this is not allowed }
  1741. if { check if empty implementation arguments match is allowed }
  1742. (
  1743. not(m_repeat_forward in aktmodeswitches) and
  1744. not(aprocdef.forwarddef) and
  1745. (aprocdef.maxparacount=0) and
  1746. not(po_overload in hd.procoptions)
  1747. ) or
  1748. { check arguments }
  1749. (
  1750. (compare_paras(aprocdef.para,hd.para,cp_none,false)>=te_equal) and
  1751. { for operators equal_paras is not enough !! }
  1752. ((aprocdef.proctypeoption<>potype_operator) or (optoken<>_ASSIGNMENT) or
  1753. equal_defs(hd.rettype.def,aprocdef.rettype.def))
  1754. ) then
  1755. begin
  1756. { Check if we've found the forwarddef, if found then
  1757. we need to update the forward def with the current
  1758. implementation settings }
  1759. if hd.forwarddef then
  1760. begin
  1761. forwardfound:=true;
  1762. { Check if the procedure type and return type are correct,
  1763. also the parameters must match also with the type }
  1764. if (hd.proctypeoption<>aprocdef.proctypeoption) or
  1765. (
  1766. (m_repeat_forward in aktmodeswitches) and
  1767. (not((aprocdef.maxparacount=0) or
  1768. (compare_paras(aprocdef.para,hd.para,cp_all,false)>=te_equal)))
  1769. ) or
  1770. (
  1771. ((m_repeat_forward in aktmodeswitches) or
  1772. not(is_void(aprocdef.rettype.def))) and
  1773. (not equal_defs(hd.rettype.def,aprocdef.rettype.def))) then
  1774. begin
  1775. MessagePos1(aprocdef.fileinfo,parser_e_header_dont_match_forward,
  1776. aprocdef.fullprocname);
  1777. break;
  1778. end;
  1779. { Check if both are declared forward }
  1780. if hd.forwarddef and aprocdef.forwarddef then
  1781. begin
  1782. MessagePos1(aprocdef.fileinfo,parser_e_function_already_declared_public_forward,
  1783. aprocdef.fullprocname);
  1784. end;
  1785. { internconst or internproc only need to be defined once }
  1786. if (hd.proccalloption=pocall_internproc) then
  1787. aprocdef.proccalloption:=hd.proccalloption
  1788. else
  1789. if (aprocdef.proccalloption=pocall_internproc) then
  1790. hd.proccalloption:=aprocdef.proccalloption;
  1791. if (po_internconst in hd.procoptions) then
  1792. include(aprocdef.procoptions,po_internconst)
  1793. else if (po_internconst in aprocdef.procoptions) then
  1794. include(hd.procoptions,po_internconst);
  1795. { Check calling convention }
  1796. if (hd.proccalloption<>aprocdef.proccalloption) then
  1797. begin
  1798. { For delphi check if the current implementation has no proccalloption, then
  1799. take the options from the interface }
  1800. if not(m_delphi in aktmodeswitches) or
  1801. (aprocdef.proccalloption<>pocall_none) then
  1802. MessagePos(aprocdef.fileinfo,parser_e_call_convention_dont_match_forward);
  1803. { restore interface settings }
  1804. aprocdef.proccalloption:=hd.proccalloption;
  1805. if hd.has_mangledname then
  1806. aprocdef.setmangledname(hd.mangledname);
  1807. end;
  1808. { Check manglednames }
  1809. if (m_repeat_forward in aktmodeswitches) or
  1810. aprocdef.haspara then
  1811. begin
  1812. { If mangled names are equal then they have the same amount of arguments }
  1813. { We can check the names of the arguments }
  1814. { both symtables are in the same order from left to right }
  1815. ad:=tsym(hd.parast.symindex.first);
  1816. fd:=tsym(aprocdef.parast.symindex.first);
  1817. repeat
  1818. { skip default parameter constsyms }
  1819. while assigned(ad) and (ad.typ<>varsym) do
  1820. ad:=tsym(ad.indexnext);
  1821. while assigned(fd) and (fd.typ<>varsym) do
  1822. fd:=tsym(fd.indexnext);
  1823. { stop when one of the two lists is at the end }
  1824. if not assigned(ad) or not assigned(fd) then
  1825. break;
  1826. if (ad.name<>fd.name) then
  1827. begin
  1828. MessagePos3(aprocdef.fileinfo,parser_e_header_different_var_names,
  1829. aprocsym.name,ad.name,fd.name);
  1830. break;
  1831. end;
  1832. ad:=tsym(ad.indexnext);
  1833. fd:=tsym(fd.indexnext);
  1834. until false;
  1835. if assigned(ad) or assigned(fd) then
  1836. internalerror(200204178);
  1837. end;
  1838. { Everything is checked, now we can update the forward declaration
  1839. with the new data from the implementation }
  1840. hd.forwarddef:=aprocdef.forwarddef;
  1841. hd.hasforward:=true;
  1842. hd.parast.address_fixup:=aprocdef.parast.address_fixup;
  1843. hd.procoptions:=hd.procoptions+aprocdef.procoptions;
  1844. if hd.extnumber=65535 then
  1845. hd.extnumber:=aprocdef.extnumber;
  1846. while not aprocdef.aliasnames.empty do
  1847. hd.aliasnames.insert(aprocdef.aliasnames.getfirst);
  1848. { update mangledname if the implementation has a fixed mangledname set }
  1849. if aprocdef.has_mangledname then
  1850. begin
  1851. { rename also asmsymbol first, because the name can already be used }
  1852. objectlibrary.renameasmsymbol(hd.mangledname,aprocdef.mangledname);
  1853. hd.setmangledname(aprocdef.mangledname);
  1854. end;
  1855. { for compilerproc defines we need to rename and update the
  1856. symbolname to lowercase }
  1857. if (aprocdef.proccalloption=pocall_compilerproc) then
  1858. begin
  1859. { rename to lowercase so users can't access it }
  1860. aprocsym.owner.rename(aprocsym.name,lower(aprocsym.name));
  1861. { also update the realname that is stored in the ppu }
  1862. stringdispose(aprocsym._realname);
  1863. aprocsym._realname:=stringdup('$'+aprocsym.name);
  1864. { the mangeled name is already changed by the pd_compilerproc }
  1865. { handler. It must be done immediately because if we have a }
  1866. { call to a compilerproc before it's implementation is }
  1867. { encountered, it must already use the new mangled name (JM) }
  1868. end;
  1869. { return the forwarddef }
  1870. aprocdef:=hd;
  1871. end
  1872. else
  1873. begin
  1874. { abstract methods aren't forward defined, but this }
  1875. { needs another error message }
  1876. if (po_abstractmethod in hd.procoptions) then
  1877. MessagePos(aprocdef.fileinfo,parser_e_abstract_no_definition)
  1878. else
  1879. MessagePos(aprocdef.fileinfo,parser_e_overloaded_have_same_parameters);
  1880. end;
  1881. { we found one proc with the same arguments, there are no others
  1882. so we can stop }
  1883. break;
  1884. end;
  1885. { check for allowing overload directive }
  1886. if not(m_fpc in aktmodeswitches) then
  1887. begin
  1888. { overload directive turns on overloading }
  1889. if ((po_overload in aprocdef.procoptions) or
  1890. (po_overload in hd.procoptions)) then
  1891. begin
  1892. { check if all procs have overloading, but not if the proc was
  1893. already declared forward, then the check is already done }
  1894. if not(hd.hasforward or
  1895. (aprocdef.forwarddef<>hd.forwarddef) or
  1896. ((po_overload in aprocdef.procoptions) and
  1897. (po_overload in hd.procoptions))) then
  1898. begin
  1899. MessagePos1(aprocdef.fileinfo,parser_e_no_overload_for_all_procs,aprocsym.realname);
  1900. break;
  1901. end;
  1902. end
  1903. else
  1904. begin
  1905. if not(hd.forwarddef) then
  1906. begin
  1907. MessagePos(aprocdef.fileinfo,parser_e_procedure_overloading_is_off);
  1908. break;
  1909. end;
  1910. end;
  1911. end; { equal arguments }
  1912. end;
  1913. { if we didn't reuse a forwarddef then we add the procdef to the overloaded
  1914. list }
  1915. if not forwardfound then
  1916. begin
  1917. aprocsym.addprocdef(aprocdef);
  1918. { add overloadnumber for unique naming, the overloadcount is
  1919. counted per module and 0 for the first procedure }
  1920. aprocdef.overloadnumber:=aprocsym.overloadcount;
  1921. inc(aprocsym.overloadcount);
  1922. end;
  1923. { insert otsym only in the right symtable }
  1924. if ((procinfo.flags and pi_operator)<>0) and
  1925. assigned(otsym) then
  1926. begin
  1927. if not parse_only then
  1928. begin
  1929. if paramanager.ret_in_param(aprocdef.rettype.def,aprocdef.proccalloption) then
  1930. begin
  1931. aprocdef.parast.insert(otsym);
  1932. { this allows to read the funcretoffset }
  1933. otsym.address:=-4;
  1934. otsym.varspez:=vs_var;
  1935. end
  1936. else
  1937. begin
  1938. if not assigned(aprocdef.localst) then
  1939. aprocdef.insert_localst;
  1940. aprocdef.localst.insert(otsym);
  1941. aprocdef.localst.insertvardata(otsym);
  1942. end;
  1943. end
  1944. else
  1945. begin
  1946. { this is not required anymore }
  1947. otsym.free;
  1948. otsym:=nil;
  1949. end;
  1950. end;
  1951. paramanager.create_param_loc_info(aprocdef);
  1952. proc_add_definition:=forwardfound;
  1953. end;
  1954. end.
  1955. {
  1956. $Log$
  1957. Revision 1.89 2002-12-15 21:07:30 peter
  1958. * don't allow external in object declarations
  1959. Revision 1.88 2002/12/15 19:34:31 florian
  1960. + some front end stuff for vs_hidden added
  1961. Revision 1.87 2002/12/07 14:27:07 carl
  1962. * 3% memory optimization
  1963. * changed some types
  1964. + added type checking with different size for call node and for
  1965. parameters
  1966. Revision 1.86 2002/12/06 17:51:10 peter
  1967. * merged cdecl and array fixes
  1968. Revision 1.85 2002/12/01 22:06:14 carl
  1969. * cleanup of error messages
  1970. Revision 1.84 2002/11/29 22:31:19 carl
  1971. + unimplemented hint directive added
  1972. * hint directive parsing implemented
  1973. * warning on these directives
  1974. Revision 1.83 2002/11/27 02:35:28 peter
  1975. * fixed typo in method comparing
  1976. Revision 1.82 2002/11/25 17:43:21 peter
  1977. * splitted defbase in defutil,symutil,defcmp
  1978. * merged isconvertable and is_equal into compare_defs(_ext)
  1979. * made operator search faster by walking the list only once
  1980. Revision 1.81 2002/11/18 17:31:58 peter
  1981. * pass proccalloption to ret_in_xxx and push_xxx functions
  1982. Revision 1.80 2002/11/17 16:31:56 carl
  1983. * memory optimization (3-4%) : cleanup of tai fields,
  1984. cleanup of tdef and tsym fields.
  1985. * make it work for m68k
  1986. Revision 1.79 2002/11/16 14:20:50 peter
  1987. * fix infinite loop in pd_inline
  1988. Revision 1.78 2002/11/15 01:58:53 peter
  1989. * merged changes from 1.0.7 up to 04-11
  1990. - -V option for generating bug report tracing
  1991. - more tracing for option parsing
  1992. - errors for cdecl and high()
  1993. - win32 import stabs
  1994. - win32 records<=8 are returned in eax:edx (turned off by default)
  1995. - heaptrc update
  1996. - more info for temp management in .s file with EXTDEBUG
  1997. Revision 1.77 2002/10/06 15:09:12 peter
  1998. * variant:=nil supported
  1999. Revision 1.76 2002/09/27 21:13:29 carl
  2000. * low-highval always checked if limit ober 2GB is reached (to avoid overflow)
  2001. Revision 1.75 2002/09/16 14:11:13 peter
  2002. * add argument to equal_paras() to support default values or not
  2003. Revision 1.74 2002/09/10 16:27:28 peter
  2004. * don't insert parast in symtablestack, because typesyms should not be
  2005. searched in the the parast
  2006. Revision 1.73 2002/09/09 19:39:07 peter
  2007. * check return type for forwarddefs also not delphi mode when
  2008. the type is not void
  2009. Revision 1.72 2002/09/09 17:34:15 peter
  2010. * tdicationary.replace added to replace and item in a dictionary. This
  2011. is only allowed for the same name
  2012. * varsyms are inserted in symtable before the types are parsed. This
  2013. fixes the long standing "var longint : longint" bug
  2014. - consume_idlist and idstringlist removed. The loops are inserted
  2015. at the callers place and uses the symtable for duplicate id checking
  2016. Revision 1.71 2002/09/07 15:25:06 peter
  2017. * old logs removed and tabs fixed
  2018. Revision 1.70 2002/09/03 16:26:27 daniel
  2019. * Make Tprocdef.defs protected
  2020. Revision 1.69 2002/09/01 12:11:33 peter
  2021. * calc param_offset after parameters are read, because the calculation
  2022. depends on po_containself
  2023. Revision 1.68 2002/08/25 19:25:20 peter
  2024. * sym.insert_in_data removed
  2025. * symtable.insertvardata/insertconstdata added
  2026. * removed insert_in_data call from symtable.insert, it needs to be
  2027. called separatly. This allows to deref the address calculation
  2028. * procedures now calculate the parast addresses after the procedure
  2029. directives are parsed. This fixes the cdecl parast problem
  2030. * push_addr_param has an extra argument that specifies if cdecl is used
  2031. or not
  2032. Revision 1.67 2002/08/25 11:33:06 peter
  2033. * also check the paratypes when a forward was found
  2034. Revision 1.66 2002/08/19 19:36:44 peter
  2035. * More fixes for cross unit inlining, all tnodes are now implemented
  2036. * Moved pocall_internconst to po_internconst because it is not a
  2037. calling type at all and it conflicted when inlining of these small
  2038. functions was requested
  2039. Revision 1.65 2002/08/18 20:06:24 peter
  2040. * inlining is now also allowed in interface
  2041. * renamed write/load to ppuwrite/ppuload
  2042. * tnode storing in ppu
  2043. * nld,ncon,nbas are already updated for storing in ppu
  2044. Revision 1.64 2002/08/17 09:23:39 florian
  2045. * first part of procinfo rewrite
  2046. Revision 1.63 2002/08/11 14:32:27 peter
  2047. * renamed current_library to objectlibrary
  2048. Revision 1.62 2002/08/11 13:24:12 peter
  2049. * saving of asmsymbols in ppu supported
  2050. * asmsymbollist global is removed and moved into a new class
  2051. tasmlibrarydata that will hold the info of a .a file which
  2052. corresponds with a single module. Added librarydata to tmodule
  2053. to keep the library info stored for the module. In the future the
  2054. objectfiles will also be stored to the tasmlibrarydata class
  2055. * all getlabel/newasmsymbol and friends are moved to the new class
  2056. Revision 1.61 2002/07/26 21:15:40 florian
  2057. * rewrote the system handling
  2058. Revision 1.60 2002/07/20 11:57:55 florian
  2059. * types.pas renamed to defbase.pas because D6 contains a types
  2060. unit so this would conflicts if D6 programms are compiled
  2061. + Willamette/SSE2 instructions to assembler added
  2062. Revision 1.59 2002/07/11 14:41:28 florian
  2063. * start of the new generic parameter handling
  2064. Revision 1.58 2002/07/01 18:46:25 peter
  2065. * internal linker
  2066. * reorganized aasm layer
  2067. Revision 1.57 2002/05/18 13:34:12 peter
  2068. * readded missing revisions
  2069. Revision 1.56 2002/05/16 19:46:42 carl
  2070. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  2071. + try to fix temp allocation (still in ifdef)
  2072. + generic constructor calls
  2073. + start of tassembler / tmodulebase class cleanup
  2074. Revision 1.54 2002/05/12 16:53:08 peter
  2075. * moved entry and exitcode to ncgutil and cgobj
  2076. * foreach gets extra argument for passing local data to the
  2077. iterator function
  2078. * -CR checks also class typecasts at runtime by changing them
  2079. into as
  2080. * fixed compiler to cycle with the -CR option
  2081. * fixed stabs with elf writer, finally the global variables can
  2082. be watched
  2083. * removed a lot of routines from cga unit and replaced them by
  2084. calls to cgobj
  2085. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  2086. u32bit then the other is typecasted also to u32bit without giving
  2087. a rangecheck warning/error.
  2088. * fixed pascal calling method with reversing also the high tree in
  2089. the parast, detected by tcalcst3 test
  2090. Revision 1.53 2002/04/21 19:02:04 peter
  2091. * removed newn and disposen nodes, the code is now directly
  2092. inlined from pexpr
  2093. * -an option that will write the secondpass nodes to the .s file, this
  2094. requires EXTDEBUG define to actually write the info
  2095. * fixed various internal errors and crashes due recent code changes
  2096. Revision 1.52 2002/04/20 21:32:24 carl
  2097. + generic FPC_CHECKPOINTER
  2098. + first parameter offset in stack now portable
  2099. * rename some constants
  2100. + move some cpu stuff to other units
  2101. - remove unused constents
  2102. * fix stacksize for some targets
  2103. * fix generic size problems which depend now on EXTEND_SIZE constant
  2104. Revision 1.51 2002/04/20 15:27:05 carl
  2105. - remove ifdef i386 define
  2106. Revision 1.50 2002/04/19 15:46:02 peter
  2107. * mangledname rewrite, tprocdef.mangledname is now created dynamicly
  2108. in most cases and not written to the ppu
  2109. * add mangeledname_prefix() routine to generate the prefix of
  2110. manglednames depending on the current procedure, object and module
  2111. * removed static procprefix since the mangledname is now build only
  2112. on demand from tprocdef.mangledname
  2113. }