blob: 65887f2dfa7240a1432a1cec08c2d53dfd3ddefc [file] [log] [blame]
Chris Lattner3690f152006-06-28 06:48:36 +00001//===--- ScratchBuffer.cpp - Scratch space for forming tokens -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ScratchBuffer interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Lex/ScratchBuffer.h"
15#include "clang/Basic/SourceBuffer.h"
16#include "clang/Basic/SourceManager.h"
17using namespace llvm;
18using namespace clang;
19
20// ScratchBufSize - The size of each chunk of scratch memory. Slightly less
21//than a page, almost certainly enough for anything. :)
22static const unsigned ScratchBufSize = 4060;
23
24ScratchBuffer::ScratchBuffer(SourceManager &SM) : SourceMgr(SM), CurBuffer(0) {
25 // Set BytesUsed so that the first call to getToken will require an alloc.
26 BytesUsed = ScratchBufSize;
27 FileID = 0;
28}
29
Chris Lattner098dfc52006-06-30 06:09:36 +000030/// getToken - Splat the specified text into a temporary SourceBuffer and
31/// return a SourceLocation that refers to the token. This is just like the
32/// method below, but returns a location that indicates the physloc of the
33/// token.
34SourceLocation ScratchBuffer::getToken(const char *Buf, unsigned Len) {
35 if (BytesUsed+Len > ScratchBufSize)
36 AllocScratchBuffer(Len);
37
38 // Copy the token data into the buffer.
39 memcpy(CurBuffer+BytesUsed, Buf, Len);
40
41 // Remember that we used these bytes.
42 BytesUsed += Len;
43
44 assert(BytesUsed-Len < (1 << SourceLocation::FilePosBits) &&
45 "Out of range file position!");
46
47 return SourceLocation(FileID, BytesUsed-Len);
48}
49
Chris Lattner3690f152006-06-28 06:48:36 +000050
51/// getToken - Splat the specified text into a temporary SourceBuffer and
52/// return a SourceLocation that refers to the token. The SourceLoc value
53/// gives a virtual location that the token will appear to be from.
54SourceLocation ScratchBuffer::getToken(const char *Buf, unsigned Len,
55 SourceLocation SourceLoc) {
Chris Lattner098dfc52006-06-30 06:09:36 +000056 // Map the physloc to the specified sourceloc.
Chris Lattner7d1b0062006-06-30 06:14:45 +000057 return SourceMgr.getInstantiationLoc(getToken(Buf, Len), SourceLoc);
Chris Lattner3690f152006-06-28 06:48:36 +000058}
59
60void ScratchBuffer::AllocScratchBuffer(unsigned RequestLen) {
61 // Only pay attention to the requested length if it is larger than our default
62 // page size. If it is, we allocate an entire chunk for it. This is to
63 // support gigantic tokens, which almost certainly won't happen. :)
64 if (RequestLen < ScratchBufSize)
65 RequestLen = ScratchBufSize;
66
67 SourceBuffer *Buf =
68 SourceBuffer::getNewMemBuffer(RequestLen, "<scratch space>");
69 FileID = SourceMgr.createFileIDForMemBuffer(Buf);
70 CurBuffer = const_cast<char*>(Buf->getBufferStart());
71 BytesUsed = 0;
72}