1

In V8.h of Google V8 Javascript engine, there is a piece of code to check if two types match at compiling stage. I can understand large part of it, but can't comprehend the syntax of static_cast<T* volatile*>, what does it mean by adding unusual volatile* and why is it needed?

#define TYPE_CHECK(T, S)                                       \
  while (false) {                                              \
    *(static_cast<T* volatile*>(0)) = static_cast<S*>(0);      \
  }

I noted same code has been discussed in this topic below, but not in detail of the question I am asking. How does the following code work?

Community
  • 1
  • 1
Rico Wang
  • 89
  • 6
  • `volatile*` means pointer-to-`volatile`. Are you asking what `volatile` means? – Emil Laine Feb 17 '16 at 00:32
  • @zenith the question is why `volatile` appears in this code at all (i.e. why would the code break or perform differently if it were removed) – M.M Feb 17 '16 at 00:33

1 Answers1

6

T* volatile* means "pointer to volatile pointer to T". So it is the same as T**, except that when dereferenced, the resulting lvalue is volatile.

As for why volatile is needed here, that's explained in the commit description, which you can view here: https://github.com/v8/v8/commit/35a80e16241308b4f476875d0f96282cf697a029

TYPE_CHECK in v8.h should assign to volatile qualified null-pointer.

Unless the pointer is volatile qualified, Clang will warn that LLVM removes the assignment during optimization. This is not a problem as that code should never execute, but the warning is treated as an error when building Chromium, and thus stops the build.

Community
  • 1
  • 1
Brian Bi
  • 111,498
  • 10
  • 176
  • 312